[gxml] Added GomObject.create_intance_property()



commit 9f4e23000e76a61feab146f4c7f1c63d3e8d8c71
Author: Daniel Espinosa <esodan gmail com>
Date:   Thu Feb 2 15:02:46 2017 -0600

    Added GomObject.create_intance_property()
    
    Along with new GomObject.find_object_property_name()
    crate_instance_property() is a convenient method
    to correctly initialize and set parent node for
    object's properties.

 gxml/GomElement.vala           |    7 ++++
 gxml/GomNode.vala              |    1 +
 gxml/GomObject.vala            |   68 ++++++++++++++++++++++++++++++++++-----
 test/GomSerializationTest.vala |    8 ++---
 4 files changed, 70 insertions(+), 14 deletions(-)
---
diff --git a/gxml/GomElement.vala b/gxml/GomElement.vala
index a000e29..9eb52f6 100644
--- a/gxml/GomElement.vala
+++ b/gxml/GomElement.vala
@@ -235,6 +235,13 @@ public class GXml.GomElement : GomNode,
    * using given local name. If {@link GomElement.initialize_with_namespace}
    * has been called in any base class, this method just change elment node's name
    * and keeps previous namespace and prefix.
+   *
+   * No {@link DomDocument} is set by default, if this is a top level element in a
+   * document, you can call {@link DomNode.owner_document} to set one if not set
+   * already.
+   *
+   * Any instance properties of type {@link GomElement} or {@link GomCollection}
+   * should be initialized using {@link GomObject.create_instance_property}
    */
   public void initialize (string local_name) {
     _local_name = local_name;
diff --git a/gxml/GomNode.vala b/gxml/GomNode.vala
index 8810bf8..6808491 100644
--- a/gxml/GomNode.vala
+++ b/gxml/GomNode.vala
@@ -256,6 +256,7 @@ public class GXml.GomNode : Object,
         }
       }
     }
+    _document = node.owner_document;
     _parent = node;
   }
 
diff --git a/gxml/GomObject.vala b/gxml/GomObject.vala
index fcd0835..daba9f6 100644
--- a/gxml/GomObject.vala
+++ b/gxml/GomObject.vala
@@ -35,12 +35,6 @@ public interface GXml.GomObject : GLib.Object,
                                   DomNode,
                                   DomElement {
   /**
-   * Controls if property name to be used when serialize to XML node
-   * attribute use property's nick name as declared in {@link GLib.ParamSpec}
-   */
-  public virtual bool use_nick_name () { return true; }
-
-  /**
    * Returns a list with all properties' nick with "::" prefix. Nick name,
    * with "::" prefix will be used on serialization to an attribute's name.
    */
@@ -57,7 +51,7 @@ public interface GXml.GomObject : GLib.Object,
     return l;
   }
   /**
-   * Returns property's name based on given nick. This function is
+   * Returns property's {@link ParamSpec} based on given nick. This function is
    * case insensitive.
    */
   public virtual ParamSpec? find_property_name (string pname) {
@@ -80,6 +74,25 @@ public interface GXml.GomObject : GLib.Object,
     return null;
   }
   /**
+   * Returns a {@link GomObject} or a {@link GomCollection} property's
+   * {@link ParamSpec} based on given name. This method is
+   * case insensitive.
+   */
+  public virtual ParamSpec? find_object_property_name (string pname) {
+    foreach (ParamSpec spec in this.get_class ().list_properties ()) {
+      if (spec.name.down () == pname.down ()) {
+        if (spec.value_type.is_a (typeof (GomObject))
+            || spec.value_type.is_a (typeof (GomCollection))) {
+#if DEBUG
+          GLib.message ("Found Property: "+pname);
+#endif
+          return spec;
+        }
+      }
+    }
+    return null;
+  }
+  /**
    * Returns a list of names for all {@link DomElement}
    * present as object's properties.
    */
@@ -274,8 +287,8 @@ public interface GXml.GomObject : GLib.Object,
     return null;
   }
   /**
-   * Search for a property and set it to null if possible, if value can't
-   * be removed, returns without change.
+   * Search for a property and set it to null if possible returning true,
+   * if value can't be removed or located, returns false without change.
    */
   public virtual bool remove_attribute (string name) {
     var prop = get_class ().find_property (name);
@@ -293,4 +306,41 @@ public interface GXml.GomObject : GLib.Object,
     }
     return false;
   }
+  /**
+   * Convenient method to create an instance of given property's
+   * name and initialize according to have same {@link DomNode.owner_document}
+   * and set its {@link DomNode.parent_node} to this.
+   * If property is a {@link GomCollection} it is initialize to use
+   * this as its {@link GomCollection.element}.
+   *
+   * Instance is set ot object's property.
+   *
+   * Property should be a {@link GomElement} or {@link GomCollection}
+   *
+   * While an object could be created and set to a Object's property, it
+   * is not correctly initialized by default. This method helps in the process.
+   *
+   * If Object's property has been set, this method overwrite it.
+   *
+   * Returns: true if property has been set and initialized, false otherwise.
+   */
+   public bool create_instance_property (string name) {
+      var prop = find_object_property_name (name);
+      if (prop == null) return false;
+      Value v = Value (prop.value_type);
+      Object obj;
+      if (prop.value_type.is_a (typeof (GomCollection))) {
+        obj = Object.new (prop.value_type, "element", this);
+        v.set_object (obj);
+        set_property (prop.name, v);
+        return true;
+      }
+      if (prop.value_type.is_a (typeof (GomElement))) {
+        obj = Object.new (prop.value_type);
+        (obj as GomNode).set_parent (this);
+        v.set_object (obj);
+        set_property (prop.name, v);
+      }
+      return false;
+   }
 }
diff --git a/test/GomSerializationTest.vala b/test/GomSerializationTest.vala
index 5ae1cdc..f3e6d2f 100644
--- a/test/GomSerializationTest.vala
+++ b/test/GomSerializationTest.vala
@@ -405,9 +405,8 @@ class GomSerializationTest : GXmlTest  {
 #endif
       assert ("<BookStand Classification=\"Science\"/>" in s);
       assert (bs.owner_document != null);
-      assert  (bs.registers == null);
-      bs.registers = new Registers ();
-      bs.registers.initialize_element (bs);
+      assert (bs.registers == null);
+      assert (bs.create_instance_property ("registers"));
       s = bs.to_string ();
       assert (s != null);
 #if DEBUG
@@ -459,8 +458,7 @@ class GomSerializationTest : GXmlTest  {
 #endif
       assert ("<BookStore/>" in s);
       assert (bs.books == null);
-      bs.books = new Books ();
-      bs.books.initialize_element (bs);
+      bs.create_instance_property ("books");
       s = bs.to_string ();
       assert (s != null);
 #if DEBUG


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