Repository: camel Updated Branches: refs/heads/master db5e6d81c -> b9d675a92
CAMEL-9079: XPathBuilder - Evaluate without exchange inconsistent response Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/b9d675a9 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/b9d675a9 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/b9d675a9 Branch: refs/heads/master Commit: b9d675a92704982993abd86f29cda23057ef084b Parents: db5e6d8 Author: Claus Ibsen <[email protected]> Authored: Thu Aug 13 15:13:05 2015 +0200 Committer: Claus Ibsen <[email protected]> Committed: Thu Aug 13 15:53:54 2015 +0200 ---------------------------------------------------------------------- .../apache/camel/builder/xml/XPathBuilder.java | 8 ++++++++ .../org/apache/camel/builder/xml/XPathTest.java | 20 +++++++++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/b9d675a9/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java index 741fec8..8b0c2f6 100644 --- a/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java +++ b/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java @@ -194,6 +194,13 @@ public class XPathBuilder extends ServiceSupport implements Expression, Predicat /** * Evaluates the given xpath using the provided body. + * <p/> + * The evaluation uses by default {@link javax.xml.xpath.XPathConstants#NODESET} as the type + * used during xpath evaluation. The output from xpath is then afterwards type converted + * using Camel's type converter to the given type. + * <p/> + * If you want to evaluate xpath using a different type, then call {@link #setResultType(Class)} + * prior to calling this evaluate method. * * @param context the camel context * @param body the body @@ -230,6 +237,7 @@ public class XPathBuilder extends ServiceSupport implements Expression, Predicat dummy.getIn().setBody(body); setResultQName(XPathConstants.STRING); + setResultType(String.class); try { return evaluate(dummy, String.class); } finally { http://git-wip-us.apache.org/repos/asf/camel/blob/b9d675a9/camel-core/src/test/java/org/apache/camel/builder/xml/XPathTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/builder/xml/XPathTest.java b/camel-core/src/test/java/org/apache/camel/builder/xml/XPathTest.java index 08ec4ff..26247a4 100644 --- a/camel-core/src/test/java/org/apache/camel/builder/xml/XPathTest.java +++ b/camel-core/src/test/java/org/apache/camel/builder/xml/XPathTest.java @@ -30,16 +30,15 @@ import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFunctionResolver; -import org.w3c.dom.Document; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; -import org.xml.sax.InputSource; - import org.apache.camel.ContextTestSupport; import org.apache.camel.Exchange; import org.apache.camel.Expression; import org.apache.camel.Predicate; import org.apache.camel.util.ObjectHelper; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; import static org.apache.camel.builder.xml.XPathBuilder.xpath; @@ -334,6 +333,17 @@ public class XPathTest extends ContextTestSupport { assertEquals(true, bool.booleanValue()); } + public void testNotUsingExchangeResultType() throws Exception { + String xml = "<xml><a>1</a><a>2</a></xml>"; + + // will evaluate as NodeSet + XPathBuilder xpb = new XPathBuilder("/xml/a/text()"); + assertEquals("12", xpb.evaluate(context, xml, String.class)); + + xpb.setResultType(String.class); + assertEquals("1", xpb.evaluate(context, xml)); + } + public void testXPathSplit() throws Exception { Object node = XPathBuilder.xpath("foo/bar").nodeResult() .evaluate(createExchange("<foo><bar>cheese</bar><bar>cake</bar><bar>beer</bar></foo>"));
