[gxml/serialization] Added Gee.TreeMap implementing Serializable



commit 0e3749b7681c3f8033fa5e2f0cfcaa318245bb9e
Author: Daniel Espinosa <esodan gmail com>
Date:   Tue Nov 12 12:33:04 2013 -0600

    Added Gee.TreeMap implementing Serializable

 gxml/Makefile.am                     |    2 +
 gxml/SerializableGeeTreeMap.vala     |  136 ++++++++++++++++++++++++++++++++++
 gxml/SerializableMapId.vala          |   27 +++++++
 gxml/SerializableObjectModel.vala    |   12 ++--
 test/GXmlTest.vala                   |    1 +
 test/Makefile.am                     |    1 +
 test/SerializableGeeTreeMapTest.vala |  129 ++++++++++++++++++++++++++++++++
 7 files changed, 302 insertions(+), 6 deletions(-)
---
diff --git a/gxml/Makefile.am b/gxml/Makefile.am
index ed8aeb4..6de2735 100644
--- a/gxml/Makefile.am
+++ b/gxml/Makefile.am
@@ -65,6 +65,8 @@ libgxml_la_SOURCES = \
        SerializableObjectModel.vala \
        SerializableJson.vala \
        Serialization.vala \
+       SerializableGeeTreeMap.vala \
+       SerializableMapId.vala \
        $(NULL)
 
 gxml.vala.stamp: $(libgxml_la_SOURCES)
diff --git a/gxml/SerializableGeeTreeMap.vala b/gxml/SerializableGeeTreeMap.vala
new file mode 100644
index 0000000..6ede0c9
--- /dev/null
+++ b/gxml/SerializableGeeTreeMap.vala
@@ -0,0 +1,136 @@
+/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+/* SerializableGeeTreeModel.vala
+ *
+ * Copyright (C) 2013  Daniel Espinosa <esodan gmail com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *      Daniel Espinosa <esodan gmail com>
+ */
+using GXml;
+
+public class GXml.SerializableTreeMap<K,V> : Gee.TreeMap<K,V>, Serializable
+{
+  protected ParamSpec[] properties { get; set; }
+  public GLib.HashTable<string,GLib.ParamSpec> ignored_serializable_properties { get; protected set; }
+  public string? serialized_xml_node_value { get; protected set; default=null; }
+  public GLib.HashTable<string,GXml.Node> unknown_serializable_property { get; protected set; }
+
+  public virtual bool property_use_nick () { return false; }
+
+  public virtual string node_name ()
+  {
+    return "";
+  }
+  public string default_node_name ()
+  {
+    return get_type().name().down();
+  }
+
+  public virtual GLib.ParamSpec? find_property_spec (string property_name)
+  {
+    return default_find_property_spec (property_name);
+  }
+
+  public virtual void init_properties ()
+  {
+    default_init_properties ();
+  }
+
+  public virtual GLib.ParamSpec[] list_serializable_properties ()
+  {
+    return default_list_serializable_properties ();
+  }
+
+  public virtual void get_property_value (GLib.ParamSpec spec, ref Value val)
+  {
+    default_get_property_value (spec, ref val);
+  }
+
+  public virtual void set_property_value (GLib.ParamSpec spec, GLib.Value val)
+  {
+    default_set_property_value (spec, val);
+  }
+
+  public virtual bool transform_from_string (string str, ref GLib.Value dest)
+  {
+    return false;
+  }
+
+  public virtual bool transform_to_string (GLib.Value val, ref string str)
+  {
+    return false;
+  }
+
+  public virtual GXml.Node? serialize (GXml.Node node)
+                              throws GLib.Error
+                              requires (node is Element)
+  {
+    return default_serialize (node);
+  }
+  public GXml.Node? default_serialize (GXml.Node node)
+                              throws GLib.Error
+                              requires (node is Element)
+  {
+    if (value_type.is_a (typeof (Serializable))) {
+      foreach (V v in values) {
+       ((GXml.Serializable) v).serialize (node);
+      }
+    }
+    return node;
+  }
+  public virtual GXml.Node? serialize_property (GXml.Element element,
+                                        GLib.ParamSpec prop)
+                                        throws GLib.Error
+  {
+    return default_serialize_property (element, prop);
+  }
+  public GXml.Node? default_serialize_property (GXml.Element element,
+                                        GLib.ParamSpec prop)
+                                        throws GLib.Error
+  {
+    return element;
+  }
+  public virtual GXml.Node? deserialize (GXml.Node node)
+                                    throws GLib.Error
+                                    requires (node_name () != null)
+  {
+    return default_deserialize (node);
+  }
+  public GXml.Node? default_deserialize (GXml.Node node)
+                    throws GLib.Error
+                    requires (value_type.is_a (typeof (GXml.Serializable)))
+                    requires (value_type.is_a (typeof (SerializableMapId)))
+  {
+    if (node is Element) {
+      foreach (GXml.Node n in node.child_nodes) {
+        var obj = Object.new (value_type);
+        ((Serializable) obj).deserialize (n);
+        @set (((SerializableMapId<K>) obj).id (), obj);
+      }
+    }
+    return node;
+  }
+  public virtual bool deserialize_property (GXml.Node property_node)
+                                            throws GLib.Error
+  {
+    return default_deserialize_property (property_node);
+  }
+  public bool default_deserialize_property (GXml.Node property_node)
+                                            throws GLib.Error
+  {
+    return true;
+  }
+}
diff --git a/gxml/SerializableMapId.vala b/gxml/SerializableMapId.vala
new file mode 100644
index 0000000..897a3d7
--- /dev/null
+++ b/gxml/SerializableMapId.vala
@@ -0,0 +1,27 @@
+/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+/* SerializableGeeTreeModel.vala
+ *
+ * Copyright (C) 2013  Daniel Espinosa <esodan gmail com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *      Daniel Espinosa <esodan gmail com>
+ */
+using GXml;
+
+public interface GXml.SerializableMapId<K> : Object
+{
+  public abstract K id ();
+}
diff --git a/gxml/SerializableObjectModel.vala b/gxml/SerializableObjectModel.vala
index 05428a9..5f4c62b 100644
--- a/gxml/SerializableObjectModel.vala
+++ b/gxml/SerializableObjectModel.vala
@@ -74,7 +74,7 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
     return false;
   }
 
-  public virtual Node? serialize (Node node)
+  public virtual GXml.Node? serialize (GXml.Node node)
                        throws GLib.Error
                        requires (node_name () != null)
                        requires (node is Document || node is Element)
@@ -82,7 +82,7 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
     return default_serialize (node);
   }
 
-  public Node? default_serialize (Node node) throws GLib.Error
+  public GXml.Node? default_serialize (GXml.Node node) throws GLib.Error
   {
     Document doc;
     if (node is Document)
@@ -115,13 +115,13 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
     return element;
   }
 
-  public virtual GXml.Node? serialize_property (Element element,
+  public virtual GXml.Node? serialize_property (GXml.Element element,
                                         GLib.ParamSpec prop)
                                         throws GLib.Error
   {
     return default_serialize_property (element, prop);
   }
-  public GXml.Node? default_serialize_property (Element element,
+  public GXml.Node? default_serialize_property (GXml.Element element,
                                         GLib.ParamSpec prop)
                                         throws GLib.Error
   {
@@ -178,13 +178,13 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
     return (Node) attr;
   }
 
-  public virtual Node? deserialize (Node node)
+  public virtual GXml.Node? deserialize (GXml.Node node)
                                     throws GLib.Error
                                     requires (node_name () != null)
   {
     return default_deserialize (node);
   }
-  public Node? default_deserialize (Node node)
+  public GXml.Node? default_deserialize (GXml.Node node)
                                     throws GLib.Error
   {
     Document doc;
diff --git a/test/GXmlTest.vala b/test/GXmlTest.vala
index 978a2b6..51fccc9 100644
--- a/test/GXmlTest.vala
+++ b/test/GXmlTest.vala
@@ -38,6 +38,7 @@ class GXmlTest {
                SerializationTest.add_tests ();
                SerializableTest.add_tests ();
                SerializableObjectModelTest.add_tests ();
+               SerializableGeeTreeMapTest.add_tests ();
 
                Test.run ();
 
diff --git a/test/Makefile.am b/test/Makefile.am
index 93d939f..4ce0b0f 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -38,6 +38,7 @@ gxml_test_SOURCES = \
        SerializableTest.vala \
        SerializationTest.vala \
        SerializableObjectModelTest.vala \
+       SerializableGeeTreeMapTest.vala \
        $(NULL)
 
 gxml_test.vala.stamp: $(gxml_test_SOURCES)
diff --git a/test/SerializableGeeTreeMapTest.vala b/test/SerializableGeeTreeMapTest.vala
new file mode 100644
index 0000000..07bd614
--- /dev/null
+++ b/test/SerializableGeeTreeMapTest.vala
@@ -0,0 +1,129 @@
+using GXml;
+using Gee;
+
+class Space : SerializableObjectModel, SerializableMapId<string>
+{
+  public string id () { return name; }
+  public string name { get; set; }
+  public Space.named (string name) { this.name = name; }
+  public override string to_string () { return name; }
+}
+
+class SerializableGeeTreeMapTest : GXmlTest
+{
+  public static void add_tests ()
+  {
+    Test.add_func ("/gxml/serializable/serializable_tree_map/api",
+    () => {
+      try {
+        var c = new SerializableTreeMap<string,Space> ();
+        var o1 = new Space.named ("Big");
+        var o2 = new Space.named ("Small");
+        c.set (o1.name, o1);
+        c.set (o2.name, o2);
+        bool found1 = false;
+        bool found2 = false;
+        foreach (Space o in c.values) {
+          if (o.name == "Big") found1 = true;
+          if (o.name == "Small") found2 = true;
+        }
+        if (!found1) {
+          stdout.printf (@"Big is not found\n");
+          assert_not_reached ();
+        }
+        if (!found2) {
+          stdout.printf (@"Small is not found\n");
+          assert_not_reached ();
+        }
+        found1 = found2 = false;
+        foreach (string k in c.keys) {
+          if ((c  get (k)).name == "Big") found1 = true;
+          if ((c  get (k)).name == "Small") found2 = true;
+        }
+        if (!found1) {
+          stdout.printf (@"Big key value is not found\n");
+          assert_not_reached ();
+        }
+        if (!found2) {
+          stdout.printf (@"Small key value is not found\n");
+          assert_not_reached ();
+        }
+      }
+      catch (GLib.Error e) {
+        stdout.printf (@"ERROR: $(e.message)");
+      }
+    });
+    Test.add_func ("/gxml/serializable/serializable_tree_map/serialize",
+    () => {
+      try {
+        var c = new SerializableTreeMap<string,Space> ();
+        var o1 = new Space.named ("Big");
+        var o2 = new Space.named ("Small");
+        c.set (o1.name, o1);
+        c.set (o2.name, o2);
+        var doc = new Document ();
+        var root = doc.create_element ("root");
+        doc.append_child (root);
+        c.serialize (root);
+        if (!root.has_child_nodes ()) {
+          stdout.printf (@"ERROR: root node have no childs $(doc)\n");
+          assert_not_reached ();
+        }
+        bool found1 = false;
+        bool found2 = false;
+        foreach (GXml.Node n in root.child_nodes) {
+          if (n is Element && n.node_name == "space") {
+            var name = ((Element) n).get_attribute_node ("name");
+            if (name != null) {
+              if (name.node_value == "Big") found1 = true;
+              if (name.node_value == "Small") found2 = true;
+            }
+          }
+        }
+        if (!found1) {
+          stdout.printf (@"ERROR: Big space node is not found\n");
+          assert_not_reached ();
+        }
+        if (!found2) {
+          stdout.printf (@"ERROR: Small space node is not found\n");
+          assert_not_reached ();
+        }
+      }
+      catch (GLib.Error e) {
+        stdout.printf (@"ERROR: $(e.message)");
+        assert_not_reached ();
+      }
+    });
+    Test.add_func ("/gxml/serializable/serializable_tree_map/deserialize",
+    () => {
+      try {
+        var doc = new Document.from_string ("""<?xml version="1.0"?>
+  <root><space name="Big"/><space name="Small"/></root>""");
+        var c = new SerializableTreeMap<string,Space> ();
+        c.deserialize (doc.document_element);
+        if (c.size != 2) {
+          stdout.printf (@"ERROR: incorrect size must be 2 got: $(c.size)\n");
+          assert_not_reached ();
+        }
+        bool found1 = false;
+        bool found2 = false;
+        foreach (string k in c.keys) {
+          if ((c  get (k)).name == "Big") found1 = true;
+          if ((c  get (k)).name == "Small") found2 = true;
+        }
+        if (!found1) {
+          stdout.printf (@"ERROR: Big key value is not found\n");
+          assert_not_reached ();
+        }
+        if (!found2) {
+          stdout.printf (@"ERROR: Small key value is not found\n");
+          assert_not_reached ();
+        }
+      }
+      catch (GLib.Error e) {
+        stdout.printf (@"ERROR: $(e.message)");
+        assert_not_reached ();
+      }
+    });
+  }
+}


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