Author: davsclaus
Date: Wed Feb 10 10:57:45 2010
New Revision: 908446
URL: http://svn.apache.org/viewvc?rev=908446&view=rev
Log:
CAMEL-1877: Made it easier to use Saxon with Camel XPathBuilder.
Added:
camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XPathTest.java
(with props)
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java
camel/trunk/components/camel-saxon/pom.xml
camel/trunk/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java
camel/trunk/parent/pom.xml
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java?rev=908446&r1=908445&r2=908446&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java
Wed Feb 10 10:57:45 2010
@@ -170,6 +170,28 @@
return answer;
}
+ /**
+ * Evaluates the given xpath using the provided body as a String return
type.
+ *
+ * @param context the camel context
+ * @param body the body
+ * @return result of the evaluation
+ */
+ public String evaluate(CamelContext context, Object body) {
+ ObjectHelper.notNull(context, "CamelContext");
+
+ // create a dummy Exchange to use during evaluation
+ Exchange dummy = new DefaultExchange(context);
+ dummy.getIn().setBody(body);
+
+ setResultQName(XPathConstants.STRING);
+ String answer = evaluate(dummy, String.class);
+
+ // remove the dummy from the thread local after usage
+ exchange.remove();
+ return answer;
+ }
+
// Builder methods
//
-------------------------------------------------------------------------
@@ -244,6 +266,17 @@
}
/**
+ * Configures to use Saxon as the XPathFactory which allows you to use
XPath 2.0 functions
+ * which may not be part of the build in JDK XPath parser.
+ *
+ * @return the current builder
+ */
+ public XPathBuilder saxon() {
+ this.objectModelUri = "http://saxon.sf.net/jaxp/xpath/om";
+ return this;
+ }
+
+ /**
* Sets the {...@link XPathFunctionResolver} instance to use on these XPath
* expressions
*
Modified: camel/trunk/components/camel-saxon/pom.xml
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-saxon/pom.xml?rev=908446&r1=908445&r2=908446&view=diff
==============================================================================
--- camel/trunk/components/camel-saxon/pom.xml (original)
+++ camel/trunk/components/camel-saxon/pom.xml Wed Feb 10 10:57:45 2010
@@ -62,6 +62,10 @@
<groupId>net.sf.saxon</groupId>
<artifactId>saxon-sql</artifactId>
</dependency>
+ <dependency>
+ <groupId>net.sf.saxon</groupId>
+ <artifactId>saxon-xpath</artifactId>
+ </dependency>
<!-- testing -->
<dependency>
Modified:
camel/trunk/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java?rev=908446&r1=908445&r2=908446&view=diff
==============================================================================
---
camel/trunk/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java
(original)
+++
camel/trunk/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java
Wed Feb 10 10:57:45 2010
@@ -90,7 +90,7 @@
Object body = evaluate(exchange);
exchange.getOut().setBody(body);
- // propogate headers
+ // propagate headers
exchange.getOut().getHeaders().putAll(exchange.getIn().getHeaders());
}
Added:
camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XPathTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XPathTest.java?rev=908446&view=auto
==============================================================================
---
camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XPathTest.java
(added)
+++
camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XPathTest.java
Wed Feb 10 10:57:45 2010
@@ -0,0 +1,95 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.builder.saxon;
+
+import javax.xml.xpath.XPathFactory;
+
+import net.sf.saxon.xpath.XPathFactoryImpl;
+import org.apache.camel.builder.xml.XPathBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+/**
+ * @version $Revision$
+ */
+public class XPathTest extends CamelTestSupport {
+
+ @Test
+ public void testXPathUsingSaxon() throws Exception {
+ XPathFactory fac = new XPathFactoryImpl();
+ XPathBuilder builder = XPathBuilder.xpath("foo/bar").factory(fac);
+
+ String name = builder.evaluate(context,
"<foo><bar>cheese</bar></foo>", String.class);
+ assertEquals("cheese", name);
+
+ name = builder.evaluate(context, "<foo><bar>cheese</bar></foo>");
+ assertEquals("cheese", name);
+ }
+
+ @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);
+ }
+
+ @Test
+ public void testXPathFunctionTokenizeUsingSaxonXPathFactory() throws
Exception {
+ // 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
+ }
+
+ @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
+ }
+
+ @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
+ }
+}
Propchange:
camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XPathTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XPathTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: camel/trunk/parent/pom.xml
URL:
http://svn.apache.org/viewvc/camel/trunk/parent/pom.xml?rev=908446&r1=908445&r2=908446&view=diff
==============================================================================
--- camel/trunk/parent/pom.xml (original)
+++ camel/trunk/parent/pom.xml Wed Feb 10 10:57:45 2010
@@ -93,7 +93,7 @@
<qpid-version>0.5.0</qpid-version>
<quartz-version>1.6.6</quartz-version>
<restlet-version>1.1.5</restlet-version>
- <saxon-version>9.1.0.1</saxon-version>
+ <saxon-version>9.1.0.8</saxon-version>
<scala-version>2.7.7</scala-version>
<scala-plugin-version>2.9.1</scala-plugin-version>
<slf4j-version>1.5.10</slf4j-version>
@@ -1016,6 +1016,11 @@
<artifactId>saxon-sql</artifactId>
<version>${saxon-version}</version>
</dependency>
+ <dependency>
+ <groupId>net.sf.saxon</groupId>
+ <artifactId>saxon-xpath</artifactId>
+ <version>${saxon-version}</version>
+ </dependency>
<!-- optional CXF support -->
<dependency>