[gxml] Added support to TDocument to read namespaces for Elements



commit 0596d3d026098547780eef4b8d25c6ab0ba709f8
Author: Daniel Espinosa <esodan gmail com>
Date:   Tue Mar 8 20:51:32 2016 -0600

    Added support to TDocument to read namespaces for Elements

 gxml/TDocument.vala     |   28 ++++++++++++++++++++--------
 test/TDocumentTest.vala |   12 ++++++++++++
 test/t-read-test.xml    |    2 +-
 3 files changed, 33 insertions(+), 9 deletions(-)
---
diff --git a/gxml/TDocument.vala b/gxml/TDocument.vala
index ce7cf42..2a4be7a 100644
--- a/gxml/TDocument.vala
+++ b/gxml/TDocument.vala
@@ -417,6 +417,7 @@ public class GXml.TDocument : GXml.TNode, GXml.Document
                                       Xml.TextReader tr,
                                       ReadTypeFunc? rntfunc = null) throws GLib.Error {
     GXml.Node n = null;
+    string prefix;
     ReadType rt = ReadType.CONTINUE;
     if (rntfunc != null) rt = rntfunc (node, tr);
     if (rt == ReadType.CONTINUE)
@@ -432,22 +433,33 @@ public class GXml.TDocument : GXml.TNode, GXml.Document
       break;
     case Xml.ReaderType.ELEMENT:
       GLib.message ("ReadNode: Element: "+tr.const_local_name ());
-      n = node.document.create_element (tr.const_local_name ()); // FIXME: Ns
+      n = node.document.create_element (tr.const_local_name ());
       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) {
-        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);
+        if (tr.is_namespace_decl () == 1) {
+          GLib.message ("Is Namespace Declaration...");
+          string nsp = tr.const_local_name ();
+          tr.read_attribute_value ();
+          if (tr.node_type () == Xml.ReaderType.TEXT) {
+            string nuri = tr.read_string ();
+            n.set_namespace (nuri,nsp);
+          }
+        } else {
+          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
       }
+       // FIXME: Ns
       while (read_node (n, tr, rntfunc) == ReadType.CONTINUE);
       GLib.message ("Current Document: "+node.document.to_string ());
       break;
diff --git a/test/TDocumentTest.vala b/test/TDocumentTest.vala
index 485f3d1..a6e2c81 100644
--- a/test/TDocumentTest.vala
+++ b/test/TDocumentTest.vala
@@ -489,5 +489,17 @@ class TDocumentTest : GXmlTest {
                                assert (a2.children[1].children[0].value == "gweasley hogwarts co uk");
                        } catch (GLib.Error e) { GLib.message ("ERROR: "+e.message); assert_not_reached (); }
                });
+               Test.add_func ("/gxml/t-document/read/namespace", () => {
+                       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.namespaces.size == 1);
+                       } 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
index eed0f5c..bfd16c3 100644
--- a/test/t-read-test.xml
+++ b/test/t-read-test.xml
@@ -1,5 +1,5 @@
 <?xml version="1.0"?>
-<Sentences>
+<Sentences xmlns:gxml="http://wiki.gnome.org/GXml";>
   <Sentence lang="en">I like the colour blue.</Sentence>
   <Sentence lang="es">EspaƱol</Sentence>
   <Authors year="2016" collection="Back">


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