[gxml] * make the <Property> elem predefined, and let serialize_property just fill in its children
- From: Richard Hans Schwarting <rschwart src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gxml] * make the <Property> elem predefined, and let serialize_property just fill in its children
- Date: Mon, 9 Jul 2012 20:21:26 +0000 (UTC)
commit 68f80820fb271d18827918beb45f65d037cc6064
Author: Richard Schwarting <aquarichy gmail com>
Date: Mon Jul 9 15:42:37 2012 -0400
* make the <Property> elem predefined, and let serialize_property just fill in its children
gxml/XmlSerializable.vala | 39 +++++++++++++++++++++------------------
test/XmlSerializableTest.vala | 38 ++++++++++----------------------------
2 files changed, 31 insertions(+), 46 deletions(-)
---
diff --git a/gxml/XmlSerializable.vala b/gxml/XmlSerializable.vala
index 2e041c6..cbe8c66 100644
--- a/gxml/XmlSerializable.vala
+++ b/gxml/XmlSerializable.vala
@@ -58,14 +58,10 @@ namespace GXmlDom {
}
}
- private static GXmlDom.Element serialize_property (GLib.Object object, ParamSpec prop_spec, GXmlDom.Document doc) throws SerializationError, DomError {
- Element prop;
+ private static GXmlDom.XNode serialize_property (GLib.Object object, ParamSpec prop_spec, GXmlDom.Document doc) throws SerializationError, DomError {
Type type;
Value value;
-
- prop = doc.create_element ("Property");
- prop.set_attribute ("ptype", prop_spec.value_type.name ());
- prop.set_attribute ("pname", prop_spec.name);
+ XNode value_node;
type = prop_spec.value_type;
@@ -83,16 +79,16 @@ namespace GXmlDom {
former by list_properties) */
value = Value (typeof (int));
object.get_property (prop_spec.name, ref value);
- prop.content = "%d".printf (value.get_int ());
+ value_node = doc.create_text_node ("%d".printf (value.get_int ()));
/* TODO: in the future, perhaps figure out GEnumClass
and save it as the human readable enum value :D */
} else if (Value.type_transformable (prop_spec.value_type, typeof (string))) { // e.g. int, double, string, bool
value = Value (typeof (string));
object.get_property (prop_spec.name, ref value);
//GLib.warning ("value: %d", value);
- prop.content = value.get_string ();
+ value_node = doc.create_text_node (value.get_string ());
} else if (type == typeof (GLib.Type)) {
- prop.content = type.name ();
+ value_node = doc.create_text_node (type.name ());
// } else if (type == typeof (GLib.HashTable)) {
// } else if (type == typeof (Gee.List)) { // TODO: can we do a catch all for Gee.Collection and have <Collection /> ?
// } else if (type.is_a (typeof (Gee.Collection))) {
@@ -101,13 +97,13 @@ namespace GXmlDom {
value = Value (typeof (GLib.Object));
object.get_property (prop_spec.name, ref value);
GLib.Object child_object = value.get_object ();
- GXmlDom.XNode child_node = Serialization.serialize_object (child_object); // catch serialisation errors?
- prop.append_child (child_node); // TODO: can we cross documents like this? Probably not :D want to be able to steal?, attributes seem to get lost
+ value_node = Serialization.serialize_object (child_object); // catch serialisation errors?
+ // TODO: caller will append_child; can we cross documents like this? Probably not :D want to be able to steal?, attributes seem to get lost
} else {
throw new SerializationError.UNSUPPORTED_TYPE ("Can't currently serialize type '%s' for property '%s' of object '%s'", type.name (), prop_spec.name, object.get_type ().name ());
}
- return prop;
+ return value_node;
}
/* TODO: so it seems we can get property information from GObjectClass
@@ -119,6 +115,7 @@ namespace GXmlDom {
ParamSpec[] prop_specs;
Element prop;
Serializable serializable = null;
+ XNode value_prop = null;
if (object.get_type ().is_a (typeof (Serializable))) {
serializable = (Serializable)object;
@@ -150,14 +147,19 @@ namespace GXmlDom {
strings. (Too bad deserialising isn't that
easy w.r.t. string conversion.) */
foreach (ParamSpec prop_spec in prop_specs) {
- prop = null;
+ prop = doc.create_element ("Property");
+ prop.set_attribute ("ptype", prop_spec.value_type.name ());
+ prop.set_attribute ("pname", prop_spec.name);
+
+ value_prop = null;
if (serializable != null) {
- prop = serializable.serialize_property (prop_spec.name, prop_spec, doc);
+ value_prop = serializable.serialize_property (prop_spec.name, prop_spec, doc);
}
- if (prop == null) {
- prop = Serialization.serialize_property (object, prop_spec, doc);
+ if (value_prop == null) {
+ value_prop = Serialization.serialize_property (object, prop_spec, doc);
}
+ prop.append_child (value_prop);
root.append_child (prop);
}
} catch (GXmlDom.DomError e) {
@@ -288,7 +290,8 @@ namespace GXmlDom {
}
if (!serialized) {
Serialization.deserialize_property (spec, prop_elem, out val);
- obj.set_property (pname, val); // TODO: should we make a note that for implementing {get,set}_property in the interface, they should specify override (in Vala)? What about in C? Need to test which one gets called in which situations (yeah, already read the tutorial)
+ obj.set_property (pname, val);
+ // TODO: should we make a note that for implementing {get,set}_property in the interface, they should specify override (in Vala)? What about in C? Need to test which one gets called in which situations (yeah, already read the tutorial)
}
} catch (SerializationError.UNSUPPORTED_TYPE e) {
throw new SerializationError.UNSUPPORTED_TYPE ("Cannot deserialize object '%s's property '%s' with type '%s/%s': %s\nXML [%s]",
@@ -398,7 +401,7 @@ namespace GXmlDom {
// TODO: value and property_name are kind of redundant: eliminate? property_name from spec.property_name and value from the object itself :)
/** Serialized properties should have the XML structure <Property pname="PropertyName">...</Property> */
// TODO: perhaps we should provide that base structure
- public virtual GXmlDom.Element? serialize_property (string property_name, /*GLib.Value value,*/ GLib.ParamSpec spec, GXmlDom.Document doc) {
+ public virtual GXmlDom.XNode? serialize_property (string property_name, /*GLib.Value value,*/ GLib.ParamSpec spec, GXmlDom.Document doc) {
return null; // default serialize_property gets used
}
diff --git a/test/XmlSerializableTest.vala b/test/XmlSerializableTest.vala
index 7e80b63..0f78286 100644
--- a/test/XmlSerializableTest.vala
+++ b/test/XmlSerializableTest.vala
@@ -29,8 +29,7 @@ public class SerializableTomato : GLib.Object, GXmlDom.Serializable {
public int weight;
private int age { get; set; }
public int height { get; set; }
- public string description;
- // public unowned GLib.List<int> ratings { get; set; }
+ public string description { get; set; }
public SerializableTomato (int weight, int age, int height, string description) {
this.weight = weight;
@@ -39,28 +38,15 @@ public class SerializableTomato : GLib.Object, GXmlDom.Serializable {
this.description = description;
}
- // TODO: make common with SerializableCapsicum's to_string ()
public string to_string () {
- string str = "SerializableTomato {weight:%d, age:%d, height:%d, ratings:".printf (weight, age, height);
- // foreach (int rating in ratings) {
- // str += "%d ".printf (rating);
- // }
- str += "}";
- return str;
+ return "SerializableTomato {weight:%d, age:%d, height:%d, description:%s}".printf (weight, age, height, description);
}
public static bool equals (SerializableTomato a, SerializableTomato b) {
bool same = (a.weight == b.weight &&
a.age == b.age &&
- a.height == b.height); // &&
- // a.ratings.length () == b.ratings.length ());
- // if (same) {
- // for (int i = 0; i < a.ratings.length (); i++) {
- // if (a.ratings.nth_data (i) != b.ratings.nth_data (i)) {
- // return false;
- // }
- // }
- // }
+ a.height == b.height &&
+ a.description == b.description);
return same;
}
@@ -114,26 +100,22 @@ public class SerializableCapsicum : GLib.Object, GXmlDom.Serializable {
return false;
}
- public GXmlDom.Element? serialize_property (string property_name, /*GLib.Value value,*/ GLib.ParamSpec spec, GXmlDom.Document doc) {
+ public GXmlDom.XNode? serialize_property (string property_name, /*GLib.Value value,*/ GLib.ParamSpec spec, GXmlDom.Document doc) {
GXmlDom.Element c_prop;
GXmlDom.Element rating;
switch (property_name) {
case "ratings":
- c_prop = doc.create_element ("Property");
- c_prop.set_attribute ("pname", property_name);
+ GXmlDom.DocumentFragment frag = doc.create_document_fragment ();
foreach (int rating_int in ratings) {
rating = doc.create_element ("rating");
rating.content = "%d".printf (rating_int);
- c_prop.append_child (rating);
+ frag.append_child (rating);
}
- return c_prop;
+ return frag;
break;
case "height":
- c_prop = doc.create_element ("Property");
- c_prop.set_attribute ("pname", property_name);
- c_prop.content = "%d".printf (height + 1);
- return c_prop;
+ return doc.create_text_node ("%d".printf (height + 1));
break;
default:
GLib.error ("Wasn't expecting the SerializableCapsicum property '%s'", property_name);
@@ -592,7 +574,7 @@ class XmlSerializableTest : GXmlTest {
capsicum = new SerializableCapsicum (2, 3, 5, ratings);
node = Serialization.serialize_object (capsicum);
- expected = "<Object otype=\"SerializableCapsicum\"><Property pname=\"height\">6</Property><Property pname=\"ratings\"><rating>8</rating><rating>13</rating><rating>21</rating></Property></Object>";
+ expected = "<Object otype=\"SerializableCapsicum\"><Property pname=\"height\" ptype=\"gint\">6</Property><Property pname=\"ratings\" ptype=\"gpointer\"><rating>8</rating><rating>13</rating><rating>21</rating></Property></Object>";
if (node.to_string () != expected) {
GLib.warning ("Did not serialize as expected. Got [%s] but expected [%s]", node.to_string (), expected);
GLib.Test.fail ();
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]