AbstractNodeDecorator
Deprecated. Use direct subclasses or dynamic proxies instead.
Use either direct subclasses of the appropriate node and set them on the
PrototypicalNodeFactory ,
or use a dynamic proxy implementing the required node type interface.
In the former case this avoids the wrapping and delegation, while the latter
case handles the wrapping and delegation without this class.
Here is an example of how to use dynamic proxies to accomplish the same
effect as using decorators to wrap Text nodes:
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.htmlparser.Parser;
import org.htmlparser.PrototypicalNodeFactory;
import org.htmlparser.Text;
import org.htmlparser.nodes.TextNode;
import org.htmlparser.util.ParserException;
public class TextProxy
implements
InvocationHandler
{
protected Object mObject;
public static Object newInstance (Object object)
{
Class cls;
cls = object.getClass ();
return (Proxy.newProxyInstance (
cls.getClassLoader (),
cls.getInterfaces (),
new TextProxy (object)));
}
private TextProxy (Object object)
{
mObject = object;
}
public Object invoke (Object proxy, Method m, Object[] args)
throws Throwable
{
Object result;
String name;
try
{
result = m.invoke (mObject, args);
name = m.getName ();
if (name.equals ("clone"))
result = newInstance (result); // wrap the cloned object
else if (name.equals ("doSemanticAction")) // or other methods
System.out.println (mObject); // do the needful on the TextNode
}
catch (InvocationTargetException e)
{
throw e.getTargetException ();
}
catch (Exception e)
{
throw new RuntimeException ("unexpected invocation exception: " +
e.getMessage());
}
finally
{
}
return (result);
}
public static void main (String[] args)
throws
ParserException
{
// create the wrapped text node and set it as the prototype
Text text = (Text) TextProxy.newInstance (new TextNode (null, 0, 0));
PrototypicalNodeFactory factory = new PrototypicalNodeFactory ();
factory.setTextPrototype (text);
// perform the parse
Parser parser = new Parser (args[0]);
parser.setNodeFactory (factory);
parser.parse (null);
}
}
|