org.exolab.adaptx.xpath

Class XPathContext

Known Direct 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: 1.1.1.1 $

Authors:
Keith Visco
Assaf Arkin

Constructor Summary

XPathContext(VariableSet variables, NodeSet nodeSet, int position)
Constructs a new XPath context.
XPathContext(XPathNode node)
Constructs a new XPath context using the given node as the context node.
XPathContext(XPathNode node, VariableSet variables)
Constructs a new XPath context using the given node as as the context node.

Method Summary

void
addNamespaceBinding(String prefix, 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, String id)
Returns the element associated with the given identifier.
XPathFunction
getFunction(String uri, String name)
Returns the XPath function by the given name, or null if no such function is defined.
String
getNamespaceURI(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(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

Constructor Details

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:
nodeSet - the context node-set for this XPathContext
position - 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.

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.

Method Details

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.

Parameters:
prefix - the prefix to associate with the namespace
namespace - the namespace URI.


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,
                                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


getFunction

public XPathFunction getFunction(String uri,
                                 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 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.

Parameters:
prefix - the namespace prefix

Returns:
the namespace uri or null.


getNode

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

Returns:
the context node


getNodeSet

public NodeSet getNodeSet()
Returns the current context node-set.

Returns:
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.

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


getVariable

public XPathResult getVariable(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


newContext

public XPathContext newContext(NodeSet nodeSet,
                               int position)
Creates a new XPathContext with this XPathContext as it's parent.

Returns:
the new XPathContext


newContext

public XPathContext newContext(XPathNode node)
Creates a new XPathContext with this XPathContext as it's parent.

Returns:
the new XPathContext


newNodeSet

public NodeSet newNodeSet()
Constructs and returns a new empty node-set.

Returns:
A new empty node-set


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


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


setPosition

public void setPosition(int position)
Sets the position of the context node within the context node-set

Parameters: