Java Code Examples for javax.xml.xpath.XPath#setXPathFunctionResolver()

The following examples show how to use javax.xml.xpath.XPath#setXPathFunctionResolver() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XPathHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: JDKEngine.java    From jlibs with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 3
Source File: XPathHelper.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 4
Source File: WorkflowUtils.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 *
 * 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;
}
 
Example 5
Source File: XPathAnyTypeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider = "xpath", expectedExceptions = NullPointerException.class)
public void testCheckXPathFunctionResolver02(XPath xpath) throws XPathExpressionException {
    xpath.setXPathFunctionResolver((functionName, arity) -> null);
    assertEquals(xpath.evaluate(null, "5"), "2");
}
 
Example 6
Source File: Xml.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
private static XPath internalXpath() {
    XPath xPath = XPathFactory.newInstance().newXPath();
    xPath.setXPathFunctionResolver(resolver);
    return xPath;
}