[gxml] Improved support for Namespaces
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gxml] Improved support for Namespaces
- Date: Wed, 10 Feb 2016 20:51:02 +0000 (UTC)
commit ef5f8070e16892ddef9368524be07c4661125373
Author: Daniel Espinosa <esodan gmail com>
Date: Wed Feb 10 14:19:53 2016 -0600
Improved support for Namespaces
* GElement implements set_ns_attr()
* xNode and derivatives enter to a maintenace status, in order
to be deprecated in future, some tests haven't been added
due to its internal complexity, like namespaces support. Is
better to relay on GNode and derivatives. DOM API will be added
in future versions to deprecate and remove xNode derivatives.
* Fixes on Unit Tests
gxml/Element.vala | 6 +++++-
gxml/GXmlElement.vala | 20 ++++++++++----------
gxml/TwElement.vala | 5 +++++
gxml/libxml-Element.vala | 17 ++++++++++++-----
po/POTFILES.in | 1 -
po/POTFILES.skip | 2 --
test/GDocumentTest.vala | 33 +++++++++++++++++++++++++++++++++
test/SerializableObjectModelTest.vala | 13 +++++++++++--
8 files changed, 76 insertions(+), 21 deletions(-)
---
diff --git a/gxml/Element.vala b/gxml/Element.vala
index aa9432b..defabe4 100644
--- a/gxml/Element.vala
+++ b/gxml/Element.vala
@@ -47,7 +47,11 @@ public interface GXml.Element : Object, GXml.Node
*/
public abstract GXml.Node get_attr (string name);
/**
- * Serach for a { link GXml.Attribute} with a given name and namespace uri.
+ * Set an { link GXml.Attribute} with a given name, value and namespace.
+ */
+ public abstract void set_ns_attr (Namespace ns, string name, string value);
+ /**
+ * Search for a { link GXml.Attribute} with a given name and namespace uri.
*
* To get a attibute from { link GXml.Node.attrs} with a given namespace
* prefix, use "prefix:name".
diff --git a/gxml/GXmlElement.vala b/gxml/GXmlElement.vala
index 74155d7..dcb9dc6 100644
--- a/gxml/GXmlElement.vala
+++ b/gxml/GXmlElement.vala
@@ -41,15 +41,8 @@ public class GXml.GElement : GXml.GNode, GXml.Element
// GXml.Element
public void set_attr (string aname, string avalue)
{
- if (":" in aname) {
- string[] cname = aname.split(":");
- if (cname.length > 2) return;
- bool found = false;
- foreach (Namespace ns in namespaces) {
- if (ns.prefix == cname[0]) found = true;
- }
- if (!found) return;
- }
+ if (_node == null) return;
+ if (":" in aname) return;
_node->set_prop (aname, avalue);
}
public GXml.Node get_attr (string name)
@@ -78,11 +71,18 @@ public class GXml.GElement : GXml.GNode, GXml.Element
}
return null;
}
+ public void set_ns_attr (Namespace ns, string name, string uri) {
+ if (_node == null) return;
+ var ins = _node->doc->search_ns (_node, ns.prefix);
+ if (ins != null) {
+ _node->set_ns_prop (ins, name, uri);
+ return;
+ }
+ }
public GXml.Node get_ns_attr (string name, string uri) {
if (_node == null) return null;
var a = _node->has_ns_prop (name, uri);
if (a == null) return null;
- Test.message ("Element NS Prop: "+a->name);
return new GAttribute (_doc, a);
}
public void normalize () {}
diff --git a/gxml/TwElement.vala b/gxml/TwElement.vala
index eec7427..6ed24b7 100644
--- a/gxml/TwElement.vala
+++ b/gxml/TwElement.vala
@@ -57,6 +57,11 @@ public class GXml.TwElement : GXml.TwNode, GXml.Element
}
return null;
}
+ public void set_ns_attr (Namespace ns, string name, string value) {
+ var att = new TwAttribute (document, name, value);
+ att.set_namespace (ns.uri, ns.prefix);
+ attrs.set (name, att);
+ }
public void normalize () {}
public string content {
owned get {
diff --git a/gxml/libxml-Element.vala b/gxml/libxml-Element.vala
index 380b7f8..8664e56 100644
--- a/gxml/libxml-Element.vala
+++ b/gxml/libxml-Element.vala
@@ -478,11 +478,18 @@ namespace GXml {
// GXml.Element interface
public void set_attr (string name, string value) { set_attribute (name, value); }
public GXml.Node get_attr (string name) { return (GXml.Node) get_attribute_node (name); }
- public GXml.Node get_ns_attr (string name, string uri) {
- var p = get_attr (name) as Attribute;
- if (p.namespace == null) return null;
- if (p.namespace.uri == uri) return (GXml.Node) p;
- return null;
+ public GXml.Node get_ns_attr (string name, string uri) {
+ if (node == null) return null;
+ var a = node->has_ns_prop (name, uri);
+ if (a == null) return null;
+ return new xAttr (a, this.owner_document);
+ }
+ public void set_ns_attr (Namespace ns, string name, string uri) {
+ if (node == null) return;
+ var attr = this.owner_document.create_attribute (name);
+ attr.value = value;
+ this.set_attribute_node (attr);
+ attr.set_namespace (ns.uri, ns.prefix);
}
}
}
diff --git a/po/POTFILES.in b/po/POTFILES.in
index e77f9a5..60d4f4f 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -17,5 +17,4 @@ gxml/SerializableGeeTreeMap.vala
gxml/SerializableObjectModel.vala
gxml/SerializableProperty.vala
gxml/Serializable.vala
-gxml/Serialization.vala
gxml/TwDocument.vala
diff --git a/po/POTFILES.skip b/po/POTFILES.skip
index faa7c52..cf84da0 100644
--- a/po/POTFILES.skip
+++ b/po/POTFILES.skip
@@ -17,7 +17,6 @@ gxml/SerializableGeeHashMap.c
gxml/SerializableGeeTreeMap.c
gxml/SerializableObjectModel.c
gxml/SerializableProperty.c
-gxml/Serialization.c
gxml/TwDocument.c
sub/gxml/Document.c
sub/gxml/Enumeration.c
@@ -35,5 +34,4 @@ sub/gxml/SerializableGeeHashMap.c
sub/gxml/SerializableGeeTreeMap.c
sub/gxml/SerializableObjectModel.c
sub/gxml/SerializableProperty.c
-sub/gxml/Serialization.c
sub/gxml/TwDocument.c
diff --git a/test/GDocumentTest.vala b/test/GDocumentTest.vala
index e294a4e..1a50b91 100644
--- a/test/GDocumentTest.vala
+++ b/test/GDocumentTest.vala
@@ -286,5 +286,38 @@ class GDocumentTest : GXmlTest {
assert (cs1[1] == "<Sentences><Sentence lang=\"en\">I like the colour
blue.</Sentence><Sentence lang=\"de\">Ich liebe die
Tür.</Sentence><Authors><Author><Name>Fred</Name><Email>fweasley hogwarts co
uk</Email></Author><Author><Name>George</Name><Email>gweasley hogwarts co
uk</Email></Author></Authors></Sentences>");
} catch { assert_not_reached (); }
});
+ Test.add_func ("/gxml/gdocument/namespace", () => {
+ try {
+ GDocument doc = new GDocument.from_string ("<root><child/></root>");
+ doc.set_namespace ("http://www.gnome.org/GXml","gxml");
+ assert (doc.root != null);
+ assert (doc.root.namespaces != null);
+ assert (doc.root.namespaces.size == 1);
+ assert (doc.root.namespaces[0].prefix == "gxml");
+ assert (doc.root.namespaces[0].uri == "http://www.gnome.org/GXml");
+ assert (doc.root.children != null);
+ assert (doc.root.children.size == 1);
+ var c = doc.root.children[0];
+ c.set_namespace ("http://www.gnome.org/GXml2","gxml2");
+ assert (c.namespaces != null);
+ assert (c.namespaces.size == 1);
+ assert (c.namespaces[0].prefix == "gxml2");
+ assert (c.namespaces[0].uri == "http://www.gnome.org/GXml2");
+ (c as Element).set_attr ("gxml:prop","val");
+ var p = (c as Element).get_attr ("gxml:prop");
+ assert (p == null);
+ Test.message ("ROOT: "+doc.root.to_string ());
+ assert (doc.root.to_string () == "<root
xmlns:gxml=\"http://www.gnome.org/GXml\"><child xmlns:gxml2=\"http://www.gnome.org/GXml2\"/></root>");
+ (c as Element).set_ns_attr (doc.root.namespaces[0], "prop", "Ten");
+ Test.message ("ROOT: "+doc.root.to_string ());
+ assert (c.attrs.size == 1);
+ var pt = c.attrs.get ("prop");
+ assert (pt != null);
+ var pt2 = (c as Element).get_ns_attr ("prop", doc.root.namespaces[0].uri);
+ } catch (GLib.Error e) {
+ GLib.message ("ERROR: "+ e.message);
+ assert_not_reached ();
+ }
+ });
}
}
diff --git a/test/SerializableObjectModelTest.vala b/test/SerializableObjectModelTest.vala
index 349155b..498fada 100644
--- a/test/SerializableObjectModelTest.vala
+++ b/test/SerializableObjectModelTest.vala
@@ -283,6 +283,7 @@ public class NameSpace : SerializableObjectModel
{
public override bool set_namespace (GXml.Node node)
{
+ Test.message ("Setting default namespace");
node.set_namespace ("http://www.gnome.org/GXml", "gxml");
return true;
}
@@ -970,7 +971,14 @@ class SerializableObjectModelTest : GXmlTest
assert (found);
var tscunkn = cunkn.children.get (0);
assert (tscunkn is GXml.Text);
- assert (element.content == "FAKE TEXT");
+ bool ctf = false;
+ foreach (GXml.Node pnt in element.children) {
+ if (pnt is GXml.Text) {
+ if (pnt.value == "FAKE TEXT")
+ ctf = true;
+ }
+ }
+ assert (ctf);
found = false;
foreach (GXml.Node n in element.children) {
if (n.name == "UnknownNode") {
@@ -1059,7 +1067,8 @@ class SerializableObjectModelTest : GXmlTest
var ns = new NameSpace ();
var doc = new GDocument ();
ns.serialize (doc);
- assert (doc.root.to_string () == "<gxml:namespace xmlns:gxml=\"http://www.gnome.org/GXml\"/>");
+ Test.message ("DOC ROOT: "+doc.to_string ());
+ assert (doc.root.to_string () == "<namespace xmlns:gxml=\"http://www.gnome.org/GXml\"/>");
} catch (GLib.Error e) {
#if DEBUG
GLib.message ("ERROR: "+e.message);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]