This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-2.19.x in repository https://gitbox.apache.org/repos/asf/camel.git
commit ab08e13d39a1b5dc5aeaf71433309d285c5f7cea Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Mon Jan 22 09:54:56 2018 +0100 CAMEL-12154: camel-saxon should set parameters on saxon using their type, eg string, boolean, integer etc. Thanks to Niels Bertram for the unit test. --- .../camel/component/xquery/XQueryBuilder.java | 37 ++++++++- .../camel/builder/saxon/ParameterDynamicTest.java | 96 ++++++++++++++++++++++ 2 files changed, 129 insertions(+), 4 deletions(-) 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 98be79b..28aacc9 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 @@ -22,6 +22,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.Reader; import java.io.StringWriter; +import java.math.BigDecimal; +import java.math.BigInteger; import java.net.URL; import java.util.Collection; import java.util.Collections; @@ -40,6 +42,14 @@ import javax.xml.transform.stax.StAXSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; +import net.sf.saxon.value.BigDecimalValue; +import net.sf.saxon.value.BooleanValue; +import net.sf.saxon.value.DoubleValue; +import net.sf.saxon.value.FloatValue; +import net.sf.saxon.value.Int64Value; +import net.sf.saxon.value.IntegerValue; +import net.sf.saxon.value.NumericValue; +import net.sf.saxon.value.StringValue; import org.w3c.dom.Node; import net.sf.saxon.Configuration; @@ -623,19 +633,19 @@ public abstract class XQueryBuilder implements Expression, Predicate, NamespaceA addParameters(dynamicQueryContext, exchange.getIn().getHeaders(), "in.headers."); dynamicQueryContext.setParameter( StructuredQName.fromClarkName("in.body"), - new ObjectValue(exchange.getIn().getBody()) + getAsParameter(exchange.getIn().getBody()) ); addParameters(dynamicQueryContext, getParameters()); dynamicQueryContext.setParameter( StructuredQName.fromClarkName("exchange"), - new ObjectValue(exchange) + getAsParameter(exchange) ); if (exchange.hasOut() && exchange.getPattern().isOutCapable()) { dynamicQueryContext.setParameter( StructuredQName.fromClarkName("out.body"), - new ObjectValue(exchange.getOut().getBody()) + getAsParameter(exchange.getOut().getBody()) ); addParameters(dynamicQueryContext, exchange.getOut().getHeaders(), "out.headers."); @@ -653,12 +663,31 @@ public abstract class XQueryBuilder implements Expression, Predicate, NamespaceA if (entry.getValue() != null) { dynamicQueryContext.setParameter( StructuredQName.fromClarkName(parameterPrefix + entry.getKey()), - new ObjectValue(entry.getValue()) + getAsParameter(entry.getValue()) ); } } } + @SuppressWarnings("unchecked") + protected Item getAsParameter(Object value) { + if (value instanceof String) { + return new StringValue((CharSequence) value); + } else if (value instanceof Boolean) { + return BooleanValue.get((Boolean) value); + } else if (value instanceof Long) { + return Int64Value.makeIntegerValue((Long) value); + } else if (value instanceof BigInteger) { + return Int64Value.makeIntegerValue((BigInteger) value); + } else if (value instanceof Double) { + return DoubleValue.makeDoubleValue((double) value); + } else if (value instanceof Float) { + return FloatValue.makeFloatValue((float) value); + } else { + return new ObjectValue(value); + } + } + protected boolean matches(Exchange exchange, List<?> results) { return ObjectHelper.matches(results); } diff --git a/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/ParameterDynamicTest.java b/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/ParameterDynamicTest.java new file mode 100644 index 0000000..c1c62f1 --- /dev/null +++ b/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/ParameterDynamicTest.java @@ -0,0 +1,96 @@ +/** + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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 net.sf.saxon.Configuration; +import net.sf.saxon.om.Item; +import net.sf.saxon.om.StructuredQName; +import net.sf.saxon.query.DynamicQueryContext; +import net.sf.saxon.query.XQueryExpression; +import net.sf.saxon.value.BooleanValue; +import net.sf.saxon.value.ObjectValue; +import org.apache.camel.Exchange; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.impl.DefaultExchange; +import org.junit.Before; +import org.junit.Test; + +import static org.apache.camel.component.xquery.XQueryBuilder.xquery; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class ParameterDynamicTest { + + private static final String TEST_QUERY = new StringBuilder() + .append("xquery version \"3.0\" encoding \"UTF-8\";\n") + .append("declare variable $extParam as xs:boolean external := false();\n") + .append("if($extParam) then(true()) else (false())") + .toString(); + + private Configuration conf = new Configuration(); + private XQueryExpression query; + private DynamicQueryContext context; + + @Before + public void setup() throws Exception { + conf.setCompileWithTracing(true); + query = conf.newStaticQueryContext().compileQuery(TEST_QUERY); + context = new DynamicQueryContext(conf); + } + + /** + * This is what Camel XQueryBuilder executes, which leads to a parameter binding type error. + */ + @Test + public void testObjectParameter() throws Exception { + context.setParameter(StructuredQName.fromClarkName("extParam"), new ObjectValue<>(true)); + try { + Item result = query.iterator(context).next(); + fail("Should have thrown an exception"); + assertTrue(result instanceof BooleanValue); + assertEquals(true, ((BooleanValue) result).getBooleanValue()); + } catch (Exception e) { + // expected + } + } + + /** + * This is what Camel XQueryBuilder should execute to allow Saxon to bind the parameter type properly. + */ + @Test + public void testBooleanParameter() throws Exception { + context.setParameter(StructuredQName.fromClarkName("extParam"), BooleanValue.TRUE); + Item result = query.iterator(context).next(); + assertTrue(result instanceof BooleanValue); + assertEquals(true, ((BooleanValue) result).getBooleanValue()); + } + + @Test + public void testXQueryBuilder() throws Exception { + Exchange exchange = new DefaultExchange(new DefaultCamelContext()); + exchange.getIn().setBody("<foo><bar>abc_def_ghi</bar></foo>"); + exchange.setProperty("extParam", true); + + Object result = xquery(TEST_QUERY).asString().evaluate(exchange, boolean.class); + assertEquals(true, result); + + exchange.setProperty("extParam", false); + result = xquery(TEST_QUERY).asString().evaluate(exchange, boolean.class); + assertEquals(false, result); + } +} -- To stop receiving notification emails like this one, please contact davscl...@apache.org.