You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/11/09 15:58:39 UTC

[camel] branch master updated: CAMEL-15818: xquery language can now configure a custom saxon configuration instance.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 34b6d90  CAMEL-15818: xquery language can now configure a custom saxon configuration instance.
34b6d90 is described below

commit 34b6d9002b1fc140bec0d0f297a37a868b3750f8
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Nov 9 16:57:03 2020 +0100

    CAMEL-15818: xquery language can now configure a custom saxon configuration instance.
---
 .../apache/camel/catalog/docs/xquery-language.adoc |  3 +-
 .../org/apache/camel/language/xquery/xquery.json   |  1 +
 .../camel-saxon/src/main/docs/xquery-language.adoc |  3 +-
 .../camel/language/xquery/XQueryLanguage.java      | 17 +++++++
 .../SaxonLanguageExtensionFunctionsTest.java       | 57 ++++++++++++++++++++++
 .../org/apache/camel/model/language/xquery.json    |  1 +
 .../camel/model/language/XQueryExpression.java     | 29 +++++++++++
 .../reifier/language/XQueryExpressionReifier.java  |  5 ++
 .../java/org/apache/camel/xml/in/ModelParser.java  |  1 +
 .../modules/languages/pages/xquery-language.adoc   |  3 +-
 10 files changed, 117 insertions(+), 3 deletions(-)

diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/xquery-language.adoc b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/xquery-language.adoc
index f97fe2f..f83ac9c 100644
--- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/xquery-language.adoc
+++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/xquery-language.adoc
@@ -20,7 +20,7 @@ Recipient List.
 == XQuery Language options
 
 // language options: START
-The XQuery language supports 3 options, which are listed below.
+The XQuery language supports 4 options, which are listed below.
 
 
 
@@ -29,6 +29,7 @@ The XQuery language supports 3 options, which are listed below.
 | Name | Default | Java Type | Description
 | type |  | String | Sets the class name of the result type (type from output) The default result type is NodeSet
 | headerName |  | String | Name of header to use as input, instead of the message body
+| configurationRef |  | String | Reference to a saxon configuration instance in the registry to use for xquery (requires camel-saxon). This may be needed to add custom functions to a saxon configuration, so these custom functions can be used in xquery expressions.
 | trim | true | Boolean | Whether to trim the value to remove leading and trailing whitespaces and line breaks
 |===
 // language options: END
diff --git a/components/camel-saxon/src/generated/resources/org/apache/camel/language/xquery/xquery.json b/components/camel-saxon/src/generated/resources/org/apache/camel/language/xquery/xquery.json
index 739370a..8e777e2 100644
--- a/components/camel-saxon/src/generated/resources/org/apache/camel/language/xquery/xquery.json
+++ b/components/camel-saxon/src/generated/resources/org/apache/camel/language/xquery/xquery.json
@@ -19,6 +19,7 @@
     "expression": { "kind": "value", "displayName": "Expression", "required": true, "type": "string", "javaType": "java.lang.String", "deprecated": false, "secret": false, "description": "The expression value in your chosen language syntax" },
     "type": { "kind": "attribute", "displayName": "Type", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "secret": false, "description": "Sets the class name of the result type (type from output) The default result type is NodeSet" },
     "headerName": { "kind": "attribute", "displayName": "Header Name", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "secret": false, "description": "Name of header to use as input, instead of the message body" },
+    "configurationRef": { "kind": "attribute", "displayName": "Configuration Ref", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "secret": false, "description": "Reference to a saxon configuration instance in the registry to use for xquery (requires camel-saxon). This may be needed to add custom functions to a saxon configuration, so these custom functions can be used in xquery expressions." },
     "trim": { "kind": "attribute", "displayName": "Trim", "required": false, "type": "boolean", "javaType": "java.lang.Boolean", "deprecated": false, "secret": false, "defaultValue": true, "description": "Whether to trim the value to remove leading and trailing whitespaces and line breaks" },
     "id": { "kind": "attribute", "displayName": "Id", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "secret": false, "description": "Sets the id of this node" }
   }
diff --git a/components/camel-saxon/src/main/docs/xquery-language.adoc b/components/camel-saxon/src/main/docs/xquery-language.adoc
index f97fe2f..f83ac9c 100644
--- a/components/camel-saxon/src/main/docs/xquery-language.adoc
+++ b/components/camel-saxon/src/main/docs/xquery-language.adoc
@@ -20,7 +20,7 @@ Recipient List.
 == XQuery Language options
 
 // language options: START
-The XQuery language supports 3 options, which are listed below.
+The XQuery language supports 4 options, which are listed below.
 
 
 
@@ -29,6 +29,7 @@ The XQuery language supports 3 options, which are listed below.
 | Name | Default | Java Type | Description
 | type |  | String | Sets the class name of the result type (type from output) The default result type is NodeSet
 | headerName |  | String | Name of header to use as input, instead of the message body
+| configurationRef |  | String | Reference to a saxon configuration instance in the registry to use for xquery (requires camel-saxon). This may be needed to add custom functions to a saxon configuration, so these custom functions can be used in xquery expressions.
 | trim | true | Boolean | Whether to trim the value to remove leading and trailing whitespaces and line breaks
 |===
 // language options: END
diff --git a/components/camel-saxon/src/main/java/org/apache/camel/language/xquery/XQueryLanguage.java b/components/camel-saxon/src/main/java/org/apache/camel/language/xquery/XQueryLanguage.java
index 783e5cb..3cb668d 100644
--- a/components/camel-saxon/src/main/java/org/apache/camel/language/xquery/XQueryLanguage.java
+++ b/components/camel-saxon/src/main/java/org/apache/camel/language/xquery/XQueryLanguage.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.language.xquery;
 
+import net.sf.saxon.Configuration;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Expression;
 import org.apache.camel.Predicate;
@@ -30,6 +31,7 @@ public class XQueryLanguage extends LanguageSupport implements PropertyConfigure
 
     private Class<?> resultType;
     private String headerName;
+    private Configuration configuration;
 
     public Class<?> getResultType() {
         return resultType;
@@ -47,6 +49,14 @@ public class XQueryLanguage extends LanguageSupport implements PropertyConfigure
         this.headerName = headerName;
     }
 
+    public Configuration getConfiguration() {
+        return configuration;
+    }
+
+    public void setConfiguration(Configuration configuration) {
+        this.configuration = configuration;
+    }
+
     @Override
     public Predicate createPredicate(String expression) {
         return (Predicate) createExpression(expression, null);
@@ -80,6 +90,9 @@ public class XQueryLanguage extends LanguageSupport implements PropertyConfigure
         if (str != null) {
             builder.setHeaderName(str);
         }
+        if (configuration != null) {
+            builder.setConfiguration(configuration);
+        }
     }
 
     @Override
@@ -96,6 +109,10 @@ public class XQueryLanguage extends LanguageSupport implements PropertyConfigure
             case "headerName":
                 setHeaderName(PropertyConfigurerSupport.property(camelContext, String.class, value));
                 return true;
+            case "configuration":
+            case "Configuration":
+                setConfiguration(PropertyConfigurerSupport.property(camelContext, Configuration.class, value));
+                return true;
             default:
                 return false;
         }
diff --git a/components/camel-saxon/src/test/java/org/apache/camel/component/xslt/extensions/SaxonLanguageExtensionFunctionsTest.java b/components/camel-saxon/src/test/java/org/apache/camel/component/xslt/extensions/SaxonLanguageExtensionFunctionsTest.java
new file mode 100644
index 0000000..c16d5e9
--- /dev/null
+++ b/components/camel-saxon/src/test/java/org/apache/camel/component/xslt/extensions/SaxonLanguageExtensionFunctionsTest.java
@@ -0,0 +1,57 @@
+/*
+ * 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.xslt.extensions;
+
+import net.sf.saxon.Configuration;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.component.xquery.XQueryWithExtensionTest;
+import org.apache.camel.language.xquery.XQueryLanguage;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+public class SaxonLanguageExtensionFunctionsTest extends CamelTestSupport {
+
+    @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 {
+                Configuration conf = new Configuration();
+                conf.registerExtensionFunction(new XQueryWithExtensionTest.SimpleExtension());
+
+                XQueryLanguage xq = (XQueryLanguage) context.resolveLanguage("xquery");
+                xq.setConfiguration(conf);
+
+                from("direct:start")
+                        .transform()
+                        .xquery("resource:classpath:org/apache/camel/component/xquery/transformWithExtension.xquery")
+                        .to("mock:result");
+            }
+        };
+    }
+}
diff --git a/core/camel-core-model/src/generated/resources/org/apache/camel/model/language/xquery.json b/core/camel-core-model/src/generated/resources/org/apache/camel/model/language/xquery.json
index 81053d2..1f2fcb5 100644
--- a/core/camel-core-model/src/generated/resources/org/apache/camel/model/language/xquery.json
+++ b/core/camel-core-model/src/generated/resources/org/apache/camel/model/language/xquery.json
@@ -15,6 +15,7 @@
     "expression": { "kind": "value", "displayName": "Expression", "required": true, "type": "string", "javaType": "java.lang.String", "deprecated": false, "secret": false, "description": "The expression value in your chosen language syntax" },
     "type": { "kind": "attribute", "displayName": "Type", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "secret": false, "description": "Sets the class name of the result type (type from output) The default result type is NodeSet" },
     "headerName": { "kind": "attribute", "displayName": "Header Name", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "secret": false, "description": "Name of header to use as input, instead of the message body" },
+    "configurationRef": { "kind": "attribute", "displayName": "Configuration Ref", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "secret": false, "description": "Reference to a saxon configuration instance in the registry to use for xquery (requires camel-saxon). This may be needed to add custom functions to a saxon configuration, so these custom functions can be used in xquery expressions." },
     "trim": { "kind": "attribute", "displayName": "Trim", "required": false, "type": "boolean", "javaType": "java.lang.Boolean", "deprecated": false, "secret": false, "defaultValue": true, "description": "Whether to trim the value to remove leading and trailing whitespaces and line breaks" },
     "id": { "kind": "attribute", "displayName": "Id", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "secret": false, "description": "Sets the id of this node" }
   }
diff --git a/core/camel-core-model/src/main/java/org/apache/camel/model/language/XQueryExpression.java b/core/camel-core-model/src/main/java/org/apache/camel/model/language/XQueryExpression.java
index d7564e8..498e73f 100644
--- a/core/camel-core-model/src/main/java/org/apache/camel/model/language/XQueryExpression.java
+++ b/core/camel-core-model/src/main/java/org/apache/camel/model/language/XQueryExpression.java
@@ -37,6 +37,11 @@ public class XQueryExpression extends NamespaceAwareExpression {
     private Class<?> resultType;
     @XmlAttribute
     private String headerName;
+    @XmlAttribute
+    @Metadata(label = "advanced")
+    private String configurationRef;
+    @XmlTransient
+    private Object configuration;
 
     public XQueryExpression() {
     }
@@ -87,4 +92,28 @@ public class XQueryExpression extends NamespaceAwareExpression {
         this.headerName = headerName;
     }
 
+    public String getConfigurationRef() {
+        return configurationRef;
+    }
+
+    /**
+     * Reference to a saxon configuration instance in the registry to use for xquery (requires camel-saxon). This may be
+     * needed to add custom functions to a saxon configuration, so these custom functions can be used in xquery
+     * expressions.
+     */
+    public void setConfigurationRef(String configurationRef) {
+        this.configurationRef = configurationRef;
+    }
+
+    public Object getConfiguration() {
+        return configuration;
+    }
+
+    /**
+     * Custom saxon configuration (requires camel-saxon). This may be needed to add custom functions to a saxon
+     * configuration, so these custom functions can be used in xquery expressions.
+     */
+    public void setConfiguration(Object configuration) {
+        this.configuration = configuration;
+    }
 }
diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/language/XQueryExpressionReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/language/XQueryExpressionReifier.java
index 24f3d30..e9f98f0 100644
--- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/language/XQueryExpressionReifier.java
+++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/language/XQueryExpressionReifier.java
@@ -24,6 +24,7 @@ import org.apache.camel.model.language.ExpressionDefinition;
 import org.apache.camel.model.language.XQueryExpression;
 import org.apache.camel.spi.Language;
 import org.apache.camel.spi.NamespaceAware;
+import org.apache.camel.support.CamelContextHelper;
 
 public class XQueryExpressionReifier extends ExpressionReifier<XQueryExpression> {
 
@@ -75,6 +76,10 @@ public class XQueryExpressionReifier extends ExpressionReifier<XQueryExpression>
                 throw RuntimeCamelException.wrapRuntimeException(e);
             }
         }
+        if (definition.getConfiguration() == null && definition.getConfigurationRef() != null) {
+            definition.setConfiguration(
+                    CamelContextHelper.mandatoryLookupAndConvert(camelContext, definition.getConfigurationRef(), Object.class));
+        }
     }
 
 }
diff --git a/core/camel-xml-io/src/generated/java/org/apache/camel/xml/in/ModelParser.java b/core/camel-xml-io/src/generated/java/org/apache/camel/xml/in/ModelParser.java
index a184110..433e123 100644
--- a/core/camel-xml-io/src/generated/java/org/apache/camel/xml/in/ModelParser.java
+++ b/core/camel-xml-io/src/generated/java/org/apache/camel/xml/in/ModelParser.java
@@ -2467,6 +2467,7 @@ public class ModelParser extends BaseParser {
     protected XQueryExpression doParseXQueryExpression() throws IOException, XmlPullParserException {
         return doParse(new XQueryExpression(), (def, key, val) -> {
             switch (key) {
+                case "configurationRef": def.setConfigurationRef(val); break;
                 case "headerName": def.setHeaderName(val); break;
                 case "type": def.setType(val); break;
                 default: return expressionDefinitionAttributeHandler().accept(def, key, val);
diff --git a/docs/components/modules/languages/pages/xquery-language.adoc b/docs/components/modules/languages/pages/xquery-language.adoc
index 72efd84..8528f01 100644
--- a/docs/components/modules/languages/pages/xquery-language.adoc
+++ b/docs/components/modules/languages/pages/xquery-language.adoc
@@ -22,7 +22,7 @@ Recipient List.
 == XQuery Language options
 
 // language options: START
-The XQuery language supports 3 options, which are listed below.
+The XQuery language supports 4 options, which are listed below.
 
 
 
@@ -31,6 +31,7 @@ The XQuery language supports 3 options, which are listed below.
 | Name | Default | Java Type | Description
 | type |  | String | Sets the class name of the result type (type from output) The default result type is NodeSet
 | headerName |  | String | Name of header to use as input, instead of the message body
+| configurationRef |  | String | Reference to a saxon configuration instance in the registry to use for xquery (requires camel-saxon). This may be needed to add custom functions to a saxon configuration, so these custom functions can be used in xquery expressions.
 | trim | true | Boolean | Whether to trim the value to remove leading and trailing whitespaces and line breaks
 |===
 // language options: END