|
| | | | 3. (optional) Create an XPathExpression | | | | |
| |
You can use the XPath.compile(String expression)
method to compile an XPath string into an XPathExpression object
for later evaluation. This is an optional step.
You can evaluate an XPath expression without compiling it first.
|
| | | | Using XPathFunctionResolver | | | | |
| |
XPathFunctionResolver provides access to the
set of user defined XPathFunctions. If an XPath expression contains
an extension function, you can use the
XPath.setXPathFunctionResolver(XPathFunctionResolver resolver)
method to set a XPathFunctionResolver on the XPath object. You can also use the
XPathFactory.setXPathFunctionResolver(XPathFunctionResolver resolver)
method to set a XPathFunctionResolver on the XPathFactory, in which case all XPath objects constructed from this
XPathFactory will use the specified XPathVariableResolver by default.
The XPathFunctionResolver interface only has one method:
public XPathFunction resolveFunction(QName functionName, int arity)
This method returns a XPathFunction object from the given function name and arity. XPath functions are resolved
by name and arity. The parameter types have no impact here. XPathFunctionResolver is only used for functions with
an explicit prefix. It is not needed for XPath built-in functions and it cannot be used to override those functions.
The XPathFunction interface has only one method:
public java.lang.Object evaluate(java.util.List args) throws XPathFunctionException
The function parameters are passed in using the args parameter as a java.util.List. And the method returns the result
of evaluating the XPath function as an Object.
To support the use of extension functions in an XPath expression, a user needs to provide implementations of
the XPathFunctionResolver and XPathFunction interfaces. The resolveFunction method of the XPathFunctionResolver
implementation should return an object of a class that implements the XPathFunction interface.
Suppose we want to evaluate the XPath expression "ext:myAdditionFunction(2, 3)" , and the purpose
of the extension function is simply to return the sum of the two parameters. Because an extension function
always has an explicit prefix, we also need to implement the NamespaceContext interface to provide a namespace
prefix to URI mapping. For this example, we need to implement three interfaces: NamespaceContext,
XPathFunctionResolver and XPathFunction. A sample implementation is as follows:
| | | |
public class MyNamespaceContext implements NamespaceContext
{
public String getNamespaceURI(String prefix)
{
if (prefix == null)
throw new IllegalArgumentException("The prefix cannot be null.");
if (prefix.equals("ext"))
return "http://ext.com";
else
return null;
}
public String getPrefix(String namespace)
{
if (namespace == null)
throw new IllegalArgumentException("The namespace uri cannot be null.");
if (namespace.equals("http://ext.com"))
return "ext";
else
return null;
}
public Iterator getPrefixes(String namespace)
{
return null;
}
}
/**
* The XPathFunctionResolver implementation is used to evaluate
* the extension function "ext:myAdditionFunction(2, 3)".
*/
public class MyFunctionResolver implements XPathFunctionResolver
{
/**
* This method returns a customized XPathFunction implementation
* for the extension function "ext:myAdditionFunction(2, 3)".
*/
public XPathFunction resolveFunction(QName fname, int arity)
{
if (fname == null)
throw new NullPointerException("The function name cannot be null.");
// We only recognize one function, i.e. ex:addFunc().
if (fname.equals(new QName("http://ext.com", "myAdditionFunction", "ext")))
/**
* Return a customized implementation of XPathFunction. We need
* to implement the evaluate(List) method.
*/
return new XPathFunction() {
/**
* The actual implementation of the extension function.
* Just cast two arguments to Double and add them together.
*/
public Object evaluate(java.util.List args) {
if (args.size() == 2) {
Double arg1 = (Double)args.get(0);
Double arg2 = (Double)args.get(1);
return new Double(arg1.doubleValue() + arg2.doubleValue());
}
else
return null;
}
};
else
return null;
}
}
| | | | |
|
|
|