[gxml/serialization] SerializableObjectModel.(de)serialize can be overrided. Added Node.add_namespace_attr()



commit ca50740c08a61177908d4799929a9c0db83987eb
Author: Daniel Espinosa <esodan gmail com>
Date:   Thu Oct 31 10:32:20 2013 -0600

    SerializableObjectModel.(de)serialize can be overrided. Added Node.add_namespace_attr()
    
    * Added test case to override serialize, by adding default implementations
      to be called by overriders
    * Added default implementations for (de)serialize(_property)
    * Added new Node.add_namespace_attr and BackedNode implementation

 gxml/BackedNode.vala                  |   12 +++++++
 gxml/NamespaceAttr.vala               |   10 +++---
 gxml/Node.vala                        |    7 ++++
 gxml/SerializableObjectModel.vala     |   29 +++++++++++++++--
 test/ElementTest.vala                 |    2 +-
 test/SerializableObjectModelTest.vala |   55 +++++++++++++++++++++++++++++++++
 6 files changed, 105 insertions(+), 10 deletions(-)
---
diff --git a/gxml/BackedNode.vala b/gxml/BackedNode.vala
index 07f885d..95e5c7f 100644
--- a/gxml/BackedNode.vala
+++ b/gxml/BackedNode.vala
@@ -83,6 +83,18 @@ namespace GXml {
                        }
                }
                /**
+                * Add a new namespace to this { link GXml.Node}
+                */
+               public override NamespaceAttr? add_namespace_attr (string uri, string prefix)
+               {
+                       //stdout.printf ("BackedNode: Before add new Namespace\n");
+                       var ns = this.node->new_ns (uri, prefix);
+                       if (ns == null)
+                               return null;
+                       else
+                               return new NamespaceAttr (ns, owner_document);
+               }
+               /**
                 * { inheritDoc}
                 */
                public override string? prefix {
diff --git a/gxml/NamespaceAttr.vala b/gxml/NamespaceAttr.vala
index 79b9f5a..ee9a508 100644
--- a/gxml/NamespaceAttr.vala
+++ b/gxml/NamespaceAttr.vala
@@ -32,13 +32,13 @@ namespace GXml {
         */
        public class NamespaceAttr : Node {
                /** Private properties */
-               private Xml.Ns *node;
+               private Xml.Ns *ns;
 
                /** Constructors */
-               internal NamespaceAttr (Xml.Ns *node, Document doc) {
+               internal NamespaceAttr (Xml.Ns *ns, Document doc) {
                        // TODO: wish valac would warn against using this. before calling base()
                        base (NodeType.ATTRIBUTE, doc); // TODO: want something other than ATTRIBUTE?
-                       this.node = node;
+                       this.ns = ns;
                }
 
                /* Public properties (Node general) */
@@ -106,7 +106,7 @@ namespace GXml {
                 */
                public override string node_name {
                        get {
-                               return this.node->prefix;
+                               return this.ns->prefix;
                        }
                        internal set {
                        }
@@ -116,7 +116,7 @@ namespace GXml {
                 */
                public override string? node_value {
                        get {
-                               return this.node->href;
+                               return this.ns->href;
                        }
                        internal set {
                        }
diff --git a/gxml/Node.vala b/gxml/Node.vala
index 711eef1..7f49c72 100644
--- a/gxml/Node.vala
+++ b/gxml/Node.vala
@@ -127,6 +127,13 @@ namespace GXml {
                        internal set {
                        }
                }
+               /**
+                * Add a new namespace to this { link GXml.Node}
+                */
+               public virtual NamespaceAttr? add_namespace_attr (string uri, string prefix)
+               {
+                       return null;
+               }
 
                /**
                 * Stores the namespace prefix for the node. This
diff --git a/gxml/SerializableObjectModel.vala b/gxml/SerializableObjectModel.vala
index 37015a2..32dd54b 100644
--- a/gxml/SerializableObjectModel.vala
+++ b/gxml/SerializableObjectModel.vala
@@ -72,7 +72,12 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
                return false;
        }
 
-       public Node? serialize (Node node) throws GLib.Error
+       public virtual Node? serialize (Node node) throws GLib.Error
+       {
+               return default_serialize (node);
+       }
+
+       public Node? default_serialize (Node node) throws GLib.Error
        {
                Document doc;
                if (node is Document)
@@ -92,8 +97,14 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
                //GLib.message ("Added a new top node: " + element.node_name);
                return element;
        }
-       
-       public GXml.Node? serialize_property (Element element,
+
+       public virtual GXml.Node? serialize_property (Element element,
+                                             GLib.ParamSpec prop)
+                                             throws GLib.Error
+       {
+               return default_serialize_property (element, prop);
+       }
+       public GXml.Node? default_serialize_property (Element element,
                                              GLib.ParamSpec prop)
                                              throws GLib.Error
        {
@@ -137,10 +148,15 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
                        attr.value = val;
                return (Node) attr;
        }
-       
+
        public virtual Node? deserialize (Node node)
                                          throws GLib.Error
        {
+               return default_deserialize (node);
+       }
+       public Node? default_deserialize (Node node)
+                                         throws GLib.Error
+       {
                Document doc;
                if (node is Document) {
                        doc = (Document) node;
@@ -176,6 +192,11 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
        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
+       {
                bool ret = false;
                var prop = find_property_spec (property_node.node_name);
                if (prop == null) {
diff --git a/test/ElementTest.vala b/test/ElementTest.vala
index 588090d..a1b27b7 100644
--- a/test/ElementTest.vala
+++ b/test/ElementTest.vala
@@ -384,4 +384,4 @@ class ElementTest : GXmlTest  {
                                // TODO: want to test with format on and off
                        });
        }
-}
\ No newline at end of file
+}
diff --git a/test/SerializableObjectModelTest.vala b/test/SerializableObjectModelTest.vala
index 6bb1da8..82e2d91 100644
--- a/test/SerializableObjectModelTest.vala
+++ b/test/SerializableObjectModelTest.vala
@@ -205,6 +205,25 @@ public class Cpu : ObjectModel
        }
 }
 
+class Configuration : ObjectModel
+{
+       public string device { get; set; }
+
+       public Configuration ()
+       {
+               serializable_property_use_nick = true;
+               serializable_node_name = "Configuration";
+       }
+       public override GXml.Node? serialize (GXml.Node node) throws GLib.Error
+       {
+               //stdout.printf ("CONFIGURATION: Before serialize\n");
+               var n = default_serialize (node);
+               //stdout.printf ("CONFIGURATION: After serialize\n");
+               n.add_namespace_attr ("http://www.gnome.org/gxml/0.4";, "om");
+               //stdout.printf (@"CONFIGURATION: Created Node: $node\n");
+               return n;
+       }
+}
 
 class SerializableObjectModelTest : GXmlTest
 {
@@ -612,6 +631,42 @@ class SerializableObjectModelTest : GXmlTest
                                assert_not_reached ();
                        }
                });
+               Test.add_func ("/gxml/serializable/object_model/override_serialize",
+               () => {
+                       var doc = new Document ();
+                       var configuration = new Configuration ();
+                       configuration.device = "Controller";
+                       try {
+                               configuration.serialize (doc);
+                               //stdout.printf (@"DOC: $doc");
+                               if (doc.document_element == null) {
+                                       stdout.printf ("DOC: No root element");
+                                       assert_not_reached ();
+                               }
+                               Element element = doc.document_element;
+                               if (element.node_name != "Configuration") {
+                                       stdout.printf (@"CONFIGURATION: Bad node name: $(element.node_name)");
+                                       assert_not_reached ();
+                               }
+                               bool found = false;
+                               foreach (GXml.Node n in element.namespace_definitions)
+                               {
+                                       if (n.node_name == "om" && n.node_value == 
"http://www.gnome.org/gxml/0.4";)
+                                               found = true;
+                               }
+                               if (!found) {
+                                       stdout.printf (@"CONFIGURATION: No namespace found:");
+                                       foreach (GXml.Node n in element.namespace_definitions) {
+                                               stdout.printf (@"CONFIGURATION: Defined Namespace: 
$(n.node_name):$(n.node_value)");
+                                       }
+                                       assert_not_reached ();
+                               }
+                       }
+                       catch (GLib.Error e) {
+                               stdout.printf (@"Error: $(e.message)");
+                               assert_not_reached ();
+                       }
+               });
        }
        static void serialize_manual_check (Element element, Manual manual)
        {


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