[gxml] GomObject: Handles properties name as nicks
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gxml] GomObject: Handles properties name as nicks
- Date: Fri, 4 Nov 2016 21:16:07 +0000 (UTC)
commit 8e3ca41dd33659112499e05241398e7ca7a78764
Author: Daniel Espinosa <esodan gmail com>
Date: Fri Nov 4 14:16:06 2016 -0600
GomObject: Handles properties name as nicks
gxml/GomObject.vala | 61 +++++++++++++++++++++++++++++++++++++--
gxml/XParser.vala | 5 ++-
test/GomSerializationTest.vala | 16 +++++++---
3 files changed, 71 insertions(+), 11 deletions(-)
---
diff --git a/gxml/GomObject.vala b/gxml/GomObject.vala
index e34302c..3889212 100644
--- a/gxml/GomObject.vala
+++ b/gxml/GomObject.vala
@@ -39,6 +39,12 @@ public interface GXml.GomObject : GLib.Object,
* attribute use property's nick name as declared in {@link GLib.ParamSpec}
*/
public virtual bool use_nick_name () { return true; }
+
+ /**
+ * Returns a hash table with key as property's name and value as property's
+ * nick, of all with a prefix "::" which will be used when getting
+ * and setting properties' values.
+ */
public virtual HashTable<string,string> get_properties_map () {
var l = new HashTable<string,string> (str_hash, str_equal);
foreach (ParamSpec spec in this.get_class ().list_properties ()) {
@@ -50,6 +56,23 @@ public interface GXml.GomObject : GLib.Object,
return l;
}
/**
+ * Returns a hash table with key as property's name and value as property's
+ * nick, of all with a prefix "::" which will be used when getting
+ * and setting properties' values.
+ */
+ public virtual string? find_property_name (string nick) {
+ foreach (ParamSpec spec in this.get_class ().list_properties ()) {
+ if ("::" in spec.get_nick ()) {
+ string name = nick.replace ("::","");
+ if (name == nick) {
+ GLib.message ("Name: "+spec.name+ " Nick: "+spec.get_nick ());
+ return name;
+ }
+ }
+ }
+ return null;
+ }
+ /**
* Search for properties in objects, it should be
* an {@link GLib.Object}'s property. If found a
* property with given name its value is returned
@@ -114,16 +137,46 @@ public interface GXml.GomObject : GLib.Object,
* this object, see {@link set_child}
*/
public virtual bool set_attribute (string name, string val) {
- var prop = get_class ().find_property (name);
+ string pname = find_property_name (name);
+ if (pname == null) return false;
+ var prop = get_class ().find_property (pname);
if (prop != null) {
+ var v = Value (prop.value_type);
if (prop.value_type == typeof(SerializableProperty)) {
- var ov = Value (prop.value_type);
- get_property (name, ref ov);
- SerializableProperty so = (Object) ov as SerializableProperty;
+ get_property (name, ref v);
+ SerializableProperty so = (Object) v as SerializableProperty;
if (so == null) return false;
so.set_serializable_property_value (val);
return true;
}
+ if (prop.value_type.is_a (typeof (string))) {
+ v.set_string (val);
+ set_property (prop.name, v);
+ }
+ if (prop.value_type.is_a (typeof (int))) {
+ v.set_string (val);
+ set_property (prop.name, v);
+ }
+ if (prop.value_type.is_a (typeof (uint))) {
+ v.set_string (val);
+ set_property (prop.name, v);
+ }
+ if (prop.value_type.is_a (typeof (double))) {
+ v.set_string (val);
+ set_property (prop.name, v);
+ }
+ if (prop.value_type.is_a (typeof (bool))) {
+ v.set_string (val);
+ set_property (prop.name, v);
+ }
+ if (prop.value_type.is_a (Type.ENUM)) {
+ try {
+ var n = (int) Enumeration.parse (prop.value_type, val).value;
+ v.set_enum (n);
+ } catch {
+ GLib.warning (_("Enumeration can't be parsed from string"));
+ }
+ }
}
return false;
}
diff --git a/gxml/XParser.vala b/gxml/XParser.vala
index 1fca471..90ee083 100644
--- a/gxml/XParser.vala
+++ b/gxml/XParser.vala
@@ -186,14 +186,15 @@ public class GXml.XParser : Object, GXml.Parser {
#if DEBUG
GLib.message ("Attribute:"+attrname+" Value: "+attrval);
#endif
- if (prefix != null) {
+ bool processed = (n as GomObject).set_attribute (attrname, attrval);
+ if (prefix != null && !processed) {
GLib.message ("Prefix found: "+prefix);
if (prefix == "xml")
nsuri = "http://www.w3.org/2000/xmlns/";
else
nsuri = tr.lookup_namespace (prefix);
(n as DomElement).set_attribute_ns (nsuri, prefix+":"+attrname, attrval);
- } else
+ } else if (!processed)
(n as DomElement).set_attribute (attrname, attrval);
}
}
diff --git a/test/GomSerializationTest.vala b/test/GomSerializationTest.vala
index 55cb6a2..ac806a9 100644
--- a/test/GomSerializationTest.vala
+++ b/test/GomSerializationTest.vala
@@ -109,17 +109,23 @@ class GomSerializationTest : GXmlTest {
GLib.message ("DOC:"+s);
});
Test.add_func ("/gxml/gom-serialization/read/properties", () => {
- /*var b = new Book ();
- (b.owner_document as GomDocument)
- .parser.read_string ("<book name=\"Loco\"/>", null);
- string s = (b.owner_document as GomDocument).to_string ();
+ var b = new Book ();
+ var parser = new XParser (b);
+ parser.read_string ("<book name=\"Loco\"/>", null);
+ string s = parser.write_string ();
+ assert (s != null);
+ GLib.message ("Doc:"+s);
+ assert (b.child_nodes.size == 0);
+ assert (b.attributes.size == 0);
+ assert (b.name == "Loco");
+ s = parser.write_string ();
assert (s != null);
assert ("<Book Name=\"Loco\"/>" in s);
GLib.message ("Doc:"+s);
b.name = "My Book";
assert (b.get_attribute ("name") == "My Book");
s = b.to_string ();
- assert ("<Book Name=\"My Book\"/>" in s);*/
+ assert ("<Book Name=\"My Book\"/>" in s);
});
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]