public abstract class JaxpXPathEvaluator extends Object implements XPathEvaluator, XPathVariableResolver
XPathEvaluator
. Because it's
based on the JAXP API (JSR-206), it's ideal for keeping a common code base which
can work with different engines, as long as they implement that API.
This base class contains all of the logic necessary to comply with the
XPathEvaluator
contract. Implementations only need to implement
createXPathFactory()
in order to provide the XPathFactory
implementation it wishes to use.
Another important feature of this implementation is that it caches compiled
versions of executed expressions to provide better performance. Expressions that haven't
been used for more than a minute are automatically evicted.
In addition to the registerNamespaces(Map)
and registerNamespaces(NamespaceManager)
methods, this implementation also provides out of the box support for the standard namespaces
defined in XPathNamespaceContext
In order to allow binding expression parameters to flow variables, this class also
implements the XPathVariableResolver
interface. Because this class caches
compiled expressions which might be executed concurrently in different threads, we need a
way to correlate different MuleEvent
instances to each invocation of the resolveVariable(QName)
method. To do that, it uses a ThreadLocal
in the evaluationEvent
attribute, so that
we can determine the corresponding event for each thread evaluating an XPath expression. Notice that
because xpath evaluation is an operation that happens in RAM memory (basically because the DOM Node
needs to be completely loaded), we can use a ThreadLocal
without risking failure if this is
executed in a non-blocking environment.Constructor and Description |
---|
JaxpXPathEvaluator() |
Modifier and Type | Method and Description |
---|---|
protected XPathExpression |
compile(String expression) |
protected abstract XPathFactory |
createXPathFactory()
Returns the
XPathFactory to be used when
compiling expressions |
String |
evaluate(String xpathExpression,
Node input,
MuleEvent event)
|
Object |
evaluate(String xpathExpression,
Node input,
XPathReturnType returnType,
MuleEvent event)
Evaluates the
xpathExpression over the input
and returns the evaluation as a type in concordance with returnType . |
Map<String,String> |
getRegisteredNamespaces()
Returns a
Map with the registered namespaces. |
protected NamespaceContext |
newNamespaceContext() |
protected XPath |
newXPath() |
void |
registerNamespaces(Map<String,String> namespaces)
Registers the given namespaces so that they can be recognized during evaluation
|
void |
registerNamespaces(NamespaceManager namespaceManager)
Registers the namespaces in
namespaceManager |
Object |
resolveVariable(QName variableName)
Resolves the given variable against the flow variables
in the
MuleEvent held by evaluationEvent |
protected abstract XPathFactory createXPathFactory()
XPathFactory
to be used when
compiling expressionsXPathFactory
public String evaluate(String xpathExpression, Node input, MuleEvent event)
xpathExpression
over the input
and returns the evaluation as a String
.
If xpathExpression
contains parameters, they will be resolved
against the flow variables in event
evaluate
in interface XPathEvaluator
xpathExpression
- the xpathExpression to be evaluatedinput
- a Node
event
- the current MuleEvent
.public Object evaluate(String xpathExpression, Node input, XPathReturnType returnType, MuleEvent event)
xpathExpression
over the input
and returns the evaluation as a type in concordance with returnType
.
If xpathExpression
contains parameters, they will be resolved
against the flow variables in event
evaluate
in interface XPathEvaluator
xpathExpression
- the xpathExpression to be evaluatedinput
- a Node
returnType
- a XPathReturnType
that will be used to decide the return type of the evaluationevent
- the current MuleEvent
.returnType
public Object resolveVariable(QName variableName)
MuleEvent
held by evaluationEvent
resolveVariable
in interface XPathVariableResolver
variableName
- the variable namenull
public void registerNamespaces(Map<String,String> namespaces)
registerNamespaces
in interface XPathEvaluator
namespaces
- a Map
in which the key is a namespace prefix and the value is its URI as
a String
public void registerNamespaces(NamespaceManager namespaceManager)
namespaceManager
registerNamespaces
in interface XPathEvaluator
namespaceManager
- a NamespaceManager
public Map<String,String> getRegisteredNamespaces()
Map
with the registered namespaces. The key of the map
is the namespace prefix and the value is its URI as a String
Returns an immutable map with the current registered namespaces.
The returned map will not reflect any changes performed afterwards.getRegisteredNamespaces
in interface XPathEvaluator
Map
protected XPath newXPath()
protected XPathExpression compile(String expression) throws XPathExpressionException
XPathExpressionException
protected NamespaceContext newNamespaceContext()
Copyright © 2003–2016 MuleSoft, Inc.. All rights reserved.