[gxml] Improved TDocument.read_doc for CONTINUE, NEXT and STOP parsing



commit 6b0e21e2e697c79e1c553f9f7e01a163fbc8024d
Author: Daniel Espinosa <esodan gmail com>
Date:   Sat Mar 26 22:30:40 2016 -0600

    Improved TDocument.read_doc for CONTINUE, NEXT and STOP parsing

 gxml/TDocument.vala     |   29 +++++++++++++++++++++++------
 test/TDocumentTest.vala |   10 +++++-----
 test/TElementTest.vala  |   11 ++++++-----
 test/t-read-test.xml    |    4 +++-
 4 files changed, 37 insertions(+), 17 deletions(-)
---
diff --git a/gxml/TDocument.vala b/gxml/TDocument.vala
index 782682e..ed73234 100644
--- a/gxml/TDocument.vala
+++ b/gxml/TDocument.vala
@@ -460,7 +460,7 @@ public class GXml.TDocument : GXml.TNode, GXml.Document
   /**
    * Delegate function to control parsing of XML documents. Return { link ReadType.NEXT}
    * to skip all children nodes of current { link GXml.Node}; { link ReadType.CONTINUE}
-   * or { link ReadType.STOP} to parse next child or node on reading.
+   * continue parsing nodes or { link ReadType.STOP} to stop reading.
    *
    * While you get the current { link Xml.TextReader} used in parsing, you can control
    * next action to take depending on current node.
@@ -484,7 +484,7 @@ public class GXml.TDocument : GXml.TNode, GXml.Document
 #if DEBUG
     GLib.message ("FILE:"+(string)b.data);
 #endif
-    var tr = new TextReader.for_memory ((char[]) b.data, (int) b.get_data_size (), "/memory");
+    var tr = new TextReader.for_memory ((char[]) b.data, (int) b.get_data_size (), "/gxml_memory");
     GXml.Node current = null;
     while (read_node (doc, tr, rtfunc) == ReadType.CONTINUE);
   }
@@ -524,10 +524,27 @@ public class GXml.TDocument : GXml.TNode, GXml.Document
       n = node.document.create_element (tr.const_local_name ());
       ReadType nrt = ReadType.CONTINUE;
       if (rntfunc != null) nrt = rntfunc (n, tr);
-      if (nrt == ReadType.NEXT)
-        tr.next ();
-      if (nrt == ReadType.CONTINUE)
-        node.children.add (n);
+      if (nrt == ReadType.NEXT) {
+        if (isempty) {
+          return ReadType.CONTINUE;
+        }
+        var cont = true;
+        while (cont) {
+          if (tr.read () != 1) return ReadType.STOP;
+          t = tr.node_type ();
+          if (t == Xml.ReaderType.END_ELEMENT) {
+            if (tr.const_local_name () == n.name) {
+              cont = false;
+            }
+          }
+        }
+        return ReadType.CONTINUE;
+      }
+      if (nrt == ReadType.STOP) {
+        tr.close ();
+        return ReadType.STOP;
+      }
+      node.children.add (n);
 #if DEBUG
       GLib.message ("ReadNode: next node:"+n.to_string ());
       GLib.message ("ReadNode: next node attributes:"+(tr.has_attributes ()).to_string ());
diff --git a/test/TDocumentTest.vala b/test/TDocumentTest.vala
index ff9527e..4a789d8 100644
--- a/test/TDocumentTest.vala
+++ b/test/TDocumentTest.vala
@@ -749,7 +749,7 @@ class TDocumentTest : GXmlTest {
                                assert (nc.name == "Read");
                                assert (nc.children.size == 2);
                                GLib.message ("from file");
-                               // Remove all
+                               // Remove all unwanted
                                TDocument.ReadTypeFunc f1 = (node, tr)=>{
                                        Test.message ("ReadType check node: "+node.name);
                                        if (node.name == "NoRead" || node.name == "NoReadChild") {
@@ -772,10 +772,10 @@ class TDocumentTest : GXmlTest {
                                assert (nc2.name == "Read");
                                assert (nc2.children.size == 1);
                                // Checking ReadType.STOP effect
-                               GLib.message ("from path");
+                               Test.message ("from path");
                                TDocument.ReadTypeFunc f2 = (node, tr)=>{
                                        Test.message ("ReadType check node: "+node.name);
-                                       if (node.name == "NoRead" || node.name == "NoReadChild") {
+                                       if (node.name == "NoReadChild") {
                                                Test.message ("Skiping node: "+node.name);
                                                return TDocument.ReadType.STOP;
                                        }
@@ -789,7 +789,7 @@ class TDocumentTest : GXmlTest {
                                assert (n3 != null);
                                assert (n3.name == "ReadTop");
                                assert (n3.children.size == 4);
-                               var nc3 = n3.children[2];
+                               var nc3 = n3.children[3];
                                assert (nc3 != null);
                                assert (nc3.name == "Read");
                                assert (nc3.children.size == 1);
@@ -803,7 +803,7 @@ class TDocumentTest : GXmlTest {
                                assert (n4 != null);
                                assert (n4.name == "ReadTop");
                                assert (n4.children.size == 4);
-                               var nc4 = n4.children[2];
+                               var nc4 = n4.children[3];
                                assert (nc4 != null);
                                assert (nc4.name == "Read");
                                assert (nc4.children.size == 1);
diff --git a/test/TElementTest.vala b/test/TElementTest.vala
index 7958249..d097fa5 100644
--- a/test/TElementTest.vala
+++ b/test/TElementTest.vala
@@ -639,37 +639,38 @@ class TElementTest : GXmlTest {
                                };
                                var d2 = new TDocument ();
                                TDocument.read_doc (d2, file, f1);
+                               GLib.message (@"$d2");
                                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
                                Test.message ("Skiping nodes using ReadType.STOP");
+                               //assert_not_reached ();
                                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);
+                                       if (node.name == "NoReadChild") {
+                                               Test.message ("Stoping on node: "+node.name);
                                                return TDocument.ReadType.STOP;
                                        }
                                        return TDocument.ReadType.CONTINUE;
                                };
                                var d3 = new TDocument ();
                                TDocument.read_doc (d3, file, f2);
-                               Test.message (@"$d3");
+                               Test.message (@"STOPED:$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];
+                               var nc3 = n3.children[3];
                                assert (nc3 != null);
                                assert (nc3.name == "Read");
                                assert (nc3.children.size == 1);
diff --git a/test/t-read-test.xml b/test/t-read-test.xml
index 9aaa842..c54e3fc 100644
--- a/test/t-read-test.xml
+++ b/test/t-read-test.xml
@@ -23,7 +23,9 @@
   <![CDATA[<greeting>Hello, world!</greeting>]]>
   <ReadTop>
     <Read/>
-    <NoRead/>
+    <NoRead>
+      <Child />
+    </NoRead>
     <Read/>
     <Read>
       <ReadChild/>


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