[gxml] StreamReader: implemented number of doc children



commit 361c7e8f386a545b37693c3992e4a7650853b3aa
Author: Daniel Espinosa <esodan gmail com>
Date:   Sun May 31 23:05:47 2020 -0500

    StreamReader: implemented number of doc children
    
    Fix issue #38

 gxml/StreamReader.vala     | 82 +++++++++++++++++++++++-----------------------
 test/StreamReaderTest.vala | 47 ++++++++++++++++++++++++++
 2 files changed, 88 insertions(+), 41 deletions(-)
---
diff --git a/gxml/StreamReader.vala b/gxml/StreamReader.vala
index 243d8c2..972fc8e 100644
--- a/gxml/StreamReader.vala
+++ b/gxml/StreamReader.vala
@@ -131,44 +131,49 @@ public class GXml.StreamReader : GLib.Object {
     parse_doc_nodes ();
   }
 
-  public void parse_doc_nodes () throws GLib.Error
-  {
-    try {
-        read_byte ();
-    } catch {
-        return;
-    }
-    while (true) {
-      if (cur_char () == '<') {
-          try {
+    public void parse_doc_nodes () throws GLib.Error
+    {
+        try {
             read_byte ();
-          } catch {
-              break;
-          }
-          if (is_space (cur_char ())) {
-            throw new StreamReaderError.INVALID_DOCUMENT_ERROR (_("Invalid document: unexpected space 
character before node's name"));
-          }
-          if (cur_char () == '?') {
-              if (start) {
-                  parse_xml_dec ();
-                  start = false;
-                  read_text_node ();
-                  continue;
-              } else {
-                  parse_pi_dec ();
-                  read_text_node ();
-                  continue;
-              }
-          } else if (cur_char () == '!') {
-              parse_comment_dec ();
-              read_text_node ();
-              continue;
-          }
-          break;
-      }
-      break;
+        } catch {
+            return;
+        }
+        while (true) {
+            if (cur_char () != '<') {
+                throw new StreamReaderError.INVALID_DOCUMENT_ERROR (_("Invalid document: expected '<' 
character"));
+            }
+            try {
+                read_byte ();
+            } catch {
+                return;
+            }
+            if (is_space (cur_char ())) {
+                throw new StreamReaderError.INVALID_DOCUMENT_ERROR (_("Invalid document: unexpected space 
character before node's name"));
+            }
+            if (cur_char () != '?' && cur_char () != '!') {
+                return;
+            }
+            if (cur_char () == '?') {
+                if (start) {
+                    parse_xml_dec ();
+                    start = false;
+                    read_text_node ();
+                    message ("Stoped at: %c", cur_char ());
+                    continue;
+                } else {
+                    parse_pi_dec ();
+                    read_text_node ();
+                    message ("Stoped at: %c", cur_char ());
+                    continue;
+                }
+            } else if (cur_char () == '!') {
+                parse_comment_dec ();
+                read_text_node ();
+                message ("Stoped at: %c", cur_char ());
+                continue;
+            }
+        }
     }
-  }
 
   private GXml.Element read_root_element () throws GLib.Error {
     return read_element (true);
@@ -412,11 +417,6 @@ public class GXml.StreamReader : GLib.Object {
 
     var t = document.create_text_node (text.str);
     document.append_child (t);
-    try {
-      read_byte ();
-    } catch {
-        return;
-    }
   }
   private bool is_space (char c) {
     return c == 0x20 || c == 0x9 || c == 0xA || c == ' ' || c == '\t' || c == '\n';
diff --git a/test/StreamReaderTest.vala b/test/StreamReaderTest.vala
index bb16712..7acff4f 100644
--- a/test/StreamReaderTest.vala
+++ b/test/StreamReaderTest.vala
@@ -399,6 +399,53 @@ class GXmlTest {
                                loop.quit ();
                                return Source.REMOVE;
       });
+      loop.run ();
+               });
+               Test.add_func ("/gxml/stream-reader/mix", () => {
+      var loop = new GLib.MainLoop (null);
+      Idle.add (()=>{
+                               string str = """<?xml version="1.0"?>
+<?test-instruction CONTENT IN PI?>
+<!--This is a comment-->
+<BookStore>
+</BookStore>
+""";
+                               message ("Stream with Comments and PI");
+                               var doc = new Library ();
+                               try {
+                                       doc.read (str);
+                                       bool found1 = false;
+                                       bool found2 = false;
+                                       for (int i = 0; i < doc.child_nodes.length; i++) {
+                                               var n = doc.child_nodes.item (i);
+                                               if (n is DomProcessingInstruction) {
+                                                       found1 = true;
+                                                       message ("Text: '%s'", ((DomProcessingInstruction) 
n).target);
+                                                       assert ("test-instruction" == 
((DomProcessingInstruction) n).target);
+                                                       assert (" CONTENT IN PI" == 
((DomProcessingInstruction) n).data);
+                                               }
+                                               if (n is DomComment) {
+                                                       found2 = true;
+                                                       message ("Text: '%s'", ((DomComment) n).data);
+                                                       assert ("This is a comment" == ((DomComment) n).data);
+                                               }
+                                               if (n is DomElement) {
+                                                       message ("Element: %s", n.node_name);
+                                               }
+                                       }
+                                       assert (found1);
+                                       assert (found2);
+                                       assert (doc.store != null);
+                                       message (doc.write_string ());
+                                       assert (doc.document_element != null);
+                                       message ("Is BookStore?");
+                                       assert (doc.document_element is BookStore);
+                               } catch (GLib.Error e) {
+                                       warning ("Error while reading stream: %s", e.message);
+                               }
+                               loop.quit ();
+                               return Source.REMOVE;
+      });
       loop.run ();
                });
                Test.run ();


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