下面列出了javax.xml.xpath.XPath#setXPathFunctionResolver ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Create a new {@link XPath} with the passed variable resolver, function
* resolver and namespace context.
*
* @param aXPathFactory
* The XPath factory object to use. May not be <code>null</code>.
* @param aVariableResolver
* Variable resolver to be used. May be <code>null</code>.
* @param aFunctionResolver
* Function resolver to be used. May be <code>null</code>.
* @param aNamespaceContext
* Namespace context to be used. May be <code>null</code>.
* @return The created non-<code>null</code> {@link XPath} object
*/
@Nonnull
public static XPath createNewXPath (@Nonnull final XPathFactory aXPathFactory,
@Nullable final XPathVariableResolver aVariableResolver,
@Nullable final XPathFunctionResolver aFunctionResolver,
@Nullable final NamespaceContext aNamespaceContext)
{
ValueEnforcer.notNull (aXPathFactory, "XPathFactory");
final XPath aXPath = aXPathFactory.newXPath ();
if (aVariableResolver != null)
aXPath.setXPathVariableResolver (aVariableResolver);
if (aFunctionResolver != null)
aXPath.setXPathFunctionResolver (aFunctionResolver);
if (aNamespaceContext != null)
aXPath.setNamespaceContext (aNamespaceContext);
return aXPath;
}
@Override
public List<Object> evaluate(TestCase testCase, String file) throws Exception{
Document doc = toDOM(file);
List<Object> results = new ArrayList<Object>(testCase.xpaths.size());
XPath xpathObj = factory.newXPath();
if(xpathObj instanceof XPathEvaluator){
XPathEvaluator xpe = (XPathEvaluator)xpathObj;
xpe.getConfiguration().setVersionWarning(false);
((StandardErrorListener)xpe.getConfiguration().getErrorListener()).setRecoveryPolicy(Configuration.RECOVER_SILENTLY);
xpe.getStaticContext().setBackwardsCompatibilityMode(true);
}
for(XPathInfo xpathInfo: testCase.xpaths){
xpathObj.setXPathVariableResolver(testCase.variableResolver);
xpathObj.setXPathFunctionResolver(testCase.functionResolver);
xpathObj.setNamespaceContext(testCase.nsContext);
if(xpathInfo.forEach==null)
results.add(xpathObj.evaluate(xpathInfo.xpath, doc, xpathInfo.resultType));
else{
List<Object> list = new ArrayList<Object>();
NodeList nodeList = (NodeList)xpathObj.evaluate(xpathInfo.forEach, doc, XPathConstants.NODESET);
for(int i=0; i<nodeList.getLength(); i++){
Object context = nodeList.item(i);
list.add(xpathObj.evaluate(xpathInfo.xpath, context, xpathInfo.resultType));
}
results.add(list);
}
}
return results;
}
/**
* Creates a new XPath instance and initializes it with the WorkflowNamespaceContext and the
* WorkflowFunctionResolver.
*/
public static XPath newXPath() {
XPath xPath = XPathFactory.newInstance().newXPath();
xPath.setNamespaceContext(new WorkflowNamespaceContext());
WorkflowFunctionResolver resolver = new WorkflowFunctionResolver();
xPath.setXPathFunctionResolver(resolver);
resolver.setXpath(xPath);
return xPath;
}
/**
*
* This method sets up the XPath with the correct workflow namespace and resolver initialized. This ensures that the XPath
* statements can use required workflow functions as part of the XPath statements.
*
* @param document - document
* @return a fully initialized XPath instance that has access to the workflow resolver and namespace.
*
*/
public final static XPath getXPath(Document document) {
XPath xpath = getXPath(RouteContext.getCurrentRouteContext());
xpath.setNamespaceContext(new WorkflowNamespaceContext());
WorkflowFunctionResolver resolver = new WorkflowFunctionResolver();
resolver.setXpath(xpath);
resolver.setRootNode(document);
xpath.setXPathFunctionResolver(resolver);
return xpath;
}
@Test(dataProvider = "xpath", expectedExceptions = NullPointerException.class)
public void testCheckXPathFunctionResolver02(XPath xpath) throws XPathExpressionException {
xpath.setXPathFunctionResolver((functionName, arity) -> null);
assertEquals(xpath.evaluate(null, "5"), "2");
}
private static XPath internalXpath() {
XPath xPath = XPathFactory.newInstance().newXPath();
xPath.setXPathFunctionResolver(resolver);
return xPath;
}