[gxml] Fixes on TwDocument.save()



commit 03fa559c809f2f2a81e02c4b4016c33c9014bc3b
Author: Daniel Espinosa <esodan gmail com>
Date:   Tue May 5 12:19:52 2015 -0500

    Fixes on TwDocument.save()
    
    * TwDocument.save () works, requires Unit Testing to check output
    * Fixed TwDocument.namespaces implementation
    * Fixed any TwNode should have its own namespaces, but search one defined in
      TwDocument to use or add new local and at document
    * Added DEBUG information on TwDocument.save()
    * Removed initialize Xml.TextReader
    * Fixes when DEBUG is enable

 gxml/Document.vala                |    2 +
 gxml/SerializableObjectModel.vala |    6 ++--
 gxml/TwDocument.vala              |   66 ++++++++++++++++++++++++++++++++-----
 gxml/TwElement.vala               |    1 -
 gxml/TwNode.vala                  |    7 ++--
 test/TwDocumentTest.vala          |   49 +++++++++++++++++++++++++++
 6 files changed, 115 insertions(+), 16 deletions(-)
---
diff --git a/gxml/Document.vala b/gxml/Document.vala
index f943471..a18090c 100644
--- a/gxml/Document.vala
+++ b/gxml/Document.vala
@@ -108,6 +108,8 @@ public interface GXml.Document : Object, GXml.Node
   public virtual void finalize_comment () { return; }
   /**
    * Save this { link GXml.Document} to { link GXml.Document.file}
+   *
+   * If { link GXml.Document.file} doesn't exists, it creates a new file to save to.
    */
   public abstract bool save (GLib.Cancellable? cancellable = null) throws GLib.Error;
   /**
diff --git a/gxml/SerializableObjectModel.vala b/gxml/SerializableObjectModel.vala
index 669f8c6..87a1b1c 100644
--- a/gxml/SerializableObjectModel.vala
+++ b/gxml/SerializableObjectModel.vala
@@ -103,7 +103,7 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
   public GXml.Node? default_serialize (GXml.Node node) throws GLib.Error
   {
 #if DEBUG
-    stdout.printf (@"$(get_type ().name ()): Serializing on node: $(node.node_name)\n");
+    stdout.printf (@"$(get_type ().name ()): Serializing on node: $(node.name)\n");
 #endif
     Document doc;
     if (node is GXml.Document)
@@ -140,7 +140,7 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
         t = serialized_xml_node_value;
       var tn = doc.create_text (t);
 #if DEBUG
-      stdout.printf (@"SETTING CONTENT FOR: $(get_type ().name ()): $(element.node_name): content '$t'\n");
+      stdout.printf (@"SETTING CONTENT FOR: $(get_type ().name ()): $(element.name): content '$t'\n");
 #endif
       element.childs.add (tn);
     }
@@ -313,7 +313,7 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
                                             throws GLib.Error
   {
 #if DEBUG
-    stdout.printf (@"Deserialize Property Node: $(property_node.node_name)\n");
+    stdout.printf (@"Deserialize Property Node: $(property_node.name)\n");
 #endif
     bool ret = false;
     var prop = find_property_spec (property_node.name);
diff --git a/gxml/TwDocument.vala b/gxml/TwDocument.vala
index 3b07d04..bdead94 100644
--- a/gxml/TwDocument.vala
+++ b/gxml/TwDocument.vala
@@ -24,7 +24,6 @@ using Xml;
 
 public class GXml.TwDocument : GXml.TwNode, GXml.Document
 {
-  Gee.ArrayList<GXml.Node> _namespaces = new Gee.ArrayList<GXml.Node> ();
   GXml.Element _root = null;
   public TwDocument (string file)
   {
@@ -78,36 +77,85 @@ public class GXml.TwDocument : GXml.TwNode, GXml.Document
     }
   }
   public bool save (GLib.Cancellable? cancellable = null)
+    requires (file != null)
   {
     var tw = new Xml.TextWriter.filename (file.get_path ());
     tw.start_document ();
     tw.set_indent (indent);
     // Root
-    if (root == null) tw.end_document ();
+    if (root == null) {
+      tw.end_document ();
+      return true;
+    }
+#if DEBUG
+    GLib.message ("Starting writting Document Root node");
+#endif
     start_node (tw, root);
+#if DEBUG
+    GLib.message ("Writting Document Root node's contents");
+#endif
     tw.write_string (root.value);
+#if DEBUG
+    GLib.message ("Ending writting Document Root node");
+#endif
     tw.end_element ();
+#if DEBUG
+    GLib.message ("Ending Document");
+#endif
     tw.end_document ();
+    tw.flush ();
     return true;
   }
   public void start_node (Xml.TextWriter tw, GXml.Node node)
   {
+#if DEBUG
+    GLib.message (@"Starting Node: start Node: '$(node.name)'");
+#endif
     if (node is GXml.Element) {
+#if DEBUG
+    GLib.message (@"Starting Element... '$(node.name)'");
+    GLib.message (@"Element Document is Null... '$((node.document == null).to_string ())'");
+    GLib.message (@"Namespaces in Element... '$(node.namespaces.size)'");
+#endif
       if (node.namespaces.size > 0) {
-        tw.start_element_ns (root.ns_prefix (), root.name, root.ns_uri ());
+#if DEBUG
+    GLib.message ("Starting Element: start with NS");
+#endif
+        tw.start_element_ns (node.ns_prefix (), node.name, node.ns_uri ());
       } else {
-        tw.start_element (root.name);
+#if DEBUG
+    GLib.message ("Starting Element: start no NS");
+#endif
+        tw.start_element (node.name);
       }
-      foreach (GXml.Node attr in attrs.values) {
-        if (attr.namespaces.size > 0)
+#if DEBUG
+    GLib.message ("Starting Element: writting attributes");
+#endif
+      foreach (GXml.Node attr in node.attrs.values) {
+        if (attr.namespaces.size > 0) {
+#if DEBUG
+    GLib.message ("Starting Element: write attribute with NS");
+#endif
           tw.write_attribute_ns (attr.ns_prefix (), attr.name, attr.ns_uri (), attr.value);
-        else
+        }
+        else {
+#if DEBUG
+    GLib.message ("Starting Element: write attribute no NS");
+#endif
           tw.write_attribute (attr.name, attr.value);
+        }
       }
-      foreach (GXml.Node n in childs) {
+#if DEBUG
+    GLib.message (@"Starting Element: writting Node '$(node.name)' childs");
+#endif
+      foreach (GXml.Node n in node.childs) {
         if (n is GXml.Element) {
+#if DEBUG
+    GLib.message (@"Starting Child Element: writting Node '$(n.name)'");
+#endif
           start_node (tw, n);
-          tw.write_string (n.value);
+          if (n.value != null)
+            tw.write_string (n.value);
           tw.end_element ();
         }
       }
diff --git a/gxml/TwElement.vala b/gxml/TwElement.vala
index 1f7ad91..bc47a22 100644
--- a/gxml/TwElement.vala
+++ b/gxml/TwElement.vala
@@ -27,7 +27,6 @@ public class GXml.TwElement : GXml.TwNode, GXml.Element
     requires (d is TwDocument)
   {
     _doc = d;
-    ((TwDocument) document).tw = ((TwDocument) d).tw;
     _name = name;
   }
   // GXml.Element
diff --git a/gxml/TwNode.vala b/gxml/TwNode.vala
index b0480d0..7677a2b 100644
--- a/gxml/TwNode.vala
+++ b/gxml/TwNode.vala
@@ -23,8 +23,9 @@ using Gee;
 
 public abstract class GXml.TwNode : Object, GXml.Node
 {
-  Gee.HashMap<string,GXml.Node> _attrs = new Gee.HashMap<string,GXml.Node> ();
-  Gee.ArrayList<GXml.Node> _childs = new Gee.ArrayList<GXml.Node> ();
+  protected Gee.HashMap<string,GXml.Node> _attrs = new Gee.HashMap<string,GXml.Node> ();
+  protected Gee.ArrayList<GXml.Node> _childs = new Gee.ArrayList<GXml.Node> ();
+  protected Gee.ArrayList<GXml.Node> _namespaces = new Gee.ArrayList<GXml.Node> ();
   protected string _name = null;
   protected string _value = null;
   protected GXml.Document _doc;
@@ -52,7 +53,7 @@ public abstract class GXml.TwNode : Object, GXml.Node
   public virtual Gee.BidirList<GXml.Node> childs { get { return _childs; } }
   public virtual GXml.Document document { get { return _doc; } }
   public virtual string name { get { return _name; } }
-  public virtual Gee.List<GXml.Namespace> namespaces { get { return document.namespaces; } }
+  public virtual Gee.List<GXml.Namespace> namespaces { get { return _namespaces; } }
   public virtual GXml.NodeType type_node { get { return GXml.NodeType.DOCUMENT; } }
   public virtual string value { get { return _value; } set  { _value = value; } }
 }
diff --git a/test/TwDocumentTest.vala b/test/TwDocumentTest.vala
new file mode 100644
index 0000000..e15ec8d
--- /dev/null
+++ b/test/TwDocumentTest.vala
@@ -0,0 +1,49 @@
+/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
+/* Notation.vala
+ *
+ * Copyright (C) 2011-2013  Richard Schwarting <aquarichy gmail com>
+ * Copyright (C) 2011-2015  Daniel Espinosa <esodan gmail com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *      Richard Schwarting <aquarichy gmail com>
+ *      Daniel Espinosa <esodan gmail com>
+ */
+
+using GXml;
+
+class TwDocumentTest : GXmlTest {
+       public static void add_tests () {
+               Test.add_func ("/gxml/tw-document/root", () => {
+                               var d = new TwDocument (GXmlTestConfig.TEST_SAVE_DIR+"/tw-test.xml");
+                               var e = d.create_element ("root");
+                               d.childs.add (e);
+                               assert (d.childs.size == 1);
+                               assert (d.root != null);
+                               assert (d.root.name == "root");
+                               assert (d.root.value == null);
+                       });
+               Test.add_func ("/gxml/tw-document/save/root", () => {
+                               var d = new TwDocument (GXmlTestConfig.TEST_SAVE_DIR+"/tw-test.xml");
+                               var e = d.create_element ("root");
+                               d.childs.add (e);
+                               assert (d.childs.size == 1);
+                               assert (d.root != null);
+                               assert (d.root.name == "root");
+                               assert (d.root.value == null);
+                               d.save ();
+                       });
+       }
+}


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