org.exolab.adaptx.xpath
Class XPathContext
java.lang.Object
org.exolab.adaptx.xpath.XPathContext
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.
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.
nodeSet
- the context node-set for this XPathContextposition
- The position of the context node (zero base)
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.
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.
node
- the context XPathNode.
addNamespaceBinding
public void addNamespaceBinding(String prefix,
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.
prefix
- the prefix to associate with the namespacenamespace
- the namespace URI.
getDocumentOrder
public int[] getDocumentOrder(XPathNode node)
Returns the document order of the given node.
- The document order of the given node
getElementById
public XPathNode getElementById(XPathNode root,
String id)
Returns the element associated with the given identifier.
Locates the element underneath the specified root node.
root
- The root nodeid
- The element's identifier
- The first element in document order with the given
identifier, or null if no such element was found
getFunction
public XPathFunction getFunction(String uri,
String name)
Returns the XPath function by the given name, or null if no
such function is defined.
uri
- The function's namespace URIname
- The function's name within that URI
- The XPath function by the given name, or null if no
such function is defined
getNamespaceURI
public String getNamespaceURI(String prefix)
Returns the namespace associated with the given prefix as
defined in this context. Null is returned if no namespace
has been defined.
prefix
- the namespace prefix
- the namespace uri or null.
getNode
public XPathNode getNode()
Returns the context node of this XPathContext
getNodeSet
public NodeSet getNodeSet()
Returns the current context node-set.
- The current context node-set
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.
- The position of the context node
getSize
public int getSize()
Returns the size of the context.
getVariable
public XPathResult getVariable(String name)
Returns the XPath result bound to the given variable name.
Returns null if the variable was not set.
newContext
public XPathContext newContext(NodeSet nodeSet,
int position)
Creates a new XPathContext with this XPathContext as it's
parent.
newContext
public XPathContext newContext(XPathNode node)
Creates a new XPathContext with this XPathContext as it's
parent.
newNodeSet
public NodeSet newNodeSet()
Constructs and returns a new empty node-set.
newNodeSet
public NodeSet newNodeSet(int size)
Constructs and returns a new node-set with the specified size.
size
- The size of the node-set
- A new node-set with that size
newNodeSet
public NodeSet newNodeSet(XPathNode node)
Constructs and returns a new node-set with one node.
node
- The node to include in the node-set
setNodeSet
public void setNodeSet(NodeSet nodeSet,
int position)
Sets the given node-set as the context node-set for
this XPathContext
nodeSet
- the node-set to use as the context node-set.position
- the position of the context node
setPosition
public void setPosition(int position)
Sets the position of the context node within the context
node-set