[gxml] Added write/read methods to GomElement



commit b521a304eb97edbc936c8e525790d046b0e4f44d
Author: Daniel Espinosa <esodan gmail com>
Date:   Mon Dec 12 20:52:23 2016 -0600

    Added write/read methods to GomElement
    
    Setting messages to show only on debug enable

 gxml/GomElement.vala       |   25 +++++++++++++++++++++
 gxml/GomObject.vala        |   18 +++++++++++++++
 gxml/XParser.vala          |   52 ++++++++++++++++++++++++++++++++++++++------
 test/DomGDocumentTest.vala |    6 ++--
 test/gxml-performance.vala |   39 +++++++++-----------------------
 5 files changed, 102 insertions(+), 38 deletions(-)
---
diff --git a/gxml/GomElement.vala b/gxml/GomElement.vala
index 1c7fa9a..f637221 100644
--- a/gxml/GomElement.vala
+++ b/gxml/GomElement.vala
@@ -37,6 +37,31 @@ public class GXml.GomElement : GomNode,
    * Derived classes should avoid to modify it.
    */
   protected Attributes _attributes;
+  // Convenient Serialization methods
+  /**
+   * Uses element's {@link GomDocument} to parse an XML file, deserializing it.
+   */
+  public void read_from_file (GLib.File f) throws GLib.Error {
+    (this.owner_document as GomDocument).read_from_file (f);
+  }
+  /**
+   * Uses element's {@link GomDocument} to parse an XML string, deserializing it.
+   */
+  public void read_from_string (string str) throws GLib.Error {
+    (this.owner_document as GomDocument).read_from_string (str);
+  }
+  /**
+   * Uses element's {@link GomDocument} to write an XML to a file, serializing it.
+   */
+  public void write_file (GLib.File f) throws GLib.Error {
+    (this.owner_document as GomDocument).write_file (f);
+  }
+  /**
+   * Uses element's {@link GomDocument} to write an XML to a stream, serializing it.
+   */
+  public void write_stream (GLib.OutputStream stream) throws GLib.Error {
+    (this.owner_document as GomDocument).write_stream (stream);
+  }
   // DomNode overrides
   public new string? lookup_prefix (string? nspace) {
     if (_namespace_uri == nspace)
diff --git a/gxml/GomObject.vala b/gxml/GomObject.vala
index a338793..62dc379 100644
--- a/gxml/GomObject.vala
+++ b/gxml/GomObject.vala
@@ -48,7 +48,9 @@ public interface GXml.GomObject : GLib.Object,
     var l = new List<string> ();
     foreach (ParamSpec spec in this.get_class ().list_properties ()) {
       if ("::" in spec.get_nick ()) {
+#if DEBUG
         GLib.message ("Name: "+spec.name+ " Nick: "+spec.get_nick ());
+#endif
         l.append (spec.get_nick ().replace ("::",""));
       }
     }
@@ -61,7 +63,9 @@ public interface GXml.GomObject : GLib.Object,
     var l = new List<ParamSpec> ();
     foreach (ParamSpec spec in this.get_class ().list_properties ()) {
       if (spec.value_type.is_a (typeof (GomProperty))) {
+#if DEBUG
           GLib.message ("GomProperty Name: "+spec.name);
+#endif
         l.append (spec);
       }
     }
@@ -79,7 +83,9 @@ public interface GXml.GomObject : GLib.Object,
         if ("::" in spec.get_nick ()) {
           string name = spec.get_nick ().replace ("::","");
           if (name.down () == nick.down ()) {
+#if DEBUG
             GLib.message ("Name: "+spec.name+ " Nick: "+spec.get_nick ());
+#endif
             return spec.name;
           }
         }
@@ -96,7 +102,9 @@ public interface GXml.GomObject : GLib.Object,
     foreach (ParamSpec spec in this.get_class ().list_properties ()) {
       if (spec.value_type.is_a (typeof (GomObject))
           || spec.value_type.is_a (typeof (GomCollection))) {
+#if DEBUG
         GLib.message ("Object Name: "+spec.name+ " Nick: "+spec.get_nick ());
+#endif
         l.append (spec);
       }
     }
@@ -119,13 +127,19 @@ public interface GXml.GomObject : GLib.Object,
    * this object, see {@link get_child}
    */
   public virtual string? get_attribute (string name) {
+#if DEBUG
     GLib.message ("GomObject: attribute: "+name);
+#endif
     string pname = find_property_name (name);
     if (pname == null) return null;
+#if DEBUG
     GLib.message ("GomObject: found attribute: "+pname);
+#endif
     var prop = get_class ().find_property (pname);
     if (prop != null) {
+#if DEBUG
       GLib.message ("Found attribute: "+prop.name);
+#endif
       var v = Value(prop.value_type);
       get_property (prop.name, ref v);
       if (prop.value_type == typeof(GomProperty)) {
@@ -170,10 +184,14 @@ public interface GXml.GomObject : GLib.Object,
    * this object, see {@link set_child}
    */
   public virtual bool set_attribute (string name, string val) {
+#if DEBUG
     GLib.message ("GomObject: searching attribute to set: "+name);
+#endif
     string pname = find_property_name (name);
     if (pname == null) return false;
+#if DEBUG
     GLib.message ("GomObject: setting attribute: "+name);
+#endif
     var prop = get_class ().find_property (pname);
     if (prop != null) {
       var v = Value (prop.value_type);
diff --git a/gxml/XParser.vala b/gxml/XParser.vala
index 0d58a8e..9a6671d 100644
--- a/gxml/XParser.vala
+++ b/gxml/XParser.vala
@@ -113,11 +113,11 @@ public class GXml.XParser : Object, GXml.Parser {
     GXml.DomNode n = node;
     string prefix = null, nsuri = null;
     int res = 1;
-//#if DEBUG
+#if DEBUG
     GLib.message ("ReadNode: Current Node: "+node.node_name
                   +" Current: "+read_current.to_string ()+
                   " Property: "+read_property.to_string ());
-//#endif
+#endif
     if (!read_property) {
       res = tr.read ();
       if (res == -1)
@@ -144,19 +144,25 @@ public class GXml.XParser : Object, GXml.Parser {
       if (!read_current && !read_property
           && node is DomElement
           && tr.const_local_name () != (node as DomElement).local_name) {
+#if DEBUG
         GLib.message ("Searching for Properties Nodes for:"+
                       (node as DomElement).local_name+
                       " Current node name: "+ tr.const_local_name ());
+#endif
         foreach (ParamSpec pspec in
                   (node as GomObject).get_property_element_list ()) {
           if (pspec.value_type.is_a (typeof (GomCollection))) {
+#if DEBUG
             GLib.message (" Is Collection in: "+(node as DomElement).local_name);
+#endif
             GomCollection col;
             Value vc = Value (pspec.value_type);
             node.get_property (pspec.name, ref vc);
             col = vc.get_object () as GomCollection;
             if (col == null) {
+#if DEBUG
               GLib.message ("Initializing Collection property...");
+#endif
               col = Object.new (pspec.value_type,
                                 "element", node) as GomCollection;
               vc.set_object (col);
@@ -179,12 +185,15 @@ public class GXml.XParser : Object, GXml.Parser {
               continue;
             }
             if (col.items_name.down () == tr.const_local_name ().down ()) {
+#if DEBUG
               GLib.message ("Is a Node to append in collection");
+#endif
               if (node.owner_document == null)
                 throw new DomError.HIERARCHY_REQUEST_ERROR
                             (_("No document is set to node"));
               var obj = Object.new (col.items_type,
                                     "owner-document", _document);
+#if DEBUG
               GLib.message ("Equal Documents:"+
                   ((obj as DomNode).owner_document == node.owner_document).to_string ());
               GLib.message ("Object Element to add in Collection: "
@@ -195,8 +204,11 @@ public class GXml.XParser : Object, GXml.Parser {
                             +(node as DomNode).owner_document.document_element.node_name);
               GLib.message ("Root Document Element: "
                             +(obj as DomNode).owner_document.document_element.node_name);
+#endif
               read_current_node (obj as DomNode, true, true);
+#if DEBUG
               GLib.message ("Adding element to collection...");
+#endif
               col.append (obj as DomElement);
               isproperty = true;
               break;
@@ -226,18 +238,22 @@ public class GXml.XParser : Object, GXml.Parser {
                         .printf ((node as DomElement).local_name));
       }
       if (!isproperty) {
+#if DEBUG
         GLib.message ("No object Property is set. Creating a standard element: "
                         +tr.const_local_name ());
+#endif
         if (node is DomDocument || !read_current) {
-          GLib.message ("No deserializing current node");
 #if DEBUG
+          GLib.message ("No deserializing current node");
           if (isempty) GLib.message ("Is Empty node:"+node.node_name);
           GLib.message ("ReadNode: Element: "+tr.const_local_name ());
 #endif
           if (!isproperty) {
             prefix = tr.prefix ();
             if (prefix != null) {
+#if DEBUG
               GLib.message ("Is namespaced element");
+#endif
               nsuri = tr.lookup_namespace (prefix);
               n = _document.create_element_ns (nsuri, tr.prefix () +":"+ tr.const_local_name ());
             } else
@@ -258,19 +274,23 @@ public class GXml.XParser : Object, GXml.Parser {
             throw new DomError.HIERARCHY_REQUEST_ERROR (_("Parsing ERROR: Fail to move to attribute number: 
%i").printf (i));
           }
           if (tr.is_namespace_decl () == 1) {
-  //#if DEBUG
+#if DEBUG
             GLib.message ("Is Namespace Declaration...");
-  //#endif
+#endif
             string nsp = tr.const_local_name ();
             string aprefix = tr.prefix ();
             tr.read_attribute_value ();
             if (tr.node_type () == Xml.ReaderType.TEXT) {
               string ansuri = tr.read_string ();
+#if DEBUG
               GLib.message ("Read: "+aprefix+":"+nsp+"="+ansuri);
+#endif
               string ansp = nsp;
               if (nsp != "xmlns")
                 ansp = aprefix+":"+nsp;
+#if DEBUG
               GLib.message ("To append: "+ansp+"="+ansuri);
+#endif
               (n as DomElement).set_attribute_ns ("http://www.w3.org/2000/xmlns/";,
                                                    ansp, ansuri);
             }
@@ -288,7 +308,9 @@ public class GXml.XParser : Object, GXml.Parser {
 #endif
               bool processed = (n as GomObject).set_attribute (attrname, attrval);
               if (prefix != null && !processed) {
+#if DEBUG
                 GLib.message ("Prefix found: "+prefix);
+#endif
                 if (prefix == "xml")
                   nsuri = "http://www.w3.org/2000/xmlns/";;
                 else
@@ -299,17 +321,23 @@ public class GXml.XParser : Object, GXml.Parser {
             }
           }
         }
+#if DEBUG
         GLib.message ("No more element attributes for: "
                         +node.node_name);
+#endif
       }
       if (isempty) {
+#if DEBUG
         GLib.message ("No child nodes returning...");
+#endif
         return true;
       }
+#if DEBUG
       GLib.message ("Getting child nodes in element");
+#endif
       while (read_current_node (n) == true);
 #if DEBUG
-      //GLib.message ("Current Document: "+node.document.to_string ());
+      GLib.message ("Current Document: "+node.document.to_string ());
 #endif
       break;
     case Xml.ReaderType.ATTRIBUTE:
@@ -437,14 +465,18 @@ public class GXml.XParser : Object, GXml.Parser {
     tw.flush ();
     string str;
     doc.dump_memory (out str, out size);
+#if DEBUG
     if (str != null)
       GLib.message ("STR: "+str);
+#endif
     return str;
   }
   private void start_node (GXml.DomNode node)
     throws GLib.Error
   {
+#if DEBUG
     GLib.message ("Starting node..."+node.node_name);
+#endif
     int size = 0;
 #if DEBUG
     GLib.message (@"Starting Node: start Node: '$(node.node_name)'");
@@ -462,13 +494,17 @@ public class GXml.XParser : Object, GXml.Parser {
       }
       if ((node as DomElement).prefix == null
             && (node as DomElement).namespace_uri != null) {
+#if DEBUG
             GLib.message ("Writting namespace definition for node");
+#endif
           tw.start_element_ns (null,
                                (node as DomElement).local_name,
                                (node as DomElement).namespace_uri);
       } else
         tw.start_element (node.node_name);
+#if DEBUG
     GLib.message ("Write down properties: size:"+(node as DomElement).attributes.size.to_string ());
+#endif
 
     // GomObject serialization
     var lp = (node as GomObject).get_properties_list ();
@@ -519,7 +555,9 @@ public class GXml.XParser : Object, GXml.Parser {
           tw.flush ();
       }
       if (n is GXml.DomText) {
-      //GLib.message ("Writting Element's contents");
+#if DEBUG
+      GLib.message ("Writting Element's contents");
+#endif
       size += tw.write_string (n.node_value);
       if (size > 1500)
         tw.flush ();
diff --git a/test/DomGDocumentTest.vala b/test/DomGDocumentTest.vala
index 122d761..a7a67b1 100644
--- a/test/DomGDocumentTest.vala
+++ b/test/DomGDocumentTest.vala
@@ -24,7 +24,7 @@ using GXml;
 
 class DomGDocumentTest : GXmlTest {
 
-static const string STRDOC = "<?xml version=\"1.0\"?>
+const string STRDOC = "<?xml version=\"1.0\"?>
 <!-- Comment -->
 <Sentences>
   <Sentence lang=\"en\">I like the colour blue.</Sentence>
@@ -42,7 +42,7 @@ static const string STRDOC = "<?xml version=\"1.0\"?>
 </Sentences>
 ";
 
-static const string HTMLDOC ="
+const string HTMLDOC ="
 <html>
 <body>
 <p class=\"black\">Text content</p>
@@ -53,7 +53,7 @@ static const string HTMLDOC ="
 </html>
 ";
 
-static const string XMLDOC ="<?xml version=\"1.0\"?>
+const string XMLDOC ="<?xml version=\"1.0\"?>
 <root>
 <project xmlns:gxml=\"http://live.gnome.org/GXml\";>
 <code class=\"parent\"/>
diff --git a/test/gxml-performance.vala b/test/gxml-performance.vala
index 83aac3f..e1f7055 100644
--- a/test/gxml-performance.vala
+++ b/test/gxml-performance.vala
@@ -392,42 +392,25 @@ public class Performance
         assert_not_reached ();
       }
     });
-    Test.add_func ("/gxml/performance/deserialize/gomdocument",
+    Test.add_func ("/gxml/performance/se-deserialize/gomdocument",
     () => {
       try {
         double time;
+        GomDocument doc;
+        var f = GLib.File.new_for_path (GXmlTestConfig.TEST_DIR + "/test-large.xml");
+        assert (f.query_exists ());
         Test.timer_start ();
-        var d = new GomDocument.from_path (GXmlTestConfig.TEST_DIR + "/test-large.xml");
-        time = Test.timer_elapsed ();
-        Test.minimized_result (time, "open document from path: %g seconds", time);
-        Test.timer_start ();
-        var bs = new GomBookStore (); // FIXME: Requires to read file to document
-        time = Test.timer_elapsed ();
-        Test.minimized_result (time, "deserialize/performance: %g seconds", time);
-      } catch (GLib.Error e) {
-#if DEBUG
-        GLib.message ("ERROR: "+e.message);
-#endif
-        assert_not_reached ();
-      }
-    });
-
-    Test.add_func ("/gxml/performance/serialize/gomdocument",
-    () => {
-      try {
-        double time;
-        Test.timer_start ();
-        var d = new GomDocument.from_path (GXmlTestConfig.TEST_DIR + "/test-large.xml");
-        time = Test.timer_elapsed ();
-        Test.minimized_result (time, "open document from path: %g seconds", time);
-        Test.timer_start ();
-        var bs = new BookStore (); // FIXME: Requires to read document
+        var bs = new GomBookStore ();
+        bs.read_from_file (f);
         time = Test.timer_elapsed ();
         Test.minimized_result (time, "deserialize/performance: %g seconds", time);
+        var of = GLib.File.new_for_path (GXmlTestConfig.TEST_SAVE_DIR + "/test-large-new.xml");
         Test.timer_start ();
-        // FIXME: Test serialization no sense, there is not this concept on GOM
+        bs.write_file (of);
         time = Test.timer_elapsed ();
-        Test.minimized_result (time, "serialize/performance: %g seconds", time);
+        Test.minimized_result (time, "Serialize/performance: %g seconds", time);
+        assert (of.query_exists ());
+        try { of.delete (); } catch { assert_not_reached (); }
       } catch (GLib.Error e) {
 #if DEBUG
         GLib.message ("ERROR: "+e.message);


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