[gxml] GomElement: Fixes on lookup namespaces



commit 6166d8fb3398fd48de701e14277181c28344b0b6
Author: Daniel Espinosa <esodan gmail com>
Date:   Tue Nov 1 16:48:16 2016 -0600

    GomElement: Fixes on lookup namespaces

 gxml/GomElement.vala      |   32 ++++++++++++++++----------------
 gxml/GomNode.vala         |   14 +++++++++++---
 test/GomDocumentTest.vala |   12 +++++++++---
 3 files changed, 36 insertions(+), 22 deletions(-)
---
diff --git a/gxml/GomElement.vala b/gxml/GomElement.vala
index 053042c..ff71140 100644
--- a/gxml/GomElement.vala
+++ b/gxml/GomElement.vala
@@ -40,7 +40,7 @@ public class GXml.GomElement : GomNode,
     foreach (string k in _attributes.keys) {
       var a = _attributes.get_named_item (k);
       if (a == null) {
-        GLib.warning (("Attribute: %s not found").printf (k));
+        GLib.warning (_("Attribute: %s not found").printf (k));
         continue;
       }
       if ((a as DomAttr).prefix == null) continue;
@@ -51,26 +51,25 @@ public class GXml.GomElement : GomNode,
     return parent_node.lookup_prefix (nspace);
   }
   public new string? lookup_namespace_uri (string? prefix) {
-    if (namespace_uri != null && this.prefix == prefix)
+    if (_namespace_uri != null && _prefix == prefix)
       return namespace_uri;
     string s = "";
     if (prefix != null) s = prefix;
-    foreach (DomNode a in attributes.values) {
+    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).namespace_uri == "http://www.w3.org/2000/xmlns/";)
-        if ((a as DomAttr).prefix != null)
-          if ((a as DomAttr).prefix.down () == "xmlns"
-              && (a as DomAttr).local_name == s)
-            if (a.node_value == "")
-              return null;
-            else
+        if ((a as DomAttr).prefix == "xmlns"
+              && a.node_name == s)
               return a.node_value;
-      if ((a as DomAttr).prefix == null
+      if (((a as DomAttr).prefix == null || (a as DomAttr).prefix == "")
           && s == ""
-          && (a as DomAttr).local_name == "xmlns"
-          && (a as DomAttr).namespace_uri == "http://www.w3.org/2000/xmlns/";)
-          if (a.node_value == "")
-            return null;
-          else
+          && a.node_name == "xmlns"
+          && (a as DomAttr).namespace_uri == "http://www.w3.org/2000/xmlns/";
+          && (prefix == null || prefix == ""))
             return a.node_value;
     }
     if (parent_node == null) return null;
@@ -299,7 +298,8 @@ public class GXml.GomElement : GomNode,
         throw new DomError.NAMESPACE_ERROR (_("Attribute's prefix and namespace URI conflics with already 
defined namespace%s").printf (snsp));
       }
       string p = "";
-      if ((node as DomAttr).prefix != null) p = (node as DomAttr).prefix + ":";
+      if ((node as DomAttr).prefix != null
+          && (node as DomAttr).prefix != "") p = (node as DomAttr).prefix + ":";
       set (p+(node as DomAttr).local_name,
           node.node_value);
       var attr = new GomAttr.namespace (_element,
diff --git a/gxml/GomNode.vala b/gxml/GomNode.vala
index 206b511..c253404 100644
--- a/gxml/GomNode.vala
+++ b/gxml/GomNode.vala
@@ -31,6 +31,7 @@ public class GXml.GomNode : Object,
   protected string _prefix;
   protected string _base_uri;
   protected string _node_value;
+  protected GXml.DomNode _parent;
   protected DomNode.NodeType _node_type;
   protected GomNodeList _child_nodes;
   public DomNode.NodeType node_type { get { return _node_type; } }
@@ -47,7 +48,6 @@ public class GXml.GomNode : Object,
   protected GXml.DomDocument _document;
   public DomDocument? owner_document { get { return (GXml.DomDocument?) _document; } }
 
-  protected GXml.DomNode _parent;
   public DomNode? parent_node { owned get { return _parent as DomNode?; } }
   public DomElement? parent_element {
     owned get {
@@ -123,6 +123,7 @@ public class GXml.GomNode : Object,
     _base_uri = null;
     _node_value = null;
     _child_nodes = new GomNodeList ();
+    _parent = null;
   }
 
   public bool has_child_nodes () { return (_child_nodes.size > 0); }
@@ -177,7 +178,7 @@ public class GXml.GomNode : Object,
     if (this is GXml.DomDocumentType ||
         this is GXml.DomDocumentFragment) return null;
     if (this is DomElement) {
-      return (this as DomElement).lookup_prefix (nspace);
+      return (this as GomElement).lookup_prefix (nspace);
     }
     if (this is DomAttr) {
       if (this.parent_node == null) return  null;
@@ -189,7 +190,7 @@ public class GXml.GomNode : Object,
     if (this is GXml.DomDocumentType ||
         this is GXml.DomDocumentFragment) return null;
     if (this is DomElement) {
-        return (this as DomElement).lookup_namespace_uri (prefix);
+        return (this as GomElement).lookup_namespace_uri (prefix);
     }
     if (this is DomAttr) {
       if (this.parent_node == null) return  null;
@@ -203,6 +204,10 @@ public class GXml.GomNode : Object,
     return false;
   }
 
+  internal void set_parent (DomNode node) {
+    _parent = node;
+  }
+
   public DomNode insert_before (DomNode node, DomNode? child) throws GLib.Error {
     if (!(node is GXml.GomNode))
       throw new DomError.INVALID_NODE_TYPE_ERROR (_("Invalid attempt to add invalid node type"));
@@ -232,6 +237,9 @@ public class GXml.GomNode : Object,
     return node;
   }
   public DomNode append_child (DomNode node) throws GLib.Error {
+    if (!(node is GomNode))
+      throw new DomError.HIERARCHY_REQUEST_ERROR (_("Node type is invalid. Can't append as child"));
+    (node as GomNode).set_parent (this);
     return insert_before (node, null);
   }
   public DomNode replace_child (DomNode node, DomNode child) throws GLib.Error {
diff --git a/test/GomDocumentTest.vala b/test/GomDocumentTest.vala
index f08e0e3..613cc3b 100644
--- a/test/GomDocumentTest.vala
+++ b/test/GomDocumentTest.vala
@@ -320,6 +320,14 @@ class GomDocumentTest : GXmlTest {
                                var p = (c as DomElement).get_attribute_ns ("http://www.gnome.org/GXml2";, 
"prop");
                                assert (p != null);
                                assert (p == "val");
+                               assert (doc.document_element.lookup_namespace_uri (null) != null);
+                               GLib.message ("NS default: "+doc.document_element.lookup_namespace_uri 
(null));
+                               assert (c.prefix == "gxml2");
+                               assert (c.namespace_uri == "http://www.gnome.org/GXml2";);
+                               assert (c.lookup_namespace_uri (null) == "http://www.gnome.org/GXml";);
+                               assert (c.lookup_namespace_uri ("gxml2") == "http://www.gnome.org/GXml2";);
+                               assert (c.lookup_prefix ("http://www.gnome.org/GXml3";) == null);
+                               assert (c.lookup_prefix ("http://www.gnome.org/GXml2";) == "gxml2");
                        } catch (GLib.Error e) {
                                GLib.message ("ERROR: "+ e.message);
                                assert_not_reached ();
@@ -350,7 +358,7 @@ class GomDocumentTest : GXmlTest {
                        assert (c.prefix == "gxml2");
                        assert (c.namespace_uri == "http://www.gnome.org/GXml2";);
                        try {
-                               GLib.message ("Setting duplicated ns");
+                               Test.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 {}
@@ -362,8 +370,6 @@ class GomDocumentTest : GXmlTest {
                        } 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 ();


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