Search in sources :

Example 11 with XPathBuilder

use of org.apache.camel.builder.xml.XPathBuilder in project camel by apache.

the class XPathTest method testXPathFunctionTokenizeUsingObjectModel.

@Ignore("See http://www.saxonica.com/documentation/index.html#!xpath-api/jaxp-xpath/factory")
@Test
public void testXPathFunctionTokenizeUsingObjectModel() throws Exception {
    // START SNIPPET: e2
    // create a builder to evaluate the xpath using saxon based on its object model uri
    XPathBuilder builder = XPathBuilder.xpath("tokenize(/foo/bar, '_')[2]").objectModel("http://saxon.sf.net/jaxp/xpath/om");
    // evaluate as a String result
    String result = builder.evaluate(context, "<foo><bar>abc_def_ghi</bar></foo>");
    assertEquals("def", result);
// END SNIPPET: e2
}
Also used : XPathBuilder(org.apache.camel.builder.xml.XPathBuilder) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 12 with XPathBuilder

use of org.apache.camel.builder.xml.XPathBuilder 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 13 with XPathBuilder

use of org.apache.camel.builder.xml.XPathBuilder in project camel by apache.

the class XPathAnnotationExpressionFactory method createExpression.

@Override
public Expression createExpression(CamelContext camelContext, Annotation annotation, LanguageAnnotation languageAnnotation, Class<?> expressionReturnType) {
    String xpath = getExpressionFromAnnotation(annotation);
    Class<?> resultType = getResultType(annotation);
    if (resultType.equals(Object.class)) {
        resultType = expressionReturnType;
    }
    XPathBuilder builder = XPathBuilder.xpath(xpath, resultType);
    NamespacePrefix[] namespaces = getExpressionNameSpacePrefix(annotation);
    if (namespaces != null) {
        for (NamespacePrefix namespacePrefix : namespaces) {
            builder = builder.namespace(namespacePrefix.prefix(), namespacePrefix.uri());
        }
    }
    // Set the header name that we want the XPathBuilder to apply the XPath expression to
    String headerName = getHeaderName(annotation);
    if (ObjectHelper.isNotEmpty(headerName)) {
        builder.setHeaderName(headerName);
    }
    return builder;
}
Also used : XPathBuilder(org.apache.camel.builder.xml.XPathBuilder) NamespacePrefix(org.apache.camel.language.NamespacePrefix)

Example 14 with XPathBuilder

use of org.apache.camel.builder.xml.XPathBuilder in project camel by apache.

the class ExpressionNodeHelper method toExpressionDefinition.

/**
     * Determines which {@link ExpressionDefinition} describes the given predicate best possible.
     * <p/>
     * This implementation will use types such as {@link SimpleExpression}, {@link XPathExpression} etc.
     * if the given predicate is detect as such a type.
     *
     * @param predicate the predicate
     * @return a definition which describes the predicate
     */
public static ExpressionDefinition toExpressionDefinition(Predicate predicate) {
    if (predicate instanceof SimpleBuilder) {
        SimpleBuilder builder = (SimpleBuilder) predicate;
        // we keep the original expression by using the constructor that accepts an expression
        SimpleExpression answer = new SimpleExpression(builder);
        answer.setExpression(builder.getText());
        return answer;
    } else if (predicate instanceof XPathBuilder) {
        XPathBuilder builder = (XPathBuilder) predicate;
        // we keep the original expression by using the constructor that accepts an expression
        XPathExpression answer = new XPathExpression(builder);
        answer.setExpression(builder.getText());
        return answer;
    } else if (predicate instanceof ValueBuilder) {
        // ValueBuilder wraps the actual predicate so unwrap
        ValueBuilder builder = (ValueBuilder) predicate;
        Expression expression = builder.getExpression();
        if (expression instanceof Predicate) {
            predicate = (Predicate) expression;
        }
    }
    if (predicate instanceof ExpressionDefinition) {
        return (ExpressionDefinition) predicate;
    }
    return new ExpressionDefinition(predicate);
}
Also used : XPathExpression(org.apache.camel.model.language.XPathExpression) ValueBuilder(org.apache.camel.builder.ValueBuilder) SimpleBuilder(org.apache.camel.builder.SimpleBuilder) XPathExpression(org.apache.camel.model.language.XPathExpression) SimpleExpression(org.apache.camel.model.language.SimpleExpression) Expression(org.apache.camel.Expression) XPathBuilder(org.apache.camel.builder.xml.XPathBuilder) ExpressionDefinition(org.apache.camel.model.language.ExpressionDefinition) SimpleExpression(org.apache.camel.model.language.SimpleExpression) Predicate(org.apache.camel.Predicate)

Aggregations

XPathBuilder (org.apache.camel.builder.xml.XPathBuilder)14 Test (org.junit.Test)6 XPathFactory (javax.xml.xpath.XPathFactory)3 XPathFactoryImpl (net.sf.saxon.xpath.XPathFactoryImpl)3 InputStream (java.io.InputStream)2 DOMSource (javax.xml.transform.dom.DOMSource)2 SimpleBuilder (org.apache.camel.builder.SimpleBuilder)2 ValueBuilder (org.apache.camel.builder.ValueBuilder)2 ExpressionDefinition (org.apache.camel.model.language.ExpressionDefinition)2 SimpleExpression (org.apache.camel.model.language.SimpleExpression)2 XPathExpression (org.apache.camel.model.language.XPathExpression)2 XMLCipher (org.apache.xml.security.encryption.XMLCipher)2 Document (org.w3c.dom.Document)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Expression (org.apache.camel.Expression)1 Predicate (org.apache.camel.Predicate)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 NamespacePrefix (org.apache.camel.language.NamespacePrefix)1