public class XPathWrapper extends Object implements Cloneable
Warning:
XPathWrapper
, like the underlying JDK XPath
object,
is not thread-safe. However, separate instances may be created and evaluated
on separate threads without explicit synchronization. To make this work, we
create a new XPathFactory
instance for each wrapper (with the
Sun JDK, this adds little overhead), synchronizing newInstance()
on XPathWrapper.class
.
Constructor and Description |
---|
XPathWrapper(String expr)
Creates a new instance, which may then be customized with various
resolvers, and used to evaluate expressions.
|
Modifier and Type | Method and Description |
---|---|
XPathWrapper |
bindDefaultNamespace(String nsURI)
Deprecated.
In practice, this method isn't particularly useful:
if you're going to specify ":" on a term, you might
as well specify a single-character prefix. It won't
be removed, but a future implementation might parse
an expression sans colons, and insert references.
|
XPathWrapper |
bindFunction(FunctionResolver.SelfDescribingFunction func)
Binds a self-describing function to this expression.
|
XPathWrapper |
bindFunction(FunctionResolver.SelfDescribingFunction func,
String prefix)
Binds a self-describing function to this expression, along with the
prefix used to access that function.
|
XPathWrapper |
bindFunction(QName name,
XPathFunction func)
Binds a standard
XPathFunction to this expression,
handling any number of arguments. |
XPathWrapper |
bindFunction(QName name,
XPathFunction func,
int arity)
Binds a standard
XPathFunction to this expression,
handling a specific number of arguments. |
XPathWrapper |
bindFunction(QName name,
XPathFunction func,
int minArity,
int maxArity)
Binds a standard
XPathFunction to this expression,
handling a specific range of arguments. |
XPathWrapper |
bindNamespace(String prefix,
String nsURI)
Adds a namespace binding to this expression.
|
XPathWrapper |
bindVariable(QName name,
Object value)
Binds a value to a variable, replacing any previous value for that
variable.
|
XPathWrapper |
bindVariable(String name,
Object value)
Binds a value to a variable, replacing any previous value for that
variable.
|
protected XPathWrapper |
clone()
Creates a shallow clone of this object, omitting the compiled
XPath expression.
|
boolean |
equals(Object obj)
Two instances are considered equal if they have the same expression,
namespace mappings, variable mappings, and function mappings.
|
List<Node> |
evaluate(Node context)
Applies this expression to the the specified node, converting the
resulting
NodeList into a java.util.List . |
<T> List<T> |
evaluate(Node context,
Class<T> klass)
Applies this expression to the the specified node, converting the
resulting
NodeList into a java.util.List
containing only nodes of the specified type. |
Boolean |
evaluateAsBoolean(Node context)
Applies this expression to the the specified node, requesting the
BOOLEAN return type. |
Element |
evaluateAsElement(Node context)
Applies this expression to the the specified node, and returning the
first
Element from the resulting nodelist. |
Number |
evaluateAsNumber(Node context)
Applies this expression to the specified node, requesting the
NUMBER return type. |
String |
evaluateAsString(Node context)
Applies this expression to the specified node, requesting the
STRING return type. |
List<String> |
evaluateAsStringList(Node context)
Applies this expression to the specified node, requesting the
NODESET return type, and then retrieving the text
content for each returned node. |
int |
hashCode()
Hash code is driven by the expression, ignoring differences between
namespaces, variables, and functions.
|
XPathWrapper |
setFunctionResolver(FunctionResolver resolver)
Attaches a pre-build
FunctionResolver to this wrapper,
replacing the existing resolver (and removing all bound functions). |
String |
toString()
The string value is the expression.
|
public XPathWrapper(String expr)
public XPathWrapper bindNamespace(String prefix, String nsURI)
evaluate()
. If you add
multiple bindings with the same namespace, only the last is retained.prefix
- The prefix used to reference this namespace in the
XPath expression. Note that this does not
need to be the same prefix used by the document.nsURI
- The namespace URI to associate with this prefix.@Deprecated public XPathWrapper bindDefaultNamespace(String nsURI)
nsURI
- The default namespace for this document.public XPathWrapper bindVariable(String name, Object value)
evaluate()
; the new values will be used
for subsequent evaluations.name
- The name of the variable; this is turned into a
QName
without namespace.value
- The value of the variable; the XPath evaluator must
be able to convert this value into a type usable in
an XPath expression.public XPathWrapper bindVariable(QName name, Object value)
evaluate()
; the new values will be used
for subsequent evaluations.name
- The fully-qualified name of the variable.value
- The value of the variable; the XPath evaluator must
be able to convert this value into a type usable in
an XPath expression.public XPathWrapper bindFunction(FunctionResolver.SelfDescribingFunction func)
Per the JDK documentation, user-defined functions must occupy their
own namespace. You must bind a namespace for this function, and use
the bound prefix to reference it in your expression. Alternatively,
you can call the variant of bindFunction()
that binds
a prefix to the function's namespace.
func
- The function.public XPathWrapper bindFunction(FunctionResolver.SelfDescribingFunction func, String prefix)
Per the JDK documentation, user-defined functions must occupy their own namespace. This method will retrieve the namespace from the function, and bind it to the passed prefix.
func
- The function.prefix
- The prefix to bind to this function's namespace.public XPathWrapper bindFunction(QName name, XPathFunction func)
XPathFunction
to this expression,
handling any number of arguments. Subsequent calls to this method
with the same name will silently replace the binding.
Per the JDK documentation, user-defined functions must occupy their own namespace. If the qualified name that you pass to this method includes a prefix, the associated namespace will be bound to that prefix. If not, you must bind the namespace explicitly. In either case, you must refer to the function in your expression using a bound prefix.
name
- The qualified name for this function. Must contain
a name and namespace, may contain a prefix.func
- The function to bind to this name.public XPathWrapper bindFunction(QName name, XPathFunction func, int arity)
XPathFunction
to this expression,
handling a specific number of arguments. Subsequent calls to this
method with the same name and arity will silently replace the binding.
Per the JDK documentation, user-defined functions must occupy their own namespace. If the qualified name that you pass to this method includes a prefix, the associated namespace will be bound to that prefix. If not, you must bind the namespace explicitly. In either case, you must refer to the function in your expression using a bound prefix.
name
- The qualified name for this function. Must contain
a name and namespace, may contain a prefix.func
- The function to bind to this name.arity
- The number of arguments accepted by this function.public XPathWrapper bindFunction(QName name, XPathFunction func, int minArity, int maxArity)
XPathFunction
to this expression,
handling a specific range of arguments. Subsequent calls to this
method with the same name and range of arguments will silently
replace the binding.
Per the JDK documentation, user-defined functions must occupy their own namespace. If the qualified name that you pass to this method includes a prefix, the associated namespace will be bound to that prefix. If not, you must bind the namespace explicitly. In either case, you must refer to the function in your expression using a bound prefix.
name
- The qualified name for this function. Must contain
a name and namespace, may contain a prefix.func
- The function to bind to this name.minArity
- The minimum number of arguments accepted by this
function.maxArity
- The maximum number of arguments accepted by this
function.public XPathWrapper setFunctionResolver(FunctionResolver resolver)
FunctionResolver
to this wrapper,
replacing the existing resolver (and removing all bound functions).
This method is intended for use by XPathWrapperFactory
, but
is exposed for applications that wish to manage their own XPaths.public List<Node> evaluate(Node context)
NodeList
into a java.util.List
.public <T> List<T> evaluate(Node context, Class<T> klass)
NodeList
into a java.util.List
containing only nodes of the specified type.public Element evaluateAsElement(Node context)
Element
from the resulting nodelist. Returns
null
if there is no element in the list.public String evaluateAsString(Node context)
STRING
return type.public List<String> evaluateAsStringList(Node context)
NODESET
return type, and then retrieving the text
content for each returned node.
Note: this method calls Node.getTextContent()
in
keeping with normal XPath behavior. If you want to get
only the immediate child text nodes, evaluate as a list
of elements and use DomUtil.getText()
.
public Number evaluateAsNumber(Node context)
NUMBER
return type.public Boolean evaluateAsBoolean(Node context)
BOOLEAN
return type.public final boolean equals(Object obj)
equals()
method on
the function implementation class.public int hashCode()
public String toString()
protected XPathWrapper clone() throws CloneNotSupportedException
clone
in class Object
CloneNotSupportedException