Updated Branches:
  refs/heads/master c148280e2 -> ed8acf931

CAMEL-6548: Fixed xquery when using custom configuration. Thanks to Niels 
Bertram for the patch.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/ed8acf93
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/ed8acf93
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/ed8acf93

Branch: refs/heads/master
Commit: ed8acf931a22fb02026970f78031f3a16b06034a
Parents: c148280
Author: Claus Ibsen <davscl...@apache.org>
Authored: Sun Jul 14 10:20:44 2013 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Sun Jul 14 10:21:02 2013 +0200

----------------------------------------------------------------------
 .../camel/component/xquery/XQueryBuilder.java   |  11 +-
 .../xquery/XQueryWithExtensionTest.java         | 117 +++++++++++++++++++
 .../xquery/transformWithExtension.xquery        |   6 +
 3 files changed, 131 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/ed8acf93/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java
----------------------------------------------------------------------
diff --git 
a/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java
 
b/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java
index 1b6e640..d7aab9a 100644
--- 
a/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java
+++ 
b/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java
@@ -636,9 +636,14 @@ public abstract class XQueryBuilder implements Expression, 
Predicate, NamespaceA
         // must use synchronized for concurrency issues and only let it 
initialize once
         if (!initialized.get()) {
             LOG.debug("Initializing XQueryBuilder {}", this);
-            configuration = new Configuration();
-            configuration.setHostLanguage(Configuration.XQUERY);
-            configuration.setStripsWhiteSpace(isStripsAllWhiteSpace() ? 
Whitespace.ALL : Whitespace.IGNORABLE);
+            if (configuration == null) {
+                configuration = new Configuration();
+                configuration.setHostLanguage(Configuration.XQUERY);
+                configuration.setStripsWhiteSpace(isStripsAllWhiteSpace() ? 
Whitespace.ALL : Whitespace.IGNORABLE);
+                LOG.debug("Created new Configuration {}");
+            } else {
+                LOG.debug("Using existing Configuration {}", configuration);
+            }
 
             staticQueryContext = getConfiguration().newStaticQueryContext();
             if (moduleURIResolver != null) {

http://git-wip-us.apache.org/repos/asf/camel/blob/ed8acf93/components/camel-saxon/src/test/java/org/apache/camel/component/xquery/XQueryWithExtensionTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-saxon/src/test/java/org/apache/camel/component/xquery/XQueryWithExtensionTest.java
 
b/components/camel-saxon/src/test/java/org/apache/camel/component/xquery/XQueryWithExtensionTest.java
new file mode 100644
index 0000000..26401da
--- /dev/null
+++ 
b/components/camel-saxon/src/test/java/org/apache/camel/component/xquery/XQueryWithExtensionTest.java
@@ -0,0 +1,117 @@
+/**
+ * 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.component.xquery;
+
+import net.sf.saxon.Configuration;
+import net.sf.saxon.expr.XPathContext;
+import net.sf.saxon.lib.ExtensionFunctionCall;
+import net.sf.saxon.lib.ExtensionFunctionDefinition;
+import net.sf.saxon.om.Item;
+import net.sf.saxon.om.Sequence;
+import net.sf.saxon.om.StructuredQName;
+import net.sf.saxon.trans.XPathException;
+import net.sf.saxon.value.SequenceType;
+import net.sf.saxon.value.StringValue;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+/**
+ * @version
+ */
+public class XQueryWithExtensionTest extends CamelTestSupport {
+
+    private Configuration conf;
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+
+        conf = new Configuration();
+        conf.registerExtensionFunction(new SimpleExtension());
+
+        jndi.bind("saxonConf", conf);
+        return jndi;
+    }
+
+    @Test
+    public void testWithExtension() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("<transformed 
extension-function-render=\"arg1[test]\"/>");
+
+        template.sendBody("direct:start", "<body>test</body>");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    
.to("xquery:org/apache/camel/component/xquery/transformWithExtension.xquery?configuration=#saxonConf")
+                    .to("mock:result");
+            }
+        };
+    }
+
+    /**
+     * This is a very simple example of a saxon extension function. We will use
+     * this for testing purposes.
+     * <p/>
+     * Example: <code>efx:simple('some text')</code> will be rendered to
+     * <code>arg1[some text]</code> and returned in the XQuery response.
+     */
+    public static final class SimpleExtension extends 
ExtensionFunctionDefinition {
+
+        private static final long serialVersionUID = 1L;
+
+        @Override
+        public SequenceType[] getArgumentTypes() {
+            return new SequenceType[]{SequenceType.SINGLE_STRING};
+        }
+
+        @Override
+        public SequenceType getResultType(SequenceType[] 
suppliedArgumentTypes) {
+            return SequenceType.SINGLE_STRING;
+        }
+
+        @Override
+        public StructuredQName getFunctionQName() {
+            return new StructuredQName("efx", "http://test/saxon/ext";, 
"simple");
+        }
+
+        @Override
+        public ExtensionFunctionCall makeCallExpression() {
+            return new ExtensionFunctionCall() {
+                @Override
+                public Sequence call(XPathContext xPathContext, Sequence[] 
sequences) throws XPathException {
+                    // get value of first arg passed to the function
+                    Item arg1 = sequences[0].head();
+                    String arg1Val = arg1.getStringValue();
+
+                    // return a altered version of the first arg
+                    return new StringValue("arg1[" + arg1Val + "]");
+                }
+            };
+        }
+    }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/ed8acf93/components/camel-saxon/src/test/resources/org/apache/camel/component/xquery/transformWithExtension.xquery
----------------------------------------------------------------------
diff --git 
a/components/camel-saxon/src/test/resources/org/apache/camel/component/xquery/transformWithExtension.xquery
 
b/components/camel-saxon/src/test/resources/org/apache/camel/component/xquery/transformWithExtension.xquery
new file mode 100644
index 0000000..dff3ece
--- /dev/null
+++ 
b/components/camel-saxon/src/test/resources/org/apache/camel/component/xquery/transformWithExtension.xquery
@@ -0,0 +1,6 @@
+xquery version "1.0" encoding "UTF-8";
+
+(: the prefix declaration for our custom extension :)
+declare namespace efx = "http://test/saxon/ext";;
+
+<transformed extension-function-render="{efx:simple(/body/text())}" />
\ No newline at end of file

Reply via email to