Package org.apache.camel.builder.xml

Examples of org.apache.camel.builder.xml.XPathBuilder


*/
public class XPathAnnotationExpressionFactory extends DefaultAnnotationExpressionFactory {
    @Override
    public Expression createExpression(CamelContext camelContext, Annotation annotation, LanguageAnnotation languageAnnotation, Class expressionReturnType) {
        String xpath = getExpressionFromAnnotation(annotation);
        XPathBuilder builder = XPathBuilder.xpath(xpath);
        if (annotation instanceof XPath) {
            XPath xpathAnnotation = (XPath) annotation;
            NamespacePrefix[] namespaces = xpathAnnotation.namespaces();
            if (namespaces != null) {
                for (NamespacePrefix namespacePrefix : namespaces) {
                    builder = builder.namespace(namespacePrefix.prefix(), namespacePrefix.uri());
                }
            }
        }
        return builder;
    }
View Full Code Here


public class XPathTest extends CamelTestSupport {

    @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);
    }
View Full Code Here

    @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);
    }
View Full Code Here

        // START SNIPPET: e1
        // create a Saxon factory
        XPathFactory fac = new net.sf.saxon.xpath.XPathFactoryImpl();

        // create a builder to evaluate the xpath using the saxon factory
        XPathBuilder builder = XPathBuilder.xpath("tokenize(/foo/bar, '_')[2]").factory(fac);

        // evaluate as a String result
        String result = builder.evaluate(context, "<foo><bar>abc_def_ghi</bar></foo>");
        assertEquals("def", result);
        // END SNIPPET: e1
    }
View Full Code Here

    @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
    }
View Full Code Here

    @Test
    public void testXPathFunctionTokenizeUsingSaxon() throws Exception {
        // START SNIPPET: e3
        // create a builder to evaluate the xpath using saxon
        XPathBuilder builder = XPathBuilder.xpath("tokenize(/foo/bar, '_')[2]").saxon();

        // evaluate as a String result
        String result = builder.evaluate(context, "<foo><bar>abc_def_ghi</bar></foo>");
        assertEquals("def", result);
        // END SNIPPET: e3
    }
View Full Code Here

        // START SNIPPET: e4
        // set system property with the XPath factory to use which is Saxon
        System.setProperty(XPathFactory.DEFAULT_PROPERTY_NAME + ":" + "http://saxon.sf.net/jaxp/xpath/om", "net.sf.saxon.xpath.XPathFactoryImpl");

        // create a builder to evaluate the xpath using saxon
        XPathBuilder builder = XPathBuilder.xpath("tokenize(/foo/bar, '_')[2]");

        // evaluate as a String result
        String result = builder.evaluate(context, "<foo><bar>abc_def_ghi</bar></foo>");
        assertEquals("def", result);
        // END SNIPPET: e4
    }
View Full Code Here

public class XPathAnnotationExpressionFactory extends DefaultAnnotationExpressionFactory {

    @Override
    public Expression createExpression(CamelContext camelContext, Annotation annotation, LanguageAnnotation languageAnnotation, Class<?> expressionReturnType) {
        String xpath = getExpressionFromAnnotation(annotation);
        XPathBuilder builder = XPathBuilder.xpath(xpath, getResultType(annotation));
        NamespacePrefix[] namespaces = getExpressionNameSpacePrefix(annotation);
        if (namespaces != null) {
            for (NamespacePrefix namespacePrefix : namespaces) {
                builder = builder.namespace(namespacePrefix.prefix(), namespacePrefix.uri());
            }

        }
        return builder;
    }
View Full Code Here

    @Override
    protected RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            public void configure() {

                XPathBuilder splitter = new XPathBuilder("//records/record");
               
                context.setTracing(true);

                from("direct:xpath").split(splitter).filter().xquery("//record[type=2]")
                    .to("mock:result");
View Full Code Here

*/
public class XPathLanguage implements Language, IsSingleton {
    private QName resultType;

    public Predicate createPredicate(String expression) {
        XPathBuilder builder = XPathBuilder.xpath(expression);
        configureBuilder(builder);
        return builder;
    }
View Full Code Here

TOP

Related Classes of org.apache.camel.builder.xml.XPathBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.