[gxml] Start implementation of TextWriter to write out GXml.Document



commit 6c0a841cffa3b7d7034e37f99f30077dcfc1f57a
Author: Daniel Espinosa <esodan gmail com>
Date:   Mon May 4 22:30:30 2015 -0500

    Start implementation of TextWriter to write out GXml.Document
    
    * Added Document.indent property
    * Fixed Node.ns_uri() method's name typo
    * First implementation of Document.save() in TwDocument
      requires Unit Tests
    * Requires Unit Tests for all TextWriter classes implementation
    * Consider to remove Xml.TextWrite field in Tw* classes

 gxml/Document.vala        |    4 ++++
 gxml/Node.vala            |    2 +-
 gxml/TwDocument.vala      |   41 +++++++++++++++++++++++++++++++++++++----
 gxml/libxml-Document.vala |    1 +
 4 files changed, 43 insertions(+), 5 deletions(-)
---
diff --git a/gxml/Document.vala b/gxml/Document.vala
index a9419d7..17489cf 100644
--- a/gxml/Document.vala
+++ b/gxml/Document.vala
@@ -35,6 +35,10 @@ public errordomain GXml.DocumentError {
 public interface GXml.Document : Object, GXml.Node
 {
   /**
+   * Controls if writting this documents should use indent.
+   */
+  public abstract bool indent { get; set; }
+  /**
    * XML document root node as a { link GXml.Element}.
    */
   public abstract GXml.Node root { get; }
diff --git a/gxml/Node.vala b/gxml/Node.vala
index 50c532c..db7ac5f 100644
--- a/gxml/Node.vala
+++ b/gxml/Node.vala
@@ -82,7 +82,7 @@ public interface GXml.Node : Object
    * This allways returns first { link GXml.Namespace}'s URI in { link GXml.Node}'s
    * namespaces collection.
    */
-  public virtual string ns_urf () { return namespaces.first ().uri; }
+  public virtual string ns_uri () { return namespaces.first ().uri; }
   /**
    * Copy a { link GXml.Node} relaing on { link GXml.Document} to other { link GXml.Node}.
    *
diff --git a/gxml/TwDocument.vala b/gxml/TwDocument.vala
index 012c6c4..20eec46 100644
--- a/gxml/TwDocument.vala
+++ b/gxml/TwDocument.vala
@@ -30,8 +30,6 @@ public class GXml.TwDocument : GXml.TwNode, GXml.Document
   {
     var f = File.new_for_path (file);
     this.file = f;
-    tw = new Xml.TextWriter.filename (file);
-    tw->start_document ();
   }
   // GXml.Node
   public override bool set_namespace (string uri, string prefix)
@@ -41,6 +39,7 @@ public class GXml.TwDocument : GXml.TwNode, GXml.Document
   }
   public override GXml.Document document { get { return this; } }
   // GXml.Document
+  public bool indent { get; set; default = false; }
   public GXml.Node create_comment (string text)
   {
     var c = new TwComment (this, text);
@@ -62,7 +61,41 @@ public class GXml.TwDocument : GXml.TwNode, GXml.Document
   public GXml.Node root { get { return _root; } }
   public bool save (GLib.Cancellable? cancellable)
   {
-    // TODO:
-    return false;
+    var tw = new Xml.TextWriter.filename (file.get_path ());
+    tw.start_document ();
+    tw.set_indent (indent);
+    // Root
+    if (root == null) tw.end_document ();
+    start_node (tw, root);
+    tw.write_string (root.value);
+    tw.end_element ();
+    tw.end_document ();
+    return true;
+  }
+  public void start_node (Xml.TextWriter tw, GXml.Node node)
+  {
+    if (node is GXml.Element) {
+      if (node.namespaces.size > 0) {
+        tw.start_element_ns (root.ns_prefix (), root.name, root.ns_uri ());
+      } else {
+        tw.start_element (root.name);
+      }
+      foreach (GXml.Node attr in attrs.values) {
+        if (attr.namespaces.size > 0)
+          tw.write_attribute_ns (attr.ns_prefix (), attr.name, attr.ns_uri (), attr.value);
+        else
+          tw.write_attribute (attr.name, attr.value);
+      }
+      foreach (GXml.Node n in childs) {
+        if (n is GXml.Element) {
+          start_node (tw, n);
+          tw.write_string (n.value);
+          tw.end_element ();
+        }
+      }
+    }
+    if (node is GXml.Comment) {
+      tw.write_comment (node.value);
+    }
   }
 }
diff --git a/gxml/libxml-Document.vala b/gxml/libxml-Document.vala
index 6872c11..b50c18d 100644
--- a/gxml/libxml-Document.vala
+++ b/gxml/libxml-Document.vala
@@ -1014,6 +1014,7 @@ namespace GXml {
                        return this.lookup_node (our_copy_xml); // inducing a GXmlNode
                }
                // GXml.Document interface
+               public bool indent { get; set; default = false; }
                public GLib.File file { get; set; }
                public virtual GXml.Node root { get { return document_element; } }
                public GXml.Node create_text (string str) { return (GXml.Node) this.create_text_node (str); }


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