[gxml] Implemented set_namespace to SerializableObjectModel



commit d3bb27ec1254d5c881e5844f7783ac9b752df5b5
Author: Daniel Espinosa <esodan gmail com>
Date:   Fri Feb 6 12:02:58 2015 -0600

    Implemented set_namespace to SerializableObjectModel

 gxml/Serializable.vala                |    2 ++
 gxml/SerializableObjectModel.vala     |    8 ++++++--
 test/SerializableObjectModelTest.vala |   23 ++++++++++++++++++++++-
 test/SerializableTest.vala            |    1 -
 4 files changed, 30 insertions(+), 4 deletions(-)
---
diff --git a/gxml/Serializable.vala b/gxml/Serializable.vala
index f14d728..4a66da4 100644
--- a/gxml/Serializable.vala
+++ b/gxml/Serializable.vala
@@ -106,6 +106,8 @@ namespace GXml {
      * { link GXml.Element} namespace should be added before to serialize any
      * property.
      *
+     * This property should have a format "prefix|url".
+     *
      * This property is ignored on serialisation.
      */
     public abstract string? serialize_set_namespace { get; set; default = null; }
diff --git a/gxml/SerializableObjectModel.vala b/gxml/SerializableObjectModel.vala
index 338bf76..c7f2e59 100644
--- a/gxml/SerializableObjectModel.vala
+++ b/gxml/SerializableObjectModel.vala
@@ -111,6 +111,12 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
     else
       doc = node.owner_document;
     var element = doc.create_element (node_name ());
+    node.append_child (element);
+    if (serialize_set_namespace != null) {
+      string[] str = serialize_set_namespace.split ("|", 2);
+      doc.document_element.add_namespace_attr (str[1], str[0]);
+      element.set_namespace (str[1], str[0]);
+    }
     foreach (ParamSpec spec in list_serializable_properties ()) {
       serialize_property (element, spec);
     }
@@ -144,8 +150,6 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
 #endif
       element.append_child (tn);
     }
-
-    node.append_child (element);
     return element;
   }
 
diff --git a/test/SerializableObjectModelTest.vala b/test/SerializableObjectModelTest.vala
index 90c82eb..fe5db06 100644
--- a/test/SerializableObjectModelTest.vala
+++ b/test/SerializableObjectModelTest.vala
@@ -273,6 +273,20 @@ class UnknownAttribute : ObjectModel
   public override bool get_enable_unknown_serializable_property () { return true; }
 }
 
+public class NameSpace : SerializableObjectModel
+{
+  public NameSpace ()
+  {
+    serialize_set_namespace = "gxml|http://www.gnome.org/GXml";;
+  }
+  public override string to_string ()
+  {
+    if (serialize_set_namespace != null)
+      return serialize_set_namespace;
+    return "";
+  }
+}
+
 class SerializableObjectModelTest : GXmlTest
 {
   public static void add_tests ()
@@ -961,6 +975,13 @@ class SerializableObjectModelTest : GXmlTest
                      }
                      catch (GXml.SerializableError e) { Test.message ("Error thrown for invalid string to 
guint"); }
                    });
+    
+    Test.add_func ("/gxml/serializable/object_model/set_namespace", () => {
+      var ns = new NameSpace ();
+      var doc = new Document ();
+      ns.serialize (doc);
+      assert (doc.document_element.to_string () == "<gxml:namespace 
xmlns:gxml=\"http://www.gnome.org/GXml\"/>");
+    });
   }
   static void serialize_manual_check (Element element, Manual manual)
   {
@@ -981,4 +1002,4 @@ class SerializableObjectModelTest : GXmlTest
       assert_not_reached ();
     }
   }
-}
\ No newline at end of file
+}
diff --git a/test/SerializableTest.vala b/test/SerializableTest.vala
index 7762604..d694e59 100644
--- a/test/SerializableTest.vala
+++ b/test/SerializableTest.vala
@@ -231,7 +231,6 @@ class SerializableTest : GXmlTest {
                                SerializableTomato tomato = new SerializableTomato (0, 0, 12, "cats");
                                var doc = new Document ();
                                tomato.serialize (doc);
-                               GLib.message (doc.to_string ());
                                SerializableTomato tomato2 = new SerializableTomato (1,1,4,"dogs");
                                tomato2.deserialize (doc);
                                assert (tomato.weight != tomato2.weight);


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