[gxml] Fixed read GomHashMap and owner_document



commit da05cd288ef676f014bca0cda258bcd83b52243f
Author: Daniel Espinosa <esodan gmail com>
Date:   Fri Jan 20 21:23:29 2017 -0600

    Fixed read GomHashMap and owner_document

 gxml/GomCollections.vala       |   21 ++++--
 gxml/GomElement.vala           |    7 ++
 gxml/XParser.vala              |  143 ++++++++++++++++++++++------------------
 test/GomDocumentTest.vala      |   19 +++++-
 test/GomSerializationTest.vala |    5 +-
 5 files changed, 122 insertions(+), 73 deletions(-)
---
diff --git a/gxml/GomCollections.vala b/gxml/GomCollections.vala
index 43c60ff..260ebc2 100644
--- a/gxml/GomCollections.vala
+++ b/gxml/GomCollections.vala
@@ -187,7 +187,7 @@ public abstract class GXml.BaseCollection : Object {
   public void initialize (GLib.Type items_type) throws GLib.Error {
     if (!items_type.is_a (typeof (GomElement))) {
       throw new DomError.INVALID_NODE_TYPE_ERROR
-                (_("Invalid atempt to initialize a collection using an unsupported type. Only GXmlGomElement 
is supported"));
+                (_("Invalid attempt to initialize a collection using an unsupported type. Only 
GXmlGomElement is supported"));
     }
     var o = Object.new (items_type) as GomElement;
     _items_name = o.local_name;
@@ -220,10 +220,10 @@ public abstract class GXml.BaseCollection : Object {
                 (_("Parent Element is invalid"));
     if (!(node is GomElement))
       throw new DomError.INVALID_NODE_TYPE_ERROR
-                (_("Invalid atempt to set unsupported type. Only GXmlGomElement is supported"));
+                (_("Invalid attempt to set unsupported type. Only GXmlGomElement is supported"));
     if (node.owner_document != _element.owner_document)
       throw new DomError.HIERARCHY_REQUEST_ERROR
-                (_("Invalid atempt to set a node with a different parent document"));
+                (_("Invalid attempt to set a node with a different parent document"));
     _element.append_child (node);
     if (_element.child_nodes.size == 0)
       throw new DomError.QUOTA_EXCEEDED_ERROR
@@ -340,10 +340,19 @@ public class GXml.GomHashMap : GXml.BaseCollection, GXml.GomCollection {
    */
   public override bool validate_add (int index, DomElement element) throws GLib.Error {
     if (!(element is GomElement)) return false;
-    var key = element.get_attribute (attribute_key);
+#if DEBUG
+    message ("Validating HashMap Element..."
+            +(element as GomElement).write_string ()
+            +" Attrs:"+(element as GomElement).attributes.length.to_string());
+#endif
+    string key = null;
+    key = (element as DomElement).get_attribute (attribute_key);
     if (key == null)
-      throw new DomError.HIERARCHY_REQUEST_ERROR
-                (_("Invalid atempt to set a node without key attribute"));
+    key = (element as DomElement).get_attribute (attribute_key.down ());
+    if (key == null) assert_not_reached ();
+#if DEBUG
+    message ("Attribute key value: "+key);
+#endif
     if (key != null) {
       _hashtable.insert (key, index);
       return true;
diff --git a/gxml/GomElement.vala b/gxml/GomElement.vala
index 35420c1..de547f7 100644
--- a/gxml/GomElement.vala
+++ b/gxml/GomElement.vala
@@ -59,6 +59,13 @@ public class GXml.GomElement : GomNode,
     parser.read_string (str, null);
   }
   /**
+   * Serialize {@link GomElement} to a string.
+   */
+  public string write_string () throws GLib.Error {
+    var parser = new XParser (this);
+    return parser.write_string ();
+  }
+  /**
    * Uses element's {@link GomDocument} to write an XML to a file, serializing it.
    */
   public void write_file (GLib.File f) throws GLib.Error {
diff --git a/gxml/XParser.vala b/gxml/XParser.vala
index cc775be..7dbc756 100644
--- a/gxml/XParser.vala
+++ b/gxml/XParser.vala
@@ -433,30 +433,25 @@ public class GXml.XParser : Object, GXml.Parser {
     bool isempty = current_is_empty_element ();
     DomNode n = null;
     if (!read_element_property (parent, out n))
-      n = create_element (parent);
+      if (!add_element_collection (parent, out n)) {
+        n = create_element (parent);
+        read_element (n as DomElement);
+    }
     if (n == null) return false;
-#if DEBUG
-    GLib.message ("Reading New Node: "+n.node_name);
-#endif
-    read_element (n as DomElement);
     if (!isempty)
       read_child_nodes (n);
     return true;
   }
   /**
    * Creates a new {@link DomElement} and append it as a child of parent: for current
-   * read node, only if :
-   *
-   * # parent: have a property as {@link DomElement} type and current
+   * read node, only if parent: have a property as {@link DomElement} type and current
    * node have same local name as property element.
-   * # parent: have a property as {@link GomCollection} type and current
-   * node have same local name as collection {@link GomCollection.items_name}
    *
-   * Returns: true if element is set to a new object, it is set as a parent:
-   * property or has been added to a parent: collection property.
+   * Returns: true if element is set to a new object, it is set as a child of parent:
+   * as a property.
    */
   public bool read_element_property (DomNode parent,
-                  out DomNode element) throws GLib.Error {
+                                    out DomNode element) throws GLib.Error {
     if (!(parent is GomObject)) return false;
 #if DEBUG
     GLib.message ("Searching for Properties Nodes for:"+
@@ -465,65 +460,87 @@ public class GXml.XParser : Object, GXml.Parser {
 #endif
     foreach (ParamSpec pspec in
               (parent as GomObject).get_property_element_list ()) {
-      if (pspec.value_type.is_a (typeof (GomCollection))) {
+      if (pspec.value_type.is_a (typeof (GomCollection))) continue;
+      var obj = Object.new (pspec.value_type,
+                            "owner-document", _document) as DomElement;
+      if (obj.local_name.down ()
+             == tr.const_local_name ().down ()) {
+        Value v = Value (pspec.value_type);
+        parent.append_child (obj as DomNode);
+        v.set_object (obj);
+        parent.set_property (pspec.name, v);
+        read_element (element as DomElement);
+        element = obj as DomNode;
+        return true;
+      }
+    }
+    return false;
+  }
+  /**
+   * Creates a new {@link DomElement} and append it as a child of parent: for current
+   * read node, only if parent: have a property as {@link GomCollection} type and current
+   * node have same local name as collection {@link GomCollection.items_name}
+   *
+   * Returns: true if element is set to a new object, it is set as a child of parent:
+   * and has been added to a parent:'s collection property.
+   */
+  public bool add_element_collection (DomNode parent,
+                  out DomNode element) throws GLib.Error {
+    if (!(parent is GomObject)) return false;
 #if DEBUG
-        GLib.message (pspec.name+" Is Collection in: "+(parent as DomElement).local_name);
+    GLib.message ("Checking if node should be added to collection in "+
+                  (parent as DomElement).local_name+
+                  " Current node name: "+ tr.const_local_name ());
 #endif
-        GomCollection col;
-        Value vc = Value (pspec.value_type);
-        parent.get_property (pspec.name, ref vc);
-        col = vc.get_object () as GomCollection;
-        if (col == null) {
+    foreach (ParamSpec pspec in
+              (parent as GomObject).get_property_element_list ()) {
+      if (!(pspec.value_type.is_a (typeof (GomCollection)))) continue;
 #if DEBUG
-          GLib.message ("Initializing Collection property...");
+      GLib.message (pspec.name+" Is Collection in: "+(parent as DomElement).local_name);
 #endif
-          col = Object.new (pspec.value_type,
-                            "element", parent) as GomCollection;
-          vc.set_object (col);
-          parent.set_property (pspec.name, vc);
-        }
-        if (col.items_type == GLib.Type.INVALID
-            || !(col.items_type.is_a (typeof (GomObject)))) {
-          throw new DomError.INVALID_NODE_TYPE_ERROR
-                      (_("Invalid object type set to Collection"));
-        }
-        if (col.items_name == "" || col.items_name == null) {
-          throw new DomError.INVALID_NODE_TYPE_ERROR
-                      (_("Invalid DomElement name for objects in Collection"));
-        }
-        if (col.element == null || !(col.element is GomElement)) {
-          throw new DomError.INVALID_NODE_TYPE_ERROR
-                      (_("Invalid Element set to Collection"));
-        }
-        if (col.items_name.down () == current_node_name ().down ()) {
+      GomCollection col;
+      Value vc = Value (pspec.value_type);
+      parent.get_property (pspec.name, ref vc);
+      col = vc.get_object () as GomCollection;
+      if (col == null) {
 #if DEBUG
-          GLib.message (current_node_name ()+" Is a Node to append in collection: "+pspec.name);
+        GLib.message ("Initializing Collection property...");
 #endif
-          if (parent.owner_document == null)
-            throw new DomError.HIERARCHY_REQUEST_ERROR
-                        (_("No document is set to node"));
-          var obj = Object.new (col.items_type,
-                                "owner-document", _document) as DomElement;
+        col = Object.new (pspec.value_type,
+                          "element", parent) as GomCollection;
+        vc.set_object (col);
+        parent.set_property (pspec.name, vc);
+      }
+      if (col.items_type == GLib.Type.INVALID
+          || !(col.items_type.is_a (typeof (GomObject)))) {
+        throw new DomError.INVALID_NODE_TYPE_ERROR
+                    (_("Invalid object type set to Collection"));
+      }
+      if (col.items_name == "" || col.items_name == null) {
+        throw new DomError.INVALID_NODE_TYPE_ERROR
+                    (_("Invalid DomElement name for objects in Collection"));
+      }
+      if (col.element == null || !(col.element is GomElement)) {
+        throw new DomError.INVALID_NODE_TYPE_ERROR
+                    (_("Invalid Element set to Collection"));
+      }
+      if (col.items_name.down () == current_node_name ().down ()) {
 #if DEBUG
-          GLib.message ("Object Element to add in Collection: "
-                          +obj.local_name);
+        GLib.message (current_node_name ()+" Is a Node to append in collection: "+pspec.name);
 #endif
-          col.append (obj);
-          element = obj;
-          return true;
-        }
-      } else {
-        var obj = Object.new (pspec.value_type,
+        if (parent.owner_document == null)
+          throw new DomError.HIERARCHY_REQUEST_ERROR
+                      (_("No document is set to node"));
+        var obj = Object.new (col.items_type,
                               "owner-document", _document) as DomElement;
-        if (obj.local_name.down ()
-               == tr.const_local_name ().down ()) {
-          Value v = Value (pspec.value_type);
-          parent.append_child (obj as DomNode);
-          v.set_object (obj);
-          parent.set_property (pspec.name, v);
-          element = obj as DomNode;
-          return true;
-        }
+#if DEBUG
+        GLib.message ("Object Element to add in Collection: "
+                        +obj.local_name);
+#endif
+        read_element (obj as DomElement);
+        col.append (obj);
+        element = obj;
+        return true;
       }
     }
     return false;
diff --git a/test/GomDocumentTest.vala b/test/GomDocumentTest.vala
index 5b917b7..b7a6f86 100644
--- a/test/GomDocumentTest.vala
+++ b/test/GomDocumentTest.vala
@@ -390,7 +390,7 @@ class GomDocumentTest : GXmlTest {
                                assert 
(d.document_element.child_nodes[1].child_nodes[1].child_nodes[1].child_nodes[0].node_value == "status_only");
                        } catch (GLib.Error e) { GLib.message ("ERROR: "+e.message); assert_not_reached (); }
                });
-               Test.add_func ("/gxml/gom-document/namespace", () => {
+               Test.add_func ("/gxml/gom-document/namespace/create", () => {
                        try {
                                DomDocument doc = new GomDocument.from_string 
("<document_element><child/></document_element>");
                                doc.document_element.set_attribute_ns ("http://www.w3.org/2000/xmlns/";,
@@ -434,6 +434,23 @@ class GomDocumentTest : GXmlTest {
                                assert_not_reached ();
                        }
                });
+               Test.add_func ("/gxml/gom-document/namespace/read", () => {
+                       try {
+                               DomDocument doc = new GomDocument.from_string ("""
+                               <Project xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
+         xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#";
+         xmlns:foaf="http://xmlns.com/foaf/0.1/";
+         xmlns:gnome="http://api.gnome.org/doap-extensions#";
+         xmlns="http://usefulinc.com/ns/doap#";><child/></Project>""");
+                               assert (doc.document_element.prefix == null);
+                               var parser = new XParser (doc);
+                               string str = parser.write_string ();
+                               message ("Read: "+str);
+                       } catch (GLib.Error e) {
+                               GLib.message ("ERROR: "+ e.message);
+                               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>"); }
diff --git a/test/GomSerializationTest.vala b/test/GomSerializationTest.vala
index 9ae0006..278150c 100644
--- a/test/GomSerializationTest.vala
+++ b/test/GomSerializationTest.vala
@@ -258,7 +258,7 @@ class GomSerializationTest : GXmlTest  {
   }
   public class Books : GomHashMap {
     construct {
-      try { initialize_with_key (typeof (Book), "name"); }
+      try { initialize_with_key (typeof (Book), "Name"); }
       catch { assert_not_reached (); }
     }
   }
@@ -647,8 +647,7 @@ class GomSerializationTest : GXmlTest  {
       string s = bs.to_string ();
       GLib.message ("doc:"+s);
       assert ("<BookStand Classification=\"Science\"/>" in s);
-      var parser = new XParser (bs);
-      parser.read_string ("<BookStand Classification=\"Science\"><Book name=\"Title1\"/><Book 
name=\"Title2\"/><Test/><Book name=\"Title3\"/></BookStand>", null);
+      bs.read_from_string ("<bookStand Classification=\"Science\"><book name=\"Title1\"/><book 
name=\"Title2\"/><Test/><book name=\"Title3\"/></bookStand>");
       //assert (bs.registers == null);
       assert (bs.books != null);
       s = bs.to_string ();


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