public class BaseXPath extends Object implements XPath, Serializable
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.
XPath for dom4j
,
XPath for JDOM
,
XPath for W3C DOM
,
Serialized FormModifier | Constructor and Description |
---|---|
protected |
BaseXPath(String xpathExpr)
Construct given an XPath expression string.
|
|
BaseXPath(String xpathExpr,
Navigator navigator)
Construct given an XPath expression string.
|
Modifier and Type | Method and Description |
---|---|
void |
addNamespace(String prefix,
String uri)
Add a namespace prefix-to-URI mapping for this XPath
expression.
|
boolean |
booleanValueOf(Object node)
Retrieve a boolean-value interpretation of this XPath
expression when evaluated against a given context.
|
protected FunctionContext |
createFunctionContext()
Create a default
FunctionContext . |
protected NamespaceContext |
createNamespaceContext()
Create a default
NamespaceContext . |
protected VariableContext |
createVariableContext()
Create a default
VariableContext . |
String |
debug()
Returns a string representation of the parse tree.
|
Object |
evaluate(Object context)
Evaluate this XPath against a given context.
|
protected Context |
getContext(Object node)
Create a
Context wrapper for the provided
implementation-specific object. |
protected ContextSupport |
getContextSupport()
Retrieve the
ContextSupport aggregation of
NamespaceContext , FunctionContext ,
VariableContext , and Navigator . |
FunctionContext |
getFunctionContext()
Retrieve the
FunctionContext used by this XPath
expression. |
NamespaceContext |
getNamespaceContext()
Retrieve the
NamespaceContext used by this XPath
expression. |
Navigator |
getNavigator()
Retrieve the XML object-model-specific
Navigator
for us in evaluating this XPath expression. |
Expr |
getRootExpr()
Retrieve the root expression of the internal
compiled form of this XPath expression.
|
VariableContext |
getVariableContext()
Retrieve the
VariableContext used by this XPath
expression. |
Number |
numberValueOf(Object node)
Retrieve a number-value interpretation of this XPath
expression when evaluated against a given context.
|
List |
selectNodes(Object node)
List all the nodes selected by this XPath
expression.
|
protected List |
selectNodesForContext(Context context)
Select all nodes that match this XPath
expression on the given Context object.
|
Object |
selectSingleNode(Object node)
Return the first node selected by this XPath
expression.
|
protected Object |
selectSingleNodeForContext(Context context)
Return only the first node that is selected by this XPath
expression.
|
void |
setFunctionContext(FunctionContext functionContext)
Set a
FunctionContext for use with this XPath
expression. |
void |
setNamespaceContext(NamespaceContext namespaceContext)
Set a
NamespaceContext for use with this
XPath expression. |
void |
setVariableContext(VariableContext variableContext)
Set a
VariableContext for use with this XPath
expression. |
String |
stringValueOf(Object node)
Retrieves the string-value of the result of
evaluating this XPath expression when evaluated
against the specified context.
|
String |
toString()
Return the original expression text.
|
protected BaseXPath(String xpathExpr) throws JaxenException
xpathExpr
- the XPath expressionJaxenException
- if there is a syntax error while
parsing the expressionpublic BaseXPath(String xpathExpr, Navigator navigator) throws JaxenException
xpathExpr
- the XPath expressionnavigator
- the XML navigator to useJaxenException
- if there is a syntax error while
parsing the expressionpublic Object evaluate(Object context) throws JaxenException
String
,
Double
, Boolean
, or List
of 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.
evaluate
in interface XPath
context
- the node, node-set or Context object for evaluation.
This value can be null.JaxenException
- if an XPath error occurs during expression evaluationClassCastException
- if the context is not a nodepublic List selectNodes(Object node) throws JaxenException
selectNodes
in interface XPath
node
- the node, node-set or Context object for evaluation.
This value can be null.JaxenException
- if an XPath error occurs during expression evaluationselectNodesForContext(org.jaxen.Context)
public Object selectSingleNode(Object node) throws JaxenException
selectSingleNode
in interface XPath
node
- the node, node-set or Context object for evaluation.
This value can be null.JaxenException
- if an XPath error occurs during expression evaluationselectNodes(java.lang.Object)
public String stringValueOf(Object node) throws JaxenException
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.
stringValueOf
in interface XPath
node
- the node, node-set or Context object for evaluation. This value can be null.JaxenException
- if an XPath error occurs during expression evaluationpublic boolean booleanValueOf(Object node) throws JaxenException
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 return false
,
while an expression that selects one or more nodes will
return true
.
booleanValueOf
in interface XPath
node
- the node, node-set or Context object for evaluation. This value can be null.JaxenException
- if an XPath error occurs during expression evaluationpublic Number numberValueOf(Object node) throws JaxenException
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.
numberValueOf
in interface XPath
node
- the node, node-set or Context object for evaluation. This value can be null.Double
indicating the numeric value of
evaluating this expression against the specified contextJaxenException
- if an XPath error occurs during expression evaluationpublic void addNamespace(String prefix, String uri) throws JaxenException
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 NamespaceContext
in place for this XPath.
If you have installed a custom NamespaceContext
that is not a SimpleNamespaceContext
,
then this method will throw a JaxenException
.
addNamespace
in interface XPath
prefix
- the namespace prefixuri
- the namespace URIJaxenException
- if the NamespaceContext
used by this XPath is not a SimpleNamespaceContext
public void setNamespaceContext(NamespaceContext namespaceContext)
NamespaceContext
for use with this
XPath expression.
A NamespaceContext
is responsible for translating
namespace prefixes within the expression into namespace URIs.
setNamespaceContext
in interface XPath
namespaceContext
- the NamespaceContext
to
install for this expressionNamespaceContext
,
NamespaceContext.translateNamespacePrefixToUri(java.lang.String)
public void setFunctionContext(FunctionContext functionContext)
FunctionContext
for use with this XPath
expression.
A FunctionContext
is responsible for resolving
all function calls used within the expression.
setFunctionContext
in interface XPath
functionContext
- the FunctionContext
to
install for this expressionFunctionContext
,
FunctionContext.getFunction(java.lang.String, java.lang.String, java.lang.String)
public void setVariableContext(VariableContext variableContext)
VariableContext
for use with this XPath
expression.
A VariableContext
is responsible for resolving
all variables referenced within the expression.
setVariableContext
in interface XPath
variableContext
- The VariableContext
to
install for this expressionVariableContext
,
VariableContext.getVariableValue(java.lang.String, java.lang.String, java.lang.String)
public NamespaceContext getNamespaceContext()
NamespaceContext
used by this XPath
expression.
A NamespaceContext
is responsible for mapping
prefixes used within the expression to namespace URIs.
If this XPath expression has not previously had a NamespaceContext
installed, a new default NamespaceContext
will be created,
installed and returned.
getNamespaceContext
in interface XPath
NamespaceContext
used by this expressionNamespaceContext
public FunctionContext getFunctionContext()
FunctionContext
used by this XPath
expression.
A FunctionContext
is responsible for resolving
all function calls used within the expression.
If this XPath expression has not previously had a FunctionContext
installed, a new default FunctionContext
will be created,
installed and returned.
getFunctionContext
in interface XPath
FunctionContext
used by this expressionFunctionContext
public VariableContext getVariableContext()
VariableContext
used by this XPath
expression.
A VariableContext
is responsible for resolving
all variables referenced within the expression.
If this XPath expression has not previously had a VariableContext
installed, a new default VariableContext
will be created,
installed and returned.
getVariableContext
in interface XPath
VariableContext
used by this expressionVariableContext
public Expr getRootExpr()
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.
public String toString()
public String debug()
protected Context getContext(Object node)
Context
wrapper for the provided
implementation-specific object.node
- the implementation-specific object
to be used as the contextContext
wrapper around the objectprotected ContextSupport getContextSupport()
ContextSupport
aggregation of
NamespaceContext
, FunctionContext
,
VariableContext
, and Navigator
.ContextSupport
for this
XPath expressionpublic Navigator getNavigator()
Navigator
for us in evaluating this XPath expression.getNavigator
in interface XPath
Navigator
protected FunctionContext createFunctionContext()
FunctionContext
.FunctionContext
protected NamespaceContext createNamespaceContext()
NamespaceContext
.NamespaceContext
instanceprotected VariableContext createVariableContext()
VariableContext
.VariableContext
instanceprotected List selectNodesForContext(Context context) throws JaxenException
context
- the Context which gets evaluatedJaxenException
- if an XPath error occurs during expression evaluationprotected Object selectSingleNodeForContext(Context context) throws JaxenException
context
- the Context against which this expression is evaluatedJaxenException
- if an XPath error occurs during expression evaluationselectNodesForContext(org.jaxen.Context)
Copyright 2001–2022 The Jaxen Project. All rights reserved.