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);
}
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);
}
}
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);
}
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);
}
Aggregations