[gxml] Added new construct methods for TDocument using custome ReadTypeFunc



commit a53859b73ae5ea241ccad9302b56a518fee5ff8e
Author: Daniel Espinosa <esodan gmail com>
Date:   Fri Mar 25 09:29:38 2016 -0600

    Added new construct methods for TDocument using custome ReadTypeFunc

 NEWS                    |   12 +++++-
 gxml/TDocument.vala     |   36 ++++++++++++++++-
 test/TDocumentTest.vala |  102 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 148 insertions(+), 2 deletions(-)
---
diff --git a/NEWS b/NEWS
index b5fd68e..422cf68 100644
--- a/NEWS
+++ b/NEWS
@@ -1,7 +1,17 @@
+
 ===============
-Version 0.9.90
+Version 0.9.92
 ===============
 
+* New contructor methods for TDocument using custome ReadTypeFunc
+* Bug fixes
+* New and updated translations:
+    Updated Polish by Piotr Drąg <piotrdrag gmail com>
+
+==========================
+Version 0.9.90 and 0.9.91
+==========================
+
 * New TDocument.from_stream(), .from_string and .from_uri() methods
 * API change TwNode and derived classes renamed to TNode
 * Added parse capabilities to TDocument
diff --git a/gxml/TDocument.vala b/gxml/TDocument.vala
index 24c7dc7..782682e 100644
--- a/gxml/TDocument.vala
+++ b/gxml/TDocument.vala
@@ -32,10 +32,13 @@ public class GXml.TDocument : GXml.TNode, GXml.Document
 {
   protected Gee.ArrayList<GXml.Node> _namespaces;
   protected Gee.ArrayList<GXml.Node> _children;
-  GXml.Element _root = null;
+  private GXml.Element _root = null;
+  private ReadTypeFunc _readtype_func = null;
+
   construct {
     _name = "#document";
   }
+
   public TDocument () {}
   public TDocument.from_path (string path) {
     this.file = GLib.File.new_for_path (path);
@@ -64,6 +67,34 @@ public class GXml.TDocument : GXml.TNode, GXml.Document
     TDocument.from_stream (minput);
   }
 
+  
+  public TDocument.from_path_with_readtype_func (string path, ReadTypeFunc func) {
+    this.file = GLib.File.new_for_path (path);
+    if (!file.query_exists ()) return;
+    try { read_doc (this, file, func); } catch {}
+    
+  }
+
+  public TDocument.from_uri_with_readtype_func (string uri, ReadTypeFunc func) {
+    this.from_file_with_readtype_func (File.new_for_uri (uri), func);
+  }
+
+  public TDocument.from_file_with_readtype_func (GLib.File file, ReadTypeFunc func) {
+    if (!file.query_exists ()) return;
+    try { read_doc (this, file, func); } catch {}
+    this.file = file;
+  }
+
+  public TDocument.from_stream_with_readtype_func (GLib.InputStream stream, ReadTypeFunc func) {
+    try { read_doc_stream (this, stream, func); } catch {}
+  }
+
+  public TDocument.from_string_with_readtype_func (string str, ReadTypeFunc func) {
+    var minput = new GLib.MemoryInputStream ();
+    minput.add_data ((uint8[]) str.dup (), null);
+    TDocument.from_stream_with_readtype_func (minput, func);
+  }
+
   // GXml.Node
   public override Gee.List<GXml.Namespace> namespaces {
     owned get {
@@ -450,6 +481,9 @@ public class GXml.TDocument : GXml.TNode, GXml.Document
   public static void read_doc_stream (GXml.Document doc, GLib.InputStream istream, ReadTypeFunc? rtfunc = 
null) {
     var b = new MemoryOutputStream.resizable ();
     b.splice (istream, 0);
+#if DEBUG
+    GLib.message ("FILE:"+(string)b.data);
+#endif
     var tr = new TextReader.for_memory ((char[]) b.data, (int) b.get_data_size (), "/memory");
     GXml.Node current = null;
     while (read_node (doc, tr, rtfunc) == ReadType.CONTINUE);
diff --git a/test/TDocumentTest.vala b/test/TDocumentTest.vala
index 8dc3d0e..ff9527e 100644
--- a/test/TDocumentTest.vala
+++ b/test/TDocumentTest.vala
@@ -733,5 +733,107 @@ class TDocumentTest : GXmlTest {
                                assert (d.root.children[1].children[0].children[0].value == "COMMUNICATIONS");
                        } catch (GLib.Error e) { GLib.message ("ERROR: "+e.message); assert_not_reached (); }
                });
+               Test.add_func ("/gxml/t-document/readtype", () => {
+                       try {
+                               var file = GLib.File.new_for_path 
(GXmlTestConfig.TEST_DIR+"/t-read-test.xml");
+                               assert (file.query_exists ());
+                               var d = new TDocument.from_file (file);
+                               assert (d.root != null);
+                               assert (d.root.children.size == 7);
+                               var n = d.root.children[6];
+                               assert (n != null);
+                               assert (n.name == "ReadTop");
+                               assert (n.children.size == 9);
+                               var nc = n.children[3];
+                               assert (nc != null);
+                               assert (nc.name == "Read");
+                               assert (nc.children.size == 2);
+                               GLib.message ("from file");
+                               // Remove all
+                               TDocument.ReadTypeFunc f1 = (node, tr)=>{
+                                       Test.message ("ReadType check node: "+node.name);
+                                       if (node.name == "NoRead" || node.name == "NoReadChild") {
+                                               Test.message ("Skiping node: "+node.name);
+                                               return TDocument.ReadType.NEXT;
+                                       }
+                                       return TDocument.ReadType.CONTINUE;
+                               };
+                               var d2 = new TDocument.from_file_with_readtype_func (file, f1);
+                               TDocument.read_doc (d2, file, f1);
+                               assert (d2.root != null);
+                               assert (d2.root.children.size == 7);
+                               var n2 = d2.root.children[6];
+                               assert (n2 != null);
+                               assert (n2.name == "ReadTop");
+                               assert (n2.children.size == 4);
+                               Test.message (@"$d2");
+                               var nc2 = n2.children[2];
+                               assert (nc2 != null);
+                               assert (nc2.name == "Read");
+                               assert (nc2.children.size == 1);
+                               // Checking ReadType.STOP effect
+                               GLib.message ("from path");
+                               TDocument.ReadTypeFunc f2 = (node, tr)=>{
+                                       Test.message ("ReadType check node: "+node.name);
+                                       if (node.name == "NoRead" || node.name == "NoReadChild") {
+                                               Test.message ("Skiping node: "+node.name);
+                                               return TDocument.ReadType.STOP;
+                                       }
+                                       return TDocument.ReadType.CONTINUE;
+                               };
+                               var d3 = new TDocument.from_path_with_readtype_func (file.get_path (), f2);
+                               Test.message (@"$d3");
+                               assert (d3.root != null);
+                               assert (d3.root.children.size == 7);
+                               var n3 = d3.root.children[6];
+                               assert (n3 != null);
+                               assert (n3.name == "ReadTop");
+                               assert (n3.children.size == 4);
+                               var nc3 = n3.children[2];
+                               assert (nc3 != null);
+                               assert (nc3.name == "Read");
+                               assert (nc3.children.size == 1);
+                               // From URI
+                               GLib.message ("from uri");
+                               var d4 = new TDocument.from_uri_with_readtype_func (file.get_uri (), f2);
+                               Test.message (@"$d3");
+                               assert (d4.root != null);
+                               assert (d4.root.children.size == 7);
+                               var n4 = d4.root.children[6];
+                               assert (n4 != null);
+                               assert (n4.name == "ReadTop");
+                               assert (n4.children.size == 4);
+                               var nc4 = n4.children[2];
+                               assert (nc4 != null);
+                               assert (nc4.name == "Read");
+                               assert (nc4.children.size == 1);
+                               // From Stream
+                               GLib.message ("from stream");
+                               var file2 = GLib.File.new_for_path 
(GXmlTestConfig.TEST_DIR+"/t-read-test.xml");
+                               var d5 = new TDocument.from_stream_with_readtype_func (file2.read (), f1);
+                               assert (d5.root != null);
+                               assert (d5.root.children.size == 7);
+                               var n5 = d5.root.children[6];
+                               assert (n5 != null);
+                               assert (n5.name == "ReadTop");
+                               assert (n5.children.size == 4);
+                               Test.message (@"$d2");
+                               var nc5 = n5.children[2];
+                               assert (nc5 != null);
+                               assert (nc5.name == "Read");
+                               assert (nc5.children.size == 1);
+                               // From string
+                               GLib.message ("from string");
+                               var d6 = new TDocument.from_string_with_readtype_func 
("<root><Read/><NoRead/><NoRead/><Read/><NoRead/></root>", f1);
+                               assert (d6.root != null);
+                               assert (d6.root.children.size == 2);
+                               var n6 = d6.root.children[1];
+                               assert (n6 != null);
+                               assert (n6.name == "Read");
+                       } catch (GLib.Error e) {
+                               GLib.message ("Error: "+e.message);
+                               assert_not_reached ();
+                       }
+               });
        }
 }


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