[gxml] GOM: Fixes on Parser to GomDocument



commit 5136efdbf16a6ff57430812cb93787c70a0c201f
Author: Daniel Espinosa <esodan gmail com>
Date:   Mon Oct 31 12:02:51 2016 -0600

    GOM: Fixes on Parser to GomDocument
    
    Fixed GomDocument.from_string() and its Unit Test
    gom-document/construct_from_string
    
    Fixed GomNode.insert_before() by checking for GomNode
    objects to add

 gxml/GomDocument.vala     |    2 ++
 gxml/GomElement.vala      |    1 +
 gxml/GomNode.vala         |    4 +---
 gxml/Parser.vala          |    4 ++++
 gxml/XParser.vala         |   41 ++++++++++++++++-------------------------
 test/GomDocumentTest.vala |    2 +-
 6 files changed, 25 insertions(+), 29 deletions(-)
---
diff --git a/gxml/GomDocument.vala b/gxml/GomDocument.vala
index 3ff3c9e..36eecd2 100644
--- a/gxml/GomDocument.vala
+++ b/gxml/GomDocument.vala
@@ -61,6 +61,8 @@ public class GXml.GomDocument : GomNode,
   }
 
   construct {
+    _local_name = "#document";
+    _node_type = DomNode.NodeType.DOCUMENT_NODE;
     _url = "about:blank";
     _origin = "";
     _compat_mode = "";
diff --git a/gxml/GomElement.vala b/gxml/GomElement.vala
index c6e42d4..edfaddf 100644
--- a/gxml/GomElement.vala
+++ b/gxml/GomElement.vala
@@ -159,6 +159,7 @@ public class GXml.GomElement : GomNode,
   construct {
     _node_type = DomNode.NodeType.ELEMENT_NODE;
     _attributes = new Attributes (this);
+    _local_name = "";
   }
 
   public GomElement (DomDocument doc, string local_name) {
diff --git a/gxml/GomNode.vala b/gxml/GomNode.vala
index 9022012..206b511 100644
--- a/gxml/GomNode.vala
+++ b/gxml/GomNode.vala
@@ -37,7 +37,6 @@ public class GXml.GomNode : Object,
   public string node_name {
     owned get {
       if (_local_name == null) return "NO NAME";
-      GLib.message ("GomNode: node_name");
       if (_prefix == null) return _local_name;
       return _prefix+":"+_local_name;
     }
@@ -124,7 +123,6 @@ public class GXml.GomNode : Object,
     _base_uri = null;
     _node_value = null;
     _child_nodes = new GomNodeList ();
-    GLib.message ("PI");
   }
 
   public bool has_child_nodes () { return (_child_nodes.size > 0); }
@@ -206,7 +204,7 @@ public class GXml.GomNode : Object,
   }
 
   public DomNode insert_before (DomNode node, DomNode? child) throws GLib.Error {
-    if (!(node is GXml.GNode))
+    if (!(node is GXml.GomNode))
       throw new DomError.INVALID_NODE_TYPE_ERROR (_("Invalid attempt to add invalid node type"));
     if (child != null && !this.contains (child))
       throw new DomError.NOT_FOUND_ERROR (_("Can't find child to insert node before"));
diff --git a/gxml/Parser.vala b/gxml/Parser.vala
index 588f805..ae27ad5 100644
--- a/gxml/Parser.vala
+++ b/gxml/Parser.vala
@@ -21,6 +21,10 @@
 
 using Gee;
 
+public errordomain GXml.ParserError {
+  INVALID_DATA
+}
+
 /**
  * XML parser engine for {@link DomDocument} implementations.
  */
diff --git a/gxml/XParser.vala b/gxml/XParser.vala
index 1dd7902..ec838f6 100644
--- a/gxml/XParser.vala
+++ b/gxml/XParser.vala
@@ -43,7 +43,8 @@ public class GXml.XParser : Object, GXml.Parser {
   }
   public void read_string (string str, GLib.Cancellable? cancellable) throws GLib.Error {
     StringBuilder s = new StringBuilder (str);
-    tr = new TextReader.for_memory ((char[]) s.data, (int) s.len, "/gxml_memory");
+    var stream = new GLib.MemoryInputStream.from_data (str.data);
+    read_stream (stream, cancellable);
   }
 
 
@@ -58,7 +59,7 @@ public class GXml.XParser : Object, GXml.Parser {
     var b = new MemoryOutputStream.resizable ();
     b.splice (istream, 0);
 #if DEBUG
-    GLib.message ("FILE:"+(string)b.data);
+    GLib.message ("DATA:"+(string)b.data);
 #endif
     tr = new TextReader.for_memory ((char[]) b.data, (int) b.get_data_size (), "/gxml_memory");
     while (read_node (_document));
@@ -72,19 +73,27 @@ public class GXml.XParser : Object, GXml.Parser {
    */
   public bool read_node (DomNode node) throws GLib.Error {
     GXml.DomNode n = null;
-    string prefix, nsuri;
-    if (tr.read () != 1) return false;
-    if (tr.next () != 1) return false;
-    var t = tr.node_type (); // FIXME: Convert to NodeType and store
+    string prefix = null, nsuri = null;
 #if DEBUG
     GLib.message ("ReadNode: Current Node:"+node.node_name);
 #endif
+    int res = tr.read ();
+    if (res == -1)
+      throw new ParserError.INVALID_DATA (_("Can't read node data"));
+#if DEBUG
+    if (res == 0)
+      GLib.message ("ReadNode: No more nodes");
+#endif
+    if (res == 0) return false;
+    var t = tr.node_type ();
     switch (t) {
     case Xml.ReaderType.NONE:
 #if DEBUG
       GLib.message ("Type NONE");
 #endif
-      if (tr.read () != 1) return false;
+      res = tr.read ();
+      if (res == -1)
+        throw new ParserError.INVALID_DATA (_("Can't read node data"));
       break;
     case Xml.ReaderType.ELEMENT:
       bool isempty = (tr.is_empty_element () == 1);
@@ -92,24 +101,6 @@ public class GXml.XParser : Object, GXml.Parser {
       if (isempty) GLib.message ("Is Empty node:"+node.node_name);
       GLib.message ("ReadNode: Element: "+tr.const_local_name ());
 #endif
-      if (isempty) {
-        return true;;
-      }
-      var cont = true;
-      while (cont) {
-        if (tr.read () != 1) return false;
-        t = tr.node_type ();
-        if (t == Xml.ReaderType.END_ELEMENT) {
-          if (tr.const_local_name () == n.node_name) {
-            cont = false;
-          }
-        }
-      }
-      return true;
-#if DEBUG
-      //GLib.message ("ReadNode: next node:"+n.to_string ());
-      //GLib.message ("ReadNode: next node attributes:"+(tr.has_attributes ()).to_string ());
-#endif
       prefix = tr.prefix ();
       if (prefix != null) {
         nsuri = tr.lookup_namespace (prefix);
diff --git a/test/GomDocumentTest.vala b/test/GomDocumentTest.vala
index 8c4b57d..fa2b5f7 100644
--- a/test/GomDocumentTest.vala
+++ b/test/GomDocumentTest.vala
@@ -144,7 +144,7 @@ class GomDocumentTest : GXmlTest {
                                GXml.DomNode document_element;
 
                                xml = "<Fruits><Apple></Apple><Orange></Orange></Fruits>";
-                               doc = new GDocument.from_string (xml);
+                               doc = new GomDocument.from_string (xml);
                                assert (doc.document_element != null);
                                document_element = doc.document_element;
                                assert (document_element.node_name == "Fruits");


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