|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.crimson.tree.TreeWalker
This class implements a preorder depth first walk over the tree rooted at a given DOM node. The traversal is "live", and terminates either when that given node is reached when climbing back up, or when a null parent node is reached. It may be restarted via reset().
The way this remains live is to have a "current" node to which the walk is tied. If the tree is modified, that current node will always still be valid ... even if it is no longer connected to the rest of the document, or if it's reconnected at a different location. The behavior of tree modifications is specified by DOM, and the interaction with a walker's current node is specified entirely by knowing that only the getNextSibling, getParentNode, and getFirstChild methods are used for walking the tree.
For example, if the current branch is cut off, the walk will stop when it tries to access what were parents or siblings of that node. (That is, the walk will continue over the branch that was cut.) If that is not the intended behaviour, one must change the "current" branch before cutting ... much like avoiding trimming a branch off a real tree if someone is sitting on it. The removeCurrent() method encapsulates that logic.
Field Summary | |
private org.w3c.dom.Node |
current
|
private org.w3c.dom.Node |
startPoint
|
Constructor Summary | |
TreeWalker(org.w3c.dom.Node initial)
Constructs a tree walker starting at the given node. |
Method Summary | |
org.w3c.dom.Node |
getCurrent()
Returns the current node. |
org.w3c.dom.Node |
getNext()
Advances to the next node, and makes that current. |
org.w3c.dom.Element |
getNextElement(String tag)
Convenience method to walk only through elements with the specified tag name. |
org.w3c.dom.Element |
getNextElement(String nsURI,
String localName)
Namespace version |
org.w3c.dom.Node |
removeCurrent()
Removes the current node; reassigns the current node to be the next one in the current walk that isn't a child of the (removed) current node, and returns that new current node. |
void |
reset()
Resets the walker to the state in which it was created: the current node will be the node given to the constructor. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
private org.w3c.dom.Node startPoint
private org.w3c.dom.Node current
Constructor Detail |
public TreeWalker(org.w3c.dom.Node initial)
Method Detail |
public org.w3c.dom.Node getCurrent()
public org.w3c.dom.Node getNext()
public org.w3c.dom.Element getNextElement(String tag)
tag
- the tag to match, or null to indicate all elements
public org.w3c.dom.Element getNextElement(String nsURI, String localName)
public void reset()
public org.w3c.dom.Node removeCurrent()
IllegalStateException
- if the current node is null
or has no parent (it is a Document or DocumentFragment)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |