[gxml/serialization] mplemented serialize non-automatic serializable properties * Implementors must attach a handler to s



commit 9da1ce02046de4c0086a8fc66bf902fc4aaed02e
Author: Daniel Espinosa <esodan gmail com>
Date:   Thu Jul 25 18:30:29 2013 -0500

    mplemented serialize non-automatic serializable properties
    * Implementors must attach a handler to serialize_unknown_property
      event in order to serialize any property type non handled
      automatically like GLib.Array, GLib.HashTable or others
    * Added serialized_node_name property to allow implementator to
      override default node name; default is GObject Type's name
    * Removed warning messsages

 gxml/Serializable.vala            |   63 ++++++++++++++++++++++++++++---------
 gxml/SerializableObjectModel.vala |    4 ++-
 test/SerializableTest.vala        |   15 ++++++---
 test/SerializationTest.vala       |    4 +-
 4 files changed, 63 insertions(+), 23 deletions(-)
---
diff --git a/gxml/Serializable.vala b/gxml/Serializable.vala
index 05ce8b6..d955013 100644
--- a/gxml/Serializable.vala
+++ b/gxml/Serializable.vala
@@ -71,6 +71,15 @@ namespace GXml {
         * For an example, look in tests/XmlSerializableTest
         */
        public interface Serializable : GLib.Object {
+               /**
+                * Defines the way to set DomNode name.
+                *
+                * By default is set to object's type's name lowercase.
+                *
+                * This property must be ignored on serialisation.
+                */
+               public abstract string serializable_node_name { get; protected set; }
+
                public abstract bool serializable_property_use_nick { get; set; }
                /**
                 * Store all properties to be ignored on serialization.
@@ -107,7 +116,7 @@ namespace GXml {
                        else
                                doc = node.owner_document;
                        GLib.message ("Serialing on ..." + node.node_name);
-                       var element = doc.create_element (this.get_type().name());
+                       var element = doc.create_element (serializable_node_name);
                        node.append_child (element);
                        if (serialized_xml_node_value != null)
                                element.content = serialized_xml_node_value;
@@ -154,6 +163,7 @@ namespace GXml {
                                var obj = (Serializable) v.get_object ();
                                return obj.serialize (element);
                        }
+                       DomNode node = null;
                        Value oval = Value (prop.value_type);
                        get_property (prop.name, ref oval);
                        string val = "";
@@ -162,16 +172,18 @@ namespace GXml {
                                Value rval = Value (typeof (string));
                                oval.transform (ref rval);
                                val = rval.dup_string ();
+                               string attr_name = prop.name.down ();
+                               var attr = element.get_attribute_node (attr_name);
+                               if (attr == null) {
+                                       GLib.message (@"New Attr to add... $(attr_name)");
+                                       element.set_attribute (attr_name, val);
+                               }
+                               else
+                                       attr.value = val;
+                               return (DomNode) attr;
                        }
-                       string attr_name = prop.name.down ();
-                       var attr = element.get_attribute_node (attr_name);
-                       if (attr == null) {
-                               GLib.message (@"New Attr to add... $(attr_name)");
-                               element.set_attribute (attr_name, val);
-                       }
-                       else
-                               attr.value = val;
-                       return (DomNode) attr;
+                       this.serialize_unknown_property (element, prop, out node);
+                       return node;
                }
 
                /**
@@ -191,7 +203,7 @@ namespace GXml {
                        else
                                doc = node.owner_document;
                        var element = (Element) doc.document_element;
-                       return_val_if_fail (element.node_name.down () == get_type().name().down(), null);
+                       return_val_if_fail (element.node_name.down () == serializable_node_name, null);
                        foreach (Attr attr in element.attributes.get_values ())
                        {
                                deserialize_property (attr);
@@ -272,10 +284,29 @@ namespace GXml {
                                        set_property (prop.name, val);
                                        return ret;
                                }
+                               // Attribute can't be deseralized with standard methods. Up to the 
implementor.
+                               this.deserialize_unknown_property (property_node, prop);
                        }
                        return false;
                }
 
+               /**
+                * Signal to serialize unknown properties.
+                * 
+                * @node a { link GXml.DomNode} to add attribute or child nodes to
+                * @prop a { link GLib.ParamSpec} describing attribute to serialize
+                * @attribute set to the { link GXml.Attr} representing this attribute
+                */
+               public signal void serialize_unknown_property (DomNode element, ParamSpec prop, out DomNode 
node);
+
+               /**
+                * Signal to deserialize array properties
+                * 
+                * @node a { link GXml.DomNode} to get attribute from
+                * @prop a { link GLib.ParamSpec} describing attribute to deserialize
+                */
+               public signal void deserialize_unknown_property (DomNode node, ParamSpec prop);
+
                /*
                 * Handles finding the { link GLib.ParamSpec} for a given property.
                 *
@@ -324,13 +355,15 @@ namespace GXml {
                        if (ignored_serializable_properties == null) {
                                ignored_serializable_properties = new HashTable<string,ParamSpec> (str_hash, 
str_equal);
                                ignored_serializable_properties.set ("ignored-serializable-properties",
-                                                                                                             
                                                                  get_class 
().find_property("ignored-serializable-properties"));
+                                                                    get_class 
().find_property("ignored-serializable-properties"));
                                ignored_serializable_properties.set ("unknown-serializable-property",
-                                                                                                             
                                                                  get_class 
().find_property("unknown-serializable-property"));
+                                                                    get_class 
().find_property("unknown-serializable-property"));
                                ignored_serializable_properties.set ("serialized-xml-node-value",
-                                                                                                             
                                                                  get_class 
().find_property("serialized-xml-node-value"));
+                                                                    get_class 
().find_property("serialized-xml-node-value"));
                                ignored_serializable_properties.set ("serializable-property-use-nick",
-                                                                                                             
                                                                  get_class 
().find_property("serializable-property-use-nick"));
+                                                                    get_class 
().find_property("serializable-property-use-nick"));
+                               ignored_serializable_properties.set ("serializable-node-name",
+                                                                    get_class 
().find_property("serializable-node-name"));
                        }
                        if (unknown_serializable_property == null) {
                                unknown_serializable_property = new HashTable<string,GXml.DomNode> (str_hash, 
str_equal);
diff --git a/gxml/SerializableObjectModel.vala b/gxml/SerializableObjectModel.vala
index f4cbdc6..a24f974 100644
--- a/gxml/SerializableObjectModel.vala
+++ b/gxml/SerializableObjectModel.vala
@@ -27,13 +27,15 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
        public bool serializable_property_use_nick { get; set; }
        public string? serialized_xml_node_value { get; protected set; default=null; }
        public GLib.HashTable<string,GXml.DomNode> unknown_serializable_property { get; protected set; }
+       public string serializable_node_name { get; protected set; }
 
        public SerializableObjectModel ()
        {
                serializable_property_use_nick = true;
                serialized_xml_node_value = null;
+               serializable_node_name = get_type().name().down();
        }
-       
+
        public abstract string to_string ();
 
        public static bool equals (SerializableObjectModel a, SerializableObjectModel b)
diff --git a/test/SerializableTest.vala b/test/SerializableTest.vala
index 32e87ff..83432df 100644
--- a/test/SerializableTest.vala
+++ b/test/SerializableTest.vala
@@ -32,6 +32,7 @@ public class SerializableTomato : GLib.Object, GXml.Serializable
        public bool serializable_property_use_nick { get; set; }
        public string? serialized_xml_node_value { get; protected set; }
        public GLib.HashTable<string,GXml.DomNode> unknown_serializable_property { get; private set; }
+       public string serializable_node_name { get; protected set; }
 
        public int weight;
        private int age { get; set; }
@@ -44,6 +45,7 @@ public class SerializableTomato : GLib.Object, GXml.Serializable
                this.age = age;
                this.height = height;
                this.description = description;
+               serializable_node_name = "tomato";
        }
 
        public string to_string ()
@@ -69,6 +71,7 @@ public class SerializableCapsicum : GLib.Object, GXml.Serializable
        public bool serializable_property_use_nick { get; set; }
        public string? serialized_xml_node_value { get; protected set; }
        public GLib.HashTable<string,GXml.DomNode> unknown_serializable_property { get; private set; }
+       public string serializable_node_name { get; protected set; }
 
        public int weight;
        private int age { get; set; }
@@ -91,10 +94,12 @@ public class SerializableCapsicum : GLib.Object, GXml.Serializable
                this.age = age;
                this.height = height;
                this.ratings = ratings;
+               serializable_node_name = "capsicum";
        }
 
        public bool deserialize_property (GXml.DomNode property_node)
-                                    throws Error
+                                    throws SerializableError,
+                                                      DomError
        {
                GLib.Value outvalue = GLib.Value (typeof (int));
 
@@ -123,7 +128,6 @@ public class SerializableCapsicum : GLib.Object, GXml.Serializable
                                            throws DomError
        {
                GXml.Document doc = element.owner_document;
-               GXml.Element c_prop;
                GXml.Element rating;
 
                switch (spec.name) {
@@ -160,6 +164,7 @@ public class SerializableBanana : GLib.Object, GXml.Serializable
        public bool serializable_property_use_nick { get; set; }
        public string? serialized_xml_node_value { get; protected set; }
        public GLib.HashTable<string,GXml.DomNode> unknown_serializable_property { get; private set; }
+       public string serializable_node_name { get; protected set; }
 
        private int private_field;
        public int public_field;
@@ -171,7 +176,7 @@ public class SerializableBanana : GLib.Object, GXml.Serializable
                this.public_field = public_field;
                this.private_property = private_property;
                this.public_property = public_property;
-
+               serializable_node_name = "banana";
        }
 
        public string to_string () {
@@ -362,7 +367,7 @@ class SerializableTest : GXmlTest
                                capsicum = new SerializableCapsicum (2, 3, 5, ratings);
                                try {
                                        node = Serialization.serialize_object (capsicum);
-                               } catch (GXml.SerializationError e) {
+                               } catch (Error e) {
                                        Test.message ("%s", e.message);
                                        assert_not_reached ();
                                }
@@ -378,7 +383,7 @@ class SerializableTest : GXmlTest
 
                                        try {
                                                capsicum_new = 
(SerializableCapsicum)Serialization.deserialize_object (node);
-                                       } catch (GXml.SerializationError e) {
+                                       } catch (Error e) {
                                                Test.message ("%s", e.message);
                                                assert_not_reached ();
                                        }
diff --git a/test/SerializationTest.vala b/test/SerializationTest.vala
index dddcdbc..56f9fa0 100644
--- a/test/SerializationTest.vala
+++ b/test/SerializationTest.vala
@@ -444,7 +444,7 @@ class SerializationTest : GXmlTest {
                                        xml = Serialization.serialize_object (obj);
 
                                        restored = 
(ComplexDuplicateProperties)Serialization.deserialize_object (xml);
-                               } catch (GXml.SerializationError e) {
+                               } catch (Error e) {
                                        Test.message ("%s", e.message);
                                        assert_not_reached ();
                                }
@@ -611,7 +611,7 @@ class SerializationTest : GXmlTest {
                                        } catch (GXml.SerializationError e) {
                                                Test.message ("%s", e.message);
                                                assert_not_reached ();
-                                       } catch (GXml.DomError e) {
+                                       } catch (Error e) {
                                                Test.message ("%s", e.message);
                                                assert_not_reached ();
                                        }


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