Class BaseXPath
- All Implemented Interfaces:
Serializable, XPath
- Direct Known Subclasses:
Dom4jXPath, DOMXPath, JavaBeanXPath, JDOMXPath
This class provides generic functionality for further-defined implementation-specific XPaths.
If you want to adapt the Jaxen engine to traverse your own
object model, then this is a good base class to derive from.
Typically you only really need to provide your own
Navigator implementation.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final StringOriginal expression text.private Navigatorthe implementation-specific Navigator for retrieving XML nodesprivate static final longprivate ContextSupportthe support information and function, namespace and variable contextsprivate final XPathExprthe parsed form of the XPath expression -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddNamespace(String prefix, String uri) Add a namespace prefix-to-URI mapping for this XPath expression.booleanbooleanValueOf(Object node) Retrieve a boolean-value interpretation of this XPath expression when evaluated against a given context.protected FunctionContextCreate a defaultFunctionContext.protected NamespaceContextCreate a defaultNamespaceContext.protected VariableContextCreate a defaultVariableContext.debug()Returns a string representation of the parse tree.Evaluate this XPath against a given context.protected ContextgetContext(Object node) Create aContextwrapper for the provided implementation-specific object.protected ContextSupportRetrieve theContextSupportaggregation ofNamespaceContext,FunctionContext,VariableContext, andNavigator.Retrieve theFunctionContextused by this XPath expression.Retrieve theNamespaceContextused by this XPath expression.Retrieve the XML object-model-specificNavigatorfor us in evaluating this XPath expression.Retrieve the root expression of the internal compiled form of this XPath expression.Retrieve theVariableContextused by this XPath expression.numberValueOf(Object node) Retrieve a number-value interpretation of this XPath expression when evaluated against a given context.selectNodes(Object node) List all the nodes selected by this XPath expression.protected ListselectNodesForContext(Context context) Select all nodes that match this XPath expression on the given Context object.selectSingleNode(Object node) Return the first node selected by this XPath expression.protected ObjectselectSingleNodeForContext(Context context) Return only the first node that is selected by this XPath expression.voidsetFunctionContext(FunctionContext functionContext) Set aFunctionContextfor use with this XPath expression.voidsetNamespaceContext(NamespaceContext namespaceContext) Set aNamespaceContextfor use with this XPath expression.voidsetVariableContext(VariableContext variableContext) Set aVariableContextfor use with this XPath expression.stringValueOf(Object node) Retrieves the string-value of the result of evaluating this XPath expression when evaluated against the specified context.toString()Return the original expression text.Deprecated.
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
exprText
Original expression text. -
xpath
the parsed form of the XPath expression -
support
the support information and function, namespace and variable contexts
-
-
Constructor Details
-
BaseXPath
Construct given an XPath expression string.- Parameters:
xpathExpr- the XPath expression- Throws:
JaxenException- if there is a syntax error while parsing the expression
-
-
Method Details
-
evaluate
Evaluate this XPath against a given context. The context of evaluation may be any object type the navigator recognizes as a node. The return value is either aString,Double,Boolean, orListof nodes.When using this method, one must be careful to test the class of the returned object. If the returned object is a list, then the items in this list will be the actual
Document,Element,Attribute, etc. objects as defined by the concrete XML object-model implementation, directly from the context document. This method does not return copies of anything, but merely returns references to objects within the source document.- Specified by:
evaluatein interfaceXPath- Parameters:
context- the node, node-set or Context object for evaluation. This value can be null.- Returns:
- the result of evaluating the XPath expression against the supplied context
- Throws:
JaxenException- if an XPath error occurs during expression evaluationClassCastException- if the context is not a node
-
selectNodes
List all the nodes selected by this XPath expression. If multiple nodes match, multiple nodes are returned. Nodes are returned in document-order, as defined by the XPath specification. If the expression selects a non-node-set (i.e. a number, boolean, or string) then a List containing just that one object is returned.- Specified by:
selectNodesin interfaceXPath- Parameters:
node- the node, node-set or Context object for evaluation. This value can be null.- Returns:
- the node-set of all items selected by this XPath expression
- Throws:
JaxenException- if an XPath error occurs during expression evaluation- See Also:
-
selectSingleNode
Return the first node selected by this XPath expression. If multiple nodes match, only one node is returned. The selected node will be the first selected node in document-order, as defined by the XPath specification.- Specified by:
selectSingleNodein interfaceXPath- Parameters:
node- the node, node-set or Context object for evaluation. This value can be null.- Returns:
- the node-set of all items selected by this XPath expression
- Throws:
JaxenException- if an XPath error occurs during expression evaluation- See Also:
-
valueOf
Deprecated.replaced bystringValueOf(Object)Returns the XPath string-value of the argument node.- Specified by:
valueOfin interfaceXPath- Parameters:
node- the node whose value to take- Returns:
- the XPath string value of this node
- Throws:
JaxenException- if an XPath error occurs during expression evaluation
-
stringValueOf
Retrieves the string-value of the result of evaluating this XPath expression when evaluated against the specified context.The string-value of the expression is determined per the
string(..)core function defined in the XPath specification. This means that an expression that selects zero nodes will return the empty string, while an expression that selects one-or-more nodes will return the string-value of the first node.- Specified by:
stringValueOfin interfaceXPath- Parameters:
node- the node, node-set or Context object for evaluation. This value can be null.- Returns:
- the string-value of the result of evaluating this expression with the specified context node
- Throws:
JaxenException- if an XPath error occurs during expression evaluation
-
booleanValueOf
Retrieve a boolean-value interpretation of this XPath expression when evaluated against a given context.The boolean-value of the expression is determined per the
boolean(..)function defined in the XPath specification. This means that an expression that selects zero nodes will returnfalse, while an expression that selects one or more nodes will returntrue.- Specified by:
booleanValueOfin interfaceXPath- Parameters:
node- the node, node-set or Context object for evaluation. This value can be null.- Returns:
- the boolean-value of the result of evaluating this expression with the specified context node
- Throws:
JaxenException- if an XPath error occurs during expression evaluation
-
numberValueOf
Retrieve a number-value interpretation of this XPath expression when evaluated against a given context.The number-value of the expression is determined per the
number(..)core function as defined in the XPath specification. This means that if this expression selects multiple nodes, the number-value of the first node is returned.- Specified by:
numberValueOfin interfaceXPath- Parameters:
node- the node, node-set or Context object for evaluation. This value can be null.- Returns:
- a
Doubleindicating the numeric value of evaluating this expression against the specified context - Throws:
JaxenException- if an XPath error occurs during expression evaluation
-
addNamespace
Add a namespace prefix-to-URI mapping for this XPath expression.Namespace prefix-to-URI mappings in an XPath are independent of those used within any document. Only the mapping explicitly added to this XPath will be available for resolving the XPath expression.
This is a convenience method for adding mappings to the default
NamespaceContextin place for this XPath. If you have installed a customNamespaceContextthat is not aSimpleNamespaceContext, then this method will throw aJaxenException.- Specified by:
addNamespacein interfaceXPath- Parameters:
prefix- the namespace prefixuri- the namespace URI- Throws:
JaxenException- if theNamespaceContextused by this XPath is not aSimpleNamespaceContext
-
setNamespaceContext
Set aNamespaceContextfor use with this XPath expression.A
NamespaceContextis responsible for translating namespace prefixes within the expression into namespace URIs.- Specified by:
setNamespaceContextin interfaceXPath- Parameters:
namespaceContext- theNamespaceContextto install for this expression- See Also:
-
setFunctionContext
Set aFunctionContextfor use with this XPath expression.A
FunctionContextis responsible for resolving all function calls used within the expression.- Specified by:
setFunctionContextin interfaceXPath- Parameters:
functionContext- theFunctionContextto install for this expression- See Also:
-
setVariableContext
Set aVariableContextfor use with this XPath expression.A
VariableContextis responsible for resolving all variables referenced within the expression.- Specified by:
setVariableContextin interfaceXPath- Parameters:
variableContext- TheVariableContextto install for this expression- See Also:
-
getNamespaceContext
Retrieve theNamespaceContextused by this XPath expression.A
NamespaceContextis responsible for mapping prefixes used within the expression to namespace URIs.If this XPath expression has not previously had a
NamespaceContextinstalled, a new defaultNamespaceContextwill be created, installed and returned.- Specified by:
getNamespaceContextin interfaceXPath- Returns:
- the
NamespaceContextused by this expression - See Also:
-
getFunctionContext
Retrieve theFunctionContextused by this XPath expression.A
FunctionContextis responsible for resolving all function calls used within the expression.If this XPath expression has not previously had a
FunctionContextinstalled, a new defaultFunctionContextwill be created, installed and returned.- Specified by:
getFunctionContextin interfaceXPath- Returns:
- the
FunctionContextused by this expression - See Also:
-
getVariableContext
Retrieve theVariableContextused by this XPath expression.A
VariableContextis responsible for resolving all variables referenced within the expression.If this XPath expression has not previously had a
VariableContextinstalled, a new defaultVariableContextwill be created, installed and returned.- Specified by:
getVariableContextin interfaceXPath- Returns:
- the
VariableContextused by this expression - See Also:
-
getRootExpr
Retrieve the root expression of the internal compiled form of this XPath expression.Internally, Jaxen maintains a form of Abstract Syntax Tree (AST) to represent the structure of the XPath expression. This is normally not required during normal consumer-grade usage of Jaxen. This method is provided for hard-core users who wish to manipulate or inspect a tree-based version of the expression.
- Returns:
- the root of the AST of this expression
-
toString
-
debug
Returns a string representation of the parse tree.- Returns:
- a string representation of the parse tree.
-
getContext
-
getContextSupport
Retrieve theContextSupportaggregation ofNamespaceContext,FunctionContext,VariableContext, andNavigator.- Returns:
- aggregate
ContextSupportfor this XPath expression
-
createFunctionContext
Create a defaultFunctionContext.- Returns:
- a default
FunctionContext
-
createNamespaceContext
Create a defaultNamespaceContext.- Returns:
- a default
NamespaceContextinstance
-
createVariableContext
Create a defaultVariableContext.- Returns:
- a default
VariableContextinstance
-
selectNodesForContext
Select all nodes that match this XPath expression on the given Context object. If multiple nodes match, multiple nodes will be returned in document-order, as defined by the XPath specification. If the expression selects a non-node-set (i.e. a number, boolean, or string) then a List containing just that one object is returned.- Parameters:
context- the Context which gets evaluated- Returns:
- the node-set of all items selected by this XPath expression
- Throws:
JaxenException- if an XPath error occurs during expression evaluation
-
selectSingleNodeForContext
Return only the first node that is selected by this XPath expression. If multiple nodes match, only one node will be returned. The selected node will be the first selected node in document-order, as defined by the XPath specification. If the XPath expression selects a double, String, or boolean, then that object is returned.- Parameters:
context- the Context against which this expression is evaluated- Returns:
- the first node in document order of all nodes selected by this XPath expression
- Throws:
JaxenException- if an XPath error occurs during expression evaluation- See Also:
-
stringValueOf(Object)