[gxml] GOM: Using property nick to find serializables



commit 887454f1fc36991b10229562aae846f14a3ea971
Author: Daniel Espinosa <esodan gmail com>
Date:   Thu Nov 3 16:50:48 2016 -0600

    GOM: Using property nick to find serializables
    
    If you define a property with a prefix "::" it
    will be serialized as a XML element attribute.
    
    Property's nick are used as attribute's name
    take care to use descriptive names using, may be,
    camel case names.

 gxml/GomObject.vala            |   10 ++++++++++
 gxml/XParser.vala              |   11 +++++++++++
 test/GomSerializationTest.vala |    7 ++++++-
 3 files changed, 27 insertions(+), 1 deletions(-)
---
diff --git a/gxml/GomObject.vala b/gxml/GomObject.vala
index 5c97c33..7a4acf5 100644
--- a/gxml/GomObject.vala
+++ b/gxml/GomObject.vala
@@ -39,6 +39,16 @@ public interface GXml.GomObject : GLib.Object,
    * attribute use property's nick name as declared in {@link GLib.ParamSpec}
    */
   public virtual bool use_nick_name () { return true; }
+  public virtual HashTable<string,string> get_properties_map () {
+    var l = new HashTable<string,string> (str_hash, str_equal);
+    foreach (ParamSpec spec in this.get_class ().list_properties ()) {
+      if ("::" in spec.get_nick ()) {
+        GLib.message ("Name: "+spec.name+ " Nick: "+spec.get_nick ());
+        l.insert (spec.name, spec.get_nick ());
+      }
+    }
+    return l;
+  }
   /**
    * Search for properties in objects, it should be
    * an {@link GLib.Object}'s property. If found a
diff --git a/gxml/XParser.vala b/gxml/XParser.vala
index fc50c41..a6609e7 100644
--- a/gxml/XParser.vala
+++ b/gxml/XParser.vala
@@ -364,6 +364,17 @@ public class GXml.XParser : Object, GXml.Parser {
       if (size > 1500)
         tw.flush ();
     }
+    // GomObject serialisation
+    var opm = (node as GomObject).get_properties_map ();
+    foreach (string pk in opm.get_keys ()) {
+      string v = (node as GomObject).get_attribute (pk);
+      if (v == null) continue;
+      string pn = opm.lookup (pk);
+      size += tw.write_attribute (pn.replace ("::",""), 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 450a3aa..39b24fa 100644
--- a/test/GomSerializationTest.vala
+++ b/test/GomSerializationTest.vala
@@ -24,8 +24,9 @@ using GXml;
 
 class GomSerializationTest : GXmlTest  {
   public class Book : GomElement {
+    [Description (nick="::Name")]
     public string name { get; set; }
-    public Book () {
+    construct {
       _local_name = "Book";
     }
     public string to_string () { return (_document as GomDocument).to_string (); }
@@ -40,6 +41,10 @@ class GomSerializationTest : GXmlTest  {
       assert (b.get_attribute ("name") == "My Book");
       s = b.to_string ();
       GLib.message ("DOC:"+s);
+      foreach (ParamSpec spec in b.get_class ().list_properties ()) {
+        if ("::" in spec.get_nick ())
+          GLib.message ("Name: "+spec.name+ " Nick: "+spec.get_nick ());
+      }
     });
   }
 }


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