[gxml] Relaxing duplicated NS checks



commit ba2db5e51cc3bc6dfa9b7fb7b8f600cd21fec1aa
Author: Daniel Espinosa <esodan gmail com>
Date:   Wed Oct 18 02:09:04 2017 -0500

    Relaxing duplicated NS checks
    
    A namespace is different if prefix and URI combination is different,
    from other already defined, so we should allow them

 gxml/GomElement.vala     |   36 ++----------------------------------
 gxml/GomNode.vala        |   17 -----------------
 test/GomElementTest.vala |   25 +++++++++++++++++++++++++
 3 files changed, 27 insertions(+), 51 deletions(-)
---
diff --git a/gxml/GomElement.vala b/gxml/GomElement.vala
index f1c6675..96f6f28 100644
--- a/gxml/GomElement.vala
+++ b/gxml/GomElement.vala
@@ -494,52 +494,24 @@ public class GXml.GomElement : GomNode,
       }
       if ((node as DomAttr).namespace_uri == "http://www.w3.org/2000/xmlns/";
           || (node as DomAttr).namespace_uri == "http://www.w3.org/2000/xmlns";) {
-#if DEBUG
-        GLib.message ("Searching for duplicated ns..."+node.node_value
-                  +" NS: "+(node as DomAttr).namespace_uri);
-#endif
         if ((node as DomAttr).local_name == "xmlns") {
           string ns_uri = _element.lookup_namespace_uri (null);
           if (ns_uri != null && ns_uri != node.node_value) {
-#if DEBUG
-            GLib.message ("Error: NSURI: "+ns_uri+" NSURI Attr:"+node.node_value);
-#endif
-            throw new DomError.NAMESPACE_ERROR
-                      (_("Redefinition of default namespace for %s")
-                        .printf (node.node_value));
+            message (_("Duplicated default namespace detected with URI: %s").printf (ns_uri));
           }
         }
         if ((node as DomAttr).prefix == "xmlns") {
-#if DEBUG
-          GLib.message ("Attr Prefix = "+(node as DomAttr).prefix
-                      + "Attr Name: "+(node as DomAttr).local_name);
-#endif
           string nsprefix = _element.lookup_prefix (node.node_value);
           string nsuri = _element.lookup_namespace_uri ((node as DomAttr).local_name);
-
-#if DEBUG
-          if (nsprefix != null || nsuri != null)
-            GLib.message ("Ns Prefix = "+nsprefix
-                      + "Ns URI: "+nsuri);
-#endif
           if ((nsprefix != null || nsuri != null)
               && (nsprefix != (node as DomAttr).local_name
                   || nsuri != node.node_value)) {
-#if DEBUG
-            GLib.message ("Prefix: "+nsprefix+" Prefix Attr:"+(node as DomAttr).local_name);
-#endif
-            throw new DomError.NAMESPACE_ERROR
-                      (_("Redefinition of namespace's prefix for %s")
-                        .printf (node.node_value));
+            message (_("Duplicated namespace detected for: %s:%s").printf ((node as DomAttr).local_name, 
node.node_value));
           }
         }
       }
       if ((node as DomAttr).namespace_uri != "http://www.w3.org/2000/xmlns/";
           && (node as DomAttr).namespace_uri != "http://www.w3.org/2000/xmlns";){
-#if DEBUG
-        GLib.message ("No namespace attribute: "+(node as DomAttr).namespace_uri
-                    + ":"+(node as DomAttr).prefix);
-#endif
         string nsn = _element.lookup_namespace_uri ((node as DomAttr).prefix);
         string nspn = _element.lookup_prefix (nsn);
         if (nspn != (node as DomAttr).prefix
@@ -558,10 +530,6 @@ public class GXml.GomElement : GomNode,
       if ((node as DomAttr).prefix != null
           && (node as DomAttr).prefix != "")
         p = (node as DomAttr).prefix + ":";
-#if DEBUG
-      GLib.message ("Attribute to set: "+p+(node as DomAttr).local_name
-                    +"="+node.node_value);
-#endif
       set (p+(node as DomAttr).local_name,
           node.node_value);
 
diff --git a/gxml/GomNode.vala b/gxml/GomNode.vala
index ed8d83d..bca3177 100644
--- a/gxml/GomNode.vala
+++ b/gxml/GomNode.vala
@@ -247,23 +247,6 @@ public class GXml.GomNode : Object,
    * Sets node's parent and checks for namespace conflics.
    */
   internal void set_parent (DomNode node) throws GLib.Error {
-    if (this is DomElement) {
-      var e = (this as DomElement);
-      if (e.namespace_uri != null || e.prefix != null) {
-        string nsprefix = node.lookup_prefix (e.namespace_uri);
-        string nsuri = node.lookup_namespace_uri (e.prefix);
-        if (nsprefix != null && nsprefix != e.prefix) {
-          throw new DomError.NAMESPACE_ERROR
-            (_("Trying to add a namespaced element to a parent with invalid prefix for namespace %s")
-              .printf (e.namespace_uri));
-        }
-        if (nsuri != null && nsuri != e.namespace_uri) {
-          throw new DomError.NAMESPACE_ERROR
-            (_("Trying to add a namespaced element to a parent with invalid URI for prefix %s")
-              .printf (e.prefix));
-        }
-      }
-    }
     _document = node.owner_document;
     _parent = node;
   }
diff --git a/test/GomElementTest.vala b/test/GomElementTest.vala
index cf2280f..d2999af 100644
--- a/test/GomElementTest.vala
+++ b/test/GomElementTest.vala
@@ -97,6 +97,31 @@ class GomElementTest : GXmlTest  {
                                GLib.message (e.message);
                                assert_not_reached ();
                        }
+               });Test.add_func ("/gxml/gom-element/read/namespace/redefinition", () => {
+                       DomDocument doc = null;
+                       try {
+                               doc = new GomDocument.from_string ("<magic:Potion 
xmlns:magic=\"http://hogwarts.co.uk/magic\"; 
xmlns:products=\"http://hogwarts.co.uk/magic\";><magic:Arc/><products:Diamond/></magic:Potion>");
+                               var r = doc.document_element;
+                               assert (r != null);
+                               assert (r.local_name == "Potion");
+                               assert (r.get_attribute_ns ("http://www.w3.org/2000/xmlns/";, "magic") == 
"http://hogwarts.co.uk/magic";);
+                               assert (r.get_attribute_ns ("http://www.w3.org/2000/xmlns/";, "products") == 
"http://hogwarts.co.uk/magic";);
+                               assert (r.child_nodes.length == 2);
+                               var n1 = r.child_nodes.item (0);
+                               assert (n1 != null);
+                               assert (n1 is DomElement);
+                               assert ((n1 as DomElement).local_name == "Arc");
+                               assert ((n1 as DomElement).prefix == "magic");
+                               assert ((n1 as DomElement).namespace_uri == "http://hogwarts.co.uk/magic";);
+                               var n2 = r.child_nodes.item (1);
+                               assert (n2 != null);
+                               assert (n2 is DomElement);
+                               assert ((n2 as DomElement).local_name == "Diamond");
+                               assert ((n2 as DomElement).prefix == "products");
+                               assert ((n2 as DomElement).namespace_uri == "http://hogwarts.co.uk/magic";);
+                       } catch (GLib.Error e) {
+                               GLib.warning (e.message);
+                       }
                });
                Test.add_func ("/gxml/gom-element/attributes", () => {
                        try {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]