[gxml] GomObject:XParser: Added support for GomProperty



commit cdb83621d162232cf2c77f3aba9361e287b4e74e
Author: Daniel Espinosa <esodan gmail com>
Date:   Sun Nov 6 21:04:38 2016 -0600

    GomObject:XParser: Added support for GomProperty
    
    XParser and GomObject search and serialize DomElement
    attributes from all GomProperty objects in a GomElement

 gxml/GomObject.vala            |   39 ++++++++++++++++++++++++++++-----------
 gxml/GomProperty.vala          |    2 +-
 gxml/XParser.vala              |   29 +++++++++++++++++++++--------
 test/GomSerializationTest.vala |   10 ++++++++--
 4 files changed, 58 insertions(+), 22 deletions(-)
---
diff --git a/gxml/GomObject.vala b/gxml/GomObject.vala
index b81cd83..847070f 100644
--- a/gxml/GomObject.vala
+++ b/gxml/GomObject.vala
@@ -55,16 +55,33 @@ public interface GXml.GomObject : GLib.Object,
     return l;
   }
   /**
+   * Returns a list with all object's {@link GomProperty} property names.
+   */
+  public virtual List<ParamSpec> get_object_properties_list () {
+    var l = new List<ParamSpec> ();
+    foreach (ParamSpec spec in this.get_class ().list_properties ()) {
+      if (spec.value_type.is_a (typeof (GomProperty))) {
+          GLib.message ("GomProperty Name: "+spec.name);
+        l.append (spec);
+      }
+    }
+    return l;
+  }
+  /**
    * Returns property's name based on given nick. This function is
    * case insensitive.
    */
   public virtual string? find_property_name (string nick) {
     foreach (ParamSpec spec in this.get_class ().list_properties ()) {
-      if ("::" in spec.get_nick ()) {
-        string name = spec.get_nick ().replace ("::","");
-        if (name.down () == nick.down ()) {
-          GLib.message ("Name: "+spec.name+ " Nick: "+spec.get_nick ());
-          return spec.name;
+      if (spec.value_type.is_a (typeof (GomProperty))) {
+        return spec.name;
+      } else {
+        if ("::" in spec.get_nick ()) {
+          string name = spec.get_nick ().replace ("::","");
+          if (name.down () == nick.down ()) {
+            GLib.message ("Name: "+spec.name+ " Nick: "+spec.get_nick ());
+            return spec.name;
+          }
         }
       }
     }
@@ -110,10 +127,10 @@ public interface GXml.GomObject : GLib.Object,
       GLib.message ("Found attribute: "+prop.name);
       var v = Value(prop.value_type);
       get_property (prop.name, ref v);
-      if (prop.value_type == typeof(SerializableProperty)) {
-        SerializableProperty so = (Object) v as SerializableProperty;
+      if (prop.value_type == typeof(GomProperty)) {
+        GomProperty so = (Object) v as GomProperty;
         if (so == null) return null;
-        return so.get_serializable_property_value ();
+        return so.value;
       }
       if (prop.value_type.is_a (typeof (string))) {
         return (string) v;
@@ -159,11 +176,11 @@ public interface GXml.GomObject : GLib.Object,
     var prop = get_class ().find_property (pname);
     if (prop != null) {
       var v = Value (prop.value_type);
-      if (prop.value_type == typeof(SerializableProperty)) {
+      if (prop.value_type == typeof(GomProperty)) {
         get_property (prop.name, ref v);
-        SerializableProperty so = (Object) v as SerializableProperty;
+        GomProperty so = (Object) v as GomProperty;
         if (so == null) return false;
-        so.set_serializable_property_value (val);
+        so.value = val;
         return true;
       }
       if (prop.value_type.is_a (typeof (string))) {
diff --git a/gxml/GomProperty.vala b/gxml/GomProperty.vala
index 724a898..92a43fe 100644
--- a/gxml/GomProperty.vala
+++ b/gxml/GomProperty.vala
@@ -58,7 +58,7 @@ public class GXml.GomDouble : Object, GomProperty {
 }
 
 public class GXml.GomFloat : GomDouble {
-  public float get_double () { return (float) _value; }
+  public float get_float () { return (float) _value; }
 }
 
 
diff --git a/gxml/XParser.vala b/gxml/XParser.vala
index 086cbce..da48475 100644
--- a/gxml/XParser.vala
+++ b/gxml/XParser.vala
@@ -404,14 +404,7 @@ public class GXml.XParser : Object, GXml.Parser {
         tw.start_element (node.node_name);
     GLib.message ("Write down properties: size:"+(node as DomElement).attributes.size.to_string ());
 
-    foreach (string ak in (node as DomElement).attributes.keys) {
-      string v = ((node as DomElement).attributes as HashMap<string,string>).get (ak);
-      size += tw.write_attribute (ak, v);
-      size += tw.end_attribute ();
-      if (size > 1500)
-        tw.flush ();
-    }
-    // GomObject serialisation
+    // GomObject serialization
     var lp = (node as GomObject).get_properties_list ();
     foreach (string pk in lp) {
       string v = (node as GomObject).get_attribute (pk);
@@ -421,6 +414,26 @@ public class GXml.XParser : Object, GXml.Parser {
       if (size > 1500)
         tw.flush ();
     }
+    // GomProperty serialization
+    var lps = (node as GomObject).get_object_properties_list ();
+    foreach (ParamSpec pspec in lps) {
+      Value v = Value (pspec.value_type);
+      node.get_property (pspec.name, ref v);
+      GomProperty gp = v.get_object () as GomProperty;
+      if (gp == null) continue;
+      size += tw.write_attribute (gp.attribute_name, gp.value);
+      size += tw.end_attribute ();
+      if (size > 1500)
+        tw.flush ();
+    }
+    // DomElement attributes
+    foreach (string ak in (node as DomElement).attributes.keys) {
+      string v = ((node as DomElement).attributes as HashMap<string,string>).get (ak);
+      size += tw.write_attribute (ak, v);
+      size += tw.end_attribute ();
+      if (size > 1500)
+        tw.flush ();
+    }
   }
   // Non Elements
 #if DEBUG
diff --git a/test/GomSerializationTest.vala b/test/GomSerializationTest.vala
index a1398ea..3a6fa09 100644
--- a/test/GomSerializationTest.vala
+++ b/test/GomSerializationTest.vala
@@ -286,11 +286,17 @@ class GomSerializationTest : GXmlTest  {
       assert ((bs.books.get("Title3") as Book).name == "Title3");
     });
     Test.add_func ("/gxml/gom-serialization/write/gom-property", () => {
-      var c = new Motor ();
-      string s = c.to_string ();
+      var m = new Motor ();
+      string s = m.to_string ();
       assert (s != null);
       GLib.message ("DOC:"+s);
       assert ("<Motor/>" in s);
+      m.is_on = new Motor.On ();
+      s = m.to_string ();
+      assert (s != null);
+      GLib.message ("DOC:"+s);
+      assert ("<Motor On=\"false\"/>" in s);
+
     });
     Test.add_func ("/gxml/gom-serialization/read/properties", () => {
       var b = new Book ();


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