Class NodeSetDTM

  • All Implemented Interfaces:
    java.lang.Cloneable, DTMIterator

    public class NodeSetDTM
    extends NodeVector
    implements DTMIterator, java.lang.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.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected boolean m_cacheNodes
      True if this list is cached.
      (package private) DTMManager m_manager  
      protected int m_next
      If this node is being used as an iterator, the next index that nextNode() will return.
      protected int m_root
      The root of the iteration, if available.
    • Constructor Summary

      Constructors 
      Constructor Description
      NodeSetDTM​(DTMIterator ni)
      Create a NodeSetDTM, and copy the members of the given DTMIterator into it.
      NodeSetDTM​(DTMManager dtmManager)
      Create an empty nodelist.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addNode​(int n)
      Add a node to the NodeSetDTM.
      void addNodeInDocOrder​(int node, boolean test, XPathContext support)
      Add the node into a vector of nodes where it should occur in document order.
      void addNodeInDocOrder​(int node, XPathContext support)
      Add the node into a vector of nodes where it should occur in document order.
      void addNodes​(DTMIterator iterator)
      Copy NodeList members into this nodelist, adding in document order.
      java.lang.Object clone()
      DTMIterator cloneWithReset()
      Get a cloned Iterator that is reset to the start of the iteration.
      boolean contains​(int s)
      Tell if the table contains the given node.
      void detach()
      Detaches the DTMIterator from the set which it iterated over, releasing any computational resources and placing the iterator in the INVALID state.
      int elementAt​(int i)
      Get the nth element.
      int getAxis()
      Returns the axis being iterated, if it is known.
      int getCurrentNode()
      Get the current node in the iterator.
      int getCurrentPos()
      Get the current position within the cached list, which is one less than the next nextNode() call will retrieve.
      DTM getDTM​(int nodeHandle)
      Get an instance of a DTM that "owns" a node handle.
      DTMManager getDTMManager()
      Get an instance of the DTMManager.
      boolean getExpandEntityReferences()
      The value of this flag determines whether the children of entity reference nodes are visible to the iterator.
      int getLength()
      The number of nodes in the list.
      int getRoot()
      The root node of the DTMIterator, as specified when it was created.
      int getWhatToShow()
      This attribute determines which node types are presented via the iterator.
      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.
      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.
      boolean isDocOrdered()
      Returns true if all the nodes in the iteration well be returned in document order.
      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.
      int item​(int index)
      Returns the node handle of an item in the collection.
      int nextNode()
      Returns the next node in the set and advances the position of the iterator in the set.
      int previousNode()
      Returns the previous node in the set and moves the position of the DTMIterator backwards in the set.
      void reset()
      Reset the iterator to the start.
      void runTo​(int index)
      If an index is requested, NodeSetDTM will call this method to run the iterator to the index.
      void setCurrentPos​(int i)
      Set the current position in the node set.
      void setRoot​(int context, java.lang.Object environment)
      Reset the root node of the DTMIterator, overriding the value specified when it was created.
      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.
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • m_next

        protected transient int m_next
        If this node is being used as an iterator, the next index that nextNode() will return.
      • m_cacheNodes

        protected transient boolean m_cacheNodes
        True if this list is cached.
      • m_root

        protected int m_root
        The root of the iteration, if available.
    • Constructor Detail

      • NodeSetDTM

        public NodeSetDTM​(DTMManager dtmManager)
        Create an empty nodelist.
      • NodeSetDTM

        public NodeSetDTM​(DTMIterator ni)
        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.
    • Method Detail

      • getRoot

        public int getRoot()
        The root node of the DTMIterator, 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:
        getRoot in interface DTMIterator
        Returns:
        nodeHandle int Handle of the context node.
      • setRoot

        public void setRoot​(int context,
                            java.lang.Object environment)
        Reset the root node of the DTMIterator, 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:
        setRoot in interface DTMIterator
        Parameters:
        context - 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.

      • clone

        public java.lang.Object clone()
                               throws java.lang.CloneNotSupportedException
        Specified by:
        clone in interface DTMIterator
        Overrides:
        clone in class NodeVector
        Returns:
        A clone of this object.
        Throws:
        java.lang.CloneNotSupportedException - if any
      • cloneWithReset

        public DTMIterator cloneWithReset()
                                   throws java.lang.CloneNotSupportedException
        Get a cloned Iterator that is reset to the start of the iteration.
        Specified by:
        cloneWithReset in interface DTMIterator
        Returns:
        A clone of this iteration that has been reset.
        Throws:
        java.lang.CloneNotSupportedException - if any
      • 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:
        reset in interface DTMIterator
      • 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 by whatToShow will be skipped, but their children may still be considered.
        Specified by:
        getWhatToShow in interface DTMIterator
        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 over whatToShow and 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 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.

        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:
        getExpandEntityReferences in interface DTMIterator
        Returns:
        true if entity references will be expanded.
      • 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:
        getDTM in interface DTMIterator
        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:
        getDTMManager in interface DTMIterator
        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 has setRoot called, the first call to nextNode() returns that root or (if it is rejected by the filters) the first node within its subtree which is not filtered out.
        Specified by:
        nextNode in interface DTMIterator
        Returns:
        The next node handle in the set being iterated over, or DTM.NULL if there are no more members in that set.
      • previousNode

        public int previousNode()
        Returns the previous node in the set and moves the position of the DTMIterator backwards in the set.
        Specified by:
        previousNode in interface DTMIterator
        Returns:
        The previous node handle in the set being iterated over, or DTM.NULL if there are no more members in that set.
      • detach

        public void detach()
        Detaches the DTMIterator from the set which it iterated over, releasing any computational resources and placing the iterator in the INVALID state. After detach has been invoked, calls to nextNode or previousNode will raise a runtime exception.
        Specified by:
        detach in interface DTMIterator
      • 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:
        isFresh in interface DTMIterator
        Returns:
        true if the iteration of this list has not yet begun.
      • 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:
        runTo in interface DTMIterator
        Parameters:
        index - The index to run to, or -1 if the iterator should be run to the end.
      • item

        public int item​(int index)
        Returns the node handle of an item in the collection. If index is greater than or equal to the number of nodes in the list, this returns null.
        Specified by:
        item in interface DTMIterator
        Parameters:
        index - of the item.
        Returns:
        The node handle at the indexth position in the DTMIterator, or -1 if 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 to length-1 inclusive. Note that this requires running the iterator to completion, and presumably filling the cache.
        Specified by:
        getLength in interface DTMIterator
        Returns:
        The number of nodes in the 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:
        java.lang.RuntimeException - thrown if this NodeSetDTM is not of a mutable type.
      • addNodes

        public void addNodes​(DTMIterator iterator)
        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:
        java.lang.RuntimeException - thrown if this NodeSetDTM is not of a mutable type.
      • addNodeInDocOrder

        public void addNodeInDocOrder​(int node,
                                      boolean test,
                                      XPathContext support)
        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 order
        support - The XPath runtime context.
        Throws:
        java.lang.RuntimeException - thrown if this NodeSetDTM is not of a mutable type.
      • addNodeInDocOrder

        public void addNodeInDocOrder​(int node,
                                      XPathContext support)
        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.
        Throws:
        java.lang.RuntimeException - thrown if this NodeSetDTM is not of a mutable type.
      • elementAt

        public int elementAt​(int i)
        Get the nth element.
        Overrides:
        elementAt in class NodeVector
        Parameters:
        i - Index of node to get
        Returns:
        Node at specified index
      • contains

        public boolean contains​(int s)
        Tell if the table contains the given node.
        Overrides:
        contains in class NodeVector
        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:
        indexOf in class NodeVector
        Parameters:
        elem - Node to look for
        index - 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:
        indexOf in class NodeVector
        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 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:
        getCurrentPos in interface DTMIterator
        Returns:
        The position of the iteration.
      • setCurrentPos

        public void setCurrentPos​(int i)
        Set the current position in the node set.
        Specified by:
        setCurrentPos in interface DTMIterator
        Parameters:
        i - Must be a valid index.
      • 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:
        getCurrentNode in interface DTMIterator
        Returns:
        The current node handle, or -1.
      • 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:
        setShouldCacheNodes in interface DTMIterator
        Parameters:
        b - true if the nodes should be cached.
      • isDocOrdered

        public boolean isDocOrdered()
        Returns true if all the nodes in the iteration well be returned in document order.
        Specified by:
        isDocOrdered in interface DTMIterator
        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:
        getAxis in interface DTMIterator
        Returns:
        Axis.CHILD, etc., or -1 if the axis is not known or is of multiple types.