This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 24f60fabd14b CAMEL-27485: camel-support - Namespaces can be created
from a string of prefix=uri mappings, for namespacesRef (#30691)
24f60fabd14b is described below
commit 24f60fabd14b4cc6320900950d85df316ba04310
Author: Claus Ibsen <[email protected]>
AuthorDate: Tue Nov 24 07:06:49 2026 +0100
CAMEL-27485: camel-support - Namespaces can be created from a string of
prefix=uri mappings, for namespacesRef (#30691)
New constructor Namespaces(String mappings), for mappings such as
"p1=uri1,p2=uri2" separated by commas or new
lines. A Namespaces bean can then be declared from one property, such as a
Kamelet option, and used with the
namespacesRef of an xpath (or xquery, xtokenize), to configure any number
of namespaces.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Claus Ibsen <[email protected]>
---
.../apache/camel/catalog/docs/xpath-language.adoc | 17 ++++++
.../camel-xpath/src/main/docs/xpath-language.adoc | 17 ++++++
.../apache/camel/support/builder/Namespaces.java | 30 +++++++++++
.../camel/support/builder/NamespacesTest.java | 63 ++++++++++++++++++++++
.../dsl/yaml/XPathNamespacesRefYamlDslTest.java | 47 ++++++++++++++++
5 files changed, 174 insertions(+)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/xpath-language.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/xpath-language.adoc
index eadf004803c8..dc8399a6d2bb 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/xpath-language.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/xpath-language.adoc
@@ -107,6 +107,23 @@ If an expression declares namespaces inline as well, then
the inline namespaces
The `namespacesRef` option is also available on the
xref:xquery-language.adoc[XQuery] and
xref:xtokenize-language.adoc[XML Tokenize] languages.
+=== Several namespaces from one string
+
+A `Namespaces` bean can also be created from a single string of `prefix=uri`
mappings, separated by commas or new
+lines. This lets one property, such as a Kamelet option, carry any number of
namespaces:
+
+[source,yaml]
+----
+- beans:
+ - name: myNamespaces
+ type: org.apache.camel.support.builder.Namespaces
+ constructors:
+ 0: "{{namespaces}}"
+----
+
+With `namespaces=c=http://acme.com/cheese,w=http://acme.com/wine`, an xpath
with `namespacesRef: "myNamespaces"` can
+use both the `c` and the `w` prefix. A mapping without a prefix or a URI fails
when the bean is created.
+
== Variables
Variables in XPath are defined in different namespaces.
diff --git a/components/camel-xpath/src/main/docs/xpath-language.adoc
b/components/camel-xpath/src/main/docs/xpath-language.adoc
index eadf004803c8..dc8399a6d2bb 100644
--- a/components/camel-xpath/src/main/docs/xpath-language.adoc
+++ b/components/camel-xpath/src/main/docs/xpath-language.adoc
@@ -107,6 +107,23 @@ If an expression declares namespaces inline as well, then
the inline namespaces
The `namespacesRef` option is also available on the
xref:xquery-language.adoc[XQuery] and
xref:xtokenize-language.adoc[XML Tokenize] languages.
+=== Several namespaces from one string
+
+A `Namespaces` bean can also be created from a single string of `prefix=uri`
mappings, separated by commas or new
+lines. This lets one property, such as a Kamelet option, carry any number of
namespaces:
+
+[source,yaml]
+----
+- beans:
+ - name: myNamespaces
+ type: org.apache.camel.support.builder.Namespaces
+ constructors:
+ 0: "{{namespaces}}"
+----
+
+With `namespaces=c=http://acme.com/cheese,w=http://acme.com/wine`, an xpath
with `namespacesRef: "myNamespaces"` can
+use both the `c` and the `w` prefix. A mapping without a prefix or a URI fails
when the bean is created.
+
== Variables
Variables in XPath are defined in different namespaces.
diff --git
a/core/camel-support/src/main/java/org/apache/camel/support/builder/Namespaces.java
b/core/camel-support/src/main/java/org/apache/camel/support/builder/Namespaces.java
index 93b5317850e4..b7d027d9686a 100644
---
a/core/camel-support/src/main/java/org/apache/camel/support/builder/Namespaces.java
+++
b/core/camel-support/src/main/java/org/apache/camel/support/builder/Namespaces.java
@@ -49,6 +49,36 @@ public class Namespaces {
add(prefix, uri);
}
+ /**
+ * Creates a namespace context from prefix to URI mappings in a single
string, such as
+ * {@code p1=http://acme.com/one,p2=http://acme.com/two}. The mappings are
separated by commas or new lines, and the
+ * prefix and the URI by the first {@code =}. Whitespace around them is
ignored, and an empty string has no
+ * mappings. This allows to configure several namespaces from one
property, for example a bean referred to by the
+ * {@code namespacesRef} of an xpath.
+ *
+ * @param mappings the mappings, such as {@code
p1=uri1,p2=uri2}
+ * @throws IllegalArgumentException if a mapping has no prefix or no URI
+ */
+ public Namespaces(String mappings) {
+ if (mappings == null) {
+ return;
+ }
+ for (String mapping : mappings.split("[,\\r\\n]")) {
+ String entry = mapping.trim();
+ if (entry.isEmpty()) {
+ continue;
+ }
+ int separator = entry.indexOf('=');
+ String prefix = separator > 0 ? entry.substring(0,
separator).trim() : "";
+ String uri = separator > 0 ? entry.substring(separator + 1).trim()
: "";
+ if (prefix.isEmpty() || uri.isEmpty()) {
+ throw new IllegalArgumentException(
+ "Invalid namespace mapping: " + entry + ". Use
prefix=uri, such as p1=http://acme.com/one");
+ }
+ add(prefix, uri);
+ }
+ }
+
/**
* Returns true if the given namespaceURI is empty or if it matches the
given expected namespace
*/
diff --git
a/core/camel-support/src/test/java/org/apache/camel/support/builder/NamespacesTest.java
b/core/camel-support/src/test/java/org/apache/camel/support/builder/NamespacesTest.java
new file mode 100644
index 000000000000..3e0f4bab721c
--- /dev/null
+++
b/core/camel-support/src/test/java/org/apache/camel/support/builder/NamespacesTest.java
@@ -0,0 +1,63 @@
+/*
+ * 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.support.builder;
+
+import java.util.Map;
+
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+class NamespacesTest {
+
+ @Test
+ void createsTheMappingsOfAString() {
+ assertThat(new
Namespaces("c=http://acme.com/cheese,w=http://acme.com/wine").getNamespaces())
+ .containsExactlyInAnyOrderEntriesOf(Map.of("c",
"http://acme.com/cheese", "w", "http://acme.com/wine"));
+ }
+
+ @Test
+ void ignoresWhitespaceAndAcceptsNewLines() {
+ assertThat(new Namespaces(" c = http://acme.com/cheese ,\n
w=urn:acme:wine\n").getNamespaces())
+ .containsExactlyInAnyOrderEntriesOf(Map.of("c",
"http://acme.com/cheese", "w", "urn:acme:wine"));
+ }
+
+ @Test
+ void keepsAnEqualsSignInTheUri() {
+ assertThat(new
Namespaces("q=http://acme.com/ns?version=2").getNamespaces())
+ .containsExactlyEntriesOf(Map.of("q",
"http://acme.com/ns?version=2"));
+ }
+
+ @Test
+ void anEmptyStringHasNoMappings() {
+ assertThat(new Namespaces("").getNamespaces()).isEmpty();
+ assertThat(new Namespaces(" , ").getNamespaces()).isEmpty();
+ assertThat(new Namespaces((String) null).getNamespaces()).isEmpty();
+ }
+
+ @Test
+ void failsOnAMappingWithoutPrefixOrUri() {
+ assertThatThrownBy(() -> new Namespaces("http://acme.com/cheese"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("Invalid namespace mapping:
http://acme.com/cheese");
+ assertThatThrownBy(() -> new Namespaces("=http://acme.com/cheese"))
+ .isInstanceOf(IllegalArgumentException.class);
+ assertThatThrownBy(() -> new Namespaces("c="))
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+}
diff --git
a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/java/org/apache/camel/dsl/yaml/XPathNamespacesRefYamlDslTest.java
b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/java/org/apache/camel/dsl/yaml/XPathNamespacesRefYamlDslTest.java
index 5726b286eb07..fdfc6f6a9967 100644
---
a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/java/org/apache/camel/dsl/yaml/XPathNamespacesRefYamlDslTest.java
+++
b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/java/org/apache/camel/dsl/yaml/XPathNamespacesRefYamlDslTest.java
@@ -16,6 +16,8 @@
*/
package org.apache.camel.dsl.yaml;
+import java.util.Properties;
+
import org.apache.camel.RoutesBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.DefaultCamelContext;
@@ -96,4 +98,49 @@ class XPathNamespacesRefYamlDslTest {
noMatch.assertIsSatisfied();
}
}
+
+ @Test
+ void loadsXPathWithNamespacesFromOneString() throws Exception {
+ try (DefaultCamelContext context = new DefaultCamelContext()) {
+ // several namespaces in one property, as a Kamelet option is a
single string
+ Properties properties = new Properties();
+ properties.setProperty("namespaces",
"c=http://acme.com/cheese,w=http://acme.com/wine");
+ context.getPropertiesComponent().setInitialProperties(properties);
+
+ YamlRoutesBuilderLoader loader = new YamlRoutesBuilderLoader();
+ loader.setCamelContext(context);
+ RoutesBuilder routesBuilder =
loader.loadRoutesBuilder(ResourceHelper.fromString("xpath-namespaces-string.yaml",
"""
+ - beans:
+ - name: myNamespaces
+ type: org.apache.camel.support.builder.Namespaces
+ constructors:
+ 0: "{{namespaces}}"
+ - route:
+ from:
+ uri: "direct:start"
+ steps:
+ - filter:
+ xpath:
+ expression: "/c:order/w:wine = 'Rioja'"
+ resultQName: "BOOLEAN"
+ namespacesRef: "myNamespaces"
+ steps:
+ - to: "mock:match"
+ """));
+ context.addRoutes(routesBuilder);
+ context.start();
+
+ MockEndpoint match = context.getEndpoint("mock:match",
MockEndpoint.class);
+ match.expectedBodiesReceived(
+ "<order xmlns=\"http://acme.com/cheese\"><wine
xmlns=\"http://acme.com/wine\">Rioja</wine></order>");
+
+ context.createProducerTemplate().sendBody("direct:start",
+ "<order xmlns=\"http://acme.com/cheese\"><wine
xmlns=\"http://acme.com/wine\">Rioja</wine></order>");
+ // the wine element in the cheese namespace: no match
+ context.createProducerTemplate().sendBody("direct:start",
+ "<order
xmlns=\"http://acme.com/cheese\"><wine>Rioja</wine></order>");
+
+ match.assertIsSatisfied();
+ }
+ }
}