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 2018/09/08 15:38:28 UTC

[camel] 01/04: CAMEL-12114: Have XmlLineNumberParser respecting namespace uri also for anonymous namespaces (without prefix) from its parent+ tag

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

commit bf416b8de7ab37b5a6bcac7ed0f6695894a48d2f
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Sep 8 17:34:27 2018 +0200

    CAMEL-12114: Have XmlLineNumberParser respecting namespace uri also for anonymous namespaces (without prefix) from its parent+ tag
---
 .../camel/parser/helper/XmlLineNumberParser.java   | 29 ++++++++++++++--
 .../camel/parser/java/XmlLineNumberParserTest.java | 39 ++++++++++++++++++++++
 2 files changed, 66 insertions(+), 2 deletions(-)

diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/XmlLineNumberParser.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/XmlLineNumberParser.java
index 9f6e63a..49088b8 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/XmlLineNumberParser.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/XmlLineNumberParser.java
@@ -19,8 +19,15 @@ package org.apache.camel.parser.helper;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.StringReader;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Deque;
 import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Map;
+import java.util.Queue;
 import java.util.Stack;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
@@ -104,6 +111,7 @@ public final class XmlLineNumberParser {
             private Locator locator;
             private boolean found;
             private final Map<String, String> localNs = new HashMap<>();
+            private final Map<String, String> anonymousNs = new LinkedHashMap<>();
 
             @Override
             public void setDocumentLocator(final Locator locator) {
@@ -148,6 +156,19 @@ public final class XmlLineNumberParser {
                                     ns = localNs.get(prefix);
                                 }
                             }
+                        } else {
+                            // maybe there is an anonymous namespace (xmlns)
+                            if (attributes != null) {
+                                ns = attributes.getValue("xmlns");
+                                if (ns != null) {
+                                    anonymousNs.put(qName, ns);
+                                } else if (!anonymousNs.isEmpty()) {
+                                    // grab latest anonymous namespace to use as the namespace as
+                                    // this child tag should use the parents+ namespace
+                                    List<String> values = new ArrayList<>(anonymousNs.values());
+                                    ns = values.get(values.size() - 1);
+                                }
+                            }
                         }
                         if (ns != null) {
                             el = doc.createElementNS(ns, qName);
@@ -156,8 +177,10 @@ public final class XmlLineNumberParser {
                         }
                     }
 
-                    for (int i = 0; i < attributes.getLength(); i++) {
-                        el.setAttribute(attributes.getQName(i), attributes.getValue(i));
+                    if (attributes != null) {
+                        for (int i = 0; i < attributes.getLength(); i++) {
+                            el.setAttribute(attributes.getQName(i), attributes.getValue(i));
+                        }
                     }
 
                     String ln = String.valueOf(this.locator.getLineNumber());
@@ -191,6 +214,8 @@ public final class XmlLineNumberParser {
                     closedEl.setUserData(LINE_NUMBER_END, ln, null);
                     closedEl.setUserData(COLUMN_NUMBER_END, cn, null);
                 }
+
+                anonymousNs.remove(qName);
             }
 
             @Override
diff --git a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/XmlLineNumberParserTest.java b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/XmlLineNumberParserTest.java
new file mode 100644
index 0000000..bec0696
--- /dev/null
+++ b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/XmlLineNumberParserTest.java
@@ -0,0 +1,39 @@
+/**
+ * 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.parser.java;
+
+import java.io.FileInputStream;
+import java.io.InputStream;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.NodeList;
+
+import org.apache.camel.parser.helper.XmlLineNumberParser;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class XmlLineNumberParserTest {
+
+    @Test
+    public void testRespectNamespace() throws Exception {
+        InputStream is = new FileInputStream("src/test/resources/org/apache/camel/parser/xml/mycamel.xml");
+        Document parsedXml = XmlLineNumberParser.parseXml(is);
+        NodeList fromCamelWithNamespace = parsedXml.getElementsByTagNameNS("http://camel.apache.org/schema/spring", "from");
+        assertEquals(1, fromCamelWithNamespace.getLength());
+    }
+}