[gxml] Fixed GomElement intialization. Fixed Collections



commit dbfc7326c98ba7b592abf5c92e1b715c764956af
Author: Daniel Espinosa <esodan gmail com>
Date:   Mon Nov 7 20:22:16 2016 -0600

    Fixed GomElement intialization. Fixed Collections
    
    GomElement initialization fixed.
    
    Collection read implemented.

 gxml/GomElement.vala           |    2 -
 gxml/GomNode.vala              |   16 +++++++-
 gxml/GomObject.vala            |   12 ++++--
 gxml/XParser.vala              |   75 +++++++++++++++++++++++++++++++---------
 test/GomSerializationTest.vala |   18 +++++++--
 5 files changed, 94 insertions(+), 29 deletions(-)
---
diff --git a/gxml/GomElement.vala b/gxml/GomElement.vala
index 1e2f98e..d81704c 100644
--- a/gxml/GomElement.vala
+++ b/gxml/GomElement.vala
@@ -167,11 +167,9 @@ public class GXml.GomElement : GomNode,
   }
 
   construct {
-    _document = new GomDocument ();
     _node_type = DomNode.NodeType.ELEMENT_NODE;
     _attributes = new Attributes (this);
     _local_name = "";
-    _document.append_child (this);
   }
 
   public GomElement.initialize (DomDocument doc, string local_name) {
diff --git a/gxml/GomNode.vala b/gxml/GomNode.vala
index 2f5134a..8f6547f 100644
--- a/gxml/GomNode.vala
+++ b/gxml/GomNode.vala
@@ -47,7 +47,15 @@ public class GXml.GomNode : Object,
 
   protected GXml.DomDocument _document;
   public DomDocument? owner_document {
-    get { return _document; }
+    get {
+      if (this is DomDocument) return (DomDocument) this;
+      if (_document == null) {
+        _document = new GomDocument ();
+        if (this is DomElement)
+          _document.append_child (this);
+      }
+      return _document;
+    }
     construct set { _document = value; }
   }
 
@@ -263,7 +271,11 @@ public class GXml.GomNode : Object,
   }
   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"));
+      throw new DomError.HIERARCHY_REQUEST_ERROR
+              (_("Node type is invalid. Can't append as child"));
+    if (owner_document != node.owner_document)
+      throw new DomError.HIERARCHY_REQUEST_ERROR
+              (_("Invalid attempt to append a child with different parent document"));
     (node as GomNode).set_parent (this);
     return insert_before (node, null);
   }
diff --git a/gxml/GomObject.vala b/gxml/GomObject.vala
index 4449186..a338793 100644
--- a/gxml/GomObject.vala
+++ b/gxml/GomObject.vala
@@ -190,22 +190,26 @@ public interface GXml.GomObject : GLib.Object,
         return true;
       }
       if (prop.value_type.is_a (typeof (int))) {
-        v.set_string (val);
+        int iv = (int) double.parse (val);
+        v.set_int (iv);
         set_property (prop.name, v);
         return true;
       }
       if (prop.value_type.is_a (typeof (uint))) {
-        v.set_string (val);
+        uint iv = (uint) double.parse (val);
+        v.set_int ((int) iv);
         set_property (prop.name, v);
         return true;
       }
       if (prop.value_type.is_a (typeof (double))) {
-        v.set_string (val);
+        double dv = double.parse (val);
+        v.set_double (dv);
         set_property (prop.name, v);
         return true;
       }
       if (prop.value_type.is_a (typeof (bool))) {
-        v.set_string (val);
+        bool bv = bool.parse (val);
+        v.set_boolean (bv);
         set_property (prop.name, v);
         return true;
       }
diff --git a/gxml/XParser.vala b/gxml/XParser.vala
index 5e4674e..9cdd0bd 100644
--- a/gxml/XParser.vala
+++ b/gxml/XParser.vala
@@ -103,7 +103,9 @@ public class GXml.XParser : Object, GXml.Parser {
     tr = new TextReader.for_memory ((char[]) b.data, (int) b.get_data_size (), "/gxml_memory");
     while (read_current_node (_node, true));
   }
-
+  /**
+   * Read current node from a TextReader
+   */
   public bool read_current_node (DomNode node,
                                 bool read_current = false,
                                 bool read_property = false)
@@ -111,9 +113,11 @@ public class GXml.XParser : Object, GXml.Parser {
     GXml.DomNode n = node;
     string prefix = null, nsuri = null;
     int res = 1;
-#if DEBUG
-    GLib.message ("ReadNode: Current Node:"+node.node_name);
-#endif
+//#if DEBUG
+    GLib.message ("ReadNode: Current Node: "+node.node_name
+                  +" Current: "+read_current.to_string ()+
+                  " Property: "+read_property.to_string ());
+//#endif
     if (!read_property) {
       res = tr.read ();
       if (res == -1)
@@ -141,35 +145,65 @@ public class GXml.XParser : Object, GXml.Parser {
           && node is DomElement
           && tr.const_local_name () != (node as DomElement).local_name) {
         GLib.message ("Searching for Properties Nodes for:"+
-                      tr.const_local_name ());
+                      (node as DomElement).local_name+
+                      " Current node name: "+ tr.const_local_name ());
         foreach (ParamSpec pspec in
                   (node as GomObject).get_property_element_list ()) {
           if (pspec.value_type.is_a (typeof (GomCollection))) {
+            GLib.message (" Is Collection in: "+(node as DomElement).local_name);
             GomCollection col;
             Value vc = Value (pspec.value_type);
-            get_property (pspec.name, ref vc);
+            node.get_property (pspec.name, ref vc);
             col = vc.get_object () as GomCollection;
             if (col == null) {
-              col = Object.new (pspec.value_type) as GomCollection;
+              GLib.message ("Initializing Collection property...");
+              col = Object.new (pspec.value_type,
+                                "element", node) as GomCollection;
               vc.set_object (col);
               node.set_property (pspec.name, vc);
             }
             if (col.items_type == GLib.Type.INVALID
-                || col.items_type.is_a (typeof (GomObject))) {
-              GLib.warning (_("Invalid object type set to Collection"));
+                || !(col.items_type.is_a (typeof (GomObject)))) {
+              throw new DomError.INVALID_NODE_TYPE_ERROR
+                          (_("Invalid object type set to Collection"));
               continue;
             }
             if (col.items_name == "" || col.items_name == null) {
-              GLib.warning (_("Invalid DomElement name for objects in Collection"));
+              throw new DomError.INVALID_NODE_TYPE_ERROR
+                          (_("Invalid DomElement name for objects in Collection"));
+              continue;
+            }
+            if (col.element == null || !(col.element is GomElement)) {
+              throw new DomError.INVALID_NODE_TYPE_ERROR
+                          (_("Invalid Element set to Collection"));
               continue;
             }
-            var obj = Object.new (col.items_type,
-                                  "owner-document", node.owner_document);
-            read_current_node (obj as DomNode, true, true);
-            col.append (obj as DomElement);
+            if (col.items_name.down () == tr.const_local_name ().down ()) {
+              GLib.message ("Is a Node to append in collection");
+              if (node.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);
+              GLib.message ("Equal Documents:"+
+                  ((obj as DomNode).owner_document == node.owner_document).to_string ());
+              GLib.message ("Object Element to add in Collection: "
+                              +(_node as DomNode).node_name);
+              GLib.message ("Root Document Element Root: "
+                            +(_node as DomNode).owner_document.document_element.node_name);
+              GLib.message ("Root Document Element Node: "
+                            +(node as DomNode).owner_document.document_element.node_name);
+              GLib.message ("Root Document Element: "
+                            +(obj as DomNode).owner_document.document_element.node_name);
+              read_current_node (obj as DomNode, true, true);
+              GLib.message ("Adding element to collection...");
+              col.append (obj as DomElement);
+              isproperty = true;
+              break;
+            }
           } else {
             var obj = Object.new (pspec.value_type,
-                                  "owner-document", node.owner_document);
+                                  "owner-document", _document);
             if ((obj as DomElement).local_name.down ()
                    == tr.const_local_name ().down ()) {
               Value v = Value (pspec.value_type);
@@ -192,7 +226,8 @@ public class GXml.XParser : Object, GXml.Parser {
                         .printf ((node as DomElement).local_name));
       }
       if (!isproperty) {
-        GLib.message ("Not property is set");
+        GLib.message ("No object Property is set. Creating a standard element: "
+                        +tr.const_local_name ());
         if (node is DomDocument || !read_current) {
           GLib.message ("No deserializing current node");
 #if DEBUG
@@ -264,8 +299,14 @@ public class GXml.XParser : Object, GXml.Parser {
             }
           }
         }
+        GLib.message ("No more element attributes for: "
+                        +(node as DomElement).local_name);
+      }
+      if (isempty) {
+        GLib.message ("No child nodes returning...");
+        return true;
       }
-      if (isempty) return true;
+      GLib.message ("Getting child nodes in element");
       while (read_current_node (n) == true);
 #if DEBUG
       //GLib.message ("Current Document: "+node.document.to_string ());
diff --git a/test/GomSerializationTest.vala b/test/GomSerializationTest.vala
index 93d5a58..3db27cf 100644
--- a/test/GomSerializationTest.vala
+++ b/test/GomSerializationTest.vala
@@ -86,7 +86,7 @@ class GomSerializationTest : GXmlTest  {
   public class BookStand : GomElement {
     [Description (nick="::Classification")]
     public string classification { get; set; default = "Science"; }
-    public GomArrayList registers { get; set; }
+    public Registers registers { get; set; }
     construct {
       _local_name = "BookStand";
     }
@@ -94,6 +94,16 @@ class GomSerializationTest : GXmlTest  {
       var parser = new XParser (this);
       return parser.write_string ();
     }
+    public class Registers : GomArrayList {
+      public Registers.initialize (BookStand stand) {
+        _element = stand;
+      }
+      construct {
+        var t = new BookRegister ();
+        _items_type = typeof (BookRegister);
+        _items_name = t.local_name;
+      }
+    }
   }
   public class BookStore : GomElement {
     public GomHashMap books { get; set; }
@@ -208,7 +218,7 @@ class GomSerializationTest : GXmlTest  {
       assert ("<BookStand Classification=\"Science\"/>" in s);
       assert (bs.registers == null);
       var br = new BookRegister ();
-      bs.registers = new GomArrayList.initialize (bs,typeof (BookRegister));
+      bs.registers = new BookStand.Registers ();
       s = bs.to_string ();
       assert (s != null);
       GLib.message ("DOC:"+s);
@@ -382,7 +392,7 @@ class GomSerializationTest : GXmlTest  {
       assert ("/>" in s);
     });
     Test.add_func ("/gxml/gom-serialization/read/property-arraylist", () => {
-      /*var bs = new BookStand ();
+      var bs = new BookStand ();
       string s = bs.to_string ();
       GLib.message ("doc:"+s);
       assert ("<BookStand Classification=\"Science\"/>" in s);
@@ -390,7 +400,7 @@ class GomSerializationTest : GXmlTest  {
       parser.read_string ("<BookStand Classification=\"Science\"><BookRegister Year=\"2016\"/><BookRegister 
Year=\"2010\"/><Test/><BookRegister Year=\"2000\"/></BookStand>", null);
       s = bs.to_string ();
       GLib.message ("doc:"+s);
-      bs.registers = new GomArrayList.initialize (bs, typeof (BookRegister));
+      /*bs.registers = new GomArrayList.initialize (bs, typeof (BookRegister));
       GLib.message ("Registers: "+bs.registers.length.to_string ());
       assert (bs.registers.length == 3);
       assert (bs.registers.nodes_index.peek_nth (0) == 0);


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