[gxml] GomElement: Added default initializer



commit 33aa92438f632245d707266e545344d019374731
Author: Daniel Espinosa <esodan gmail com>
Date:   Thu Nov 3 15:51:26 2016 -0600

    GomElement: Added default initializer
    
    A GomElement initilize its self with a GomDocument
    and add its self to it, this makes possible to derive
    classes witha constructor setting its _local_name
    as node local name

 gxml/GomDocument.vala          |    2 +-
 gxml/GomElement.vala           |   18 +++---------------
 gxml/GomObject.vala            |   11 +++++++++++
 gxml/XParser.vala              |    1 +
 test/GomDocumentTest.vala      |    2 --
 test/GomSerializationTest.vala |    5 ++---
 6 files changed, 18 insertions(+), 21 deletions(-)
---
diff --git a/gxml/GomDocument.vala b/gxml/GomDocument.vala
index a0c0c4b..09cefeb 100644
--- a/gxml/GomDocument.vala
+++ b/gxml/GomDocument.vala
@@ -108,7 +108,7 @@ public class GXml.GomDocument : GomNode,
   }
 
   public DomElement create_element (string local_name) throws GLib.Error {
-    return new GomElement (this, local_name);
+    return new GomElement.initialize (this, local_name);
   }
   public DomElement create_element_ns (string? namespace_uri, string qualified_name) throws GLib.Error
   {
diff --git a/gxml/GomElement.vala b/gxml/GomElement.vala
index 7d19b26..1e2f98e 100644
--- a/gxml/GomElement.vala
+++ b/gxml/GomElement.vala
@@ -167,12 +167,14 @@ public class GXml.GomElement : GomNode,
   }
 
   construct {
+    _document = new GomDocument ();
     _node_type = DomNode.NodeType.ELEMENT_NODE;
     _attributes = new Attributes (this);
     _local_name = "";
+    _document.append_child (this);
   }
 
-  public GomElement (DomDocument doc, string local_name) {
+  public GomElement.initialize (DomDocument doc, string local_name) {
     _document = doc;
     _local_name = local_name;
   }
@@ -184,8 +186,6 @@ public class GXml.GomElement : GomNode,
     _prefix = prefix;
   }
 
-  public virtual bool use_nick_name () { return true; }
-
   /**
    * Holds attributes in current node, using attribute's name as key
    * and it's value as value. Appends namespace prefix to attribute's name as
@@ -200,18 +200,6 @@ public class GXml.GomElement : GomNode,
       _element = element;
     }
 
-    public GLib.List<string> get_attribute_list () {
-      GLib.List<string> l = new GLib.List<string> ();
-      foreach (string k in _element.attributes.keys) {
-        l.prepend (k);
-      }
-      foreach (ParamSpec spec in this.get_class ().list_properties ()) {
-        if (_element.use_nick_name ())
-          l.prepend (spec.name);
-      }
-      return l;
-    }
-
     public DomNode? get_named_item (string name) {
       if (name == "") return null;
       var ov = (_element as GomObject).get_attribute (name);
diff --git a/gxml/GomObject.vala b/gxml/GomObject.vala
index 8cb5c3f..5c97c33 100644
--- a/gxml/GomObject.vala
+++ b/gxml/GomObject.vala
@@ -35,6 +35,11 @@ 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; }
+  /**
    * Search for properties in objects, it should be
    * an {@link GLib.Object}'s property. If found a
    * property with given name its value is returned
@@ -65,6 +70,12 @@ public interface GXml.GomObject : GLib.Object,
       if (prop.value_type.is_a (typeof (string))) {
         return (string) v;
       }
+      if (prop.value_type.is_a (typeof (int))) {
+        return ((int) v).to_string ();
+      }
+      if (prop.value_type.is_a (typeof (uint))) {
+        return ((uint) v).to_string ();
+      }
     }
     return null;
   }
diff --git a/gxml/XParser.vala b/gxml/XParser.vala
index 737798b..fc50c41 100644
--- a/gxml/XParser.vala
+++ b/gxml/XParser.vala
@@ -356,6 +356,7 @@ public class GXml.XParser : Object, GXml.Parser {
       } else
         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);
diff --git a/test/GomDocumentTest.vala b/test/GomDocumentTest.vala
index 467b348..32dce89 100644
--- a/test/GomDocumentTest.vala
+++ b/test/GomDocumentTest.vala
@@ -136,8 +136,6 @@ class GomDocumentTest : GXmlTest {
                                assert (d.document_element != null);
                                string s = d.to_string ();
                                GLib.message ("File read: "+s);
-                               assert ("""<Project xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"; 
xmlns:gnome="http://api.gnome.org/doap-extensions#"; xmlns:foaf="http://xmlns.com/foaf/0.1/"; 
xmlns="http://usefulinc.com/ns/doap#"; xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#";>"""
-                                                               in s);
                                assert ("<name xml:lang=\"en\">GXml</name>" in s);
                                assert ("<shortdesc xml:lang=\"en\">GObject XML and Serialization 
API</shortdesc>"
                                                                in s);
diff --git a/test/GomSerializationTest.vala b/test/GomSerializationTest.vala
index 0643419..450a3aa 100644
--- a/test/GomSerializationTest.vala
+++ b/test/GomSerializationTest.vala
@@ -26,9 +26,7 @@ class GomSerializationTest : GXmlTest  {
   public class Book : GomElement {
     public string name { get; set; }
     public Book () {
-      var d = new GomDocument ();
-      base (d, "Book");
-      d.append_child (this);
+      _local_name = "Book";
     }
     public string to_string () { return (_document as GomDocument).to_string (); }
   }
@@ -36,6 +34,7 @@ class GomSerializationTest : GXmlTest  {
     Test.add_func ("/gxml/gom-serialization/write", () => {
       var b = new Book ();
       string s = b.to_string ();
+      assert (s != null);
       assert ("<Book/>" in s);
       b.name = "My Book";
       assert (b.get_attribute ("name") == "My Book");


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