[gxml] Fixed bug on GXml.Namespaces implementation in old libxml



commit 6f76aad61bd05bc2314766373d92f2e787774537
Author: Daniel Espinosa <esodan gmail com>
Date:   Thu Apr 30 14:19:00 2015 -0500

    Fixed bug on GXml.Namespaces implementation in old libxml
    
    * NamespaceAttrNodeList now is fully ported to Gee.List<Namespace>
    * NamespaceAttrNodeList uses xmlGetNsList to find all namespaces the
      document
    * Now set_namespace work properly
    * Deserialize with namespaces is working
    * Added Xmlx.doc_get_ns_list wrapping xmlGetNsList

 gxml/Serializable.vala                 |    1 +
 gxml/SerializableGeeHashMap.vala       |    2 +-
 gxml/SerializableGeeTreeMap.vala       |    2 +-
 gxml/SerializableObjectModel.vala      |   14 +++---
 gxml/libxml-NamespaceAttrNodeList.vala |   69 +++++++++++++------------------
 gxml/xlibxml.c                         |    5 ++
 gxml/xlibxml.h                         |    1 +
 test/SerializableObjectModelTest.vala  |   20 +++++++--
 vapi/xlibxml-1.0.vapi                  |    2 +
 9 files changed, 62 insertions(+), 54 deletions(-)
---
diff --git a/gxml/Serializable.vala b/gxml/Serializable.vala
index 2c3210e..aee2983 100644
--- a/gxml/Serializable.vala
+++ b/gxml/Serializable.vala
@@ -111,6 +111,7 @@ namespace GXml {
      * This property is ignored on serialisation.
      */
     public abstract bool set_namespace (GXml.Node node);
+    //public abstract Namespace @namespace { get; set; default = null; }
       /**
       * Used to check { link GXml.Element}'s contents must be deseralized.
       * 
diff --git a/gxml/SerializableGeeHashMap.vala b/gxml/SerializableGeeHashMap.vala
index b681197..b0fe3b2 100644
--- a/gxml/SerializableGeeHashMap.vala
+++ b/gxml/SerializableGeeHashMap.vala
@@ -125,7 +125,7 @@ public class GXml.SerializableHashMap<K,V> : Gee.HashMap<K,V>, Serializable, Ser
       foreach (GXml.Node n in node.childs) {
         if (n is Element) {
 #if DEBUG
-          stdout.printf (@"Node $(node.node_name) for type '$(get_type ().name ())'\n");
+          stdout.printf (@"Node $(node.name) for type '$(get_type ().name ())'\n");
 #endif
           var obj = Object.new (value_type);
           if (n.name == ((Serializable) obj).node_name ()) {
diff --git a/gxml/SerializableGeeTreeMap.vala b/gxml/SerializableGeeTreeMap.vala
index e987315..354dfae 100644
--- a/gxml/SerializableGeeTreeMap.vala
+++ b/gxml/SerializableGeeTreeMap.vala
@@ -128,7 +128,7 @@ public class GXml.SerializableTreeMap<K,V> : Gee.TreeMap<K,V>, Serializable, Ser
       foreach (GXml.Node n in node.childs) {
         if (n is Element) {
 #if DEBUG
-          stdout.printf (@"Node $(node.node_name) for type '$(get_type ().name ())'\n");
+          stdout.printf (@"Node $(node.name) for type '$(get_type ().name ())'\n");
 #endif
           var obj = Object.new (value_type);
           if (n.name == ((Serializable) obj).node_name ()) {
diff --git a/gxml/SerializableObjectModel.vala b/gxml/SerializableObjectModel.vala
index 285161f..c10d480 100644
--- a/gxml/SerializableObjectModel.vala
+++ b/gxml/SerializableObjectModel.vala
@@ -242,19 +242,19 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
       return null;
     }
 #if DEBUG
-    if (element.node_name.down () != node_name ().down ()) {
-      GLib.warning (@"Actual node's name is '$(element.node_name.down ())' expected '$(node_name ().down 
())'");
+    if (element.name.down () != node_name ().down ()) {
+      GLib.warning (@"Actual node's name is '$(element.name.down ())' expected '$(node_name ().down ())'");
     }
-    stdout.printf (@"Deserialize Node: $(element.node_name)\n");
+    stdout.printf (@"Deserialize Node: $(element.name)\n");
     stdout.printf (@"Node is: $(element)\n\n");
-    stdout.printf (@"Attributes in Node: $(element.node_name)\n");
+    stdout.printf (@"Attributes in Node: $(element.name)\n");
 #endif
     foreach (GXml.Node attr in ((xElement)element).attributes.get_values ())
     {
       deserialize_property ((xNode)attr);
     }
 #if DEBUG
-    stdout.printf (@"Elements Nodes in Node: $(element.node_name)\n");
+    stdout.printf (@"Elements Nodes in Node: $(element.name)\n");
 #endif
     if (((xElement)element).has_child_nodes ())
     {
@@ -287,7 +287,7 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
           if (serialize_use_xml_node_value ()) {
             serialized_xml_node_value = n.value;
 #if DEBUG
-            stdout.printf (@"$(get_type ().name ()): NODE '$(element.node_name)' CONTENT 
'$(n.node_value)'\n");
+            stdout.printf (@"$(get_type ().name ()): NODE '$(element.name)' CONTENT '$(n.value)'\n");
 #endif
           } else {
             if (get_enable_unknown_serializable_property ()) {
@@ -298,7 +298,7 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
         }
         if (n is xElement  && !cnodes.has_key (n.name)) {
 #if DEBUG
-            stdout.printf (@"$(get_type ().name ()): DESERIALIZING ELEMENT '$(n.node_name)'\n");
+            stdout.printf (@"$(get_type ().name ()): DESERIALIZING ELEMENT '$(n.name)'\n");
 #endif
           deserialize_property ((xNode)n);
         }
diff --git a/gxml/libxml-NamespaceAttrNodeList.vala b/gxml/libxml-NamespaceAttrNodeList.vala
index e394b1b..307b379 100644
--- a/gxml/libxml-NamespaceAttrNodeList.vala
+++ b/gxml/libxml-NamespaceAttrNodeList.vala
@@ -21,14 +21,17 @@
  */
 using Gee;
 using Xml;
+using Xmlx;
 // FIXME: Port this class as an implementation of Gee interfaces
 internal abstract class GXml.AbstractNamespaceAttrNodeList : Object,
   Gee.Traversable<Namespace>, Gee.Iterable<Namespace>, Gee.Collection<Namespace>
 {
   protected unowned BackedNode node;
+  protected Xml.Ns*[] name_spaces;
 
   internal AbstractNamespaceAttrNodeList (BackedNode root) {
     this.node = root;
+    name_spaces = Xmlx.doc_get_ns_list (node.node->doc, node.node);
   }
   public bool add (Namespace item) { return false; }
   public void clear ()
@@ -50,17 +53,7 @@ internal abstract class GXml.AbstractNamespaceAttrNodeList : Object,
   {
     owned get { return new NamespaceAttrNodeList (this.node); }
   }
-  public int size {
-    get {
-      int i = 0;
-      Xml.Ns* cur = node.node->ns;
-      while (cur != null) {
-        i++;
-        cur = cur->next;
-      }
-      return i;
-    }
-  }
+  public int size { get { return name_spaces.length; } }
   public Gee.Iterator<Namespace> iterator () { return new Iterator (this); }
 
   public new bool @foreach (Gee.ForallFunc<Namespace> f) {
@@ -83,37 +76,34 @@ internal abstract class GXml.AbstractNamespaceAttrNodeList : Object,
     }
     public bool has_next ()
     {
-      if (cur == null) {
-        if (nl.node.node == null) return false;
-        if (nl.node.node->ns == null) return false;
-        return true;
+      if (nl.name_spaces.length > 0) {
+        
+      if (i < nl.name_spaces.length) return true;
       }
-      if (cur->next == null) return false;
-      return true;
+      return false;
     }
                public bool next ()
     {
-      if (cur == null) {
-        if (nl.node.node == null) return false;
-        if (nl.node.node->ns == null) return false;
-        cur = nl.node.node->ns;
-        i++;
+      i++;
+      if (i < nl.name_spaces.length){
+        cur = nl.name_spaces [i];
+        return true;
       }
-      if (cur->next == null) return false;
-      cur = cur->next;
-      return true;
+      return false;
     }
     public void remove () { return; }
     public bool read_only { get { return true; } }
     public bool valid
     {
       get {
+        if (i < 0 || i >= nl.name_spaces.length)
         if (cur == null) return false;
         return true;
       }
     }
     public new bool @foreach (Gee.ForallFunc<Namespace> f)
     {
+      GLib.message ("Iterating over all Namespaces");
       while (has_next ()) {
         next ();
         if (!f(@get())) return false;
@@ -128,19 +118,18 @@ internal class GXml.NamespaceAttrNodeList : AbstractNamespaceAttrNodeList,
 {
   public NamespaceAttrNodeList (BackedNode root) {
     base (root);
+#if DEBUG
+    GLib.message (@"Initialized Namespace List...)");
+    GLib.message (@"Checking Namespaces: Xml.Node null? $((node.node == null).to_string ()) - Ns is null? 
$((node.node->ns == null).to_string ()) - NsList: $((name_spaces == null).to_string ())");
+    GLib.message (@"Number of Namespaces found: $(name_spaces.length)");
+#endif
   }
   public new Namespace @get (int index)
   {
     Xml.Ns* cur = null;
-    if (this.node.node != null) {
-      if (node.node->ns != null) {
-        cur = node.node->ns;
-        int i = 0;
-        while (cur != null) {
-          if (index == i) break;
-          cur = cur->next;
-          i++;
-        }
+    if (name_spaces != null) {
+      if (index >= 0 && index < name_spaces.length) {
+        cur = name_spaces[index];
       }
     }
     return (Namespace) new NamespaceAttr (cur, node.owner_document);
@@ -149,12 +138,12 @@ internal class GXml.NamespaceAttrNodeList : AbstractNamespaceAttrNodeList,
   {
     int i = -1;
     Xml.Ns* cur = null;
-    if (node.node != null) {
-      cur = node.node->ns;
-      while (cur != null) {
-        i++;
-        if (item.uri == cur->href && item.prefix == cur->prefix) break;
-        cur = cur->next;
+    if (name_spaces != null) {
+      for (int j = 0; j < name_spaces.length; j++) {
+        if (item.uri == cur->href && item.prefix == cur->prefix) {
+          i = j;
+          break;
+        }
       }
     }
     return i;
diff --git a/gxml/xlibxml.c b/gxml/xlibxml.c
index 33f6f13..c9371a4 100644
--- a/gxml/xlibxml.c
+++ b/gxml/xlibxml.c
@@ -40,3 +40,8 @@ xmlErrorPtr gxml_get_last_error ()
 {
   return xmlGetLastError ();
 }
+
+xmlNsPtr* gxml_doc_get_ns_list (xmlDoc* doc, xmlNode* node)
+{
+  return xmlGetNsList (doc, node);
+}
diff --git a/gxml/xlibxml.h b/gxml/xlibxml.h
index 586bb38..da25682 100644
--- a/gxml/xlibxml.h
+++ b/gxml/xlibxml.h
@@ -28,5 +28,6 @@ void*       gxml_doc_get_intsubset_entities    (xmlDoc *doc);
 int         gxml_validate_name                 (xmlChar* name, int space);
 xmlErrorPtr gxml_parser_context_get_last_error (void* ctx);
 xmlErrorPtr gxml_get_last_error                ();
+xmlNsPtr*   gxml_doc_get_ns_list               (xmlDoc* doc, xmlNode* node);
 
 #endif
diff --git a/test/SerializableObjectModelTest.vala b/test/SerializableObjectModelTest.vala
index 8da3fb9..c48f461 100644
--- a/test/SerializableObjectModelTest.vala
+++ b/test/SerializableObjectModelTest.vala
@@ -240,18 +240,22 @@ class Configuration : ObjectModel
   public override GXml.Node? serialize (GXml.Node node) throws GLib.Error
   {
     var n = (xNode) default_serialize ((xNode)node);
-    n.add_namespace_attr ("http://www.gnome.org/gxml/0.4";, "om");
+    n.set_namespace ("http://www.gnome.org/gxml/0.4";, "om");
     return (GXml.Node)n;
   }
   public override GXml.Node? deserialize (GXml.Node node) throws GLib.Error
   {
-    //stdout.printf (@"CONFIGURATOR: Namespaces Check");
+#if DEBUG
+    GLib.message (@"CONFIGURATOR: Deserializing... $(node.to_string ())");
+#endif
     GXml.Node n;
     if (node is Document)
       n = (GXml.Node) (((GXml.Document) node).root);
     else
       n = node;
-
+#if DEBUG
+    GLib.message ("Checking namespaces... in GXml.Node");
+#endif
     foreach (GXml.Namespace ns in n.namespaces) {
 #if DEBUG
       GLib.message (@"Namespace = $(ns.prefix):$(ns.uri)");
@@ -704,12 +708,16 @@ class SerializableObjectModelTest : GXmlTest
                        configuration.serialize (doc);
                        //stdout.printf (@"DOC: $doc");
                        if (doc.document_element == null) {
-                         stdout.printf ("DOC: No root element");
+#if DEBUG
+                         GLib.message ("DOC: No root element");
+#endif
                          assert_not_reached ();
                        }
                        GXml.Element element = (GXml.Element) doc.root;
                        if (element.name != "Configuration") {
-                         stdout.printf (@"CONFIGURATION: Bad node name: $(element.name)");
+#if DEBUG
+                         GLib.message (@"CONFIGURATION: Bad node name: $(element.name)");
+#endif
                          assert_not_reached ();
                        }
                        bool found = false;
@@ -719,10 +727,12 @@ class SerializableObjectModelTest : GXmlTest
                            found = true;
                        }
                        if (!found) {
+#if DEBUG
                          stdout.printf (@"CONFIGURATION: No namespace found: size: 
$(element.namespaces.size)");
                          foreach (GXml.Namespace n in element.namespaces) {
                            stdout.printf (@"CONFIGURATION: Defined Namespace: $(n.prefix):$(n.uri)");
                          }
+#endif
                          assert_not_reached ();
                        }
                      }
diff --git a/vapi/xlibxml-1.0.vapi b/vapi/xlibxml-1.0.vapi
index bfa8929..19b33a7 100644
--- a/vapi/xlibxml-1.0.vapi
+++ b/vapi/xlibxml-1.0.vapi
@@ -28,4 +28,6 @@ namespace Xmlx {
   static Xml.Error* parser_context_get_last_error (Xml.ParserCtxt ctx);
   [CCode (cname = "gxml_get_last_error", cheader_filename = "gxml/xlibxml.h")]
   static Xml.Error* get_last_error ();
+  [CCode (cname = "gxml_doc_get_ns_list", array_null_terminated = true, cheader_filename = "gxml/xlibxml.h")]
+  static Xml.Ns*[] doc_get_ns_list (Xml.Doc* doc, Xml.Node* node);
 }


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