[gxml] Added TDocument new .for_path(), .for_file() and .for_stream()



commit 2776441a38064edae1755de265360085ff92efe0
Author: Daniel Espinosa <esodan gmail com>
Date:   Wed Mar 9 17:32:50 2016 -0600

    Added TDocument new .for_path(), .for_file() and .for_stream()

 NEWS                    |    1 +
 gxml/TDocument.vala     |   30 ++++++++++++++++++++++++------
 test/TDocumentTest.vala |   10 ++++------
 3 files changed, 29 insertions(+), 12 deletions(-)
---
diff --git a/NEWS b/NEWS
index 1f87887..95c6922 100644
--- a/NEWS
+++ b/NEWS
@@ -3,6 +3,7 @@ Version 0.9.3
 ===============
 
 * API change TwNode and derived classes renamed to TNode
+* Added Read capabilities to TDocument
 
 
 ===============
diff --git a/gxml/TDocument.vala b/gxml/TDocument.vala
index 4eb8a1b..93a8ef5 100644
--- a/gxml/TDocument.vala
+++ b/gxml/TDocument.vala
@@ -37,11 +37,23 @@ public class GXml.TDocument : GXml.TNode, GXml.Document
     _name = "#document";
   }
   public TDocument () {}
-  public TDocument.for_path (string file)
-  {
-    var f = File.new_for_path (file);
-    this.file = f;
+  public TDocument.for_path (string path) {
+    this.file = GLib.File.new_for_path (path);
+    if (!file.query_exists ()) return;
+    try { read_doc (this, file, null); } catch {}
+    
+  }
+
+  public TDocument.for_file (GLib.File file) {
+    if (!file.query_exists ()) return;
+    try { read_doc (this, file, null); } catch {}
+    this.file = file;
+  }
+
+  public TDocument.for_stream (GLib.InputStream stream) {
+    try { read_doc_stream (this, stream, null); } catch {}
   }
+
   // GXml.Node
   public override Gee.List<GXml.Namespace> namespaces {
     owned get {
@@ -418,9 +430,15 @@ public class GXml.TDocument : GXml.TNode, GXml.Document
   public static void read_doc (GXml.Document doc, GLib.File file, ReadTypeFunc? rtfunc = null) throws 
GLib.Error {
     if (!file.query_exists ())
       throw new GXml.DocumentError.INVALID_FILE (_("File doesn't exists"));
+    read_doc_stream (doc, file.read (), rtfunc);
+  }
+  /**
+   * Reads document from { link GLib.InputStream} objects.
+   */
+  public static void read_doc_stream (GXml.Document doc, GLib.InputStream istream, ReadTypeFunc? rtfunc = 
null) {
     var b = new MemoryOutputStream.resizable ();
-    b.splice (file.read (), 0);
-    var tr = new TextReader.for_memory ((char[]) b.data, (int) b.get_data_size (), file.get_uri ());
+    b.splice (istream, 0);
+    var tr = new TextReader.for_memory ((char[]) b.data, (int) b.get_data_size (), "/memory");
     GXml.Node current = null;
     ReadType rt = ReadType.CONTINUE;
     while (read_node (doc, tr, rtfunc) == ReadType.CONTINUE);
diff --git a/test/TDocumentTest.vala b/test/TDocumentTest.vala
index c3a0c17..5223cf5 100644
--- a/test/TDocumentTest.vala
+++ b/test/TDocumentTest.vala
@@ -498,8 +498,7 @@ class TDocumentTest : GXmlTest {
                        try {
                                var f = GLib.File.new_for_path (GXmlTestConfig.TEST_DIR+"/t-read-test.xml");
                                assert (f.query_exists ());
-                               var d = new TDocument ();
-                               TDocument.read_doc (d, f, null);
+                               var d = new TDocument.for_file (f);
                                //GLib.message ("Doc:"+d.to_string ());
                                assert (d.root != null);
                                assert (d.root.name == "Sentences");
@@ -542,8 +541,7 @@ class TDocumentTest : GXmlTest {
                        try {
                                var f = GLib.File.new_for_path (GXmlTestConfig.TEST_DIR+"/t-read-test.xml");
                                assert (f.query_exists ());
-                               var d = new TDocument ();
-                               TDocument.read_doc (d, f, null);
+                               var d = new TDocument.for_file (f);
                                assert (d.children[0] is GXml.Comment);
                                assert (d.children[0].value == " Top Level Comment ");
                                var a = d.root.children[2];
@@ -559,7 +557,7 @@ class TDocumentTest : GXmlTest {
                        try {
                                var f = GLib.File.new_for_path (GXmlTestConfig.TEST_DIR+"/t-read-test.xml");
                                assert (f.query_exists ());
-                               var d = new TDocument ();
+                               var d = new TDocument.for_file (f);
                                TDocument.read_doc (d, f, null);
                                assert (d.children[1] is GXml.ProcessingInstruction);
                                assert ((d.children[1] as GXml.ProcessingInstruction).target == "target");
@@ -583,7 +581,7 @@ class TDocumentTest : GXmlTest {
                        try {
                                var f = GLib.File.new_for_path (GXmlTestConfig.TEST_DIR+"/t-read-test.xml");
                                assert (f.query_exists ());
-                               var d = new TDocument ();
+                               var d = new TDocument.for_file (f);
                                TDocument.read_doc (d, f, null);
                                assert (d.root.children.size == 6);
                                var p = (d.root.children[5]);


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