org.htmlparser.util

Class NodeTreeWalker

public class NodeTreeWalker extends Object implements NodeIterator

A class for walking a tree of Node objects, in either a depth-first or breadth-first manner. The following two diagrams show the represent tree traversal with the two different methods. Diagram showing depth-first tree traversalDiagram showing breadth-first tree traversal
Depth-first traversal Breadth-first traversal

Author: ian_macfarlane

Field Summary
protected NodemCurrentNode
The current Node element, which will be a child of the root Node, or null.
protected booleanmDepthFirst
Whether the tree traversal method used is depth-first (default) or breadth-first.
protected intmMaxDepth
The maximum depth (child-parent links) from which this NodeTreeWalker may be removed from the root Node.
protected NodemNextNode
The next Node element after the current Node element.
protected NodemRootNode
The root Node element which defines the scope of the current tree to walk.
Constructor Summary
NodeTreeWalker(Node rootNode)
Creates a new instance of NodeTreeWalker using depth-first tree traversal, without limits on how deep it may traverse.
NodeTreeWalker(Node rootNode, boolean depthFirst)
Creates a new instance of NodeTreeWalker using the specified type of tree traversal, without limits on how deep it may traverse.
NodeTreeWalker(Node rootNode, boolean depthFirst, int maxDepth)
Creates a new instance of NodeTreeWalker using the specified type of tree traversal and maximum depth from the root Node to traverse.
Method Summary
NodegetCurrentNode()
Get the Node in the tree that the NodeTreeWalker is current at.
intgetCurrentNodeDepth()
Get the number of places down that the current Node is from the root Node.
intgetMaxDepth()
The maximum depth (number of child-parent links) below the root Node that this NodeTreeWalker may traverse.
protected NodegetNextNodeBreadthFirst()
Traverses to the next Node from the current Node using breadth-first tree traversal
protected NodegetNextNodeDepthFirst()
Traverses to the next Node from the current Node using depth-first tree traversal
NodegetRootNode()
Get the root Node that defines the scope of the tree to traverse.
booleanhasMoreNodes()
Returns whether or not there are more nodes available based on the current configuration of this NodeTreeWalker.
protected voidinitRootNode(Node rootNode)
Sets the root Node to be the given Node.
booleanisDepthFirst()
Whether the NodeTreeWalker is currently set to use depth-first or breadth-first tree traversal.
NodenextNode()
Traverses to the next Node from the current Node, using either depth-first or breadth-first tree traversal as appropriate.
voidremoveMaxDepthRestriction()
Removes any restrictions in place that prevent this NodeTreeWalker from traversing beyond a certain depth.
voidreset()
Resets the current position in the tree, such that calling nextNode() will return the first Node again.
voidsetCurrentNodeAsRootNode()
Sets the current Node as the root Node.
voidsetDepthFirst(boolean depthFirst)
Sets whether the NodeTreeWalker should use depth-first or breadth-first tree traversal.
voidsetRootNode(Node rootNode)
Sets the specified Node as the root Node.

Field Detail

mCurrentNode

protected Node mCurrentNode
The current Node element, which will be a child of the root Node, or null.

mDepthFirst

protected boolean mDepthFirst
Whether the tree traversal method used is depth-first (default) or breadth-first.

mMaxDepth

protected int mMaxDepth
The maximum depth (child-parent links) from which this NodeTreeWalker may be removed from the root Node. A value of -1 indicates that there is no depth restriction.

mNextNode

protected Node mNextNode
The next Node element after the current Node element. Stored for internal use only.

mRootNode

protected Node mRootNode
The root Node element which defines the scope of the current tree to walk.

Constructor Detail

NodeTreeWalker

public NodeTreeWalker(Node rootNode)
Creates a new instance of NodeTreeWalker using depth-first tree traversal, without limits on how deep it may traverse.

Parameters: rootNode Node The Node to set as the root of the tree.

Throws: NullPointerException if root Node is null.

NodeTreeWalker

public NodeTreeWalker(Node rootNode, boolean depthFirst)
Creates a new instance of NodeTreeWalker using the specified type of tree traversal, without limits on how deep it may traverse.

Parameters: rootNode The Node to set as the root of the tree. depthFirst Whether to use depth-first (true) or breadth-first (false) tree traversal.

Throws: NullPointerException if rootNode is null.

NodeTreeWalker

public NodeTreeWalker(Node rootNode, boolean depthFirst, int maxDepth)
Creates a new instance of NodeTreeWalker using the specified type of tree traversal and maximum depth from the root Node to traverse.

Parameters: rootNode The Node to set as the root of the tree. depthFirst Whether to use depth-first (true) or breadth-first (false) tree traversal. maxDepth The maximum depth from the root Node that this NodeTreeWalker may traverse. This must be > 0 or equal to -1.

Throws: NullPointerException if rootNode is null. IllegalArgumentException maxDepth is not > 0 or equal to -1.

Method Detail

getCurrentNode

public Node getCurrentNode()
Get the Node in the tree that the NodeTreeWalker is current at.

Returns: The current Node.

getCurrentNodeDepth

public int getCurrentNodeDepth()
Get the number of places down that the current Node is from the root Node. Returns 1 if current Node is a child of the root Node. Returns 0 if this NodeTreeWalker has not yet traversed to any Nodes.

Returns: The depth the current Node is from the root Node.

getMaxDepth

public int getMaxDepth()
The maximum depth (number of child-parent links) below the root Node that this NodeTreeWalker may traverse.

Returns: The maximum depth that this NodeTreeWalker can traverse to.

getNextNodeBreadthFirst

protected Node getNextNodeBreadthFirst()
Traverses to the next Node from the current Node using breadth-first tree traversal

Returns: The next Node from the current Node using breadth-first tree traversal.

getNextNodeDepthFirst

protected Node getNextNodeDepthFirst()
Traverses to the next Node from the current Node using depth-first tree traversal

Returns: The next Node from the current Node using depth-first tree traversal.

getRootNode

public Node getRootNode()
Get the root Node that defines the scope of the tree to traverse.

Returns: The root Node.

hasMoreNodes

public boolean hasMoreNodes()
Returns whether or not there are more nodes available based on the current configuration of this NodeTreeWalker.

Returns: True if there are more Nodes available, based on the current configuration, or false otherwise.

initRootNode

protected void initRootNode(Node rootNode)
Sets the root Node to be the given Node. Resets the current position in the tree.

Parameters: rootNode The Node to set as the root of the tree.

Throws: NullPointerException if rootNode is null.

isDepthFirst

public boolean isDepthFirst()
Whether the NodeTreeWalker is currently set to use depth-first or breadth-first tree traversal.

Returns: True if depth-first tree-traversal is used, or false if breadth-first tree-traversal is being used.

nextNode

public Node nextNode()
Traverses to the next Node from the current Node, using either depth-first or breadth-first tree traversal as appropriate.

Returns: The next Node from the current Node.

removeMaxDepthRestriction

public void removeMaxDepthRestriction()
Removes any restrictions in place that prevent this NodeTreeWalker from traversing beyond a certain depth.

reset

public void reset()
Resets the current position in the tree, such that calling nextNode() will return the first Node again.

setCurrentNodeAsRootNode

public void setCurrentNodeAsRootNode()
Sets the current Node as the root Node. Resets the current position in the tree.

Throws: NullPointerException if the current Node is null (i.e. if the tree traversal has not yet begun).

setDepthFirst

public void setDepthFirst(boolean depthFirst)
Sets whether the NodeTreeWalker should use depth-first or breadth-first tree traversal.

Parameters: depthFirst Whether to use depth-first (true) or breadth-first (false) tree traversal.

setRootNode

public void setRootNode(Node rootNode)
Sets the specified Node as the root Node. Resets the current position in the tree.

Parameters: rootNode The Node to set as the root of the tree.

Throws: NullPointerException if rootNode is null.

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