public class PrecedingAxisIterator
extends java.lang.Object
implements java.util.Iterator
Represents the XPath preceding
axis.
The "preceding
axis contains all nodes in the same document as the context
node that are before the context node in document order, excluding any ancestors
and excluding attribute nodes and namespace nodes."
This implementation of 'preceding
' works like so:
the preceding
axis includes preceding siblings of this node and
their descendants. Also, for each ancestor node of this node, it includes
all preceding siblings of that ancestor, and their descendants. Finally, it
includes the ancestor nodes themselves.
The reversed descendant-or-self
axes that are required are calculated using a
stack of reversed 'child-or-self' axes. When asked for a node, it is always taken
from a child-or-self axis. If it was the last node on that axis, the node is returned.
Otherwise, this axis is pushed on the stack, and the process is repeated with the child-or-self
of the node. Eventually this recurses down to the last descendant of any node, then works
back up to the root.
Most object models could provide a faster implementation of the reversed 'children-or-self' used here.
Modifier and Type | Field and Description |
---|---|
private java.util.Iterator |
ancestorOrSelf |
private java.util.ListIterator |
childrenOrSelf |
private Navigator |
navigator |
private java.util.Iterator |
precedingSibling |
private java.util.ArrayList |
stack |
Constructor and Description |
---|
PrecedingAxisIterator(java.lang.Object contextNode,
Navigator navigator)
Create a new
preceding axis iterator. |
Modifier and Type | Method and Description |
---|---|
private java.util.ListIterator |
childrenOrSelf(java.lang.Object node) |
boolean |
hasNext()
Returns true if there are any preceding nodes remaining; false otherwise.
|
java.lang.Object |
next()
Returns the next preceding node.
|
void |
remove()
This operation is not supported.
|
private java.util.Iterator ancestorOrSelf
private java.util.Iterator precedingSibling
private java.util.ListIterator childrenOrSelf
private java.util.ArrayList stack
private Navigator navigator
public PrecedingAxisIterator(java.lang.Object contextNode, Navigator navigator) throws UnsupportedAxisException
preceding
axis iterator.contextNode
- the node to start fromnavigator
- the object model specific navigatorUnsupportedAxisException
public boolean hasNext()
hasNext
in interface java.util.Iterator
Iterator.hasNext()
private java.util.ListIterator childrenOrSelf(java.lang.Object node)
public java.lang.Object next() throws java.util.NoSuchElementException
next
in interface java.util.Iterator
java.util.NoSuchElementException
- if no preceding nodes remainIterator.next()
public void remove() throws java.lang.UnsupportedOperationException
remove
in interface java.util.Iterator
java.lang.UnsupportedOperationException
- always