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/07/29 05:22:11 UTC

[camel] branch camel-3.4.x updated: CAMEL-15346 Let xml-io pass the namespace info to NamespaceAware elements

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

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


The following commit(s) were added to refs/heads/camel-3.4.x by this push:
     new bcba03f  CAMEL-15346 Let xml-io pass the namespace info to NamespaceAware elements
bcba03f is described below

commit bcba03f68cab6c6cb0a2146733f5ea09d534145e
Author: Peter Palaga <pp...@redhat.com>
AuthorDate: Tue Jul 28 14:28:18 2020 +0200

    CAMEL-15346 Let xml-io pass the namespace info to NamespaceAware
    elements
---
 .../java/org/apache/camel/xml/in/BaseParser.java   | 17 ++++++++++++++
 .../org/apache/camel/xml/in/ModelParserTest.java   | 26 ++++++++++++++++++++++
 2 files changed, 43 insertions(+)

diff --git a/core/camel-xml-io/src/main/java/org/apache/camel/xml/in/BaseParser.java b/core/camel-xml-io/src/main/java/org/apache/camel/xml/in/BaseParser.java
index 8b166e6..2afa821 100644
--- a/core/camel-xml-io/src/main/java/org/apache/camel/xml/in/BaseParser.java
+++ b/core/camel-xml-io/src/main/java/org/apache/camel/xml/in/BaseParser.java
@@ -23,19 +23,26 @@ import java.lang.reflect.Array;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Base64;
+import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Objects;
 import java.util.Set;
 import java.util.function.Consumer;
 
 import org.apache.camel.model.language.ExpressionDefinition;
+import org.apache.camel.spi.NamespaceAware;
 import org.apache.camel.xml.io.MXParser;
 import org.apache.camel.xml.io.XmlPullParser;
 import org.apache.camel.xml.io.XmlPullParserException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class BaseParser {
 
+    private static final Logger LOG = LoggerFactory.getLogger(BaseParser.class);
+
     protected final MXParser parser;
     protected String namespace;
 
@@ -63,6 +70,16 @@ public class BaseParser {
 
     protected <T> T doParse(T definition, AttributeHandler<T> attributeHandler, ElementHandler<T> elementHandler, ValueHandler<T> valueHandler)
         throws IOException, XmlPullParserException {
+        if (definition instanceof NamespaceAware) {
+            final Map<String, String> namespaces = new LinkedHashMap<>();
+            for (int i = 0; i < parser.getNamespaceCount(parser.getDepth()); i++) {
+                final String prefix = parser.getNamespacePrefix(i);
+                if (prefix != null) {
+                    namespaces.put(prefix, parser.getNamespaceUri(i));
+                }
+            }
+            ((NamespaceAware) definition).setNamespaces(namespaces);
+        }
         for (int i = 0; i < parser.getAttributeCount(); i++) {
             String name = parser.getAttributeName(i);
             String ns = parser.getAttributeNamespace(i);
diff --git a/core/camel-xml-io/src/test/java/org/apache/camel/xml/in/ModelParserTest.java b/core/camel-xml-io/src/test/java/org/apache/camel/xml/in/ModelParserTest.java
index 0ddd919..5094e51 100644
--- a/core/camel-xml-io/src/test/java/org/apache/camel/xml/in/ModelParserTest.java
+++ b/core/camel-xml-io/src/test/java/org/apache/camel/xml/in/ModelParserTest.java
@@ -22,12 +22,16 @@ import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Map;
 import java.util.stream.Collectors;
 
 import org.apache.camel.model.RoutesDefinition;
+import org.apache.camel.model.SetBodyDefinition;
+import org.apache.camel.model.language.XPathExpression;
 import org.apache.camel.model.rest.RestsDefinition;
 import org.junit.jupiter.api.Test;
 
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 
 public class ModelParserTest {
@@ -59,6 +63,28 @@ public class ModelParserTest {
         assertNotNull(routes);
     }
 
+    @Test
+    public void namespaces() throws Exception {
+        final String routesXml = "<routes xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
+                + "       xmlns:foo=\"http://camel.apache.org/foo\">\n"
+                + "   <route id=\"xpath-route\">\n"
+                + "      <from uri=\"direct:test\"/>\n"
+                + "      <setBody>\n"
+                + "         <xpath resultType=\"java.lang.String\">\n"
+                + "            /foo:orders/order[1]/country/text()\n"
+                + "         </xpath>\n"
+                + "      </setBody>\n"
+                + "   </route>\n"
+                + "</routes>";
+        final RoutesDefinition routes = new ModelParser(new StringReader(routesXml)).parseRoutesDefinition();
+        final RouteDefinition route0 = routes.getRoutes().get(0);
+        final SetBodyDefinition setBody = (SetBodyDefinition) route0.getOutputs().get(0);
+        final XPathExpression xPath = (XPathExpression) setBody.getExpression();
+        final Map<String, String> namespaces = xPath.getNamespaces();
+        assertNotNull(namespaces);
+        assertEquals("http://camel.apache.org/foo", namespaces.get("foo"));
+    }
+
     private Path getResourceFolder() {
         String url = getClass().getClassLoader().getResource("barInterceptorRoute.xml").toString();
         if (url.startsWith("file:")) {