Class NodeSetDTM
- All Implemented Interfaces:
Serializable,Cloneable,DTMIterator
- Direct Known Subclasses:
RTFIterator
public class NodeSetDTM extends NodeVector implements DTMIterator, Cloneable
The NodeSetDTM class can act as either a NodeVector, NodeList, or NodeIterator. However, in order for it to act as a NodeVector or NodeList, it's required that setShouldCacheNodes(true) be called before the first nextNode() is called, in order that nodes can be added as they are fetched. Derived classes that implement iterators must override runTo(int index), in order that they may run the iteration to the given index.
Note that we directly implement the DOM's NodeIterator interface. We do not emulate all the behavior of the standard NodeIterator. In particular, we do not guarantee to present a "live view" of the document ... but in XSLT, the source document should never be mutated, so this should never be an issue.
Thought: Should NodeSetDTM really implement NodeList and NodeIterator, or should there be specific subclasses of it which do so? The advantage of doing it all here is that all NodeSetDTMs will respond to the same calls; the disadvantage is that some of them may return less-than-enlightening results when you do so.
- See Also:
- Serialized Form
-
Field Summary
Fields Modifier and Type Field Description protected booleanm_cacheNodesTrue if this list is cached.protected booleanm_mutableTrue if this list can be mutated.protected intm_nextIf this node is being used as an iterator, the next index that nextNode() will return.protected intm_rootThe root of the iteration, if available.Fields inherited from class org.apache.xml.utils.NodeVector
m_firstFreeFields inherited from interface org.apache.xml.dtm.DTMIterator
FILTER_ACCEPT, FILTER_REJECT, FILTER_SKIP -
Constructor Summary
Constructors Constructor Description NodeSetDTM(int blocksize, int dummy, DTMManager dtmManager)Create an empty, using the given block size.NodeSetDTM(int node, DTMManager dtmManager)Create a NodeSetDTM which contains the given Node.NodeSetDTM(DTMIterator ni)Create a NodeSetDTM, and copy the members of the given DTMIterator into it.NodeSetDTM(DTMManager dtmManager)Create an empty nodelist.NodeSetDTM(NodeSetDTM nodelist)Create a NodeSetDTM, and copy the members of the given NodeSetDTM into it.NodeSetDTM(NodeList nodeList, XPathContext xctxt)Create a NodeSetDTM, and copy the members of the given DTMIterator into it.NodeSetDTM(NodeIterator iterator, XPathContext xctxt)Create a NodeSetDTM, and copy the members of the given DTMIterator into it. -
Method Summary
Modifier and Type Method Description voidaddElement(int value)Append a Node onto the vector.voidaddNode(int n)Add a node to the NodeSetDTM.intaddNodeInDocOrder(int node, boolean test, XPathContext support)Add the node into a vector of nodes where it should occur in document order.intaddNodeInDocOrder(int node, XPathContext support)Add the node into a vector of nodes where it should occur in document order.voidaddNodes(DTMIterator iterator)Copy NodeList members into this nodelist, adding in document order.voidaddNodesInDocOrder(DTMIterator iterator, XPathContext support)Copy NodeList members into this nodelist, adding in document order.voidallowDetachToRelease(boolean allowRelease)Specify if it's OK for detach to release the iterator for reuse.voidappendNodes(NodeVector nodes)Append the nodes to the list.Objectclone()Clone this NodeSetDTM.DTMIteratorcloneWithReset()Get a cloned Iterator, and reset its state to the beginning of the iteration.booleancontains(int s)Tell if the table contains the given node.voiddetach()Detaches the iterator from the set which it iterated over, releasing any computational resources and placing the iterator in the INVALID state.intelementAt(int i)Get the nth element.intgetAxis()Returns the axis being iterated, if it is known.intgetCurrentNode()Return the last fetched node.intgetCurrentPos()Get the current position, 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.DTMFiltergetFilter()The filter object used to screen nodes.intgetLast()intgetLength()The number of nodes in the list.intgetRoot()The root node of theDTMIterator, as specified when it was created.booleangetShouldCacheNodes()Get whether or not this is a cached node set.intgetWhatToShow()This attribute determines which node types are presented via the iterator.intindexOf(int elem)Searches for the first occurence of the given argument, beginning the search at index, and testing for equality using the equals method.intindexOf(int elem, int index)Searches for the first occurence of the given argument, beginning the search at index, and testing for equality using the equals method.voidinsertElementAt(int value, int at)Inserts the specified node in this vector at the specified index.voidinsertNode(int n, int pos)Insert a node at a given position.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.booleanisMutable()Tells if this iterator can have nodes added to it or set via thesetItem(int node, int index)method.intitem(int index)Returns theindexth item in the collection.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 the iterator backwards in the set.voidremoveAllElements()Inserts the specified node in this vector at the specified index.booleanremoveElement(int s)Removes the first occurrence of the argument from this vector.voidremoveElementAt(int i)Deletes the component at the specified index.voidremoveNode(int n)Remove a node.voidreset()Reset the iterator.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.voidsetElementAt(int node, int index)Sets the component at the specified index of this vector to be the specified object.voidsetEnvironment(Object environment)Set the environment in which this iterator operates, which should provide: a node (the context node...voidsetItem(int node, int index)Same as setElementAt.voidsetLast(int last)voidsetRoot(int context, Object environment)Initialize the context values for this expression after it is cloned.voidsetShouldCacheNodes(boolean b)If setShouldCacheNodes(true) is called, then nodes will be cached.intsize()Get the length of the list.Methods inherited from class org.apache.xml.utils.NodeVector
insertInOrder, peepOrNull, peepTail, peepTailSub1, pop, popAndTop, popPair, popQuick, push, pushPair, RemoveAllNoClear, setTail, setTailSub1, sort, sort
-
Field Details
-
m_next
protected transient int m_nextIf this node is being used as an iterator, the next index that nextNode() will return. -
m_mutable
protected transient boolean m_mutableTrue if this list can be mutated. -
m_cacheNodes
protected transient boolean m_cacheNodesTrue if this list is cached. -
m_root
protected int m_rootThe root of the iteration, if available.
-
-
Constructor Details
-
NodeSetDTM
Create an empty nodelist. -
NodeSetDTM
Create an empty, using the given block size.- Parameters:
blocksize- Size of blocks to allocatedummy- pass zero for right now...
-
NodeSetDTM
Create a NodeSetDTM, and copy the members of the given NodeSetDTM into it.- Parameters:
nodelist- Set of Nodes to be made members of the new set.
-
NodeSetDTM
Create a NodeSetDTM, and copy the members of the given DTMIterator into it.- Parameters:
ni- Iterator which yields Nodes to be made members of the new set.
-
NodeSetDTM
Create a NodeSetDTM, and copy the members of the given DTMIterator into it.- Parameters:
iterator- Iterator which yields Nodes to be made members of the new set.
-
NodeSetDTM
Create a NodeSetDTM, and copy the members of the given DTMIterator into it. -
NodeSetDTM
Create a NodeSetDTM which contains the given Node.- Parameters:
node- Single node to be added to the new set.
-
-
Method Details
-
setEnvironment
Set 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.
- Parameters:
environment- The environment object.
-
getRoot
public int getRoot()Description copied from interface:DTMIteratorThe 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:
- The root node of the Iterator, as specified when it was created. For non-Iterator NodeSetDTMs, this will be null.
-
setRoot
Initialize the context values for this expression after it is cloned.- Specified by:
setRootin interfaceDTMIterator- Parameters:
context- The XPath runtime context for this transformation.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.
-
clone
Clone this NodeSetDTM. At this time, we only expect this to be used with LocPathIterators; it may not work with other kinds of NodeSetDTMs.- Specified by:
clonein interfaceDTMIterator- Overrides:
clonein classNodeVector- Returns:
- a new NodeSetDTM of the same type, having the same state... though unless overridden in the subclasses, it may not copy all the state information.
- Throws:
CloneNotSupportedException- if this subclass of NodeSetDTM does not support the clone() operation.
-
cloneWithReset
Get a cloned Iterator, and reset its state to the beginning of the iteration.- Specified by:
cloneWithResetin interfaceDTMIterator- Returns:
- a new NodeSetDTM of the same type, having the same state... except that the reset() operation has been called.
- Throws:
CloneNotSupportedException- if this subclass of NodeSetDTM does not support the clone() operation.
-
reset
public void reset()Reset the iterator. May have no effect on non-iterator Nodesets.- 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 in theDTMFilterinterface. For NodeSetDTMs, the mask has been hardcoded to show all nodes except EntityReference nodes, which have no equivalent in the XPath data model.- Specified by:
getWhatToShowin interfaceDTMIterator- Returns:
- integer used as a bit-array, containing flags defined in
the DOM's DTMFilter class. The value will be
SHOW_ALL & ~SHOW_ENTITY_REFERENCE, meaning that only entity references are suppressed.
-
getFilter
The filter object used to screen nodes. Filters are applied to further reduce (and restructure) the DTMIterator's view of the document. In our case, we will be using hardcoded filters built into our iterators... but getFilter() is part of the DOM's DTMIterator interface, so we have to support it.- Returns:
- null, which is slightly misleading. True, there is no user-written filter object, but in fact we are doing some very sophisticated custom filtering. A DOM purist might suggest returning a placeholder object just to indicate that this is not going to return all nodes selected by whatToShow.
-
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 will be skipped over.
To produce a view of the document that has entity references expanded and does not expose the entity reference node itself, use the whatToShow flags to hide the entity reference node and set expandEntityReferences to true when creating the iterator. To produce a view of the document that has entity reference nodes but no entity expansion, use the whatToShow flags to show the entity reference node and set expandEntityReferences to false.- Specified by:
getExpandEntityReferencesin interfaceDTMIterator- Returns:
- true for all iterators based on NodeSetDTM, meaning that the contents of EntityRefrence nodes may be returned (though whatToShow says that the EntityReferences themselves are not shown.)
-
getDTM
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
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.
-
nextNode
public int nextNode()Returns the next node in the set and advances the position of the iterator in the set. After a DTMIterator is created, the first call to nextNode() returns the first node in the set.- Specified by:
nextNodein interfaceDTMIterator- Returns:
- The next
Nodein the set being iterated over, orDTM.NULLif there are no more members in that set. - Throws:
DOMException- INVALID_STATE_ERR: Raised if this method is called after thedetachmethod was invoked.
-
previousNode
public int previousNode()Returns the previous node in the set and moves the position of the iterator backwards in the set.- Specified by:
previousNodein interfaceDTMIterator- Returns:
- The previous
Nodein the set being iterated over, orDTM.NULLif there are no more members in that set. - Throws:
DOMException- INVALID_STATE_ERR: Raised if this method is called after thedetachmethod was invoked.RuntimeException- thrown if this NodeSetDTM is not of a cached type, and hence doesn't know what the previous node was.
-
detach
public void detach()Detaches the iterator from the set which it iterated over, releasing any computational resources and placing the iterator in the INVALID state. Afterdetachhas been invoked, calls tonextNodeorpreviousNodewill raise the exception INVALID_STATE_ERR.This operation is a no-op in NodeSetDTM, and will not cause INVALID_STATE_ERR to be raised by later operations.
- Specified by:
detachin interfaceDTMIterator
-
allowDetachToRelease
public void allowDetachToRelease(boolean allowRelease)Specify if it's OK for detach to release the iterator for reuse.- Specified by:
allowDetachToReleasein interfaceDTMIterator- Parameters:
allowRelease- true if it is OK for detach to release this iterator for pooling.
-
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 nextNode() would return the first node in the set, false if it would return a later one.
-
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.- Specified by:
runToin interfaceDTMIterator- Parameters:
index- Position to advance (or retreat) to, with 0 requesting the reset ("fresh") position and -1 (or indeed any out-of-bounds value) requesting the final position.- Throws:
RuntimeException- thrown if this NodeSetDTM is not one of the types which supports indexing/counting.
-
item
public int item(int index)Returns theindexth item in the collection. Ifindexis greater than or equal to the number of nodes in the list, this returnsnull. TODO: What happens if index is out of range?- Specified by:
itemin interfaceDTMIterator- Parameters:
index- Index into the collection.- Returns:
- The node at the
indexth position in theNodeList, ornullif 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 operation requires finding all the matching nodes, which may defeat attempts to defer that work.- Specified by:
getLengthin interfaceDTMIterator- Returns:
- integer indicating how many nodes are represented by this list.
-
addNode
public void addNode(int n)Add a node to the NodeSetDTM. Not all types of NodeSetDTMs support this operation- Parameters:
n- Node to be added- Throws:
RuntimeException- thrown if this NodeSetDTM is not of a mutable type.
-
insertNode
public void insertNode(int n, int pos)Insert a node at a given position.- Parameters:
n- Node to be addedpos- Offset at which the node is to be inserted, with 0 being the first position.- Throws:
RuntimeException- thrown if this NodeSetDTM is not of a mutable type.
-
removeNode
public void removeNode(int n)Remove a node.- Parameters:
n- Node to be added- Throws:
RuntimeException- thrown if this NodeSetDTM is not of a mutable type.
-
addNodes
Copy NodeList members into this nodelist, adding in document order. Null references are not added.- Parameters:
iterator- DTMIterator which yields the nodes to be added.- Throws:
RuntimeException- thrown if this NodeSetDTM is not of a mutable type.
-
addNodesInDocOrder
Copy NodeList members into this nodelist, adding in document order. If a node is null, don't add it.- Parameters:
iterator- DTMIterator which yields the nodes to be added.support- The XPath runtime context.- Throws:
RuntimeException- thrown if this NodeSetDTM is not of a mutable type.
-
addNodeInDocOrder
Add the node into a vector of nodes where it should occur in document order.- Parameters:
node- The node to be added.test- true if we should test for doc ordersupport- The XPath runtime context.- Returns:
- insertIndex.
- Throws:
RuntimeException- thrown if this NodeSetDTM is not of a mutable type.
-
addNodeInDocOrder
Add the node into a vector of nodes where it should occur in document order.- Parameters:
node- The node to be added.support- The XPath runtime context.- Returns:
- The index where it was inserted.
- Throws:
RuntimeException- thrown if this NodeSetDTM is not of a mutable type.
-
size
public int size()Get the length of the list.- Overrides:
sizein classNodeVector- Returns:
- The size of this node set.
-
addElement
public void addElement(int value)Append a Node onto the vector.- Overrides:
addElementin classNodeVector- Parameters:
value- The node to be added.- Throws:
RuntimeException- thrown if this NodeSetDTM is not of a mutable type.
-
insertElementAt
public void insertElementAt(int value, int at)Inserts the specified node in this vector at the specified index. Each component in this vector with an index greater or equal to the specified index is shifted upward to have an index one greater than the value it had previously.- Overrides:
insertElementAtin classNodeVector- Parameters:
value- The node to be inserted.at- The index where the insert should occur.- Throws:
RuntimeException- thrown if this NodeSetDTM is not of a mutable type.
-
appendNodes
Append the nodes to the list.- Overrides:
appendNodesin classNodeVector- Parameters:
nodes- The nodes to be appended to this node set.- Throws:
RuntimeException- thrown if this NodeSetDTM is not of a mutable type.
-
removeAllElements
public void removeAllElements()Inserts the specified node in this vector at the specified index. Each component in this vector with an index greater or equal to the specified index is shifted upward to have an index one greater than the value it had previously.- Overrides:
removeAllElementsin classNodeVector- Throws:
RuntimeException- thrown if this NodeSetDTM is not of a mutable type.
-
removeElement
public boolean removeElement(int s)Removes the first occurrence of the argument from this vector. If the object is found in this vector, each component in the vector with an index greater or equal to the object's index is shifted downward to have an index one smaller than the value it had previously.- Overrides:
removeElementin classNodeVector- Parameters:
s- The node to be removed.- Returns:
- True if the node was successfully removed
- Throws:
RuntimeException- thrown if this NodeSetDTM is not of a mutable type.
-
removeElementAt
public void removeElementAt(int i)Deletes the component at the specified index. Each component in this vector with an index greater or equal to the specified index is shifted downward to have an index one smaller than the value it had previously.- Overrides:
removeElementAtin classNodeVector- Parameters:
i- The index of the node to be removed.- Throws:
RuntimeException- thrown if this NodeSetDTM is not of a mutable type.
-
setElementAt
public void setElementAt(int node, int index)Sets the component at the specified index of this vector to be the specified object. The previous component at that position is discarded. The index must be a value greater than or equal to 0 and less than the current size of the vector.- Overrides:
setElementAtin classNodeVector- Parameters:
node- The node to be set.index- The index of the node to be replaced.- Throws:
RuntimeException- thrown if this NodeSetDTM is not of a mutable type.
-
setItem
public void setItem(int node, int index)Same as setElementAt.- Specified by:
setItemin interfaceDTMIterator- Parameters:
node- The node to be set.index- The index of the node to be replaced.- Throws:
RuntimeException- thrown if this NodeSetDTM is not of a mutable type.
-
elementAt
public int elementAt(int i)Get the nth element.- Overrides:
elementAtin classNodeVector- Parameters:
i- The index of the requested node.- Returns:
- Node at specified index.
-
contains
public boolean contains(int s)Tell if the table contains the given node.- Overrides:
containsin classNodeVector- Parameters:
s- Node to look for- Returns:
- True if the given node was found.
-
indexOf
public int indexOf(int elem, int index)Searches for the first occurence of the given argument, beginning the search at index, and testing for equality using the equals method.- Overrides:
indexOfin classNodeVector- Parameters:
elem- Node to look forindex- Index of where to start the search- Returns:
- the index of the first occurrence of the object argument in this vector at position index or later in the vector; returns -1 if the object is not found.
-
indexOf
public int indexOf(int elem)Searches for the first occurence of the given argument, beginning the search at index, and testing for equality using the equals method.- Overrides:
indexOfin classNodeVector- Parameters:
elem- Node to look for- Returns:
- the index of the first occurrence of the object argument in this vector at position index or later in the vector; returns -1 if the object is not found.
-
getCurrentPos
public int getCurrentPos()Get the current position, 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 the current position index.
-
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.- Throws:
RuntimeException- thrown if this NodeSetDTM is not of a cached type, and thus doesn't permit indexed access.
-
getCurrentNode
public int getCurrentNode()Return the last fetched node. Needed to support the UnionPathIterator.- Specified by:
getCurrentNodein interfaceDTMIterator- Returns:
- the last fetched node.
- Throws:
RuntimeException- thrown if this NodeSetDTM is not of a cached type, and thus doesn't permit indexed access.
-
getShouldCacheNodes
public boolean getShouldCacheNodes()Get whether or not this is a cached node set.- Returns:
- True if this list is cached.
-
setShouldCacheNodes
public void setShouldCacheNodes(boolean b)If setShouldCacheNodes(true) is called, then nodes will be cached. They are not cached by default. This switch must be set before the first call to nextNode is made, to ensure that all nodes are cached.- Specified by:
setShouldCacheNodesin interfaceDTMIterator- Parameters:
b- true if this node set should be cached.- Throws:
RuntimeException- thrown if an attempt is made to request caching after we've already begun stepping through the nodes in this set.
-
isMutable
public boolean isMutable()Tells if this iterator can have nodes added to it or set via thesetItem(int node, int index)method.- Specified by:
isMutablein interfaceDTMIterator- Returns:
- True if the nodelist can be mutated.
-
getLast
public int getLast() -
setLast
public void setLast(int last) -
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 as a default.
-
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.
-