Class NodeSequence
- java.lang.Object
-
- org.htmlunit.xpath.Expression
-
- org.htmlunit.xpath.objects.XObject
-
- org.htmlunit.xpath.axes.NodeSequence
-
- All Implemented Interfaces:
java.lang.Cloneable,javax.xml.transform.SourceLocator,PathComponent,ExpressionNode,DTMIterator,XPathVisitable
- Direct Known Subclasses:
XNodeSet
public class NodeSequence extends XObject implements DTMIterator, java.lang.Cloneable, PathComponent
This class is the dynamic wrapper for a Xalan DTMIterator instance, and provides random access capabilities.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classNodeSequence.IteratorCacheEach NodeSequence object has an iterator which is "walked".
-
Field Summary
Fields Modifier and Type Field Description private NodeSequence.IteratorCachem_cacheA cache of a list of nodes obtained from the iterator so far.protected DTMManagerm_dtmMgrThe DTMManager to use if we're using a NodeVector only.protected DTMIteratorm_iterThe functional iterator that fetches nodes.protected intm_lastThe index of the last node in the iteration.protected intm_nextThe index of the next node to be fetched.-
Fields inherited from class org.htmlunit.xpath.objects.XObject
CLASS_BOOLEAN, CLASS_NODESET, CLASS_NULL, CLASS_NUMBER, CLASS_RTREEFRAG, CLASS_STRING, CLASS_UNKNOWN, m_obj
-
Fields inherited from interface org.htmlunit.xpath.xml.dtm.DTMIterator
FILTER_ACCEPT, FILTER_SKIP
-
-
Constructor Summary
Constructors Constructor Description NodeSequence()Create a new NodeSequence in an invalid (null) state.NodeSequence(java.lang.Object nodeVector)Create a new NodeSequence from a (already cloned) iterator.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected intaddNodeInDocOrder(int node)Add the node into a vector of nodes where it should occur in document order.private booleancacheComplete()If this NodeSequence has a cache, and that cache is fully populated then this method returns true, otherwise if there is no cache or it is not complete it returns false.java.lang.Objectclone()Get a clone of this iterator, but don't reset the iteration in the process, so that it may be used from the current position.DTMIteratorcloneWithReset()Get a cloned Iterator that is reset to the start of the iteration.voiddetach()Detaches theDTMIteratorfrom the set which it iterated over, releasing any computational resources and placing the iterator in the INVALID state.intgetAnalysisBits()Get the analysis bits for this path component, as defined in the WalkerFactory.intgetAxis()Returns the axis being iterated, if it is known.private NodeSequence.IteratorCachegetCache()Get the cache (if any) of nodes obtained from the iterator so far.intgetCurrentNode()Get the current node in the iterator.intgetCurrentPos()Get the current position within the cached list, which is one less than the next nextNode() call will retrieve.DTMgetDTM(int nodeHandle)Get an instance of a DTM that "owns" a node handle.DTMManagergetDTMManager()Get an instance of the DTMManager.booleangetExpandEntityReferences()The value of this flag determines whether the children of entity reference nodes are visible to the iterator.protected NodeSequence.IteratorCachegetIteratorCache()Get the cached list of nodes appended with values obtained from the iterator as a NodeSequence is walked when its nextNode() method is called.intgetLength()The number of nodes in the list.intgetRoot()The root node of theDTMIterator, as specified when it was created.protected NodeVectorgetVector()If this iterator needs to cache nodes that are fetched, they are stored in the Vector in the generic object.intgetWhatToShow()This attribute determines which node types are presented via the iterator.booleanhasCache()If the iterator needs to cache nodes as they are fetched, then this method returns true.booleanisDocOrdered()Returns true if all the nodes in the iteration well be returned in document order.booleanisFresh()Tells if this NodeSetDTM is "fresh", in other words, if the first nextNode() that is called will return the first node in the set.intitem(int index)Returns thenode handleof an item in the collection.private voidmarkCacheComplete()If this NodeSequence has a cache, mark that it is complete.intnextNode()Returns the next node in the set and advances the position of the iterator in the set.intpreviousNode()Returns the previous node in the set and moves the position of theDTMIteratorbackwards in the set.voidreset()Reset the iterator to the start.voidrunTo(int index)If an index is requested, NodeSetDTM will call this method to run the iterator to the index.voidsetCurrentPos(int i)Set the current position in the node set.voidsetIter(DTMIterator iter)Set the functional iterator that fetches nodes.protected voidsetObject(java.lang.Object obj)voidsetRoot(int nodeHandle, java.lang.Object environment)Reset the root node of theDTMIterator, overriding the value specified when it was created.voidsetShouldCacheNodes(boolean b)If setShouldCacheNodes(true) is called, then nodes will be cached, enabling random access, and giving the ability to do sorts and the like.protected voidsetVector(NodeVector v)Set the vector where nodes will be cached.-
Methods inherited from class org.htmlunit.xpath.objects.XObject
bool, boolWithSideEffects, callVisitors, deepEquals, equals, error, error, execute, getType, getTypeString, greaterThan, greaterThanOrEqual, iter, lessThan, lessThanOrEqual, mutableNodeset, nodelist, nodeset, notEquals, num, numWithSideEffects, object, str, toString, xstr
-
Methods inherited from class org.htmlunit.xpath.Expression
asIterator, asNode, assertion, bool, canTraverseOutsideSubtree, error, execute, execute, execute, exprGetParent, exprSetParent, getColumnNumber, getExpressionOwner, getLineNumber, getPublicId, getSystemId, isSameClass, isStableNumber, num
-
-
-
-
Field Detail
-
m_last
protected int m_last
The index of the last node in the iteration.
-
m_next
protected int m_next
The index of the next node to be fetched. Useful if this is a cached iterator, and is being used as random access NodeList.
-
m_cache
private NodeSequence.IteratorCache m_cache
A cache of a list of nodes obtained from the iterator so far. This list is appended to until the iterator is exhausted and the cache is complete.Multiple NodeSequence objects may share the same cache.
-
m_iter
protected DTMIterator m_iter
The functional iterator that fetches nodes.
-
m_dtmMgr
protected DTMManager m_dtmMgr
The DTMManager to use if we're using a NodeVector only. We may well want to do away with this, and store it in the NodeVector.
-
-
Method Detail
-
getVector
protected NodeVector getVector()
If this iterator needs to cache nodes that are fetched, they are stored in the Vector in the generic object.
-
getCache
private NodeSequence.IteratorCache getCache()
Get the cache (if any) of nodes obtained from the iterator so far. Note that the cache keeps growing until the iterator is walked to exhaustion, at which point the cache is "complete".
-
setVector
protected void setVector(NodeVector v)
Set the vector where nodes will be cached.
-
hasCache
public boolean hasCache()
If the iterator needs to cache nodes as they are fetched, then this method returns true.
-
cacheComplete
private boolean cacheComplete()
If this NodeSequence has a cache, and that cache is fully populated then this method returns true, otherwise if there is no cache or it is not complete it returns false.
-
markCacheComplete
private void markCacheComplete()
If this NodeSequence has a cache, mark that it is complete. This method should be called after the iterator is exhausted.
-
setIter
public final void setIter(DTMIterator iter)
Set the functional iterator that fetches nodes.- Parameters:
iter- The iterator that is to be contained.
-
getDTM
public DTM getDTM(int nodeHandle)
Get an instance of a DTM that "owns" a node handle. Since a node iterator may be passed without a DTMManager, this allows the caller to easily get the DTM using just the iterator.- Specified by:
getDTMin interfaceDTMIterator- Parameters:
nodeHandle- the nodeHandle.- Returns:
- a non-null DTM reference.
-
getDTMManager
public DTMManager getDTMManager()
Get an instance of the DTMManager. Since a node iterator may be passed without a DTMManager, this allows the caller to easily get the DTMManager using just the iterator.- Specified by:
getDTMManagerin interfaceDTMIterator- Returns:
- a non-null DTMManager reference.
-
getRoot
public int getRoot()
The root node of theDTMIterator, as specified when it was created. Note the root node is not the root node of the document tree, but the context node from where the iteration begins and ends.- Specified by:
getRootin interfaceDTMIterator- Returns:
- nodeHandle int Handle of the context node.
-
setRoot
public void setRoot(int nodeHandle, java.lang.Object environment)Reset the root node of theDTMIterator, overriding the value specified when it was created. Note the root node is not the root node of the document tree, but the context node from where the iteration begins.- Specified by:
setRootin interfaceDTMIterator- Parameters:
nodeHandle- int Handle of the context node.environment- The environment object. The environment in which this iterator operates, which should provide:- a node (the context node... same value as "root" defined below)
- a pair of non-zero positive integers (the context position and the context size)
- a set of variable bindings
- a function library
- the set of namespace declarations in scope for the expression.
At this time the exact implementation of this environment is application dependent. Probably a proper interface will be created fairly soon.
-
reset
public void reset()
Reset the iterator to the start. After resetting, the next node returned will be the root node -- or, if that's filtered out, the first node within the root's subtree which is _not_ skipped by the filters.- Specified by:
resetin interfaceDTMIterator
-
getWhatToShow
public int getWhatToShow()
This attribute determines which node types are presented via the iterator. The available set of constants is defined above. Nodes not accepted bywhatToShowwill be skipped, but their children may still be considered.- Specified by:
getWhatToShowin interfaceDTMIterator- Returns:
- one of the SHOW_XXX constants, or several ORed together.
-
getExpandEntityReferences
public boolean getExpandEntityReferences()
The value of this flag determines whether the children of entity reference nodes are visible to the iterator. If false, they and their descendants will be rejected. Note that this rejection takes precedence overwhatToShowand the filter.To produce a view of the document that has entity references expanded and does not expose the entity reference node itself, use the
whatToShowflags to hide the entity reference node and setexpandEntityReferencesto true when creating the iterator. To produce a view of the document that has entity reference nodes but no entity expansion, use thewhatToShowflags to show the entity reference node and setexpandEntityReferencesto false.NOTE: In Xalan's use of DTM we will generally have fully expanded entity references when the document tree was built, and thus this flag will have no effect.
- Specified by:
getExpandEntityReferencesin interfaceDTMIterator- Returns:
- true if entity references will be expanded.
-
nextNode
public int nextNode()
Returns the next node in the set and advances the position of the iterator in the set. After aDTMIteratorhas setRoot called, the first call tonextNode()returns that root or (if it is rejected by the filters) the first node within its subtree which is not filtered out.- Specified by:
nextNodein interfaceDTMIterator- Returns:
- The next node handle in the set being iterated over, or
DTM.NULLif there are no more members in that set.
-
previousNode
public int previousNode()
Returns the previous node in the set and moves the position of theDTMIteratorbackwards in the set.- Specified by:
previousNodein interfaceDTMIterator- Returns:
- The previous node handle in the set being iterated over, or
DTM.NULLif there are no more members in that set.
-
detach
public void detach()
Detaches theDTMIteratorfrom the set which it iterated over, releasing any computational resources and placing the iterator in the INVALID state. Afterdetachhas been invoked, calls tonextNodeorpreviousNodewill raise a runtime exception.- Specified by:
detachin interfaceDTMIterator- Overrides:
detachin classXObject
-
getCurrentNode
public int getCurrentNode()
Get the current node in the iterator. Note that this differs from the DOM's NodeIterator, where the current position lies between two nodes (as part of the maintain-relative-position semantic).- Specified by:
getCurrentNodein interfaceDTMIterator- Returns:
- The current node handle, or -1.
-
isFresh
public boolean isFresh()
Tells if this NodeSetDTM is "fresh", in other words, if the first nextNode() that is called will return the first node in the set.- Specified by:
isFreshin interfaceDTMIterator- Returns:
- true if the iteration of this list has not yet begun.
-
setShouldCacheNodes
public void setShouldCacheNodes(boolean b)
If setShouldCacheNodes(true) is called, then nodes will be cached, enabling random access, and giving the ability to do sorts and the like. They are not cached by default.%REVIEW% Shouldn't the other random-access methods throw an exception if they're called on a DTMIterator with this flag set false?
- Specified by:
setShouldCacheNodesin interfaceDTMIterator- Parameters:
b- true if the nodes should be cached.
-
getCurrentPos
public int getCurrentPos()
Get the current position within the cached list, which is one less than the next nextNode() call will retrieve. i.e. if you call getCurrentPos() and the return is 0, the next fetch will take place at index 1.- Specified by:
getCurrentPosin interfaceDTMIterator- Returns:
- The position of the iteration.
-
runTo
public void runTo(int index)
If an index is requested, NodeSetDTM will call this method to run the iterator to the index. By default this sets m_next to the index. If the index argument is -1, this signals that the iterator should be run to the end and completely fill the cache.- Specified by:
runToin interfaceDTMIterator- Parameters:
index- The index to run to, or -1 if the iterator should be run to the end.
-
setCurrentPos
public void setCurrentPos(int i)
Set the current position in the node set.- Specified by:
setCurrentPosin interfaceDTMIterator- Parameters:
i- Must be a valid index.
-
item
public int item(int index)
Returns thenode handleof an item in the collection. Ifindexis greater than or equal to the number of nodes in the list, this returnsnull.- Specified by:
itemin interfaceDTMIterator- Parameters:
index- of the item.- Returns:
- The node handle at the
indexth position in theDTMIterator, or-1if that is not a valid index.
-
getLength
public int getLength()
The number of nodes in the list. The range of valid child node indices is 0 tolength-1inclusive. Note that this requires running the iterator to completion, and presumably filling the cache.- Specified by:
getLengthin interfaceDTMIterator- Returns:
- The number of nodes in the list.
-
cloneWithReset
public DTMIterator cloneWithReset() throws java.lang.CloneNotSupportedException
Get a cloned Iterator that is reset to the start of the iteration.- Specified by:
cloneWithResetin interfaceDTMIterator- Returns:
- A clone of this iteration that has been reset.
- Throws:
java.lang.CloneNotSupportedException- if any
-
clone
public java.lang.Object clone() throws java.lang.CloneNotSupportedExceptionGet a clone of this iterator, but don't reset the iteration in the process, so that it may be used from the current position.- Specified by:
clonein interfaceDTMIterator- Overrides:
clonein classjava.lang.Object- Returns:
- A clone of this object.
- Throws:
java.lang.CloneNotSupportedException- if any
-
isDocOrdered
public boolean isDocOrdered()
Returns true if all the nodes in the iteration well be returned in document order.- Specified by:
isDocOrderedin interfaceDTMIterator- Returns:
- true if all the nodes in the iteration well be returned in document order.
-
getAxis
public int getAxis()
Returns the axis being iterated, if it is known.- Specified by:
getAxisin interfaceDTMIterator- Returns:
- Axis.CHILD, etc., or -1 if the axis is not known or is of multiple types.
-
getAnalysisBits
public int getAnalysisBits()
Get the analysis bits for this path component, as defined in the WalkerFactory.- Specified by:
getAnalysisBitsin interfacePathComponent- Returns:
- One of WalkerFactory#BIT_DESCENDANT, etc.
-
addNodeInDocOrder
protected int addNodeInDocOrder(int node)
Add the node into a vector of nodes where it should occur in document order.- Parameters:
node- The node to be added.- Returns:
- insertIndex.
- Throws:
java.lang.RuntimeException- thrown if this NodeSetDTM is not of a mutable type.
-
getIteratorCache
protected NodeSequence.IteratorCache getIteratorCache()
Get the cached list of nodes appended with values obtained from the iterator as a NodeSequence is walked when its nextNode() method is called.
-
-