[gxml] GomDocument: Added write to file



commit ef2cf3a08aad29dd62b3af033972b1b28b2fb51d
Author: Daniel Espinosa <esodan gmail com>
Date:   Wed Nov 2 18:41:39 2016 -0600

    GomDocument: Added write to file
    
    Implemented Parser.write_file() and associated to
    allows file writing and streaming

 gxml/GomDocument.vala     |   10 ++++++++-
 gxml/Parser.vala          |   33 +++++++++++++++++++++++++-----
 gxml/XParser.vala         |   48 ++++++++++++++++++++++++++++++++++++--------
 test/GomDocumentTest.vala |   16 +++++++-------
 4 files changed, 83 insertions(+), 24 deletions(-)
---
diff --git a/gxml/GomDocument.vala b/gxml/GomDocument.vala
index c931cad..e700d0d 100644
--- a/gxml/GomDocument.vala
+++ b/gxml/GomDocument.vala
@@ -83,7 +83,7 @@ public class GXml.GomDocument : GomNode,
   }
 
   public GomDocument.from_file (GLib.File file) throws GLib.Error {
-    _parser.read (file, null);
+    _parser.read_file (file, null);
   }
 
   public GomDocument.from_stream (GLib.InputStream stream) throws GLib.Error {
@@ -99,6 +99,14 @@ public class GXml.GomDocument : GomNode,
     return _parser.write_string ();
   }
 
+  public void write_file (GLib.File file) throws GLib.Error {
+    _parser.write_file (file, null);
+  }
+
+  public void write_stream (GLib.OutputStream stream) throws GLib.Error {
+    _parser.write_stream (stream, null);
+  }
+
   public DomElement create_element (string local_name) throws GLib.Error {
     return new GomElement (this, local_name);
   }
diff --git a/gxml/Parser.vala b/gxml/Parser.vala
index 4d16ca3..aedf528 100644
--- a/gxml/Parser.vala
+++ b/gxml/Parser.vala
@@ -32,14 +32,30 @@ public errordomain GXml.ParserError {
  */
 public interface GXml.Parser : Object {
   /**
+   * Controls if, when writing to a file, a backup should
+   * be created.
+   */
+  public abstract bool backup { get; set; }
+  /**
+   * Controls if, when writing, identation should be used.
+   */
+  public abstract bool indent { get; set; }
+  /**
    * A {@link GXml.DomDocument} to read to or write from
    */
   public abstract DomDocument document { get; }
   /**
    * Writes a {@link GXml.DomDocument} to a {@link GLib.File}
    */
-  public abstract void write (GLib.File f,
-                            GLib.Cancellable? cancellable) throws GLib.Error;
+  public virtual void write_file (GLib.File file,
+                            GLib.Cancellable? cancellable)
+                            throws GLib.Error {
+    if (!file.query_exists ())
+      throw new GXml.ParserError.INVALID_FILE_ERROR (_("File doesn't exist"));
+    var ostream = file.replace (null, backup,
+                            GLib.FileCreateFlags.NONE, cancellable);
+    write_stream (ostream, cancellable);
+  }
   /**
    * Writes a {@link GXml.DomDocument} to a string
    */
@@ -49,12 +65,17 @@ public interface GXml.Parser : Object {
    */
   public abstract void write_stream (OutputStream stream,
                                     GLib.Cancellable? cancellable) throws GLib.Error;
-
   /**
-   * Read a {@link GXml.DomDocument} from a {@link GLib.File}
+   * Writes a {@link GXml.DomDocument} to a {@link GLib.OutputStream}
    */
-  public abstract void read (GLib.File f,
-                            GLib.Cancellable? cancellable) throws GLib.Error;
+  public virtual void read_file (GLib.File file,
+                                    GLib.Cancellable? cancellable)
+                                    throws GLib.Error {
+    if (!file.query_exists ())
+      throw new GXml.ParserError.INVALID_FILE_ERROR (_("File doesn't exist"));
+    read_stream (file.read (), cancellable);
+  }
+
   /**
    * Read a {@link GXml.DomDocument} from a {@link GLib.InputStream}
    */
diff --git a/gxml/XParser.vala b/gxml/XParser.vala
index 7f80c7d..9d44a75 100644
--- a/gxml/XParser.vala
+++ b/gxml/XParser.vala
@@ -30,14 +30,50 @@ public class GXml.XParser : Object, GXml.Parser {
   private TextReader tr;
   private Xml.TextWriter tw;
 
+  public bool backup { get; set; }
+  public bool indent { get; set; }
+
   public DomDocument document { get { return _document; } }
 
-  public bool indent { get; set; }
 
   public XParser (DomDocument doc) { _document = doc; }
 
-  public void write (GLib.File f, GLib.Cancellable? cancellable) throws GLib.Error {}
-  public void write_stream (OutputStream stream, GLib.Cancellable? cancellable) throws GLib.Error {}
+  construct {
+    backup = true;
+    indent = false;
+  }
+
+  public void write_stream (OutputStream stream,
+                            GLib.Cancellable? cancellable) throws GLib.Error {
+    var buf = new Xml.Buffer ();
+    tw = Xmlx.new_text_writer_memory (buf, 0);
+    tw.start_document ();
+    tw.set_indent (indent);
+    // Root
+    if (_document.document_element == null) {
+      tw.end_document ();
+    }
+    var dns = new ArrayList<string> ();
+#if DEBUG
+    GLib.message ("Starting writting Document child nodes");
+#endif
+    start_node (_document);
+#if DEBUG
+    GLib.message ("Ending writting Document child nodes");
+#endif
+    tw.end_element ();
+#if DEBUG
+    GLib.message ("Ending Document");
+#endif
+    tw.end_document ();
+    tw.flush ();
+    var s = new GLib.StringBuilder ();
+    s.append (buf.content ());
+    var b = new GLib.MemoryInputStream.from_data (s.data, null);
+    stream.splice (b, GLib.OutputStreamSpliceFlags.NONE);
+    stream.close ();
+  }
+
   public string write_string () throws GLib.Error  {
     return dump ();
   }
@@ -50,12 +86,6 @@ public class GXml.XParser : Object, GXml.Parser {
   }
 
 
-  public void read (GLib.File file,  GLib.Cancellable? cancellable) throws GLib.Error {
-    if (!file.query_exists ())
-      throw new GXml.ParserError.INVALID_FILE_ERROR (_("File doesn't exist"));
-    read_stream (file.read (), cancellable);
-  }
-
   public void read_stream (GLib.InputStream istream,
                           GLib.Cancellable? cancellable) throws GLib.Error {
     var b = new MemoryOutputStream.resizable ();
diff --git a/test/GomDocumentTest.vala b/test/GomDocumentTest.vala
index f28bb0f..a4a05a6 100644
--- a/test/GomDocumentTest.vala
+++ b/test/GomDocumentTest.vala
@@ -78,9 +78,9 @@ class GomDocumentTest : GXmlTest {
                                if (f.query_exists ()) f.delete ();
                                var s = new GLib.StringBuilder ();
                                s.append ("""<document_element />""");
-                               var d = new GDocument.from_string (s.str);
+                               var d = new GomDocument.from_string (s.str);
                                Test.message ("Saving to file: "+f.get_uri ()+d.to_string ());
-                               d.save_as (f);
+                               d.write_file (f);
                                assert (f.query_exists ());
                                var d2 = new GomDocument.from_file (f);
                                assert (d2 != null);
@@ -99,7 +99,7 @@ class GomDocumentTest : GXmlTest {
                                        GLib.message ("No remote file available. Skiping...");
                                        return;
                                }
-                               var d = new GDocument.from_file (rf);
+                               var d = new GomDocument.from_file (rf);
                                assert (d != null);
                                assert (d.document_element != null);
                                assert (d.document_element.node_name == "Project");
@@ -116,7 +116,7 @@ class GomDocumentTest : GXmlTest {
                                assert (fdescription);
                                assert (fhomepage);
                                var f = GLib.File.new_for_path (GXmlTestConfig.TEST_SAVE_DIR+"/xml.doap");
-                               d.save_as (f);
+                               d.write_file (f);
                                assert (f.query_exists ());
                                f.delete ();
                        } catch (GLib.Error e) {
@@ -181,9 +181,9 @@ class GomDocumentTest : GXmlTest {
                                int exit_status;
 
                                try {
-                                       doc = new GDocument.from_string ("<document_element />");
+                                       doc = new GomDocument.from_string ("<document_element />");
                                        var f = GLib.File.new_for_path 
(GXmlTestConfig.TEST_SAVE_DIR+"/test_out_path.xml");
-                                       (doc as GDocument).save_as (f);
+                                       (doc as GomDocument).write_file (f);
                                        assert (f.query_exists ());
                                        f.delete ();
                                } catch (GLib.Error e) {
@@ -195,8 +195,8 @@ class GomDocumentTest : GXmlTest {
                                DomDocument doc;
 
                                try {
-                                       doc = new GDocument.from_string ("<document_element />");
-                                       (doc as GDocument).save_as (GLib.File.new_for_path 
("/tmp/a/b/c/d/e/f/g/h/i"));
+                                       doc = new GomDocument.from_string ("<document_element />");
+                                       (doc as GomDocument).write_file (GLib.File.new_for_path 
("/tmp/a/b/c/d/e/f/g/h/i"));
                                        assert_not_reached ();
                                } catch {}
                        });


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