Updated Branches: refs/heads/camel-2.11.x dd72c9646 -> 4af18e9c7
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/4af18e9c Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/4af18e9c Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/4af18e9c Branch: refs/heads/camel-2.11.x Commit: 4af18e9c7ad8218f48b9c86bcc6f87655500e6ab Parents: dd72c96 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:48:43 2013 +0200 ---------------------------------------------------------------------- .../camel/component/xquery/XQueryBuilder.java | 11 +- .../xquery/XQueryWithExtensionTest.java | 118 +++++++++++++++++++ .../xquery/transformWithExtension.xquery | 6 + 3 files changed, 132 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/4af18e9c/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..d0fc7bf 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 {}", configuration); + } else { + LOG.debug("Using existing Configuration {}", configuration); + } staticQueryContext = getConfiguration().newStaticQueryContext(); if (moduleURIResolver != null) { http://git-wip-us.apache.org/repos/asf/camel/blob/4af18e9c/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..75efb24 --- /dev/null +++ b/components/camel-saxon/src/test/java/org/apache/camel/component/xquery/XQueryWithExtensionTest.java @@ -0,0 +1,118 @@ +/** + * 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.SequenceIterator; +import net.sf.saxon.om.StructuredQName; +import net.sf.saxon.trans.XPathException; +import net.sf.saxon.tree.iter.SingletonIterator; +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 SequenceIterator<? extends Item> call(SequenceIterator<? extends Item>[] sequenceIterators, XPathContext xPathContext) throws XPathException { + Item arg1 = sequenceIterators[0].next(); + String arg1Val = arg1.getStringValue(); + + // return a altered version of the first arg + StringValue sv = new StringValue("arg1[" + arg1Val + "]"); + return SingletonIterator.makeIterator(sv); + } + }; + } + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/4af18e9c/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