[gxml] Fixes on GomNode, GomAttr and XParser



commit 42f8f854f59eff255fa4fa60176f583761e70793
Author: Daniel Espinosa <esodan gmail com>
Date:   Wed Jan 18 19:41:41 2017 -0600

    Fixes on GomNode, GomAttr and XParser
    
    * GomAttr: use local_name for node's name
    * GomNode and GomAttr node_name, return local_name
      if prefix is null or empty
    * Improved error messages
    * XParser fixes parse documents with just root nodes
    * XParser fixes try to add a Text nodes to DomDocument

 gxml/GomAttr.vala    |    2 +-
 gxml/GomElement.vala |   49 +++++++++++++++++++++++++++++++++----------------
 gxml/GomNode.vala    |    2 +-
 gxml/XParser.vala    |   24 +++++++++++++++++-------
 4 files changed, 52 insertions(+), 25 deletions(-)
---
diff --git a/gxml/GomAttr.vala b/gxml/GomAttr.vala
index 2c0ac34..02186da 100644
--- a/gxml/GomAttr.vala
+++ b/gxml/GomAttr.vala
@@ -30,7 +30,7 @@ public class GXml.GomAttr : GXml.GomNode, GXml.DomAttr {
   public string name {
     owned get {
       string s = "";
-      if (_prefix != null) s = _prefix+":";
+      if (_prefix != null && _prefix != "") s = _prefix+":";
       return s+_local_name;
     }
   }
diff --git a/gxml/GomElement.vala b/gxml/GomElement.vala
index a0284da..929643a 100644
--- a/gxml/GomElement.vala
+++ b/gxml/GomElement.vala
@@ -303,13 +303,13 @@ public class GXml.GomElement : GomNode,
      * namespace
      */
     public DomNode? set_named_item (DomNode node) throws GLib.Error {
-      if ((":" in node.node_name)
-          || node.node_name == "")
-        throw new DomError.INVALID_CHARACTER_ERROR (_("Invalid attribute name"));
+      if ((":" in (node as DomAttr).local_name)
+          || (node as DomAttr).local_name == "")
+        throw new DomError.INVALID_CHARACTER_ERROR (_("Invalid attribute name: %s"), (node as 
DomAttr).local_name);
       if (!(node is DomAttr))
         throw new DomError.HIERARCHY_REQUEST_ERROR (_("Invalid node type. DomAttr was expected"));
-      set (node.node_name, node.node_value);
-      return new GomAttr (_element, node.node_name, node.node_value);
+      set ((node as DomAttr).local_name, node.node_value);
+      return new GomAttr (_element, (node as DomAttr).local_name, node.node_value);
     }
     public DomNode? remove_named_item (string name) throws GLib.Error {
       if (":" in name) return null;
@@ -342,23 +342,25 @@ public class GXml.GomElement : GomNode,
     }
     // Introduced in DOM Level 2:
     public DomNode? set_named_item_ns (DomNode node) throws GLib.Error {
-      if (node.node_name == "")
-        throw new DomError.INVALID_CHARACTER_ERROR (_("Invalid attribute name"));
+      if ((node as DomAttr).local_name == "" || ":" in (node as DomAttr).local_name)
+        throw new DomError.INVALID_CHARACTER_ERROR (_("Invalid attribute name: %s"),(node as 
DomAttr).local_name);
       if (!(node is DomAttr))
         throw new DomError.HIERARCHY_REQUEST_ERROR (_("Invalid node type. DomAttr was expected"));
-      if (((node as DomAttr).prefix == null || (node as DomAttr).prefix == "")
-          && node.node_name != "xmlns")
-        throw new DomError.NAMESPACE_ERROR (_("Invalid namespaced attribute's name and prefix"));
+
       if ((node as DomAttr).prefix == "xmlns"
           && (node as DomAttr).namespace_uri != "http://www.w3.org/2000/xmlns/";
               && (node as DomAttr).namespace_uri != "http://www.w3.org/2000/xmlns";)
         throw new DomError.NAMESPACE_ERROR (_("Namespace attributes prefixed with xmlns should use a 
namespace uri http://www.w3.org/2000/xmlns";));
       if ((node as DomAttr).prefix == ""
           || (node as DomAttr).prefix == null
-          && node.node_name != "xmlns")
-        throw new DomError.NAMESPACE_ERROR (_("Namespaced attributes should provide a valid prefix and 
namespace"));
+          && (node as DomAttr).local_name != "xmlns") {
+        string s = (node as DomAttr).local_name;
+        if ((node as DomAttr).namespace_uri != null)
+          s += "=<"+(node as DomAttr).namespace_uri+">";
+        throw new DomError.NAMESPACE_ERROR (_("Namespaced attributes should provide a non-null, non-empty 
prefix: %s"),s);
+      }
       if ((node as DomAttr).prefix == "xmlns"
-          && node.node_name == "xmlns")
+          && (node as DomAttr).local_name == "xmlns")
         throw new DomError.NAMESPACE_ERROR (_("Invalid namespace attribute's name."));
       if ((node as DomAttr).prefix == "xmlns"
           || (node as DomAttr).local_name == "xmlns") {
@@ -368,26 +370,34 @@ public class GXml.GomElement : GomNode,
       }
       if ((node as DomAttr).namespace_uri == "http://www.w3.org/2000/xmlns/";
           || (node as DomAttr).namespace_uri == "http://www.w3.org/2000/xmlns";) {
+#if DEBUG
         GLib.message ("Searching for duplicated ns..."+node.node_value
                   +" NS: "+(node as DomAttr).namespace_uri);
-        if (node.node_name == "xmlns") {
+#endif
+        if ((node as DomAttr).local_name == "xmlns") {
           string ns_uri = _element.lookup_namespace_uri (null);
           if (ns_uri != null && ns_uri != node.node_value) {
+#if DEBUG
             GLib.message ("NSURI: "+ns_uri+" NSURI Attr:"+node.node_value);
+#endif
             throw new DomError.NAMESPACE_ERROR
                       (_("Redefinition of default namespace for %s")
                         .printf (node.node_value));
           }
         }
         if ((node as DomAttr).prefix == "xmlns") {
+#if DEBUG
           GLib.message ("Attr Prefix = "+(node as DomAttr).prefix
                       + "Attr Name: "+(node as DomAttr).local_name);
+#endif
           string nsprefix = _element.lookup_prefix (node.node_value);
-          string nsuri = _element.lookup_namespace_uri (node.node_name);
+          string nsuri = _element.lookup_namespace_uri ((node as DomAttr).local_name);
           if ((nsprefix != null || nsuri != null)
               && (nsprefix != (node as DomAttr).local_name
                   || nsuri != node.node_value)) {
+#if DEBUG
             GLib.message ("Prefix: "+nsprefix+" Prefix Attr:"+(node as DomAttr).local_name);
+#endif
             throw new DomError.NAMESPACE_ERROR
                       (_("Redefinition of namespace's prefix for %s")
                         .printf (node.node_value));
@@ -396,8 +406,10 @@ public class GXml.GomElement : GomNode,
       }
       if ((node as DomAttr).namespace_uri != "http://www.w3.org/2000/xmlns/";
           && (node as DomAttr).namespace_uri != "http://www.w3.org/2000/xmlns";){
+#if DEBUG
         GLib.message ("No namespace attribute: "+(node as DomAttr).namespace_uri
                     + ":"+(node as DomAttr).prefix);
+#endif
         string nsn = _element.lookup_namespace_uri ((node as DomAttr).prefix);
         string nspn = _element.lookup_prefix (nsn);
         if (nspn != (node as DomAttr).prefix
@@ -416,15 +428,18 @@ public class GXml.GomElement : GomNode,
       if ((node as DomAttr).prefix != null
           && (node as DomAttr).prefix != "")
         p = (node as DomAttr).prefix + ":";
+#if DEBUG
       GLib.message ("Attribute to set: "+p+(node as DomAttr).local_name
                     +"="+node.node_value);
+#endif
       set (p+(node as DomAttr).local_name,
           node.node_value);
 
       var attr = new GomAttr.namespace (_element,
                                     (node as DomAttr).namespace_uri,
                                     (node as DomAttr).prefix,
-                                    node.node_name, node.node_value);
+                                    (node as DomAttr).local_name,
+                                    node.node_value);
       return attr;
     }
   }
@@ -462,6 +477,8 @@ public class GXml.GomElement : GomNode,
         throw new DomError.NAMESPACE_ERROR (_("Invalid attribute name. Just one prefix is allowed"));
       p = s[0];
       n = s[1];
+      if (":" in n)
+        throw new DomError.NAMESPACE_ERROR (_("Invalid attribute name. Invalid use of colon: %s"), n);
     } else
       n = name;
     if (namespace_uri == null && p == "")
diff --git a/gxml/GomNode.vala b/gxml/GomNode.vala
index 3c51908..018af80 100644
--- a/gxml/GomNode.vala
+++ b/gxml/GomNode.vala
@@ -61,7 +61,7 @@ public class GXml.GomNode : Object,
   public string node_name {
     owned get {
       if (_local_name == null) return "NO NAME";
-      if (_prefix == null) return _local_name;
+      if (_prefix == null || _prefix == "") return _local_name;
       return _prefix+":"+_local_name;
     }
   }
diff --git a/gxml/XParser.vala b/gxml/XParser.vala
index c081891..348e6e9 100644
--- a/gxml/XParser.vala
+++ b/gxml/XParser.vala
@@ -134,7 +134,10 @@ public class GXml.XParser : Object, GXml.Parser {
 #if DEBUG
         GLib.message ("Parsing child nodes of: "+node.node_name);
 #endif
-    read_child_nodes (node);
+    if (current_is_element ())
+      read_child_element (node);
+    else
+      read_child_nodes (node);
   }
   /**
    * Use parser to go to next parsed node.
@@ -275,9 +278,12 @@ public class GXml.XParser : Object, GXml.Parser {
   public void read_child_nodes (DomNode parent) throws GLib.Error {
     bool cont = true;
     while (cont) {
+#if DEBUG
+      GLib.message ("Parent: "+parent.node_name+" Current node: "+current_node_name ());
+#endif
       if (!move_next_node ()) return;
 #if DEBUG
-      GLib.message ("Parent: "+parent.node_name+" Current child node: "+current_node_name ());
+      GLib.message ("Parent: "+parent.node_name+" Next Current child node: "+current_node_name ());
 #endif
       if (current_is_element ())
         cont = read_child_element (parent);
@@ -294,7 +300,7 @@ public class GXml.XParser : Object, GXml.Parser {
    *
    * Returns: true if node has been created and appended to parent.
    */
-  public bool read_child_node (DomNode parent) {
+  public bool read_child_node (DomNode parent) throws GLib.Error {
     DomNode n = null;
     var t = tr.node_type ();
     switch (t) {
@@ -315,8 +321,10 @@ public class GXml.XParser : Object, GXml.Parser {
       GLib.message ("Type TEXT");
       GLib.message ("ReadNode: Text Node : '"+txtval+"'");
 #endif
-      n = _document.create_text_node (txtval);
-      parent.append_child (n);
+      if (!(parent is DomDocument)) {
+        n = _document.create_text_node (txtval);
+        parent.append_child (n);
+      }
       break;
     case Xml.ReaderType.CDATA:
       break;
@@ -383,8 +391,10 @@ public class GXml.XParser : Object, GXml.Parser {
       GLib.message ("ReadNode: Text Node : '"+stxtval+"'");
       GLib.message ("Type SIGNIFICANT_WHITESPACE");
 #endif
-      n = _document.create_text_node (stxtval);
-      parent.append_child (n);
+      if (!(parent is DomDocument)) {
+        n = _document.create_text_node (stxtval);
+        parent.append_child (n);
+      }
       break;
     case Xml.ReaderType.END_ELEMENT:
 #if DEBUG


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