Search in sources :

Example 1 with XPathFactoryImpl

use of net.sf.saxon.xpath.XPathFactoryImpl in project camel by apache.

the class XPathTest method testXPathUsingSaxon.

@Test
public void testXPathUsingSaxon() throws Exception {
    XPathFactory fac = new XPathFactoryImpl();
    XPathBuilder builder = XPathBuilder.xpath("foo/bar").factory(fac);
    // will evaluate as XPathConstants.NODESET and have Camel convert that to String
    // this should return the String incl. xml tags
    String name = builder.evaluate(context, "<foo><bar id=\"1\">cheese</bar></foo>", String.class);
    assertEquals("<bar id=\"1\">cheese</bar>", name);
    // will evaluate using XPathConstants.STRING which just return the text content (eg like text())
    name = builder.evaluate(context, "<foo><bar id=\"1\">cheese</bar></foo>");
    assertEquals("cheese", name);
}
Also used : XPathFactoryImpl(net.sf.saxon.xpath.XPathFactoryImpl) XPathFactory(javax.xml.xpath.XPathFactory) XPathBuilder(org.apache.camel.builder.xml.XPathBuilder) Test(org.junit.Test)

Example 2 with XPathFactoryImpl

use of net.sf.saxon.xpath.XPathFactoryImpl in project carbon-business-process by wso2.

the class XPathExpressionRuntime method evaluate.

private Object evaluate(String exp, EvaluationContext evalCtx, QName type) {
    try {
        XPathFactory xpf = new XPathFactoryImpl();
        JaxpFunctionResolver funcResolve = new JaxpFunctionResolver(evalCtx);
        xpf.setXPathFunctionResolver(funcResolve);
        XPathEvaluator xpe = (XPathEvaluator) xpf.newXPath();
        xpe.setXPathFunctionResolver(funcResolve);
        xpe.setBackwardsCompatible(true);
        xpe.setNamespaceContext(evalCtx.getNameSpaceContextOfTask());
        XPathExpression xpathExpression = xpe.compile(exp);
        Node contextNode = evalCtx.getRootNode() == null ? DOMUtils.newDocument() : evalCtx.getRootNode();
        Object evalResult = xpathExpression.evaluate(contextNode, type);
        if (evalResult != null && log.isDebugEnabled()) {
            log.debug("Expression " + exp + " generate result " + evalResult + " - type=" + evalResult.getClass().getName());
            if (evalCtx.getRootNode() != null) {
                log.debug("Was using context node " + DOMUtils.domToString(evalCtx.getRootNode()));
            }
        }
        return evalResult;
    } catch (XPathFactoryConfigurationException e) {
        log.error("Exception occurred while creating XPathFactory.", e);
        throw new XPathProcessingException("Exception occurred while creating XPathFactory.", e);
    } catch (XPathExpressionException e) {
        String msg = "Error evaluating XPath expression: " + exp;
        log.error(msg, e);
        throw new XPathProcessingException(msg, e);
    } catch (ParserConfigurationException e) {
        String msg = "XML Parsing error.";
        log.error(msg, e);
        throw new XPathProcessingException(msg, e);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new XPathProcessingException(e.getMessage(), e);
    }
}
Also used : XPathFactoryImpl(net.sf.saxon.xpath.XPathFactoryImpl) XPathEvaluator(net.sf.saxon.xpath.XPathEvaluator) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) HumanTaskRuntimeException(org.wso2.carbon.humantask.core.engine.runtime.api.HumanTaskRuntimeException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 3 with XPathFactoryImpl

use of net.sf.saxon.xpath.XPathFactoryImpl in project camel by apache.

the class XPathTest method testXPathFunctionSubstringUsingSaxon.

@Test
public void testXPathFunctionSubstringUsingSaxon() throws Exception {
    String xml = "<foo><bar>Hello World</bar></foo>";
    XPathFactory fac = new XPathFactoryImpl();
    XPathBuilder builder = XPathBuilder.xpath("substring(/foo/bar, 7)").factory(fac);
    String result = builder.resultType(String.class).evaluate(context, xml, String.class);
    assertEquals("World", result);
    result = builder.evaluate(context, xml);
    assertEquals("World", result);
}
Also used : XPathFactoryImpl(net.sf.saxon.xpath.XPathFactoryImpl) XPathFactory(javax.xml.xpath.XPathFactory) XPathBuilder(org.apache.camel.builder.xml.XPathBuilder) Test(org.junit.Test)

Example 4 with XPathFactoryImpl

use of net.sf.saxon.xpath.XPathFactoryImpl in project mule by mulesoft.

the class ExtensionResourcesGeneratorAnnotationProcessorTestCase method before.

@Before
public void before() throws Exception {
    XPathFactory xpathFactory = new XPathFactoryImpl();
    xpath = xpathFactory.newXPath();
    builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);
}
Also used : XPathFactoryImpl(net.sf.saxon.xpath.XPathFactoryImpl) XPathFactory(javax.xml.xpath.XPathFactory) Before(org.junit.Before)

Aggregations

XPathFactoryImpl (net.sf.saxon.xpath.XPathFactoryImpl)4 XPathFactory (javax.xml.xpath.XPathFactory)3 XPathBuilder (org.apache.camel.builder.xml.XPathBuilder)2 Test (org.junit.Test)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 XPathEvaluator (net.sf.saxon.xpath.XPathEvaluator)1 Before (org.junit.Before)1 HumanTaskRuntimeException (org.wso2.carbon.humantask.core.engine.runtime.api.HumanTaskRuntimeException)1