[gxml/serialization: 4/10] Implemented Serializable.deserialize() * Added test case for serialize() * Added SerializableObjectM



commit d37d5ec6c9301490cef18e687995ac6833d5fdcb
Author: Daniel Espinosa <esodan gmail com>
Date:   Mon Jul 22 21:51:04 2013 -0500

    Implemented Serializable.deserialize()
    * Added test case for serialize()
    * Added SerializableObjectModel class to help Serializable implementators

 gxml/Makefile.am                  |    1 +
 gxml/Serializable.vala            |   56 +++++++++++++++++++++++---
 gxml/SerializableObjectModel.vala |   66 ++++++++++++++++++++++++++++++
 test/SerializableTest.vala        |   81 +++++++++++++++++--------------------
 4 files changed, 154 insertions(+), 50 deletions(-)
---
diff --git a/gxml/Makefile.am b/gxml/Makefile.am
index 206e059..bb6b4b6 100644
--- a/gxml/Makefile.am
+++ b/gxml/Makefile.am
@@ -59,6 +59,7 @@ libgxml_la_SOURCES = \
        ProcessingInstruction.vala \
        Text.vala \
        Serializable.vala \
+       SerializableObjectModel.vala \
        Serialization.vala \
        $(NULL)
 
diff --git a/gxml/Serializable.vala b/gxml/Serializable.vala
index 303b890..6a86ca7 100644
--- a/gxml/Serializable.vala
+++ b/gxml/Serializable.vala
@@ -113,19 +113,55 @@ namespace GXml {
                                doc = (Document) node;
                        else
                                doc = node.owner_document;
-
-                       serialized_xml_node = doc.create_element (this.get_type ().name ());
+                       if (serialized_xml_node == null)
+                               serialized_xml_node = doc.create_element (this.get_type ().name ());
                        foreach (ParamSpec spec in list_serializable_properties ()) {
-                               GLib.message ("Serializing: " + spec.name + 
-                                             " on node: " + serialized_xml_node.node_name);
                                serialize_property (spec);
-                               GLib.message ("Done");
                        }
+                       /* FIXME: Save unknown nodes */
+//                     if (unknown_serializable_property.size >= 1) {
+//                             foreach (DomNode node in unknown_serializable_property.get_values ()) {
+//                                     if (node is Attr) {
+//                                             var att = (Attr) node;
+//                                             serialized_xml_node.set_attribute (att.name, )
+//                                     }
+//                             }
+//                             serialized_xml_node.append_child ()
+//                     }
                        if (serialized_xml_node_value != null)
                                serialized_xml_node.content = serialized_xml_node_value;
                        node.append_child (serialized_xml_node);
                        return serialized_xml_node;
                }
+
+               public virtual DomNode? deserialize (DomNode node) throws DomError
+               {
+                       Document doc;
+                       if (node is Document) {
+                               doc = (Document) node;
+                               return_val_if_fail (doc.document_element != null, null);
+                       }
+                       else
+                               doc = node.owner_document;
+                       if (node is Element)
+                               serialized_xml_node = (Element) node;
+                       else
+                               serialized_xml_node = (Element) doc.document_element;
+                       if (serialized_xml_node.node_name == this.get_type().name() 
+                           || serialized_xml_node.node_name == (this.get_type().name()).down ())
+                       {
+                               foreach (Attr att in serialized_xml_node.attributes.get_values ())
+                               {
+                                       var spec = find_property_spec (att.name);
+                                       if (spec != null) {
+                                               deserialize_property (spec, att);
+                                       }
+                               }
+                               if (serialized_xml_node.content != null)
+                                       serialized_xml_node_value = serialized_xml_node.content;
+                       }
+                       return null;
+               }
                /**
                 * Handles deserializing individual properties.
                 *
@@ -159,6 +195,7 @@ namespace GXml {
                 */
                public virtual bool deserialize_property (GLib.ParamSpec spec,
                                                          GXml.DomNode property_node)
+                                                         throws Error
                {
                        bool ret = false;
                        var prop = find_property_spec (property_node.node_name);
@@ -166,6 +203,14 @@ namespace GXml {
                                unknown_serializable_property.set (property_node.node_name, property_node);
                                return false;
                        }
+                       if (prop.value_type.is_a (typeof (Serializable)))
+                       {
+                               GLib.message (@"$(prop.name): Is Serializable...");
+                               var obj = Object.new  (prop.value_type);
+                               ((Serializable) obj).deserialize (property_node);
+                               set_property (prop.name, obj);
+                               return true;
+                       }
                        Value val = Value (prop.value_type);
                        if (Value.type_transformable (typeof (DomNode), prop.value_type)) {
                                Value tmp = Value (typeof (DomNode));
@@ -218,7 +263,6 @@ namespace GXml {
                                return null;
                        }
                        if (prop.value_type.is_a (typeof (Serializable))) {
-                       GLib.message ("Is a Serializable Property");
                                var v = Value (typeof (Object));
                                get_property (spec.name, ref v);
                                var obj = (Serializable) v.get_object ();
diff --git a/gxml/SerializableObjectModel.vala b/gxml/SerializableObjectModel.vala
new file mode 100644
index 0000000..cc3c7ea
--- /dev/null
+++ b/gxml/SerializableObjectModel.vala
@@ -0,0 +1,66 @@
+/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+/* ObjectModel.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>
+ */
+
+public abstract class GXml.SerializableObjectModel : Object, Serializable
+{
+       /* Serializable interface properties */
+       public GLib.HashTable<string,GLib.ParamSpec> ignored_serializable_properties { get; protected set; }
+       public bool serializable_property_use_nick { get; set; }
+       public GXml.Element serialized_xml_node { get; protected set; }
+       public string serialized_xml_node_value { get; protected set; default=null; }
+       public GLib.HashTable<string,GXml.DomNode> unknown_serializable_property { get; protected set; }
+
+       /* No serializable properties */
+       [Description (blurb="GXml.DomNode contents")]
+       public string @value {
+               get { return serialized_xml_node_value; } 
+               set { serialized_xml_node_value = value; }
+       }
+       public SerializableObjectModel ()
+       {
+               serializable_property_use_nick = true;
+               var pvalue = find_property_spec ("value");
+               ignored_serializable_properties.set ("value", pvalue);
+       }
+       
+       public abstract string to_string ();
+
+       public static bool equals (SerializableObjectModel a, SerializableObjectModel b)
+       {
+               if (b.get_type () == a.get_type ()) {
+                       var alp = ((Serializable)a).list_serializable_properties ();
+                       var blp = ((Serializable)b).list_serializable_properties ();
+                       bool ret = true;
+                       foreach (ParamSpec p in alp) {
+                               var bp = ((Serializable)b).find_property_spec (p.name);
+                               if (bp != null) {
+                                       var apval = ((Serializable)a).get_property_value (p);
+                                       var bpval = ((Serializable)b).get_property_value (bp);
+                                       if ( apval != bpval)
+                                               ret = false;
+                               }
+                       }
+                       return ret;
+               }
+               return false;
+       }
+}
diff --git a/test/SerializableTest.vala b/test/SerializableTest.vala
index 2964cb9..76c625d 100644
--- a/test/SerializableTest.vala
+++ b/test/SerializableTest.vala
@@ -264,25 +264,9 @@ public class SerializableBanana : GLib.Object, GXml.Serializable
        }
 }
 
-class XmlObjectModel : Object, Serializable
+class ObjectModel : SerializableObjectModel
 {
-       /* Serializable interface properties */
-       public GLib.HashTable<string,GLib.ParamSpec> ignored_serializable_properties { get; protected set; }
-       public bool serializable_property_use_nick { get; set; }
-       public GXml.Element serialized_xml_node { get; protected set; }
-       public string serialized_xml_node_value { get; protected set; }
-       public GLib.HashTable<string,GXml.DomNode> unknown_serializable_property { get; protected set; }
-
-       /* No serializable properties */
-       public string @value { get; set; }
-       public XmlObjectModel ()
-       {
-               serializable_property_use_nick = true;
-               var pvalue = find_property_spec ("value");
-               ignored_serializable_properties.set ("value", pvalue);
-       }
-       
-       public string to_string ()
+       public override string to_string ()
        {
                var lp = list_serializable_properties ();
                string ret = this.get_type ().name () +"{Properties:\n";
@@ -291,37 +275,17 @@ class XmlObjectModel : Object, Serializable
                }
                return ret + "}";
        }
-
-       public static bool equals (Object a, Object b)
-              requires ((a is Serializable) && (b is Serializable))
-       {
-               if (b.get_type () == a.get_type ()) {
-                       var alp = ((Serializable)a).list_serializable_properties ();
-                       var blp = ((Serializable)b).list_serializable_properties ();
-                       bool ret = true;
-                       foreach (ParamSpec p in alp) {
-                               var bp = ((Serializable)b).find_property_spec (p.name);
-                               if (bp != null) {
-                                       var apval = ((Serializable)a).get_property_value (p);
-                                       var bpval = ((Serializable)b).get_property_value (bp);
-                                       if ( apval != bpval)
-                                               ret = false;
-                               }
-                       }
-                       return ret;
-               }
-               return false;
-       }
 }
 
-class Laptop : XmlObjectModel
+class Computer : ObjectModel
 {
+       [Description (nick="Manufacturer")]
        public string manufacturer { get; set; }
        public string model { get; set; }
        public int cores { get; set; }
        public float ghz { get; set; }
-       
-       public Laptop ()
+
+       public Computer ()
        {
                manufacturer = "MexicanLaptop, Inc.";
                model = "LQ59678";
@@ -330,6 +294,35 @@ class Laptop : XmlObjectModel
        }
 }
 
+class Manual : ObjectModel
+{
+       public string document { get; set; }
+       public int pages { get; set; }
+
+       public Manual ()
+       {
+               document = "MANUAL DOCUMENTATION";
+               pages = 3;
+               value = "TEXT INTO THE MANUAL DOCUMENT";
+       }
+}
+
+class Package : ObjectModel
+{
+       public Computer computer { get; set; }
+       public Manual manual { get; set; }
+       public string source { get; set; }
+       public string destiny { get; set; }
+
+       public Package ()
+       {
+               computer = new Computer ();
+               manual = new Manual ();
+               source = "Mexico";
+               destiny = "World";
+       }
+}
+
 class SerializableTest : GXmlTest
 {
        public static void add_tests () {
@@ -393,8 +386,8 @@ class SerializableTest : GXmlTest
                                SerializationTest.test_serialization_deserialization (banana, 
"interface_override_properties", (GLib.EqualFunc)SerializableBanana.equals, 
(SerializationTest.StringifyFunc)SerializableBanana.to_string);
                        });
                Test.add_func ("/gxml/serializable/xml_object_model/derived_class", () => {
-                       var lap = new Laptop ();
-                       SerializationTest.test_serialization_deserialization (lap, "derived_class", 
(GLib.EqualFunc)XmlObjectModel.equals, (SerializationTest.StringifyFunc)XmlObjectModel.to_string);
+                       var computer = new Computer ();
+                       SerializationTest.test_serialization_deserialization (computer, "derived_class", 
(GLib.EqualFunc)SerializableObjectModel.equals, (SerializationTest.StringifyFunc)ObjectModel.to_string);
                        });
        }
 }


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