HTML Parser Home Page

org.htmlparser.nodeDecorators
Class AbstractNodeDecorator

java.lang.Object
  extended byorg.htmlparser.nodeDecorators.AbstractNodeDecorator
All Implemented Interfaces:
Node
Direct Known Subclasses:
DecodingNode, EscapeCharacterRemovingNode, NonBreakingSpaceConvertingNode

public abstract class AbstractNodeDecorator
extends Object
implements Node


Field Summary
protected  Node delegate
           
 
Constructor Summary
protected AbstractNodeDecorator(Node delegate)
           
 
Method Summary
 void accept(Object visitor)
          Apply the visitor object (of type NodeVisitor) to this node.
 void collectInto(NodeList list, NodeFilter filter)
          Collect this node and its child nodes (if-applicable) into the collectionList parameter, provided the node satisfies the filtering criteria.
 void doSemanticAction()
          Perform the meaning of this tag.
 int elementBegin()
          Returns the beginning position of the tag.
 int elementEnd()
          Returns the ending position fo the tag
deprecated Use getEndPosition()
 boolean equals(Object arg0)
           
 NodeList getChildren()
          Get the children of this node.
 int getEndPosition()
          Gets the ending position of the node.
 Node getParent()
          Get the parent of this node.
 int getStartPosition()
          Gets the starting position of the node.
 String getText()
          Returns the text of the node.
 void setChildren(NodeList children)
          Set the children of this node.
 void setEndPosition(int position)
          Sets the ending position of the node.
 void setParent(Node node)
          Sets the parent of this node.
 void setStartPosition(int position)
          Sets the starting position of the node.
 void setText(String text)
          Sets the string contents of the node.
 String toHtml()
          This method will make it easier when using html parser to reproduce html pages (with or without modifications) Applications reproducing html can use this method on nodes which are to be used or transferred as they were recieved, with the original html
 String toPlainTextString()
          Returns a string representation of the node.
 String toString()
          Return the string representation of the node.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

delegate

protected Node delegate
Constructor Detail

AbstractNodeDecorator

protected AbstractNodeDecorator(Node delegate)
Method Detail

accept

public void accept(Object visitor)
Description copied from interface: Node
Apply the visitor object (of type NodeVisitor) to this node.

Specified by:
accept in interface Node

collectInto

public void collectInto(NodeList list,
                        NodeFilter filter)
Description copied from interface: Node
Collect this node and its child nodes (if-applicable) into the collectionList parameter, provided the node satisfies the filtering criteria.

This mechanism allows powerful filtering code to be written very easily, without bothering about collection of embedded tags separately. e.g. when we try to get all the links on a page, it is not possible to get it at the top-level, as many tags (like form tags), can contain links embedded in them. We could get the links out by checking if the current node is a CompositeTag, and going through its children. So this method provides a convenient way to do this.

Using collectInto(), programs get a lot shorter. Now, the code to extract all links from a page would look like:

 NodeList collectionList = new NodeList();
 NodeFilter filter = new TagNameFilter ("A");
 for (NodeIterator e = parser.elements(); e.hasMoreNodes();)
      e.nextNode().collectInto(collectionList, filter);
 
Thus, collectionList will hold all the link nodes, irrespective of how deep the links are embedded.

Another way to accomplish the same objective is:

 NodeList collectionList = new NodeList();
 NodeFilter filter = new TagClassFilter (LinkTag.class);
 for (NodeIterator e = parser.elements(); e.hasMoreNodes();)
      e.nextNode().collectInto(collectionList, filter);
 
This is slightly less specific because the LinkTag class may be registered for more than one node name, e.g. <LINK> tags too.

Specified by:
collectInto in interface Node

elementBegin

public int elementBegin()
Description copied from interface: Node
Returns the beginning position of the tag.
deprecated Use Node.getStartPosition()

Specified by:
elementBegin in interface Node

elementEnd

public int elementEnd()
Description copied from interface: Node
Returns the ending position fo the tag
deprecated Use Node.getEndPosition()

Specified by:
elementEnd in interface Node

getStartPosition

public int getStartPosition()
Gets the starting position of the node.

Specified by:
getStartPosition in interface Node
Returns:
The start position.

setStartPosition

public void setStartPosition(int position)
Sets the starting position of the node.

Specified by:
setStartPosition in interface Node
Parameters:
position - The new start position.

getEndPosition

public int getEndPosition()
Gets the ending position of the node.

Specified by:
getEndPosition in interface Node
Returns:
The end position.

setEndPosition

public void setEndPosition(int position)
Sets the ending position of the node.

Specified by:
setEndPosition in interface Node
Parameters:
position - The new end position.

equals

public boolean equals(Object arg0)

getParent

public Node getParent()
Description copied from interface: Node
Get the parent of this node. This will always return null when parsing without scanners, i.e. if semantic parsing was not performed. The object returned from this method can be safely cast to a CompositeTag.

Specified by:
getParent in interface Node
Returns:
The parent of this node, if it's been set, null otherwise.

getText

public String getText()
Description copied from interface: Node
Returns the text of the node.

Specified by:
getText in interface Node

setParent

public void setParent(Node node)
Description copied from interface: Node
Sets the parent of this node.

Specified by:
setParent in interface Node
Parameters:
node - The node that contains this node. Must be a CompositeTag.

getChildren

public NodeList getChildren()
Get the children of this node.

Specified by:
getChildren in interface Node
Returns:
The list of children contained by this node, if it's been set, null otherwise.

setChildren

public void setChildren(NodeList children)
Set the children of this node.

Specified by:
setChildren in interface Node
Parameters:
children - The new list of children this node contains.

setText

public void setText(String text)
Description copied from interface: Node
Sets the string contents of the node.

Specified by:
setText in interface Node
Parameters:
text - The new text for the node.

toHtml

public String toHtml()
Description copied from interface: Node
This method will make it easier when using html parser to reproduce html pages (with or without modifications) Applications reproducing html can use this method on nodes which are to be used or transferred as they were recieved, with the original html

Specified by:
toHtml in interface Node

toPlainTextString

public String toPlainTextString()
Description copied from interface: Node
Returns a string representation of the node. This is an important method, it allows a simple string transformation of a web page, regardless of a node.
Typical application code (for extracting only the text from a web page) would then be simplified to :
 Node node;
 for (Enumeration e = parser.elements();e.hasMoreElements();) {
    node = (Node)e.nextElement();
    System.out.println(node.toPlainTextString()); // Or do whatever processing you wish with the plain text string
 }
 

Specified by:
toPlainTextString in interface Node

toString

public String toString()
Description copied from interface: Node
Return the string representation of the node. Subclasses must define this method, and this is typically to be used in the manner
System.out.println(node)

Specified by:
toString in interface Node

doSemanticAction

public void doSemanticAction()
                      throws ParserException
Description copied from interface: Node
Perform the meaning of this tag. This is defined by the tag, for example the bold tag <B> may switch bold text on and off. Only a few tags have semantic meaning to the parser. These have to do with the character set to use (<META>), the base URL to use (<BASE>). Other than that, the semantic meaning is up to the application and it's custom nodes.

Specified by:
doSemanticAction in interface Node
Throws:
ParserException

© 2004 Somik Raha
Mar 14, 2004

HTML Parser is an open source library released under LGPL.
SourceForge.net