[gxml] GomCollection: Require object type



commit 087fc1b593bfdca6e85e6f1543e8d89367dc185f
Author: Daniel Espinosa <esodan gmail com>
Date:   Mon Nov 7 17:17:01 2016 -0600

    GomCollection: Require object type
    
    Object type allows to instantiate objects of the same type
    in collection when reading XML trees.

 gxml/GomCollections.vala       |   48 ++++++++++++++++++++++++++++++----------
 test/GomSerializationTest.vala |    7 ++---
 2 files changed, 39 insertions(+), 16 deletions(-)
---
diff --git a/gxml/GomCollections.vala b/gxml/GomCollections.vala
index 367ce4d..6b00997 100644
--- a/gxml/GomCollections.vala
+++ b/gxml/GomCollections.vala
@@ -41,7 +41,14 @@ public interface GXml.GomCollection : Object
    * Local name of {@link DomElement} objects of {@link element}, which could be
    * contained in this collection.
    */
-  public abstract string items_name { get; construct set; }
+  public abstract string items_name { get; }
+  /**
+   * A {@link Type} of {@link DomElement} child objects of {@link element},
+   * which could be contained in this collection.
+   *
+   * Type should be an {@link GomObject}.
+   */
+  public abstract Type items_type { get; construct set; }
   /**
    * Search and add references to all {@link GomObject} nodes as child of
    * {@link element} with same, case insensitive, name of {@link element_name}
@@ -81,7 +88,8 @@ public interface GXml.GomCollection : Object
 public class GXml.GomArrayList : Object, GomCollection {
   protected Queue<int> _nodes_index = new Queue<int> ();
   protected GomElement _element;
-  protected string _items_name;
+  protected string _items_name = "";
+  protected GLib.Type _items_type = GLib.Type.INVALID;
   public Queue<int> nodes_index { get { return _nodes_index; } }
   public DomElement element {
     get { return _element; }
@@ -94,14 +102,21 @@ public class GXml.GomArrayList : Object, GomCollection {
       }
     }
   }
-  public string items_name {
-    get { return _items_name; } construct set { _items_name = value; }
+  public string items_name { get { return _items_name; } }
+  public Type items_type {
+    get { return _items_type; } construct set { _items_type = value; }
   }
 
-  public GomArrayList.initialize (GomElement element, string items_name) {
+  public GomArrayList.initialize (GomElement element, GLib.Type items_type) {
     _element = element;
-    _items_name = items_name;
-    search ();
+    _items_name = "";
+    if (!(items_type.is_a (typeof (GomObject)))) {
+      warning (_("Invalid object item type to initialize ArrayList"));
+    } else {
+      var tmp = Object.new (items_type) as GomElement;
+      _items_name = tmp.local_name;
+      search ();
+    }
   }
   /**
    * Adds an {@link DomElement} of type {@link GomObject} as a child of
@@ -142,7 +157,8 @@ public class GXml.GomHashMap : Object, GomCollection {
   protected Queue<int> _nodes_index = new Queue<int> ();
   protected HashTable<string,int> _hashtable = new HashTable<string,int> (str_hash,str_equal);
   protected GomElement _element;
-  protected string _items_name;
+  protected string _items_name = "";
+  protected GLib.Type _items_type = GLib.Type.INVALID;
   protected string _attribute_key;
   public Queue<int> nodes_index { get { return _nodes_index; } }
   public DomElement element {
@@ -156,8 +172,9 @@ public class GXml.GomHashMap : Object, GomCollection {
       }
     }
   }
-  public string items_name {
-    get { return _items_name; } construct set { _items_name = value; }
+  public string items_name { get { return _items_name; } }
+  public GLib.Type items_type {
+    get { return _items_type; } construct set { _items_type = value; }
   }
   /**
    * An attribute's name in items to be added and used to retrieve a key to
@@ -168,11 +185,18 @@ public class GXml.GomHashMap : Object, GomCollection {
   }
 
   public GomHashMap.initialize (GomElement element,
-                                  string items_name,
+                                  GLib.Type items_type,
                                   string attribute_key) {
     _element = element;
-    _items_name = items_name;
+    _items_name = "";
     _attribute_key = attribute_key;
+    if (!(items_type.is_a (typeof (GomObject)))) {
+      warning (_("Invalid object item type to initialize HashMap"));
+    } else {
+      var tmp = Object.new (items_type) as GomElement;
+      _items_name = tmp.local_name;
+      search ();
+    }
   }
   /**
    * Sets an {@link DomElement} of type {@link GomObject} as a child of
diff --git a/test/GomSerializationTest.vala b/test/GomSerializationTest.vala
index ca56646..3922ea6 100644
--- a/test/GomSerializationTest.vala
+++ b/test/GomSerializationTest.vala
@@ -208,7 +208,7 @@ class GomSerializationTest : GXmlTest  {
       assert ("<BookStand Classification=\"Science\"/>" in s);
       assert (bs.registers == null);
       var br = new BookRegister ();
-      bs.registers = new GomArrayList.initialize (bs,br.local_name);
+      bs.registers = new GomArrayList.initialize (bs,typeof (BookRegister));
       s = bs.to_string ();
       assert (s != null);
       GLib.message ("DOC:"+s);
@@ -247,7 +247,7 @@ class GomSerializationTest : GXmlTest  {
       assert ("<BookStore/>" in s);
       assert (bs.books == null);
       var b = new Book ();
-      bs.books = new GomHashMap.initialize (bs,b.local_name,"name");
+      bs.books = new GomHashMap.initialize (bs,typeof (Book),"name");
       s = bs.to_string ();
       assert (s != null);
       GLib.message ("DOC:"+s);
@@ -390,8 +390,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);
-      var tmp = new BookRegister ();
-      bs.registers = new GomArrayList.initialize (bs, tmp.local_name);
+      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]