Class XPathContext

  • Direct Known Subclasses:
    ProcessorState

    public class XPathContext
    extends java.lang.Object
    Base implementation of an XPath context. An XPath context provides a way to manage a stack of node-sets, resolve variable and function names, and return the size and position of the context. This implementation is not thread-safe, care must be taken not to use the same context when evaluating expressions concurrently.

    Impelementations may wish to extend this class and provide additional facilities for locating the document order of a node, or providing a function library.

    Version:
    $Revision: 3633 $
    Author:
    Keith Visco, Assaf Arkin
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addNamespaceBinding​(java.lang.String prefix, java.lang.String namespace)
      Creates a binding within this XPathContext between a given prefix and a namespace URI.
      int[] getDocumentOrder​(XPathNode node)
      Returns the document order of the given node.
      XPathNode getElementById​(XPathNode root, java.lang.String id)
      Returns the element associated with the given identifier.
      XPathFunction getFunction​(java.lang.String uri, java.lang.String name)
      Returns the XPath function by the given name, or null if no such function is defined.
      java.lang.String getNamespaceURI​(java.lang.String prefix)
      Returns the namespace associated with the given prefix as defined in this context.
      XPathNode getNode()
      Returns the context node of this XPathContext
      NodeSet getNodeSet()
      Returns the current context node-set.
      int getPosition()
      Returns the position of the context node.
      int getSize()
      Returns the size of the context.
      XPathResult getVariable​(java.lang.String name)
      Returns the XPath result bound to the given variable name.
      XPathContext newContext​(NodeSet nodeSet, int position)
      Creates a new XPathContext with this XPathContext as it's parent.
      XPathContext newContext​(XPathNode node)
      Creates a new XPathContext with this XPathContext as it's parent.
      NodeSet newNodeSet()
      Constructs and returns a new empty node-set.
      NodeSet newNodeSet​(int size)
      Constructs and returns a new node-set with the specified size.
      NodeSet newNodeSet​(XPathNode node)
      Constructs and returns a new node-set with one node.
      void setNodeSet​(NodeSet nodeSet, int position)
      Sets the given node-set as the context node-set for this XPathContext
      void setPosition​(int position)
      Sets the position of the context node within the context node-set
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • XPathContext

        public XPathContext​(XPathNode node)
        Constructs a new XPath context using the given node as the context node. The size is set to 1 and position is set to 0 and no variable bindings are provided.
        Parameters:
        node - the context XPathNode.
      • XPathContext

        public XPathContext​(XPathNode node,
                            VariableSet variables)
        Constructs a new XPath context using the given node as as the context node. The size is set to 1 and position is set to 0. Variables bindings may be optionally provided.
        Parameters:
        node - the context XPathNode.
        variable - Variable bindings, or null
      • XPathContext

        public XPathContext​(VariableSet variables,
                            NodeSet nodeSet,
                            int position)
        Constructs a new XPath context. Variables bindings may be optionally provided, size and position are required.
        Parameters:
        variable - Variable bindings, or null
        nodeSet - the context node-set for this XPathContext
        position - The position of the context node (zero base)
    • Method Detail

      • addNamespaceBinding

        public void addNamespaceBinding​(java.lang.String prefix,
                                        java.lang.String namespace)
        Creates a binding within this XPathContext between a given prefix and a namespace URI. This namespace binding will be override any binding for the given prefix within a parent context, and will be available to all sub-contexts unless they also override the namespace binding with one of their own.
        Parameters:
        prefix - the prefix to associate with the namespace
        namespace - the namespace URI.
      • getNode

        public XPathNode getNode()
        Returns the context node of this XPathContext
        Returns:
        the context node
      • getPosition

        public int getPosition()
        Returns the position of the context node. The position is a value between zero and the context size minus one. One must be added in order to obtain a value XPath position.
        Returns:
        The position of the context node
        See Also:
        getSize()
      • getSize

        public int getSize()
        Returns the size of the context.
        Returns:
        The size of the context
      • newContext

        public XPathContext newContext​(XPathNode node)
        Creates a new XPathContext with this XPathContext as it's parent.
        Returns:
        the new XPathContext
      • newContext

        public XPathContext newContext​(NodeSet nodeSet,
                                       int position)
        Creates a new XPathContext with this XPathContext as it's parent.
        Returns:
        the new XPathContext
      • newNodeSet

        public NodeSet newNodeSet​(int size)
        Constructs and returns a new node-set with the specified size.
        Parameters:
        size - The size of the node-set
        Returns:
        A new node-set with that size
      • newNodeSet

        public NodeSet newNodeSet​(XPathNode node)
        Constructs and returns a new node-set with one node.
        Parameters:
        node - The node to include in the node-set
        Returns:
        A new node-set
      • newNodeSet

        public NodeSet newNodeSet()
        Constructs and returns a new empty node-set.
        Returns:
        A new empty node-set
      • getNodeSet

        public NodeSet getNodeSet()
        Returns the current context node-set.
        Returns:
        The current context node-set
      • setNodeSet

        public void setNodeSet​(NodeSet nodeSet,
                               int position)
        Sets the given node-set as the context node-set for this XPathContext
        Parameters:
        nodeSet - the node-set to use as the context node-set.
        position - the position of the context node
      • getDocumentOrder

        public int[] getDocumentOrder​(XPathNode node)
        Returns the document order of the given node.
        Returns:
        The document order of the given node
      • getElementById

        public XPathNode getElementById​(XPathNode root,
                                        java.lang.String id)
        Returns the element associated with the given identifier. Locates the element underneath the specified root node.
        Parameters:
        root - The root node
        id - The element's identifier
        Returns:
        The first element in document order with the given identifier, or null if no such element was found
      • getVariable

        public XPathResult getVariable​(java.lang.String name)
        Returns the XPath result bound to the given variable name. Returns null if the variable was not set.
        Parameters:
        name - The variable name
        Returns:
        The variable's value
      • getFunction

        public XPathFunction getFunction​(java.lang.String uri,
                                         java.lang.String name)
        Returns the XPath function by the given name, or null if no such function is defined.
        Parameters:
        uri - The function's namespace URI
        name - The function's name within that URI
        Returns:
        The XPath function by the given name, or null if no such function is defined
      • getNamespaceURI

        public java.lang.String getNamespaceURI​(java.lang.String prefix)
        Returns the namespace associated with the given prefix as defined in this context. Null is returned if no namespace has been defined.
        Parameters:
        prefix - the namespace prefix
        Returns:
        the namespace uri or null.
      • setPosition

        public void setPosition​(int position)
        Sets the position of the context node within the context node-set
        Parameters:
        the - position of the context node within the context node-set
        Throws:
        java.lang.IndexOutOfBoundsException - when the position is not within the bounds of the context node-set.