[gxml] Basic Reading implemented for TDocument using Xml.TextReader



commit 6f417a48a96d23cd6e3ecba78b8a6524aab0238c
Author: Daniel Espinosa <esodan gmail com>
Date:   Tue Mar 8 20:11:32 2016 -0600

    Basic Reading implemented for TDocument using Xml.TextReader

 gxml/TDocument.vala     |   88 +++++++++++++++++++++++++++++------------------
 test/Makefile.am        |    1 +
 test/TDocumentTest.vala |   39 +++++++++++++++++++++
 test/t-read-test.xml    |   15 ++++++++
 4 files changed, 109 insertions(+), 34 deletions(-)
---
diff --git a/gxml/TDocument.vala b/gxml/TDocument.vala
index d183400..ce7cf42 100644
--- a/gxml/TDocument.vala
+++ b/gxml/TDocument.vala
@@ -404,86 +404,106 @@ public class GXml.TDocument : GXml.TNode, GXml.Document
     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 ());
-    GXml.Node current, next;
-    current = next = null;
+    GXml.Node current = null;
     ReadType rt = ReadType.CONTINUE;
-    while (rt == ReadType.CONTINUE) {
-      next = read_node (doc, current, tr);
-      if (rtfunc != null) rt = rtfunc (next, tr);
-      else rt = ReadType.CONTINUE;
-      switch (rt) {
-      case ReadType.NEXT:
-        if (tr.next () == 1) {
-          rt = ReadType.CONTINUE;
-          current = next;
-        }
-        else rt = ReadType.STOP;
-        break;
-      case ReadType.CONTINUE:
-        if (tr.read () == 1) {
-          current = next;
-          rt = ReadType.CONTINUE;
-        } else rt = ReadType.STOP;
-        break;
-      }
-    }
+    while (read_node (doc, tr, rtfunc) == ReadType.CONTINUE);
   }
   /**
    * Parse current node in { link Xml.TextReader}.
    *
    * Returns: a { link GXml.Node} respresenting current parsed one.
    */
-  public static GXml.Node? read_node (GXml.Document doc, GXml.Node? node, Xml.TextReader tr) throws 
GLib.Error {
+  public static ReadType read_node (GXml.Node node,
+                                      Xml.TextReader tr,
+                                      ReadTypeFunc? rntfunc = null) throws GLib.Error {
     GXml.Node n = null;
+    ReadType rt = ReadType.CONTINUE;
+    if (rntfunc != null) rt = rntfunc (node, tr);
+    if (rt == ReadType.CONTINUE)
+      if (tr.read () != 1) return ReadType.STOP;
+    if (rt == ReadType.NEXT)
+      if (tr.next () != 1) return ReadType.STOP;
     var t = tr.node_type ();
+    GLib.message ("ReadNode: Current Node:"+node.name);
     switch (t) {
     case Xml.ReaderType.NONE:
-      return null;
+      GLib.message ("Type NONE");
+      if (tr.read () != 1) return ReadType.STOP;
+      break;
     case Xml.ReaderType.ELEMENT:
-      n = doc.create_element (tr.const_local_name ()); // FIXME: Ns
-      if (node == null)
-        doc.children.add (n);
-      else
-        node.children.add (n);
+      GLib.message ("ReadNode: Element: "+tr.const_local_name ());
+      n = node.document.create_element (tr.const_local_name ()); // FIXME: Ns
+      node.children.add (n);
+      GLib.message ("ReadNode: next node:"+n.to_string ());
+      GLib.message ("ReadNode: next node attributes:"+(tr.has_attributes ()).to_string ());
       var c = tr.move_to_first_attribute ();
       while (c == 1) {
-        (n as GXml.Element).set_attr (tr.const_local_name (), tr.get_attribute (tr.const_local_name ()));
+        var attrname = tr.const_local_name ();
+        GLib.message ("Attribute: "+tr.const_local_name ());
+        tr.read_attribute_value ();
+        if (tr.node_type () == Xml.ReaderType.TEXT) {
+          var attrval = tr.read_string ();
+          GLib.message ("Attribute:"+attrname+" Value: "+attrval);
+          (n as GXml.Element).set_attr (attrname, attrval);
+        }
         c = tr.move_to_next_attribute (); // FIXME: Ns
       }
-      return n;
+      while (read_node (n, tr, rntfunc) == ReadType.CONTINUE);
+      GLib.message ("Current Document: "+node.document.to_string ());
+      break;
     case Xml.ReaderType.ATTRIBUTE:
+      GLib.message ("Type ATTRIBUTE");
       break;
     case Xml.ReaderType.TEXT:
+      GLib.message ("Type TEXT");
+      var txtval = tr.read_string ();
+      GLib.message ("ReadNode: Text Node : '"+txtval+"'");
+      n = node.document.create_text (txtval);
+      node.children.add (n);
       break;
     case Xml.ReaderType.CDATA:
+      GLib.message ("Type CDATA");
       break;
     case Xml.ReaderType.ENTITY_REFERENCE:
+      GLib.message ("Type ENTITY_REFERENCE");
       break;
     case Xml.ReaderType.ENTITY:
+      GLib.message ("Type ENTITY");
       break;
     case Xml.ReaderType.PROCESSING_INSTRUCTION:
+      GLib.message ("Type PROCESSING_INSTRUCTION");
       break;
     case Xml.ReaderType.COMMENT:
+      GLib.message ("Type COMMENT");
       break;
     case Xml.ReaderType.DOCUMENT:
+      GLib.message ("Type DOCUMENT");
       break;
     case Xml.ReaderType.DOCUMENT_TYPE:
+      GLib.message ("Type DOCUMENT_TYPE");
       break;
     case Xml.ReaderType.DOCUMENT_FRAGMENT:
+      GLib.message ("Type DOCUMENT_FRAGMENT");
       break;
     case Xml.ReaderType.NOTATION:
+      GLib.message ("Type NOTATION");
       break;
     case Xml.ReaderType.WHITESPACE:
+      GLib.message ("Type WHITESPACE");
       break;
     case Xml.ReaderType.SIGNIFICANT_WHITESPACE:
+      GLib.message ("Type SIGNIFICANT_WHITESPACE");
       break;
     case Xml.ReaderType.END_ELEMENT:
-      break;
+      GLib.message ("Type END_ELEMENT");
+      return ReadType.STOP;
     case Xml.ReaderType.END_ENTITY:
-      break;
+      GLib.message ("Type END_ENTITY");
+      return ReadType.STOP;
     case Xml.ReaderType.XML_DECLARATION:
+      GLib.message ("Type XML_DECLARATION");
       break;
     }
-    return null;
+    return ReadType.CONTINUE;
   }
 }
diff --git a/test/Makefile.am b/test/Makefile.am
index 05b1282..b8b120c 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -158,5 +158,6 @@ EXTRA_DIST += \
        test.xml \
        test-collection.xml \
        index.html \
+       t-read-test.xml \
        $(NULL)
 
diff --git a/test/TDocumentTest.vala b/test/TDocumentTest.vala
index 6278a51..905b315 100644
--- a/test/TDocumentTest.vala
+++ b/test/TDocumentTest.vala
@@ -426,5 +426,44 @@ class TDocumentTest : GXmlTest {
                        var doc = new TDocument ();
                        assert (doc.parent == null);
                });
+               Test.add_func ("/gxml/t-document/read/basic", () => {
+                       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);
+                               GLib.message ("Doc:"+d.to_string ());
+                               assert (d.root != null);
+                               assert (d.root.name == "Sentences");
+                               assert (d.root.children.size == 3);
+                               var s1 = d.root.children[0];
+                               assert (s1 != null);
+                               assert (s1.name == "Sentence");
+                               var p1 = s1.attrs["lang"];
+                               assert (p1 != null);
+                               assert (p1.value == "en");
+                               assert (s1.children.size == 1);
+                               assert (s1.children[0] is GXml.Text);
+                               assert (s1.children[0].value == "I like the colour blue.");
+                               var s2 = d.root.children[1];
+                               assert (s2 != null);
+                               assert (s2.name == "Sentence");
+                               var p2 = s2.attrs["lang"];
+                               assert (p2 != null);
+                               assert (p2.value == "es");
+                               assert (s2.children.size == 1);
+                               assert (s2.children[0] is GXml.Text);
+                               assert (s2.children[0].value == "Español");
+                               var s3  = d.root.children[2];
+                               assert (s3 != null);
+                               assert (s3.name == "Authors");
+                               var p3 = s3.attrs["year"];
+                               assert (p3 != null);
+                               assert (p3.value == "2016");
+                               assert (s3.children.size == 2);
+                               assert (s3.children[0] is GXml.Element);
+                               assert (s3.children[0].name == "Author");
+                       } catch (GLib.Error e) { GLib.message ("ERROR: "+e.message); assert_not_reached (); }
+               });
        }
 }
diff --git a/test/t-read-test.xml b/test/t-read-test.xml
new file mode 100644
index 0000000..eed0f5c
--- /dev/null
+++ b/test/t-read-test.xml
@@ -0,0 +1,15 @@
+<?xml version="1.0"?>
+<Sentences>
+  <Sentence lang="en">I like the colour blue.</Sentence>
+  <Sentence lang="es">Español</Sentence>
+  <Authors year="2016" collection="Back">
+    <Author>
+      <Name>Fred</Name>
+      <Email>fweasley hogwarts co uk</Email>
+    </Author>
+    <Author>
+      <Name>George</Name>
+      <Email>gweasley hogwarts co uk</Email>
+    </Author>
+  </Authors>
+</Sentences>


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