[gxml] GomElement: Fixed lookup_prefix and errors in ns



commit e0b836fade9dd8dde4b2ba6b7b02ec1a251b7bff
Author: Daniel Espinosa <esodan gmail com>
Date:   Tue Nov 1 14:49:31 2016 -0600

    GomElement: Fixed lookup_prefix and errors in ns

 gxml/GomElement.vala      |   28 +++++++++++++++++++++-------
 test/GomDocumentTest.vala |   40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+), 7 deletions(-)
---
diff --git a/gxml/GomElement.vala b/gxml/GomElement.vala
index 1da0c5f..053042c 100644
--- a/gxml/GomElement.vala
+++ b/gxml/GomElement.vala
@@ -35,9 +35,14 @@ public class GXml.GomElement : GomNode,
   protected Attributes _attributes;
   // DomNode overrides
   public new string? lookup_prefix (string? nspace) {
-    if (namespace_uri == nspace && this.prefix != null)
-      return this.prefix;
-    foreach (DomNode a in attributes.values) {
+    if (_namespace_uri == nspace && _prefix != null)
+      return _prefix;
+    foreach (string k in _attributes.keys) {
+      var a = _attributes.get_named_item (k);
+      if (a == null) {
+        GLib.warning (("Attribute: %s not found").printf (k));
+        continue;
+      }
       if ((a as DomAttr).prefix == null) continue;
       if ((a as DomAttr).prefix.down () == "xmlns" && a.node_value == nspace)
         return (a as DomAttr).local_name;
@@ -272,18 +277,25 @@ public class GXml.GomElement : GomNode,
           || (node as DomAttr).prefix == null
           && node.node_name != "xmlns")
         throw new DomError.NAMESPACE_ERROR (_("Namespaced attributes should provide a valid prefix and 
namespace"));
-      string nsp = null;
+      string nsp = "";
+    GLib.message ("Searching for duplicated ns..."+node.node_value);
       if ((node as DomAttr).prefix == "xmlns")
         nsp = _element.lookup_prefix (node.node_value);
+    GLib.message ("Searching for duplicated ns...");
       if ((node as DomAttr).prefix != "xmlns")
         nsp = _element.lookup_prefix ((node as DomAttr).namespace_uri);
+    GLib.message ("Searching for duplicated ns...");
       if (nsp != null)
-        GLib.message ("Found PREFIX: "+nsp+" Node prefix: "+(node as DomAttr).prefix+"Node Value: 
"+node.node_value);
+        GLib.message ("Found PREFIX: "+nsp+" Node prefix: "+(node as DomAttr).prefix+
+                      " Node name: "+node.node_name+"Node Value: "+node.node_value);
+    GLib.message ("Searching for duplicated ns...");
       if ((node as DomAttr).prefix == "xmlns" && node.node_name != nsp
-          || ((node as DomAttr).prefix != "xmlns")
-              && (node as DomAttr).prefix != nsp) {
+          || (((node as DomAttr).prefix != "xmlns")
+              && (node as DomAttr).prefix != nsp)) {
         string snsp = "";
         if (nsp != null) snsp = ": "+nsp;
+    GLib.message ("Duplicated ns");
+    GLib.message ("Found Duplicated ns"+snsp);
         throw new DomError.NAMESPACE_ERROR (_("Attribute's prefix and namespace URI conflics with already 
defined namespace%s").printf (snsp));
       }
       string p = "";
@@ -349,6 +361,8 @@ public class GXml.GomElement : GomNode,
     if (p == "xml" && namespace_uri != "http://www.w3.org/2000/xmlns/";)
        throw new DomError.NAMESPACE_ERROR (_("Invalid namespace. If prefix is xml name space uri shoud be 
http://www.w3.org/2000/xmlns/";));
     if (p == "xmlns" && namespace_uri != "http://www.w3.org/2000/xmlns/";)
+       throw new DomError.NAMESPACE_ERROR (_("Invalid namespace. If attribute's prefix is xmlns name space 
uri shoud be http://www.w3.org/2000/xmlns/";));
+    if (p == "" && n == "xmlns" && namespace_uri != "http://www.w3.org/2000/xmlns/";)
        throw new DomError.NAMESPACE_ERROR (_("Invalid namespace. If attribute's name is xmlns name space uri 
shoud be http://www.w3.org/2000/xmlns/";));
     if (p == "" && n != "xmlns")
       throw new DomError.NAMESPACE_ERROR (_("Invalid attribute name. No prefixed attributes should use xmlns 
name"));
diff --git a/test/GomDocumentTest.vala b/test/GomDocumentTest.vala
index 2875ca5..f08e0e3 100644
--- a/test/GomDocumentTest.vala
+++ b/test/GomDocumentTest.vala
@@ -325,6 +325,46 @@ class GomDocumentTest : GXmlTest {
                                assert_not_reached ();
                        }
                });
+               Test.add_func ("/gxml/gom-document/namespace/invalid", () => {
+                       DomDocument doc = null;
+                       try  { doc = new GomDocument.from_string 
("<document_element><child/></document_element>"); }
+                       catch (GLib.Error e) { GLib.message ("ERROR: "+e.message); }
+                       try {
+                               doc.document_element.set_attribute_ns ("http://local";,
+                                                                                                             
                                                                          
"xmlns:","http://www.gnome.org/GXml";);
+                               assert_not_reached ();
+                       } catch {}
+                       assert (doc.document_element != null);
+                       assert (doc.document_element.namespace_uri == null);
+                       assert (doc.document_element.prefix == null);
+                       assert (doc.document_element.child_nodes != null);
+                       try {
+                               doc.document_element.set_attribute_ns ("http://www.gnome.org/GXml";,
+                                                                                                             
                                                                          
"xmlns","http://www.gnome.org/GXml";);
+                               assert_not_reached ();
+                       } catch {}
+                       assert (doc.document_element.child_nodes.size == 1);
+                       var c = doc.document_element.child_nodes[0] as DomElement;
+                       assert (c is DomElement);
+                       c.set_attribute_ns ("http://www.w3.org/2000/xmlns/","xmlns:gxml2";, 
"http://www.gnome.org/GXml2";);
+                       assert (c.prefix == "gxml2");
+                       assert (c.namespace_uri == "http://www.gnome.org/GXml2";);
+                       try {
+                               GLib.message ("Setting duplicated ns");
+                               c.set_attribute_ns ("http://www.w3.org/2000/xmlns/","xmlns:gxml2";, 
"http://www.gnome.org/GXml3";);
+                               assert_not_reached ();
+                       } catch {}
+                               assert (c.prefix == "gxml2");
+                               assert (c.namespace_uri == "http://www.gnome.org/GXml2";);
+                       try {
+                               c.set_attribute_ns ("http://www.gnome.org/GXml2","gxml3:prop","val";);
+                               assert_not_reached ();
+                       } catch {}
+                               var p = (c as DomElement).get_attribute_ns ("http://www.gnome.org/GXml2";, 
"prop");
+                               assert (p == null);
+                               assert (c.prefix == "gxml2");
+                               assert (c.namespace_uri == "http://www.gnome.org/GXml2";);
+               });
                Test.add_func ("/gxml/gom-document/parent", () => {
                        var doc = new GomDocument ();
                        assert (doc.parent_node == null);


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