Class BaseXPath
- java.lang.Object
-
- org.jaxen.BaseXPath
-
- All Implemented Interfaces:
java.io.Serializable
,XPath
- Direct Known Subclasses:
Dom4jXPath
,DOMXPath
,JavaBeanXPath
,JDOMXPath
,XOMXPath
public class BaseXPath extends java.lang.Object implements XPath, java.io.Serializable
Base functionality for all concrete, implementation-specific XPaths.This class provides generic functionality for further-defined implementation-specific XPaths.
If you want to adapt the Jaxen engine so that it can 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.- Author:
- bob mcwhirter, James Strachan
- See Also:
XPath for dom4j
,XPath for JDOM
,XPath for W3C DOM
, Serialized Form
-
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addNamespace(java.lang.String prefix, java.lang.String uri)
Add a namespace prefix-to-URI mapping for this XPath expression.boolean
booleanValueOf(java.lang.Object node)
Retrieve a boolean-value interpretation of this XPath expression when evaluated against a given context.protected FunctionContext
createFunctionContext()
Create a defaultFunctionContext
.protected NamespaceContext
createNamespaceContext()
Create a defaultNamespaceContext
.protected VariableContext
createVariableContext()
Create a defaultVariableContext
.java.lang.String
debug()
Returns a string representation of the parse tree.java.lang.Object
evaluate(java.lang.Object context)
Evaluate this XPath against a given context.protected Context
getContext(java.lang.Object node)
Create aContext
wrapper for the provided implementation-specific object.protected ContextSupport
getContextSupport()
Retrieve theContextSupport
aggregation ofNamespaceContext
,FunctionContext
,VariableContext
, andNavigator
.FunctionContext
getFunctionContext()
Retrieve theFunctionContext
used by this XPath expression.NamespaceContext
getNamespaceContext()
Retrieve theNamespaceContext
used by this XPath expression.Navigator
getNavigator()
Retrieve the XML object-model-specificNavigator
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 theVariableContext
used by this XPath expression.java.lang.Number
numberValueOf(java.lang.Object node)
Retrieve a number-value interpretation of this XPath expression when evaluated against a given context.java.util.List
selectNodes(java.lang.Object node)
Select all nodes that are selected by this XPath expression.protected java.util.List
selectNodesForContext(Context context)
Select all nodes that match this XPath expression on the given Context object.java.lang.Object
selectSingleNode(java.lang.Object node)
Select only the first node selected by this XPath expression.protected java.lang.Object
selectSingleNodeForContext(Context context)
Return only the first node that is selected by this XPath expression.void
setFunctionContext(FunctionContext functionContext)
Set aFunctionContext
for use with this XPath expression.void
setNamespaceContext(NamespaceContext namespaceContext)
Set aNamespaceContext
for use with this XPath expression.void
setVariableContext(VariableContext variableContext)
Set aVariableContext
for use with this XPath expression.java.lang.String
stringValueOf(java.lang.Object node)
Retrieves the string-value of the result of evaluating this XPath expression when evaluated against the specified context.java.lang.String
toString()
Return the original expression text.java.lang.String
valueOf(java.lang.Object node)
Deprecated.replaced bystringValueOf(java.lang.Object)
-
-
-
Constructor Detail
-
BaseXPath
protected BaseXPath(java.lang.String xpathExpr) throws JaxenException
Construct given an XPath expression string.- Parameters:
xpathExpr
- the XPath expression- Throws:
JaxenException
- if there is a syntax error while parsing the expression
-
BaseXPath
public BaseXPath(java.lang.String xpathExpr, Navigator navigator) throws JaxenException
Construct given an XPath expression string.- Parameters:
xpathExpr
- the XPath expressionnavigator
- the XML navigator to use- Throws:
JaxenException
- if there is a syntax error while parsing the expression
-
-
Method Detail
-
evaluate
public java.lang.Object evaluate(java.lang.Object context) throws JaxenException
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
, orList
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.- Specified by:
evaluate
in 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 evaluationjava.lang.ClassCastException
- if the context is not a node
-
selectNodes
public java.util.List selectNodes(java.lang.Object node) throws JaxenException
Select all nodes that are selected by this XPath expression. If multiple nodes match, multiple nodes will be returned. 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.- Specified by:
selectNodes
in 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:
selectNodesForContext(org.jaxen.Context)
-
selectSingleNode
public java.lang.Object selectSingleNode(java.lang.Object node) throws JaxenException
Select only the first node 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.- Specified by:
selectSingleNode
in 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:
selectNodes(java.lang.Object)
-
valueOf
public java.lang.String valueOf(java.lang.Object node) throws JaxenException
Deprecated.replaced bystringValueOf(java.lang.Object)
Returns the XPath string-value of the argument node.- Specified by:
valueOf
in 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
public java.lang.String stringValueOf(java.lang.Object node) throws JaxenException
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:
stringValueOf
in 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
public boolean booleanValueOf(java.lang.Object node) throws JaxenException
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:
booleanValueOf
in 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
public java.lang.Number numberValueOf(java.lang.Object node) throws JaxenException
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:
numberValueOf
in interfaceXPath
- Parameters:
node
- the node, node-set or Context object for evaluation. This value can be null.- Returns:
- a
Double
indicating the numeric value of evaluating this expression against the specified context - Throws:
JaxenException
- if an XPath error occurs during expression evaluation
-
addNamespace
public void addNamespace(java.lang.String prefix, java.lang.String uri) throws JaxenException
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
NamespaceContext
in place for this XPath. If you have installed a customNamespaceContext
that is not aSimpleNamespaceContext
, then this method will throw aJaxenException
.- Specified by:
addNamespace
in interfaceXPath
- Parameters:
prefix
- the namespace prefixuri
- the namespace URI- Throws:
JaxenException
- if theNamespaceContext
used by this XPath is not aSimpleNamespaceContext
-
setNamespaceContext
public void setNamespaceContext(NamespaceContext namespaceContext)
Set aNamespaceContext
for use with this XPath expression.A
NamespaceContext
is responsible for translating namespace prefixes within the expression into namespace URIs.- Specified by:
setNamespaceContext
in interfaceXPath
- Parameters:
namespaceContext
- theNamespaceContext
to install for this expression- See Also:
NamespaceContext
,NamespaceContext.translateNamespacePrefixToUri(java.lang.String)
-
setFunctionContext
public void setFunctionContext(FunctionContext functionContext)
Set aFunctionContext
for use with this XPath expression.A
FunctionContext
is responsible for resolving all function calls used within the expression.- Specified by:
setFunctionContext
in interfaceXPath
- Parameters:
functionContext
- theFunctionContext
to install for this expression- See Also:
FunctionContext
,FunctionContext.getFunction(java.lang.String, java.lang.String, java.lang.String)
-
setVariableContext
public void setVariableContext(VariableContext variableContext)
Set aVariableContext
for use with this XPath expression.A
VariableContext
is responsible for resolving all variables referenced within the expression.- Specified by:
setVariableContext
in interfaceXPath
- Parameters:
variableContext
- TheVariableContext
to install for this expression- See Also:
VariableContext
,VariableContext.getVariableValue(java.lang.String, java.lang.String, java.lang.String)
-
getNamespaceContext
public NamespaceContext getNamespaceContext()
Retrieve theNamespaceContext
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 defaultNamespaceContext
will be created, installed and returned.- Specified by:
getNamespaceContext
in interfaceXPath
- Returns:
- the
NamespaceContext
used by this expression - See Also:
NamespaceContext
-
getFunctionContext
public FunctionContext getFunctionContext()
Retrieve theFunctionContext
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 defaultFunctionContext
will be created, installed and returned.- Specified by:
getFunctionContext
in interfaceXPath
- Returns:
- the
FunctionContext
used by this expression - See Also:
FunctionContext
-
getVariableContext
public VariableContext getVariableContext()
Retrieve theVariableContext
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 defaultVariableContext
will be created, installed and returned.- Specified by:
getVariableContext
in interfaceXPath
- Returns:
- the
VariableContext
used by this expression - See Also:
VariableContext
-
getRootExpr
public Expr 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
public java.lang.String toString()
Return the original expression text.- Overrides:
toString
in classjava.lang.Object
- Returns:
- the normalized XPath expression string
-
debug
public java.lang.String debug()
Returns a string representation of the parse tree.- Returns:
- a string representation of the parse tree.
-
getContext
protected Context getContext(java.lang.Object node)
Create aContext
wrapper for the provided implementation-specific object.- Parameters:
node
- the implementation-specific object to be used as the context- Returns:
- a
Context
wrapper around the object
-
getContextSupport
protected ContextSupport getContextSupport()
Retrieve theContextSupport
aggregation ofNamespaceContext
,FunctionContext
,VariableContext
, andNavigator
.- Returns:
- aggregate
ContextSupport
for this XPath expression
-
getNavigator
public Navigator getNavigator()
Retrieve the XML object-model-specificNavigator
for us in evaluating this XPath expression.- Specified by:
getNavigator
in interfaceXPath
- Returns:
- the implementation-specific
Navigator
-
createFunctionContext
protected FunctionContext createFunctionContext()
Create a defaultFunctionContext
.- Returns:
- a default
FunctionContext
-
createNamespaceContext
protected NamespaceContext createNamespaceContext()
Create a defaultNamespaceContext
.- Returns:
- a default
NamespaceContext
instance
-
createVariableContext
protected VariableContext createVariableContext()
Create a defaultVariableContext
.- Returns:
- a default
VariableContext
instance
-
selectNodesForContext
protected java.util.List selectNodesForContext(Context context) throws JaxenException
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
protected java.lang.Object selectSingleNodeForContext(Context context) throws JaxenException
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:
selectNodesForContext(org.jaxen.Context)
-
-