[gxml/serialization] Format: replace tabs with spaces at Serializable's
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gxml/serialization] Format: replace tabs with spaces at Serializable's
- Date: Tue, 12 Nov 2013 18:34:54 +0000 (UTC)
commit f3f9ef7c3d8ac6684bb1750addefaa4598c9a28b
Author: Daniel Espinosa <esodan gmail com>
Date: Tue Nov 12 09:26:52 2013 -0600
Format: replace tabs with spaces at Serializable's
gxml/Serializable.vala | 1012 +++++++++---------
gxml/SerializableJson.vala | 378 ++++----
gxml/SerializableObjectModel.vala | 547 +++++-----
test/SerializableObjectModelTest.vala | 1980 ++++++++++++++++----------------
4 files changed, 1956 insertions(+), 1961 deletions(-)
---
diff --git a/gxml/Serializable.vala b/gxml/Serializable.vala
index 492a7f2..42b5353 100644
--- a/gxml/Serializable.vala
+++ b/gxml/Serializable.vala
@@ -46,535 +46,535 @@
json_serializable_{find,list,get,set}_propert{y,ies} -> iface->{find,list,get,set}_propert{y,ies}
these all get init'd to -> json_serializable_real_{find,list,get,set}_propert{y,ies}
- these all call -> g_object_{class,}_{find,list,get,set}_propert{y,ies}
+ these all call -> g_object_{class,}_{find,list,get,set}_propert{y,ies}
*/
using GXml;
namespace GXml {
- /**
- * Interface allowing implementors direct control over serialisation of properties and other data
- *
- * A class that implements this interface will still be passed
- * to { link GXml.Serialization.serialize_object} for
- * serialization. That function will check whether the object
- * implements { link GXml.Serializable} and will then prefer
- * overridden methods instead of standard ones. Most of the
- * methods for this interface can indicate (via return value)
- * that, for a given property, the standard serialization
- * approach should be used instead. Indeed, not all methods
- * need to be implemented, but some accompany one another and
- * should be implemented carefully, corresponding to one
- * another. You can also create virtual properties from
- * non-public property fields to enable their serialization.
- *
- * For an example, look in tests/XmlSerializableTest
- */
- public interface Serializable : GLib.Object {
- protected abstract ParamSpec[] properties { get; set; }
- /**
- * Store all properties to be ignored on serialization.
- *
- * Use property's cannonical name as key and its { link GLib.ParamSpec}. To
- * get the last one use { link GLib.get_class} and use, again, property's
- * cannonical name to find it.
- *
- * Long named properties like this 'ignored_serializable_properties' are stored
- * by GObject using its cannonical name, then you must use it as key, in this
- * case use 'ignored-serializable-properties'.
- *
- * This property is ignored on serialisation.
- *
- * Implementors: By default { link list_serializable_properties} initialize
- * this property to store all public properties, except this one. Make shure t
- * call { link init_properties()} before add new propeties.
- */
- public abstract HashTable<string,GLib.ParamSpec> ignored_serializable_properties { get;
protected set; }
- /**
- * On deserialization stores any { link GXml.Node} not used on this
- * object, but exists in current XML file.
- *
- * Node's name is used as key to find stored { link GXml.Node}.
- *
- * This property is ignored on serialisation.
- */
- public abstract HashTable<string,GXml.Node> unknown_serializable_property { get; protected
set; }
+ /**
+ * Interface allowing implementors direct control over serialisation of properties and other data
+ *
+ * A class that implements this interface will still be passed
+ * to { link GXml.Serialization.serialize_object} for
+ * serialization. That function will check whether the object
+ * implements { link GXml.Serializable} and will then prefer
+ * overridden methods instead of standard ones. Most of the
+ * methods for this interface can indicate (via return value)
+ * that, for a given property, the standard serialization
+ * approach should be used instead. Indeed, not all methods
+ * need to be implemented, but some accompany one another and
+ * should be implemented carefully, corresponding to one
+ * another. You can also create virtual properties from
+ * non-public property fields to enable their serialization.
+ *
+ * For an example, look in tests/XmlSerializableTest
+ */
+ public interface Serializable : GLib.Object {
+ protected abstract ParamSpec[] properties { get; set; }
+ /**
+ * Store all properties to be ignored on serialization.
+ *
+ * Use property's cannonical name as key and its { link GLib.ParamSpec}. To
+ * get the last one use { link GLib.get_class} and use, again, property's
+ * cannonical name to find it.
+ *
+ * Long named properties like this 'ignored_serializable_properties' are stored
+ * by GObject using its cannonical name, then you must use it as key, in this
+ * case use 'ignored-serializable-properties'.
+ *
+ * This property is ignored on serialisation.
+ *
+ * Implementors: By default { link list_serializable_properties} initialize
+ * this property to store all public properties, except this one. Make shure t
+ * call { link init_properties()} before add new propeties.
+ */
+ public abstract HashTable<string,GLib.ParamSpec> ignored_serializable_properties { get; protected set; }
+ /**
+ * On deserialization stores any { link GXml.Node} not used on this
+ * object, but exists in current XML file.
+ *
+ * Node's name is used as key to find stored { link GXml.Node}.
+ *
+ * This property is ignored on serialisation.
+ */
+ public abstract HashTable<string,GXml.Node> unknown_serializable_property { get; protected set; }
- /**
- * Used to add content in an { link GXml.Element}.
- *
- * This property is ignored on serialisation.
- */
- public abstract string? serialized_xml_node_value { get; protected set; default = null; }
- /**
- * Defines the way to set Node name.
- */
- public abstract string node_name ();
/**
- * Defines the way to set Node's property name, by using
- * it's nick instead of default name.
- */
- public abstract bool property_use_nick ();
- /**
- * Serialize this object.
- *
- * @doc an { link GXml.Document} object to serialise to
- */
- public abstract Node? serialize (Node node) throws GLib.Error;
+ * Used to add content in an { link GXml.Element}.
+ *
+ * This property is ignored on serialisation.
+ */
+ public abstract string? serialized_xml_node_value { get; protected set; default = null; }
+ /**
+ * Defines the way to set Node name.
+ */
+ public abstract string node_name ();
+ /**
+ * Defines the way to set Node's property name, by using
+ * it's nick instead of default name.
+ */
+ public abstract bool property_use_nick ();
+ /**
+ * Serialize this object.
+ *
+ * @doc an { link GXml.Document} object to serialise to
+ */
+ public abstract Node? serialize (Node node) throws GLib.Error;
- /**
- * Handles serializing individual properties.
- *
- * Interface method to handle serialization of an
- * individual property. The implementing class
- * receives a description of it, and should create a
- * { link GXml.Node} that encapsulates the property.
- * { link GXml.Serialization} will embed the { link GXml.Node} into
- * a "Property" { link GXml.Element}, so the { link GXml.Node}
- * returned can often be something as simple as
- * { link GXml.Text}.
- *
- * To let { link GXml.Serialization} attempt to automatically
- * serialize the property itself, do not implement
- * this method. If the method returns %NULL,
- * { link GXml.Serialization} will attempt handle it itsel.
- *
- * Implementors:
- * Use Serializable.get_property_value in order to allow derived classes to
- * override the properties to serialize.
- *
- * @param property_name string name of a property to serialize.
- * @param spec the { link GLib.ParamSpec} describing the property.
- * @param doc the { link GXml.Document} the returned { link GXml.Node} should belong to
- * @return a new { link GXml.Node}, or %NULL
- */
- public abstract GXml.Node? serialize_property (Element element,
- GLib.ParamSpec prop)
- throws GLib.Error;
+ /**
+ * Handles serializing individual properties.
+ *
+ * Interface method to handle serialization of an
+ * individual property. The implementing class
+ * receives a description of it, and should create a
+ * { link GXml.Node} that encapsulates the property.
+ * { link GXml.Serialization} will embed the { link GXml.Node} into
+ * a "Property" { link GXml.Element}, so the { link GXml.Node}
+ * returned can often be something as simple as
+ * { link GXml.Text}.
+ *
+ * To let { link GXml.Serialization} attempt to automatically
+ * serialize the property itself, do not implement
+ * this method. If the method returns %NULL,
+ * { link GXml.Serialization} will attempt handle it itsel.
+ *
+ * Implementors:
+ * Use Serializable.get_property_value in order to allow derived classes to
+ * override the properties to serialize.
+ *
+ * @param property_name string name of a property to serialize.
+ * @param spec the { link GLib.ParamSpec} describing the property.
+ * @param doc the { link GXml.Document} the returned { link GXml.Node} should belong to
+ * @return a new { link GXml.Node}, or %NULL
+ */
+ public abstract GXml.Node? serialize_property (Element element,
+ GLib.ParamSpec prop)
+ throws GLib.Error;
- /**
- * Deserialize this object.
- *
- * @node { link GXml.Node} used to deserialize from.
- */
- public abstract Node? deserialize (Node node)
- throws GLib.Error;
- /**
- * Handles deserializing individual properties.
- *
- * Interface method to handle deserialization of an
- * individual property. The implementing class
- * receives a description of the property and the
- * { link GXml.Node} that contains the content. The
- * implementing { link GXml.Serializable} object can extract
- * the data from the { link GXml.Node} and store it in its
- * property itself. Note that the { link GXml.Node} may be
- * as simple as a { link GXml.Text} that stores the data as a
- * string.
- *
- * Implementors:
- * Use Serializable.get_property_value in order to allow derived classes to
- * override the properties to serialize.
- *
- * @param property_name the name of the property as a string
- * @param spec the { link GLib.ParamSpec} describing the property.
- * @param property_node the { link GXml.Node} encapsulating data to deserialize
- * @return `true` if the property was handled, `false` if { link GXml.Serialization} should
handle it.
- */
- public abstract bool deserialize_property (GXml.Node property_node)
- throws GLib.Error;
+ /**
+ * Deserialize this object.
+ *
+ * @node { link GXml.Node} used to deserialize from.
+ */
+ public abstract Node? deserialize (Node node)
+ throws GLib.Error;
+ /**
+ * Handles deserializing individual properties.
+ *
+ * Interface method to handle deserialization of an
+ * individual property. The implementing class
+ * receives a description of the property and the
+ * { link GXml.Node} that contains the content. The
+ * implementing { link GXml.Serializable} object can extract
+ * the data from the { link GXml.Node} and store it in its
+ * property itself. Note that the { link GXml.Node} may be
+ * as simple as a { link GXml.Text} that stores the data as a
+ * string.
+ *
+ * Implementors:
+ * Use Serializable.get_property_value in order to allow derived classes to
+ * override the properties to serialize.
+ *
+ * @param property_name the name of the property as a string
+ * @param spec the { link GLib.ParamSpec} describing the property.
+ * @param property_node the { link GXml.Node} encapsulating data to deserialize
+ * @return `true` if the property was handled, `false` if { link GXml.Serialization} should handle it.
+ */
+ public abstract bool deserialize_property (GXml.Node property_node)
+ throws GLib.Error;
- /**
- * Signal to serialize unknown properties. Any new node must be added to
- * @element before return the new @node added.
- *
- * @element a { link GXml.Node} to add attribute or child nodes to
- * @prop a { link GLib.ParamSpec} describing attribute to serialize
- * @node set to the { link GXml.Node} representing this attribute
- */
- public signal void serialize_unknown_property (Node element, ParamSpec prop, out Node node);
+ /**
+ * Signal to serialize unknown properties. Any new node must be added to
+ * @element before return the new @node added.
+ *
+ * @element a { link GXml.Node} to add attribute or child nodes to
+ * @prop a { link GLib.ParamSpec} describing attribute to serialize
+ * @node set to the { link GXml.Node} representing this attribute
+ */
+ public signal void serialize_unknown_property (Node element, ParamSpec prop, out Node node);
- /**
- * Signal to serialize unknown properties. Any new node must be added to
- * @element before return the new @node added.
- *
- * @element a { link GXml.Node} to add attribute or child nodes to
- * @prop a { link GLib.ParamSpec} describing attribute to serialize
- * @node set to the { link GXml.Node} representing this attribute
- */
- public signal void serialize_unknown_property_type (Node element, ParamSpec prop, out Node
node);
+ /**
+ * Signal to serialize unknown properties. Any new node must be added to
+ * @element before return the new @node added.
+ *
+ * @element a { link GXml.Node} to add attribute or child nodes to
+ * @prop a { link GLib.ParamSpec} describing attribute to serialize
+ * @node set to the { link GXml.Node} representing this attribute
+ */
+ public signal void serialize_unknown_property_type (Node element, ParamSpec prop, out Node node);
- /**
- * Signal to deserialize unknown properties.
- *
- * @node a { link GXml.Node} to get attribute from
- * @prop a { link GLib.ParamSpec} describing attribute to deserialize
- */
- public signal void deserialize_unknown_property (Node node, ParamSpec prop);
+ /**
+ * Signal to deserialize unknown properties.
+ *
+ * @node a { link GXml.Node} to get attribute from
+ * @prop a { link GLib.ParamSpec} describing attribute to deserialize
+ */
+ public signal void deserialize_unknown_property (Node node, ParamSpec prop);
- /**
- * Signal to deserialize unknown properties' type.
- *
- * @node a { link GXml.Node} to get attribute from
- * @prop a { link GLib.ParamSpec} describing attribute to deserialize
- */
- public signal void deserialize_unknown_property_type (Node node, ParamSpec prop);
+ /**
+ * Signal to deserialize unknown properties' type.
+ *
+ * @node a { link GXml.Node} to get attribute from
+ * @prop a { link GLib.ParamSpec} describing attribute to deserialize
+ */
+ public signal void deserialize_unknown_property_type (Node node, ParamSpec prop);
- /**
- * Handles finding the { link GLib.ParamSpec} for a given property.
- *
- * @param property_name the name of a property to obtain a { link GLib.ParamSpec} for
- * @return a { link GLib.ParamSpec} describing the named property
- *
- * { link GXml.Serialization} uses { link
- * GLib.ObjectClass.find_property} (as well as { link
- * GLib.ObjectClass.list_properties}, { link
- * GLib.Object.get_property}, and { link
- * GLib.Object.set_property}) to manage serialization
- * of properties. { link GXml.Serializable} gives the
- * implementing class an opportunity to override
- * { link GLib.ObjectClass.find_property} to control
- * what properties exist for { link GXml.Serialization}'s
- * purposes.
- *
- * For instance, if an object has private data fields
- * that are not installed public properties, but that
- * should be serialized, find_property can be defined
- * to return a { link GLib.ParamSpec} for non-installed
- * properties. Other { link GXml.Serializable} functions
- * should be consistent with it.
- *
- * An implementing class might wish to maintain such
- * { link GLib.ParamSpec} s separately, rather than creating new
- * ones for each call.
- */
- public abstract GLib.ParamSpec? find_property_spec (string property_name);
+ /**
+ * Handles finding the { link GLib.ParamSpec} for a given property.
+ *
+ * @param property_name the name of a property to obtain a { link GLib.ParamSpec} for
+ * @return a { link GLib.ParamSpec} describing the named property
+ *
+ * { link GXml.Serialization} uses { link
+ * GLib.ObjectClass.find_property} (as well as { link
+ * GLib.ObjectClass.list_properties}, { link
+ * GLib.Object.get_property}, and { link
+ * GLib.Object.set_property}) to manage serialization
+ * of properties. { link GXml.Serializable} gives the
+ * implementing class an opportunity to override
+ * { link GLib.ObjectClass.find_property} to control
+ * what properties exist for { link GXml.Serialization}'s
+ * purposes.
+ *
+ * For instance, if an object has private data fields
+ * that are not installed public properties, but that
+ * should be serialized, find_property can be defined
+ * to return a { link GLib.ParamSpec} for non-installed
+ * properties. Other { link GXml.Serializable} functions
+ * should be consistent with it.
+ *
+ * An implementing class might wish to maintain such
+ * { link GLib.ParamSpec} s separately, rather than creating new
+ * ones for each call.
+ */
+ public abstract GLib.ParamSpec? find_property_spec (string property_name);
- /**
- * Default implementation for find_property_spec ().
- *
- */
- public virtual GLib.ParamSpec? default_find_property_spec (string property_name) {
- init_properties ();
- var props = list_serializable_properties ();
- foreach (ParamSpec spec in props) {
- if (spec.name.down () == property_name.down ())
- return spec;
- if (property_use_nick ())
- if (spec.get_nick ().down () == property_name.down ())
- return spec;
- }
- return null;
- }
+ /**
+ * Default implementation for find_property_spec ().
+ *
+ */
+ public virtual GLib.ParamSpec? default_find_property_spec (string property_name) {
+ init_properties ();
+ var props = list_serializable_properties ();
+ foreach (ParamSpec spec in props) {
+ if (spec.name.down () == property_name.down ())
+ return spec;
+ if (property_use_nick ())
+ if (spec.get_nick ().down () == property_name.down ())
+ return spec;
+ }
+ return null;
+ }
- /**
- * Used internally to initialize { link ignored_serializable_properties} property
- * and default not to be serialized properties. Unless you override any function
- * is not required to be called at class implementor's construction time.
- *
- */
- public abstract void init_properties ();
+ /**
+ * Used internally to initialize { link ignored_serializable_properties} property
+ * and default not to be serialized properties. Unless you override any function
+ * is not required to be called at class implementor's construction time.
+ *
+ */
+ public abstract void init_properties ();
- /**
- * Default implementation for init_properties ().
- *
- */
- public virtual void default_init_properties ()
- {
- if (ignored_serializable_properties == null) {
- ignored_serializable_properties = new HashTable<string,ParamSpec> (str_hash,
str_equal);
- ignored_serializable_properties.set ("ignored-serializable-properties",
- get_class
().find_property("ignored-serializable-properties"));
- ignored_serializable_properties.set ("unknown-serializable-property",
- get_class
().find_property("unknown-serializable-property"));
- ignored_serializable_properties.set ("serialized-xml-node-value",
- get_class
().find_property("serialized-xml-node-value"));
- }
- if (unknown_serializable_property == null) {
- unknown_serializable_property = new HashTable<string,GXml.Node> (str_hash,
str_equal);
- }
- }
+ /**
+ * Default implementation for init_properties ().
+ *
+ */
+ public virtual void default_init_properties ()
+ {
+ if (ignored_serializable_properties == null) {
+ ignored_serializable_properties = new HashTable<string,ParamSpec> (str_hash, str_equal);
+ ignored_serializable_properties.set ("ignored-serializable-properties",
+ get_class ().find_property("ignored-serializable-properties"));
+ ignored_serializable_properties.set ("unknown-serializable-property",
+ get_class ().find_property("unknown-serializable-property"));
+ ignored_serializable_properties.set ("serialized-xml-node-value",
+ get_class ().find_property("serialized-xml-node-value"));
+ }
+ if (unknown_serializable_property == null) {
+ unknown_serializable_property = new HashTable<string,GXml.Node> (str_hash, str_equal);
+ }
+ }
- /**
- * List the known properties for an object's class
- *
- * @return an array of { link GLib.ParamSpec} of
- * "properties" for the object.
- *
- * { link GXml.Serialization} uses
- * { link GLib.ObjectClass.list_properties} (as well as
- * { link GLib.ObjectClass.find_property},
- * { link GLib.Object.get_property}, and { link GLib.Object.set_property})
- * to manage serialization of an object's properties.
- * { link GXml.Serializable} gives an implementing class an
- * opportunity to override
- * { link GLib.ObjectClass.list_properties} to control which
- * properties exist for { link GXml.Serialization}'s purposes.
- *
- * For instance, if an object has private data fields
- * that are not installed public properties, but that
- * should be serialized, list_properties can be
- * defined to return a list of { link GLib.ParamSpec} s covering
- * all the "properties" to serialize. Other
- * { link GXml.Serializable} functions should be consistent
- * with it.
- *
- * An implementing class might wish to maintain such
- * { link GLib.ParamSpec} s separately, rather than creating new
- * ones for each call.
- */
- public abstract GLib.ParamSpec[] list_serializable_properties ();
- /**
- * Default implementation for list_serializable_properties ().
- *
- */
- public virtual GLib.ParamSpec[] default_list_serializable_properties ()
- {
- init_properties ();
- if (properties == null) {
- ParamSpec[] props = {};
- foreach (ParamSpec spec in this.get_class ().list_properties ()) {
- if (!ignored_serializable_properties.contains (spec.name)) {
- props += spec;
- }
- }
- properties = props;
- }
- return properties;
- }
+ /**
+ * List the known properties for an object's class
+ *
+ * @return an array of { link GLib.ParamSpec} of
+ * "properties" for the object.
+ *
+ * { link GXml.Serialization} uses
+ * { link GLib.ObjectClass.list_properties} (as well as
+ * { link GLib.ObjectClass.find_property},
+ * { link GLib.Object.get_property}, and { link GLib.Object.set_property})
+ * to manage serialization of an object's properties.
+ * { link GXml.Serializable} gives an implementing class an
+ * opportunity to override
+ * { link GLib.ObjectClass.list_properties} to control which
+ * properties exist for { link GXml.Serialization}'s purposes.
+ *
+ * For instance, if an object has private data fields
+ * that are not installed public properties, but that
+ * should be serialized, list_properties can be
+ * defined to return a list of { link GLib.ParamSpec} s covering
+ * all the "properties" to serialize. Other
+ * { link GXml.Serializable} functions should be consistent
+ * with it.
+ *
+ * An implementing class might wish to maintain such
+ * { link GLib.ParamSpec} s separately, rather than creating new
+ * ones for each call.
+ */
+ public abstract GLib.ParamSpec[] list_serializable_properties ();
+ /**
+ * Default implementation for list_serializable_properties ().
+ *
+ */
+ public virtual GLib.ParamSpec[] default_list_serializable_properties ()
+ {
+ init_properties ();
+ if (properties == null) {
+ ParamSpec[] props = {};
+ foreach (ParamSpec spec in this.get_class ().list_properties ()) {
+ if (!ignored_serializable_properties.contains (spec.name)) {
+ props += spec;
+ }
+ }
+ properties = props;
+ }
+ return properties;
+ }
- /**
- * Get a string version of the specified property
- *
- * @param spec The property we're retrieving as a string
- *
- * { link GXml.Serialization} uses { link GLib.Object.get_property} (as
- * well as { link GLib.ObjectClass.find_property},
- * { link GLib.ObjectClass.list_properties}, and
- * { link GLib.Object.set_property}) to manage serialization of
- * an object's properties. { link GXml.Serializable} gives an
- * implementing class an opportunity to override
- * { link GLib.Object.get_property} to control what value is
- * returned for a given parameter.
- *
- * For instance, if an object has private data fields
- * that are not installed public properties, but that
- * should be serialized,
- * { link GXml.Serializable.get_property} can be used to
- * handle this case as a virtual property, supported
- * by the other { link GXml.Serializable} functions.
- *
- * `spec` is usually obtained from list_properties or find_property.
- */
- public abstract void get_property_value (GLib.ParamSpec spec, ref Value val);
- /**
- * Default implementation for get_property_value ().
- *
- */
- public virtual void default_get_property_value (GLib.ParamSpec spec, ref Value val)
- {
- if (!ignored_serializable_properties.contains (spec.name))
- ((GLib.Object)this).get_property (spec.name, ref val);
- }
- /**
- * Set a property's value.
- *
- * @param spec Specifies the property whose value will be set
- * @param val The value to set the property to.
- *
- * { link GXml.Serialization} uses { link GLib.Object.set_property} (as
- * well as { link GLib.ObjectClass.find_property},
- * { link GLib.ObjectClass.list_properties}, and
- * { link GLib.Object.get_property}) to manage serialization of
- * an object's properties. { link GXml.Serializable} gives an
- * implementing class an opportunity to override
- * { link GLib.Object.set_property} to control how a property's
- * value is set.
- *
- * For instance, if an object has private data fields
- * that are not installed public properties, but that
- * should be serialized,
- * { link GXml.Serializable.set_property} can be used to
- * handle this case as a virtual property, supported
- * by the other { link GXml.Serializable} functions.
- */
- public abstract void set_property_value (GLib.ParamSpec spec, GLib.Value val);
- /**
- * Default implementation for set_property_value ().
- *
- */
- public virtual void default_set_property_value (GLib.ParamSpec spec, GLib.Value val)
- {
- if (!ignored_serializable_properties.contains (spec.name)) {
- ((GLib.Object)this).set_property (spec.name, val);
- }
- }
+ /**
+ * Get a string version of the specified property
+ *
+ * @param spec The property we're retrieving as a string
+ *
+ * { link GXml.Serialization} uses { link GLib.Object.get_property} (as
+ * well as { link GLib.ObjectClass.find_property},
+ * { link GLib.ObjectClass.list_properties}, and
+ * { link GLib.Object.set_property}) to manage serialization of
+ * an object's properties. { link GXml.Serializable} gives an
+ * implementing class an opportunity to override
+ * { link GLib.Object.get_property} to control what value is
+ * returned for a given parameter.
+ *
+ * For instance, if an object has private data fields
+ * that are not installed public properties, but that
+ * should be serialized,
+ * { link GXml.Serializable.get_property} can be used to
+ * handle this case as a virtual property, supported
+ * by the other { link GXml.Serializable} functions.
+ *
+ * `spec` is usually obtained from list_properties or find_property.
+ */
+ public abstract void get_property_value (GLib.ParamSpec spec, ref Value val);
+ /**
+ * Default implementation for get_property_value ().
+ *
+ */
+ public virtual void default_get_property_value (GLib.ParamSpec spec, ref Value val)
+ {
+ if (!ignored_serializable_properties.contains (spec.name))
+ ((GLib.Object)this).get_property (spec.name, ref val);
+ }
+ /**
+ * Set a property's value.
+ *
+ * @param spec Specifies the property whose value will be set
+ * @param val The value to set the property to.
+ *
+ * { link GXml.Serialization} uses { link GLib.Object.set_property} (as
+ * well as { link GLib.ObjectClass.find_property},
+ * { link GLib.ObjectClass.list_properties}, and
+ * { link GLib.Object.get_property}) to manage serialization of
+ * an object's properties. { link GXml.Serializable} gives an
+ * implementing class an opportunity to override
+ * { link GLib.Object.set_property} to control how a property's
+ * value is set.
+ *
+ * For instance, if an object has private data fields
+ * that are not installed public properties, but that
+ * should be serialized,
+ * { link GXml.Serializable.set_property} can be used to
+ * handle this case as a virtual property, supported
+ * by the other { link GXml.Serializable} functions.
+ */
+ public abstract void set_property_value (GLib.ParamSpec spec, GLib.Value val);
+ /**
+ * Default implementation for set_property_value ().
+ *
+ */
+ public virtual void default_set_property_value (GLib.ParamSpec spec, GLib.Value val)
+ {
+ if (!ignored_serializable_properties.contains (spec.name)) {
+ ((GLib.Object)this).set_property (spec.name, val);
+ }
+ }
- /**
- * Method to provide custome transformations from strings to
- * a { link GLib.Value}. Could be used on { link serialize} or simple
- * transformations from string.
- *
- * Some specialized classes, like derived from { link Serializable} class
- * implementator, can provide custome transformations.
- *
- * Returns: { link true} if transformation was handled, { link false} otherwise.
- *
- * Implementors:
- * To be overrided by derived classes of implementators to provide custome
- * transformations. Declare it as virtual if you want derived classes of
- * implementators to provide custome transformations.
- * Call this method before use standard Serializable or implementator ones.
- *
- * @node a { link GXml.Node} to get attribute from
- * @prop a { link GLib.ParamSpec} describing attribute to deserialize
- */
- public abstract bool transform_from_string (string str, ref GLib.Value dest);
+ /**
+ * Method to provide custome transformations from strings to
+ * a { link GLib.Value}. Could be used on { link serialize} or simple
+ * transformations from string.
+ *
+ * Some specialized classes, like derived from { link Serializable} class
+ * implementator, can provide custome transformations.
+ *
+ * Returns: { link true} if transformation was handled, { link false} otherwise.
+ *
+ * Implementors:
+ * To be overrided by derived classes of implementators to provide custome
+ * transformations. Declare it as virtual if you want derived classes of
+ * implementators to provide custome transformations.
+ * Call this method before use standard Serializable or implementator ones.
+ *
+ * @node a { link GXml.Node} to get attribute from
+ * @prop a { link GLib.ParamSpec} describing attribute to deserialize
+ */
+ public abstract bool transform_from_string (string str, ref GLib.Value dest);
- /**
- * Transforms a string into another type hosted by { link GLib.Value}.
- *
- * A utility function that handles converting a string
- * representation of a value into the type specified by the
- * supplied #GValue dest. A #GXmlSerializableError will be
- * set if the string cannot be parsed into the desired type.
- *
- * @param str the string to transform into the given #GValue object
- * @param dest the #GValue out parameter that will contain the parsed value from the string
- * @return `true` if parsing succeeded, otherwise `false`
- */
- public static bool string_to_gvalue (string str, ref GLib.Value dest)
- throws SerializableError
- {
- Type t = dest.type ();
- GLib.Value dest2 = Value (t);
- bool ret = false;
+ /**
+ * Transforms a string into another type hosted by { link GLib.Value}.
+ *
+ * A utility function that handles converting a string
+ * representation of a value into the type specified by the
+ * supplied #GValue dest. A #GXmlSerializableError will be
+ * set if the string cannot be parsed into the desired type.
+ *
+ * @param str the string to transform into the given #GValue object
+ * @param dest the #GValue out parameter that will contain the parsed value from the string
+ * @return `true` if parsing succeeded, otherwise `false`
+ */
+ public static bool string_to_gvalue (string str, ref GLib.Value dest)
+ throws SerializableError
+ {
+ Type t = dest.type ();
+ GLib.Value dest2 = Value (t);
+ bool ret = false;
- if (t == typeof (int64)) {
- int64 val;
- if (ret = int64.try_parse (str, out val)) {
- dest2.set_int64 (val);
- }
- } else if (t == typeof (int)) {
- int64 val;
- if (ret = int64.try_parse (str, out val)) {
- dest2.set_int ((int)val);
- }
- } else if (t == typeof (long)) {
- int64 val;
- if (ret = int64.try_parse (str, out val)) {
- dest2.set_long ((long)val);
- }
- } else if (t == typeof (uint)) {
- uint64 val;
- if (ret = uint64.try_parse (str, out val)) {
- dest2.set_uint ((uint)val);
- }
- } else if (t == typeof (ulong)) {
- uint64 val;
- if (ret = uint64.try_parse (str, out val)) {
- dest2.set_ulong ((ulong)val);
- }
- } else if ((int)t == 20) { // gboolean
- bool val = (str == "TRUE");
- dest2.set_boolean (val); // TODO: huh, investigate why the type is gboolean
and not bool coming out but is going in
- ret = true;
- } else if (t == typeof (bool)) {
- bool val;
- if (ret = bool.try_parse (str, out val)) {
- dest2.set_boolean (val);
- }
- } else if (t == typeof (float)) {
- double val;
- if (ret = double.try_parse (str, out val)) {
- dest2.set_float ((float)val);
- }
- } else if (t == typeof (double)) {
- double val;
- if (ret = double.try_parse (str, out val)) {
- dest2.set_double (val);
- }
- } else if (t == typeof (string)) {
- dest2.set_string (str);
- ret = true;
- } else if (t == typeof (char)) {
- int64 val;
- if (ret = int64.try_parse (str, out val)) {
- dest2.set_char ((char)val);
- }
- } else if (t == typeof (uchar)) {
- int64 val;
- if (ret = int64.try_parse (str, out val)) {
- dest2.set_uchar ((uchar)val);
- }
- } else if (t == Type.BOXED) {
- } else if (t.is_enum ()) {
- int64 val;
- if (ret = int64.try_parse (str, out val)) {
- dest2.set_enum ((int)val);
- }
- } else if (t.is_flags ()) {
- } else if (t.is_object ()) {
- } else {
- }
+ if (t == typeof (int64)) {
+ int64 val;
+ if (ret = int64.try_parse (str, out val)) {
+ dest2.set_int64 (val);
+ }
+ } else if (t == typeof (int)) {
+ int64 val;
+ if (ret = int64.try_parse (str, out val)) {
+ dest2.set_int ((int)val);
+ }
+ } else if (t == typeof (long)) {
+ int64 val;
+ if (ret = int64.try_parse (str, out val)) {
+ dest2.set_long ((long)val);
+ }
+ } else if (t == typeof (uint)) {
+ uint64 val;
+ if (ret = uint64.try_parse (str, out val)) {
+ dest2.set_uint ((uint)val);
+ }
+ } else if (t == typeof (ulong)) {
+ uint64 val;
+ if (ret = uint64.try_parse (str, out val)) {
+ dest2.set_ulong ((ulong)val);
+ }
+ } else if ((int)t == 20) { // gboolean
+ bool val = (str == "TRUE");
+ dest2.set_boolean (val); // TODO: huh, investigate why the type is gboolean and not bool coming out
but is going in
+ ret = true;
+ } else if (t == typeof (bool)) {
+ bool val;
+ if (ret = bool.try_parse (str, out val)) {
+ dest2.set_boolean (val);
+ }
+ } else if (t == typeof (float)) {
+ double val;
+ if (ret = double.try_parse (str, out val)) {
+ dest2.set_float ((float)val);
+ }
+ } else if (t == typeof (double)) {
+ double val;
+ if (ret = double.try_parse (str, out val)) {
+ dest2.set_double (val);
+ }
+ } else if (t == typeof (string)) {
+ dest2.set_string (str);
+ ret = true;
+ } else if (t == typeof (char)) {
+ int64 val;
+ if (ret = int64.try_parse (str, out val)) {
+ dest2.set_char ((char)val);
+ }
+ } else if (t == typeof (uchar)) {
+ int64 val;
+ if (ret = int64.try_parse (str, out val)) {
+ dest2.set_uchar ((uchar)val);
+ }
+ } else if (t == Type.BOXED) {
+ } else if (t.is_enum ()) {
+ int64 val;
+ if (ret = int64.try_parse (str, out val)) {
+ dest2.set_enum ((int)val);
+ }
+ } else if (t.is_flags ()) {
+ } else if (t.is_object ()) {
+ } else {
+ }
- if (ret == true) {
- dest = dest2;
- return true;
- } else {
- throw new SerializableError.UNSUPPORTED_TYPE ("%s/%s", t.name (), t.to_string
());
- }
- }
+ if (ret == true) {
+ dest = dest2;
+ return true;
+ } else {
+ throw new SerializableError.UNSUPPORTED_TYPE ("%s/%s", t.name (), t.to_string ());
+ }
+ }
- /**
- * Method to provide custome transformations from
- * a { link GLib.Value} to strings. Could be used on { link deserialize} or simple
- * transformations to strings.
- *
- * Some specialized classes, like derived from { link Serializable} class
- * implementator, can provide custome transformations.
- *
- * Returns: { link true} if transformation was handled, { link false} otherwise.
- *
- * Implementors:
- * To be overrided by derived classes of implementators to provide custome
- * transformations. Declare it as virtual if you want derived classes of
- * implementators to provide custome transformations.
- * Call this method before use standard Serializable or implementator ones.
- *
- * @node a { link GXml.Node} to get attribute from
- * @prop a { link GLib.ParamSpec} describing attribute to deserialize
- */
- public abstract bool transform_to_string (GLib.Value val, ref string str);
- /**
- * Transforms a { link GLib.Value} to its string representation.
- *
- * By default use GObject standard transformations.
- *
- */
- public static string gvalue_to_string (GLib.Value val)
- throws SerializableError
- {
- Value ret = "";
- if (Value.type_transformable (val.type (), typeof (string)))
- {
- val.transform (ref ret);
- return ret.dup_string ();
- }
- else
- {
- throw new SerializableError.UNSUPPORTED_TYPE ("Can't transform '%s' to
string", val.type ().name ());
- }
- }
- }
+ /**
+ * Method to provide custome transformations from
+ * a { link GLib.Value} to strings. Could be used on { link deserialize} or simple
+ * transformations to strings.
+ *
+ * Some specialized classes, like derived from { link Serializable} class
+ * implementator, can provide custome transformations.
+ *
+ * Returns: { link true} if transformation was handled, { link false} otherwise.
+ *
+ * Implementors:
+ * To be overrided by derived classes of implementators to provide custome
+ * transformations. Declare it as virtual if you want derived classes of
+ * implementators to provide custome transformations.
+ * Call this method before use standard Serializable or implementator ones.
+ *
+ * @node a { link GXml.Node} to get attribute from
+ * @prop a { link GLib.ParamSpec} describing attribute to deserialize
+ */
+ public abstract bool transform_to_string (GLib.Value val, ref string str);
+ /**
+ * Transforms a { link GLib.Value} to its string representation.
+ *
+ * By default use GObject standard transformations.
+ *
+ */
+ public static string gvalue_to_string (GLib.Value val)
+ throws SerializableError
+ {
+ Value ret = "";
+ if (Value.type_transformable (val.type (), typeof (string)))
+ {
+ val.transform (ref ret);
+ return ret.dup_string ();
+ }
+ else
+ {
+ throw new SerializableError.UNSUPPORTED_TYPE ("Can't transform '%s' to string", val.type ().name ());
+ }
+ }
+ }
- /**
- * Errors from { link Serialization}.
- */
- public errordomain SerializableError {
- /**
- * An object with a known { link GLib.Type} that we do not support was encountered.
- */
- UNSUPPORTED_TYPE
- }
+ /**
+ * Errors from { link Serialization}.
+ */
+ public errordomain SerializableError {
+ /**
+ * An object with a known { link GLib.Type} that we do not support was encountered.
+ */
+ UNSUPPORTED_TYPE
+ }
}
diff --git a/gxml/SerializableJson.vala b/gxml/SerializableJson.vala
index b1d34eb..e164c84 100644
--- a/gxml/SerializableJson.vala
+++ b/gxml/SerializableJson.vala
@@ -34,49 +34,49 @@
*/
public class GXml.SerializableJson : GLib.Object, Serializable
{
- /* Serializable Interface properties */
- protected ParamSpec[] properties { get; set; }
- public HashTable<string,GLib.ParamSpec> ignored_serializable_properties { get; protected set; }
- public HashTable<string,GXml.Node> unknown_serializable_property { get; protected set; }
+ /* Serializable Interface properties */
+ protected ParamSpec[] properties { get; set; }
+ public HashTable<string,GLib.ParamSpec> ignored_serializable_properties { get; protected set; }
+ public HashTable<string,GXml.Node> unknown_serializable_property { get; protected set; }
public string? serialized_xml_node_value { get; protected set; default = null; }
- public virtual string node_name () { return "Object"; }
- public virtual bool property_use_nick () { return false; }
+ public virtual string node_name () { return "Object"; }
+ public virtual bool property_use_nick () { return false; }
- public virtual GLib.ParamSpec? find_property_spec (string property_name)
- {
- return default_find_property_spec (property_name);
- }
+ public virtual GLib.ParamSpec? find_property_spec (string property_name)
+ {
+ return default_find_property_spec (property_name);
+ }
- public virtual void init_properties ()
- {
- default_init_properties ();
- }
+ public virtual void init_properties ()
+ {
+ default_init_properties ();
+ }
- public virtual GLib.ParamSpec[] list_serializable_properties ()
- {
- return default_list_serializable_properties ();
- }
+ public virtual GLib.ParamSpec[] list_serializable_properties ()
+ {
+ return default_list_serializable_properties ();
+ }
- public virtual void get_property_value (GLib.ParamSpec spec, ref Value val)
- {
- default_get_property_value (spec, ref val);
- }
+ public virtual void get_property_value (GLib.ParamSpec spec, ref Value val)
+ {
+ default_get_property_value (spec, ref val);
+ }
- public virtual void set_property_value (GLib.ParamSpec spec, GLib.Value val)
- {
- default_set_property_value (spec, val);
- }
+ public virtual void set_property_value (GLib.ParamSpec spec, GLib.Value val)
+ {
+ default_set_property_value (spec, val);
+ }
- public virtual bool transform_from_string (string str, ref GLib.Value dest)
- {
- return false;
- }
+ public virtual bool transform_from_string (string str, ref GLib.Value dest)
+ {
+ return false;
+ }
- public virtual bool transform_to_string (GLib.Value val, ref string str)
- {
- return false;
- }
+ public virtual bool transform_to_string (GLib.Value val, ref string str)
+ {
+ return false;
+ }
/**
* If @node is a Document serialize just add an <Object> element.
*
@@ -85,176 +85,176 @@ public class GXml.SerializableJson : GLib.Object, Serializable
* Is up to you to add convenient Element node to a Document, in order to be
* used by serialize and add new <Object> tags per object to serialize.
*/
- public Node? serialize (Node node) throws GLib.Error
- {
- Document doc;
- Element root;
- ParamSpec[] props;
- string oid = "%p".printf(this);
+ public Node? serialize (Node node) throws GLib.Error
+ {
+ Document doc;
+ Element root;
+ ParamSpec[] props;
+ string oid = "%p".printf(this);
- if (node is Document)
- doc = (Document) node;
- else
- doc = node.owner_document;
+ if (node is Document)
+ doc = (Document) node;
+ else
+ doc = node.owner_document;
- root = doc.create_element ("Object");
- doc.append_child (root);
- root.set_attribute ("otype", this.get_type ().name ());
- root.set_attribute ("oid", oid);
- props = list_serializable_properties ();
- foreach (ParamSpec prop_spec in props) {
- serialize_property (root, prop_spec);
- }
- return root;
- }
+ root = doc.create_element ("Object");
+ doc.append_child (root);
+ root.set_attribute ("otype", this.get_type ().name ());
+ root.set_attribute ("oid", oid);
+ props = list_serializable_properties ();
+ foreach (ParamSpec prop_spec in props) {
+ serialize_property (root, prop_spec);
+ }
+ return root;
+ }
- public virtual GXml.Node? serialize_property (Element element,
- GLib.ParamSpec prop)
- throws GLib.Error
- {
- Type type;
- Value val;
- Node value_node = null;
- Element prop_node;
+ public virtual GXml.Node? serialize_property (Element element,
+ GLib.ParamSpec prop)
+ throws GLib.Error
+ {
+ Type type;
+ Value val;
+ Node value_node = null;
+ Element prop_node;
- type = prop.value_type;
+ type = prop.value_type;
- if (type.is_a (typeof (Serializable))) {
- val = Value (type);
- this.get_property_value (prop, ref val);
- return ((Serializable) val.get_object ()).serialize (element);
- }
+ if (type.is_a (typeof (Serializable))) {
+ val = Value (type);
+ this.get_property_value (prop, ref val);
+ return ((Serializable) val.get_object ()).serialize (element);
+ }
- var doc = element.owner_document;
- prop_node = doc.create_element ("Property");
- prop_node.set_attribute ("ptype", prop.value_type.name ());
- prop_node.set_attribute ("pname", prop.name);
- element.append_child (prop_node);
+ var doc = element.owner_document;
+ prop_node = doc.create_element ("Property");
+ prop_node.set_attribute ("ptype", prop.value_type.name ());
+ prop_node.set_attribute ("pname", prop.name);
+ element.append_child (prop_node);
- if (type.is_enum ())
- {
- val = Value (typeof (int));
- this.get_property_value (prop, ref val);
- value_node = doc.create_text_node ("%d".printf (val.get_int ()));
- prop_node.append_child (value_node);
- return prop_node;
- }
- else if (Value.type_transformable (type, typeof (string)))
- { // e.g. int, double, string, bool
-// GLib.message ("DEBUG: Transforming property name '%s' of object '%s', using GLib defaults",
prop.name, this.get_type ().name ());
- val = Value (type);
- Value t = Value (typeof (string));
- this.get_property_value (prop, ref val);
- val.transform (ref t);
- value_node = doc.create_text_node (t.get_string ());
- prop_node.append_child (value_node);
- return prop_node;
- }
- else if (type == typeof (GLib.Type)) {
- value_node = doc.create_text_node (type.name ());
- prop_node.append_child (value_node);
- return prop_node;
- }
- else if (type.is_a (typeof (GLib.Object))
- && ! type.is_a (typeof (Gee.Collection)))
- {
- GLib.Object child_object;
+ if (type.is_enum ())
+ {
+ val = Value (typeof (int));
+ this.get_property_value (prop, ref val);
+ value_node = doc.create_text_node ("%d".printf (val.get_int ()));
+ prop_node.append_child (value_node);
+ return prop_node;
+ }
+ else if (Value.type_transformable (type, typeof (string)))
+ { // e.g. int, double, string, bool
+// GLib.message ("DEBUG: Transforming property name '%s' of object '%s', using GLib defaults",
prop.name, this.get_type ().name ());
+ val = Value (type);
+ Value t = Value (typeof (string));
+ this.get_property_value (prop, ref val);
+ val.transform (ref t);
+ value_node = doc.create_text_node (t.get_string ());
+ prop_node.append_child (value_node);
+ return prop_node;
+ }
+ else if (type == typeof (GLib.Type)) {
+ value_node = doc.create_text_node (type.name ());
+ prop_node.append_child (value_node);
+ return prop_node;
+ }
+ else if (type.is_a (typeof (GLib.Object))
+ && ! type.is_a (typeof (Gee.Collection)))
+ {
+ GLib.Object child_object;
- // TODO: this is going to get complicated
- val = Value (typeof (GLib.Object));
- this.get_property_value (prop, ref val);
- child_object = val.get_object ();
- Document value_doc = Serialization.serialize_object (child_object);
- value_node = doc.copy_node (value_doc.document_element);
- prop_node.append_child (value_node);
- return prop_node;
- }
- //GLib.message ("DEBUG: serialing unknown property type '%s' of object '%s'", prop.name,
this.get_type ().name ());
- serialize_unknown_property_type (prop_node, prop, out value_node);
- return prop_node;
- }
+ // TODO: this is going to get complicated
+ val = Value (typeof (GLib.Object));
+ this.get_property_value (prop, ref val);
+ child_object = val.get_object ();
+ Document value_doc = Serialization.serialize_object (child_object);
+ value_node = doc.copy_node (value_doc.document_element);
+ prop_node.append_child (value_node);
+ return prop_node;
+ }
+ //GLib.message ("DEBUG: serialing unknown property type '%s' of object '%s'", prop.name, this.get_type
().name ());
+ serialize_unknown_property_type (prop_node, prop, out value_node);
+ return prop_node;
+ }
- public Node? deserialize (Node node) throws GLib.Error
- {
- Element obj_elem;
- ParamSpec[] specs;
+ public Node? deserialize (Node node) throws GLib.Error
+ {
+ Element obj_elem;
+ ParamSpec[] specs;
- if (node is Document) {
- obj_elem = node.owner_document.document_element;
- }
- else {
- obj_elem = (Element) node;
- }
+ if (node is Document) {
+ obj_elem = node.owner_document.document_element;
+ }
+ else {
+ obj_elem = (Element) node;
+ }
- specs = this.list_serializable_properties ();
+ specs = this.list_serializable_properties ();
- foreach (Node child_node in obj_elem.child_nodes) {
- deserialize_property (child_node);
- }
- return obj_elem;
- }
+ foreach (Node child_node in obj_elem.child_nodes) {
+ deserialize_property (child_node);
+ }
+ return obj_elem;
+ }
- public virtual bool deserialize_property (GXml.Node property_node) throws GLib.Error
- {
- //GLib.message ("At SerializableJson.deserialize_property");
- if (property_node.node_name == "Property")
- {
- Element prop_elem;
- string pname;
- string ptype;
- Type type;
- Value val;
- ParamSpec spec;
- //string ptype;
+ public virtual bool deserialize_property (GXml.Node property_node) throws GLib.Error
+ {
+ //GLib.message ("At SerializableJson.deserialize_property");
+ if (property_node.node_name == "Property")
+ {
+ Element prop_elem;
+ string pname;
+ string ptype;
+ Type type;
+ Value val;
+ ParamSpec spec;
+ //string ptype;
- prop_elem = (Element)property_node;
- pname = prop_elem.get_attribute ("pname");
- ptype = prop_elem.get_attribute ("ptype");
- type = Type.from_name (ptype);
- // Check name and type for property
- spec = this.find_property_spec (pname);
+ prop_elem = (Element)property_node;
+ pname = prop_elem.get_attribute ("pname");
+ ptype = prop_elem.get_attribute ("ptype");
+ type = Type.from_name (ptype);
+ // Check name and type for property
+ spec = this.find_property_spec (pname);
- if (spec == null) {
- GLib.message ("Deserializing object of type '%s' claimed unknown property
named '%s'\nXML [%s]", ptype, pname, property_node.to_string ());
- unknown_serializable_property.set (property_node.node_name, property_node);
- }
- else {
- if (spec.value_type.is_a (typeof(Serializable)))
- {
- Value vobj = Value (spec.value_type);
- this.get_property (pname, ref vobj);
- ((Serializable) vobj).deserialize (property_node);
- }
- else {
- val = Value (type);
- if (transform_from_string (prop_elem.content, ref val)) {
- this.set_property_value (spec, val);
- return true;
- }
- else if (GLib.Value.type_transformable (type, typeof (string))) {
- Serializable.string_to_gvalue (prop_elem.content, ref val);
- this.set_property_value (spec, val);
- //GLib.message (@"Setting value to property $(spec.name)");
- }
- else if (type.is_a (typeof (GLib.Object)))
- {
- GXml.Node prop_elem_child;
- Object property_object;
+ if (spec == null) {
+ GLib.message ("Deserializing object of type '%s' claimed unknown property named '%s'\nXML [%s]",
ptype, pname, property_node.to_string ());
+ unknown_serializable_property.set (property_node.node_name, property_node);
+ }
+ else {
+ if (spec.value_type.is_a (typeof(Serializable)))
+ {
+ Value vobj = Value (spec.value_type);
+ this.get_property (pname, ref vobj);
+ ((Serializable) vobj).deserialize (property_node);
+ }
+ else {
+ val = Value (type);
+ if (transform_from_string (prop_elem.content, ref val)) {
+ this.set_property_value (spec, val);
+ return true;
+ }
+ else if (GLib.Value.type_transformable (type, typeof (string))) {
+ Serializable.string_to_gvalue (prop_elem.content, ref val);
+ this.set_property_value (spec, val);
+ //GLib.message (@"Setting value to property $(spec.name)");
+ }
+ else if (type.is_a (typeof (GLib.Object)))
+ {
+ GXml.Node prop_elem_child;
+ Object property_object;
- prop_elem_child = prop_elem.first_child;
+ prop_elem_child = prop_elem.first_child;
- property_object = Serialization.deserialize_object_from_node
(prop_elem_child);
- val.set_object (property_object);
- return true;
- }
- else {
- deserialize_unknown_property_type (prop_elem, spec);
- return false;
- }
- }
- }
- return true;
- }
- return false;
- }
+ property_object = Serialization.deserialize_object_from_node (prop_elem_child);
+ val.set_object (property_object);
+ return true;
+ }
+ else {
+ deserialize_unknown_property_type (prop_elem, spec);
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+ return false;
+ }
}
diff --git a/gxml/SerializableObjectModel.vala b/gxml/SerializableObjectModel.vala
index 1308647..05428a9 100644
--- a/gxml/SerializableObjectModel.vala
+++ b/gxml/SerializableObjectModel.vala
@@ -22,297 +22,292 @@
public abstract class GXml.SerializableObjectModel : Object, Serializable
{
- /* Serializable interface properties */
- protected ParamSpec[] properties { get; set; }
- public GLib.HashTable<string,GLib.ParamSpec> ignored_serializable_properties { get; protected set; }
- public string? serialized_xml_node_value { get; protected set; default=null; }
- public GLib.HashTable<string,GXml.Node> unknown_serializable_property { get; protected set; }
+ /* Serializable interface properties */
+ protected ParamSpec[] properties { get; set; }
+ public GLib.HashTable<string,GLib.ParamSpec> ignored_serializable_properties { get; protected set; }
+ public string? serialized_xml_node_value { get; protected set; default=null; }
+ public GLib.HashTable<string,GXml.Node> unknown_serializable_property { get; protected set; }
public virtual bool property_use_nick () { return false; }
- public virtual string node_name ()
- {
- return default_node_name ();
- }
- public string default_node_name ()
- {
- return get_type().name().down();
- }
+ public virtual string node_name ()
+ {
+ return default_node_name ();
+ }
+ public string default_node_name ()
+ {
+ return get_type().name().down();
+ }
- public SerializableObjectModel ()
- {
- serialized_xml_node_value = null;
- }
+ public virtual GLib.ParamSpec? find_property_spec (string property_name)
+ {
+ return default_find_property_spec (property_name);
+ }
- public virtual GLib.ParamSpec? find_property_spec (string property_name)
- {
- return default_find_property_spec (property_name);
- }
+ public virtual void init_properties ()
+ {
+ default_init_properties ();
+ }
- public virtual void init_properties ()
- {
- default_init_properties ();
- }
+ public virtual GLib.ParamSpec[] list_serializable_properties ()
+ {
+ return default_list_serializable_properties ();
+ }
- public virtual GLib.ParamSpec[] list_serializable_properties ()
- {
- return default_list_serializable_properties ();
- }
+ public virtual void get_property_value (GLib.ParamSpec spec, ref Value val)
+ {
+ default_get_property_value (spec, ref val);
+ }
- public virtual void get_property_value (GLib.ParamSpec spec, ref Value val)
- {
- default_get_property_value (spec, ref val);
- }
+ public virtual void set_property_value (GLib.ParamSpec spec, GLib.Value val)
+ {
+ default_set_property_value (spec, val);
+ }
- public virtual void set_property_value (GLib.ParamSpec spec, GLib.Value val)
- {
- default_set_property_value (spec, val);
- }
+ public virtual bool transform_from_string (string str, ref GLib.Value dest)
+ {
+ return false;
+ }
- public virtual bool transform_from_string (string str, ref GLib.Value dest)
- {
- return false;
- }
+ public virtual bool transform_to_string (GLib.Value val, ref string str)
+ {
+ return false;
+ }
- public virtual bool transform_to_string (GLib.Value val, ref string str)
- {
- return false;
- }
+ public virtual Node? serialize (Node node)
+ throws GLib.Error
+ requires (node_name () != null)
+ requires (node is Document || node is Element)
+ {
+ return default_serialize (node);
+ }
- public virtual Node? serialize (Node node)
- throws GLib.Error
- requires (node_name () != null)
- requires (node is Document || node is Element)
- {
- return default_serialize (node);
- }
+ public Node? default_serialize (Node node) throws GLib.Error
+ {
+ Document doc;
+ if (node is Document)
+ doc = (Document) node;
+ else
+ doc = node.owner_document;
+ var element = doc.create_element (node_name ());
+ foreach (ParamSpec spec in list_serializable_properties ()) {
+ serialize_property (element, spec);
+ }
+ foreach (Node n in unknown_serializable_property.get_values ()) {
+ if (n is Element) {
+ var e = (Node) doc.create_element (n.node_name);
+ n.copy (ref e, true);
+ element.append_child (e);
+ }
+ if (n is Attr) {
+ element.set_attribute (n.node_name, n.node_value);
+ var a = (Node) element.get_attribute_node (n.node_name);
+ n.copy (ref a);
+ }
+ }
+ // Setting element content
+ if (serialized_xml_node_value != null) {
+ var t = doc.create_text_node (serialized_xml_node_value);
+ element.append_child (t);
+ }
- public Node? default_serialize (Node node) throws GLib.Error
- {
- Document doc;
- if (node is Document)
- doc = (Document) node;
- else
- doc = node.owner_document;
- var element = doc.create_element (node_name ());
- foreach (ParamSpec spec in list_serializable_properties ()) {
- serialize_property (element, spec);
- }
- foreach (Node n in unknown_serializable_property.get_values ()) {
- if (n is Element) {
- var e = (Node) doc.create_element (n.node_name);
- n.copy (ref e, true);
- element.append_child (e);
- }
- if (n is Attr) {
- element.set_attribute (n.node_name, n.node_value);
- var a = (Node) element.get_attribute_node (n.node_name);
- n.copy (ref a);
- }
- }
- // Setting element content
- if (serialized_xml_node_value != null) {
- var t = doc.create_text_node (serialized_xml_node_value);
- element.append_child (t);
- }
+ node.append_child (element);
+ return element;
+ }
- node.append_child (element);
- return element;
- }
+ public virtual GXml.Node? serialize_property (Element element,
+ GLib.ParamSpec prop)
+ throws GLib.Error
+ {
+ return default_serialize_property (element, prop);
+ }
+ public GXml.Node? default_serialize_property (Element element,
+ GLib.ParamSpec prop)
+ throws GLib.Error
+ {
+ if (prop.value_type.is_a (typeof (Serializable)))
+ {
+ var v = Value (typeof (Object));
+ get_property (prop.name, ref v);
+ var obj = (Serializable) v.get_object ();
+ if (obj != null)
+ return obj.serialize (element);
+ }
+ Value oval;
+ if (prop.value_type.is_a (Type.ENUM))
+ oval = Value (typeof (int));
+ else
+ oval = Value (prop.value_type);
+ get_property (prop.name, ref oval);
+ string val = "";
+ if (prop.value_type.is_a (Type.ENUM)) {
+ try {
+ val = Enumeration.get_nick_camelcase (prop.value_type, oval.get_int ());
+ } catch (EnumerationError e) { val = null; }
+ }
+ else
+ {
+ if (!transform_to_string (oval, ref val)) {
+ if (Value.type_transformable (prop.value_type, typeof (string)))
+ {
+ Value rval = Value (typeof (string));
+ oval.transform (ref rval);
+ val = rval.dup_string ();
+ }
+ else {
+ Node node = null;
+ this.serialize_unknown_property (element, prop, out node);
+ return node;
+ }
+ }
+ }
+ string attr_name;
+ if (property_use_nick () &&
+ prop.get_nick () != null &&
+ prop.get_nick () != "")
+ attr_name = prop.get_nick ();
+ else
+ attr_name = prop.get_name ();
+ var attr = element.get_attribute_node (attr_name);
+ if (attr == null) {
+ if (val != null)
+ element.set_attribute (attr_name, val);
+ }
+ else
+ attr.value = val;
+ return (Node) attr;
+ }
- public virtual GXml.Node? serialize_property (Element element,
- GLib.ParamSpec prop)
- throws GLib.Error
- {
- return default_serialize_property (element, prop);
- }
- public GXml.Node? default_serialize_property (Element element,
- GLib.ParamSpec prop)
- throws GLib.Error
- {
- if (prop.value_type.is_a (typeof (Serializable)))
- {
- var v = Value (typeof (Object));
- get_property (prop.name, ref v);
- var obj = (Serializable) v.get_object ();
- if (obj != null)
- return obj.serialize (element);
- }
- Value oval;
- if (prop.value_type.is_a (Type.ENUM))
- oval = Value (typeof (int));
- else
- oval = Value (prop.value_type);
- get_property (prop.name, ref oval);
- string val = "";
- if (prop.value_type.is_a (Type.ENUM)) {
- try {
- val = Enumeration.get_nick_camelcase (prop.value_type, oval.get_int ());
- } catch (EnumerationError e) { val = null; }
- }
- else
- {
- if (!transform_to_string (oval, ref val)) {
- if (Value.type_transformable (prop.value_type, typeof (string)))
- {
- Value rval = Value (typeof (string));
- oval.transform (ref rval);
- val = rval.dup_string ();
- }
- else {
- Node node = null;
- this.serialize_unknown_property (element, prop, out node);
- return node;
- }
- }
- }
- string attr_name;
- if (property_use_nick () &&
- prop.get_nick () != null &&
- prop.get_nick () != "")
- attr_name = prop.get_nick ();
- else
- attr_name = prop.get_name ();
- var attr = element.get_attribute_node (attr_name);
- if (attr == null) {
- if (val != null)
- element.set_attribute (attr_name, val);
- }
- else
- attr.value = val;
- return (Node) attr;
- }
+ public virtual Node? deserialize (Node node)
+ throws GLib.Error
+ requires (node_name () != null)
+ {
+ return default_deserialize (node);
+ }
+ public Node? default_deserialize (Node node)
+ throws GLib.Error
+ {
+ Document doc;
+ if (node is Document) {
+ doc = (Document) node;
+ return_val_if_fail (doc.document_element != null, null);
+ }
+ else
+ doc = node.owner_document;
+ Element element;
+ if (node is Element)
+ element = (Element) node;
+ else
+ element = (Element) doc.document_element;
+ return_val_if_fail (element != null, null);
+ if (node_name () == null) {
+ message (@"WARNING: Object type '$(get_type ().name ())' have no Node Name defined");
+ assert_not_reached ();
+ }
+ if (element.node_name.down () != node_name ().down ()) {
+ message (@"WARNING: wrong node name is '$(element.node_name.down ())' is different to '$(node_name
().down ())'");
+ }
+ foreach (Attr attr in element.attributes.get_values ())
+ {
+ deserialize_property (attr);
+ }
+
+ if (element.has_child_nodes ())
+ {
+ foreach (Node n in element.child_nodes)
+ {
+ if (n is Text)
+ serialized_xml_node_value = n.node_value;
+ else
+ deserialize_property (n);
+ }
+ }
+ return null;
+ }
- public virtual Node? deserialize (Node node)
- throws GLib.Error
- requires (node_name () != null)
- {
- return default_deserialize (node);
- }
- public Node? default_deserialize (Node node)
- throws GLib.Error
- {
- Document doc;
- if (node is Document) {
- doc = (Document) node;
- return_val_if_fail (doc.document_element != null, null);
- }
- else
- doc = node.owner_document;
- Element element;
- if (node is Element)
- element = (Element) node;
- else
- element = (Element) doc.document_element;
- return_val_if_fail (element != null, null);
- if (node_name () == null) {
- message (@"WARNING: Object type '$(get_type ().name ())' have no Node Name defined");
- assert_not_reached ();
- }
- if (element.node_name.down () != node_name ().down ()) {
- message (@"WARNING: wrong node name is '$(element.node_name.down ())' is different to
'$(node_name ().down ())'");
- }
- foreach (Attr attr in element.attributes.get_values ())
- {
- deserialize_property (attr);
- }
-
- if (element.has_child_nodes ())
- {
- foreach (Node n in element.child_nodes)
- {
- if (n is Text)
- serialized_xml_node_value = n.node_value;
- else
- deserialize_property (n);
- }
- }
- return null;
- }
+ public virtual bool deserialize_property (GXml.Node property_node)
+ throws GLib.Error
+ {
+ return default_deserialize_property (property_node);
+ }
+ public bool default_deserialize_property (GXml.Node property_node)
+ throws GLib.Error
+ {
+ bool ret = false;
+ var prop = find_property_spec (property_node.node_name);
+ if (prop == null) {
+ // FIXME: Event emit
+ if (!(property_node is Text))
+ unknown_serializable_property.set (property_node.node_name, property_node);
+ return true;
+ }
+ if (prop.value_type.is_a (typeof (Serializable)))
+ {
+ Value vobj = Value (typeof(Object));
+ get_property (prop.name, ref vobj);
+ if (vobj.get_object () == null) {
+ var obj = Object.new (prop.value_type);
+ ((Serializable) obj).deserialize (property_node);
+ set_property (prop.name, obj);
+ }
+ else
+ ((Serializable) vobj.get_object ()).deserialize (property_node);
+ return true;
+ }
+ else {
+ Value val;
+ if (prop.value_type == Type.ENUM)
+ val = Value (typeof (int));
+ else
+ val = Value (prop.value_type);
+ if (property_node is GXml.Attr)
+ {
+ if (prop.value_type.is_a (Type.ENUM)) {
+ EnumValue env;
+ try {
+ env = Enumeration.parse (prop.value_type, property_node.node_value);
+ val.set_enum (env.value);
+ }
+ catch (EnumerationError e) {}
+ }
+ else {
+ if (!transform_from_string (property_node.node_value, ref val)) {
+ Value ptmp = Value (typeof (string));
+ ptmp.set_string (property_node.node_value);
+ if (Value.type_transformable (typeof (string), prop.value_type))
+ ret = ptmp.transform (ref val);
+ else
+ ret = string_to_gvalue (property_node.node_value, ref val);
+ }
+ }
+ set_property (prop.name, val);
+ return ret;
+ }
+ }
+ // Attribute can't be deseralized with standard methods. Up to the implementor.
+ this.deserialize_unknown_property (property_node, prop);
+ return true;
+ }
+ public abstract string to_string ();
- public virtual bool deserialize_property (GXml.Node property_node)
- throws GLib.Error
- {
- return default_deserialize_property (property_node);
- }
- public bool default_deserialize_property (GXml.Node property_node)
- throws GLib.Error
- {
- bool ret = false;
- var prop = find_property_spec (property_node.node_name);
- if (prop == null) {
- // FIXME: Event emit
- if (!(property_node is Text))
- unknown_serializable_property.set (property_node.node_name, property_node);
- return true;
- }
- if (prop.value_type.is_a (typeof (Serializable)))
- {
- Value vobj = Value (typeof(Object));
- get_property (prop.name, ref vobj);
- if (vobj.get_object () == null) {
- var obj = Object.new (prop.value_type);
- ((Serializable) obj).deserialize (property_node);
- set_property (prop.name, obj);
- }
- else
- ((Serializable) vobj.get_object ()).deserialize (property_node);
- return true;
- }
- else {
- Value val;
- if (prop.value_type == Type.ENUM)
- val = Value (typeof (int));
- else
- val = Value (prop.value_type);
- if (property_node is GXml.Attr)
- {
- if (prop.value_type.is_a (Type.ENUM)) {
- EnumValue env;
- try {
- env = Enumeration.parse (prop.value_type,
property_node.node_value);
- val.set_enum (env.value);
- }
- catch (EnumerationError e) {}
- }
- else {
- if (!transform_from_string (property_node.node_value, ref val)) {
- Value ptmp = Value (typeof (string));
- ptmp.set_string (property_node.node_value);
- if (Value.type_transformable (typeof (string),
prop.value_type))
- ret = ptmp.transform (ref val);
- else
- ret = string_to_gvalue (property_node.node_value, ref
val);
- }
- }
- set_property (prop.name, val);
- return ret;
- }
- }
- // Attribute can't be deseralized with standard methods. Up to the implementor.
- this.deserialize_unknown_property (property_node, prop);
- return true;
- }
- public abstract string to_string ();
-
- public static bool equals (SerializableObjectModel a, SerializableObjectModel b)
- {
- if (b.get_type () == a.get_type ()) {
- var alp = ((Serializable)a).list_serializable_properties ();
- bool ret = true;
- foreach (ParamSpec p in alp) {
- var bp = ((Serializable)b).find_property_spec (p.name);
- if (bp != null) {
- Value apval = Value (p.value_type);
- ((Serializable)a).get_property_value (p, ref apval);
- Value bpval = Value (bp.value_type);;
- ((Serializable)b).get_property_value (bp, ref bpval);
- if ( apval != bpval)
- ret = false;
- }
- }
- return ret;
- }
- return false;
- }
+ public static bool equals (SerializableObjectModel a, SerializableObjectModel b)
+ {
+ if (b.get_type () == a.get_type ()) {
+ var alp = ((Serializable)a).list_serializable_properties ();
+ bool ret = true;
+ foreach (ParamSpec p in alp) {
+ var bp = ((Serializable)b).find_property_spec (p.name);
+ if (bp != null) {
+ Value apval = Value (p.value_type);
+ ((Serializable)a).get_property_value (p, ref apval);
+ Value bpval = Value (bp.value_type);;
+ ((Serializable)b).get_property_value (bp, ref bpval);
+ if ( apval != bpval)
+ ret = false;
+ }
+ }
+ return ret;
+ }
+ return false;
+ }
}
diff --git a/test/SerializableObjectModelTest.vala b/test/SerializableObjectModelTest.vala
index 3fc2b6f..504582e 100644
--- a/test/SerializableObjectModelTest.vala
+++ b/test/SerializableObjectModelTest.vala
@@ -30,1058 +30,1058 @@ const string XML_CPU_FILE =
public class ObjectModel : SerializableObjectModel
{
- public override string to_string ()
- {
- var lp = list_serializable_properties ();
- string ret = this.get_type ().name () +"{Properties:\n";
- foreach (ParamSpec p in lp) {
- Value v = Value (p.value_type);
- get_property_value (p, ref v);
- string t;
- try { t = gvalue_to_string (v); } catch { t = "[CANT_TRANSFORM]"; }
- ret += @"[$(p.name)]{" + t + "}\n";
- }
- return ret + "}";
- }
+ public override string to_string ()
+ {
+ var lp = list_serializable_properties ();
+ string ret = this.get_type ().name () +"{Properties:\n";
+ foreach (ParamSpec p in lp) {
+ Value v = Value (p.value_type);
+ get_property_value (p, ref v);
+ string t;
+ try { t = gvalue_to_string (v); } catch { t = "[CANT_TRANSFORM]"; }
+ ret += @"[$(p.name)]{" + t + "}\n";
+ }
+ return ret + "}";
+ }
}
public class Computer : ObjectModel
{
- [Description (nick="Manufacturer")]
- public string manufacturer { get; set; }
- public string model { get; set; }
- public int cores { get; set; }
- public float ghz { get; set; }
-
- public Computer ()
- {
- manufacturer = "MexicanLaptop, Inc.";
- model = "LQ59678";
- cores = 8;
- ghz = (float) 3.5;
- }
+ [Description (nick="Manufacturer")]
+ public string manufacturer { get; set; }
+ public string model { get; set; }
+ public int cores { get; set; }
+ public float ghz { get; set; }
+
+ public Computer ()
+ {
+ manufacturer = "MexicanLaptop, Inc.";
+ model = "LQ59678";
+ cores = 8;
+ ghz = (float) 3.5;
+ }
}
public class Manual : ObjectModel
{
- public string document { get; set; }
- public int pages { get; set; }
- public string contents {
- get { return serialized_xml_node_value; }
- set { serialized_xml_node_value = value; }
- }
-
- public Manual ()
- {
- document = "MANUAL DOCUMENTATION";
- pages = 3;
- contents = "TEXT INTO THE MANUAL DOCUMENT";
- init_properties ();
- ignored_serializable_properties.set ("contents", get_class ().find_property ("contents"));
- }
-
- public override string to_string ()
- {
- return base.to_string () + @"CONTENTS: { $contents }";
- }
+ public string document { get; set; }
+ public int pages { get; set; }
+ public string contents {
+ get { return serialized_xml_node_value; }
+ set { serialized_xml_node_value = value; }
+ }
+
+ public Manual ()
+ {
+ document = "MANUAL DOCUMENTATION";
+ pages = 3;
+ contents = "TEXT INTO THE MANUAL DOCUMENT";
+ init_properties ();
+ ignored_serializable_properties.set ("contents", get_class ().find_property ("contents"));
+ }
+
+ public override string to_string ()
+ {
+ return base.to_string () + @"CONTENTS: { $contents }";
+ }
}
public class Package : ObjectModel
{
- Array<string> _tags = new Array<string> ();
- public Computer computer { get; set; }
- public Manual manual { get; set; }
- public string source { get; set; }
- public string destiny { get; set; }
- [Description (nick="tag", blurb="tags in package")]
- public Array<string> tags { get {return _tags;} }
+ Array<string> _tags = new Array<string> ();
+ public Computer computer { get; set; }
+ public Manual manual { get; set; }
+ public string source { get; set; }
+ public string destiny { get; set; }
+ [Description (nick="tag", blurb="tags in package")]
+ public Array<string> tags { get {return _tags;} }
- public override bool property_use_nick () { return true; }
+ public override bool property_use_nick () { return true; }
- public Package ()
- {
- computer = new Computer ();
- manual = new Manual ();
- source = "Mexico";
- destiny = "World";
- ((Serializable) this).serialize_unknown_property.connect ( (element, prop, out node) => {
- //GLib.message (@"Serializing Unknown Property: $(prop.name) | $(prop.get_nick ())");
- if (prop.name == "tags")
- {
- for (int i = 0; i < tags.length; i++) {
- var str = tags.index (i);
- node = element.owner_document.create_element ("tag");
- ((Element) node).content = str;
- element.append_child (node);
- }
- }
- });
- ((Serializable) this).deserialize_unknown_property.connect ( (element, prop) => {
- //GLib.message (@"Deserializing Unknown Property: $(prop.name) | $(prop.get_nick
())");
- if (element.node_name == "tag") {
- tags.append_val (((Element) element).content);
- }
- });
- }
+ public Package ()
+ {
+ computer = new Computer ();
+ manual = new Manual ();
+ source = "Mexico";
+ destiny = "World";
+ ((Serializable) this).serialize_unknown_property.connect ( (element, prop, out node) => {
+ //GLib.message (@"Serializing Unknown Property: $(prop.name) | $(prop.get_nick ())");
+ if (prop.name == "tags")
+ {
+ for (int i = 0; i < tags.length; i++) {
+ var str = tags.index (i);
+ node = element.owner_document.create_element ("tag");
+ ((Element) node).content = str;
+ element.append_child (node);
+ }
+ }
+ });
+ ((Serializable) this).deserialize_unknown_property.connect ( (element, prop) => {
+ //GLib.message (@"Deserializing Unknown Property: $(prop.name) | $(prop.get_nick ())");
+ if (element.node_name == "tag") {
+ tags.append_val (((Element) element).content);
+ }
+ });
+ }
- public string unknown_to_string ()
- {
- string t = "";
- foreach (GXml.Node node in unknown_serializable_property.get_values ())
- {
- t+= node.to_string () ;
- }
- return @"Unknown Properties: {$t}";
- }
+ public string unknown_to_string ()
+ {
+ string t = "";
+ foreach (GXml.Node node in unknown_serializable_property.get_values ())
+ {
+ t+= node.to_string () ;
+ }
+ return @"Unknown Properties: {$t}";
+ }
}
public class Monitor : ObjectModel
{
- public string resolution { get; set; }
- [Description (nick="AcPower")]
- public int ac_power { get; set; }
- [Description (nick="DcPower")]
- public int dc_power { get; set; }
+ public string resolution { get; set; }
+ [Description (nick="AcPower")]
+ public int ac_power { get; set; }
+ [Description (nick="DcPower")]
+ public int dc_power { get; set; }
- public override bool property_use_nick () { return true; }
+ public override bool property_use_nick () { return true; }
}
public class Cpu : ObjectModel
{
- public float ghz { get; set; default = (float) 0.0; }
- public Gee.ArrayList<int> piles { get; set; }
+ public float ghz { get; set; default = (float) 0.0; }
+ public Gee.ArrayList<int> piles { get; set; }
- public Cpu ()
- {
- piles = new Gee.ArrayList<int> ();
- }
+ public Cpu ()
+ {
+ piles = new Gee.ArrayList<int> ();
+ }
- public override bool transform_to_string (GLib.Value val, ref string str)
- {
- if (val.type ().is_a (typeof (float))) {
- str = "%.2f".printf (val.get_float ());
- return true;
- }
- if (val.type ().is_a (typeof (Gee.ArrayList))) {
- str = piles_to_string ();
- return true;
- }
- return false;
- }
- public override bool transform_from_string (string str, ref GLib.Value val)
- {
- //stdout.printf (@"Transforming from string type $(val.type ().name ())\n");
- if (val.type ().is_a (typeof (Gee.ArrayList))) {
- //stdout.printf ("Is ArraySize: from string\n");
- var a = new Gee.ArrayList<int> ();
- foreach (string s in str.split (",")) {
- a.add (int.parse (s));
- }
- val.set_object (a);
- return true;
- }
- return false;
- }
- public string piles_to_string ()
- {
- string str = "";
- int i = 0;
- while (i < piles.size) {
- str += @"$(piles.get (i))";
- if ( i + 1 < piles.size)
- str += ",";
- i++;
- }
- return str;
- }
+ public override bool transform_to_string (GLib.Value val, ref string str)
+ {
+ if (val.type ().is_a (typeof (float))) {
+ str = "%.2f".printf (val.get_float ());
+ return true;
+ }
+ if (val.type ().is_a (typeof (Gee.ArrayList))) {
+ str = piles_to_string ();
+ return true;
+ }
+ return false;
+ }
+ public override bool transform_from_string (string str, ref GLib.Value val)
+ {
+ //stdout.printf (@"Transforming from string type $(val.type ().name ())\n");
+ if (val.type ().is_a (typeof (Gee.ArrayList))) {
+ //stdout.printf ("Is ArraySize: from string\n");
+ var a = new Gee.ArrayList<int> ();
+ foreach (string s in str.split (",")) {
+ a.add (int.parse (s));
+ }
+ val.set_object (a);
+ return true;
+ }
+ return false;
+ }
+ public string piles_to_string ()
+ {
+ string str = "";
+ int i = 0;
+ while (i < piles.size) {
+ str += @"$(piles.get (i))";
+ if ( i + 1 < piles.size)
+ str += ",";
+ i++;
+ }
+ return str;
+ }
}
class NodeName : ObjectModel
{
- public bool invalid { get; set; default = true; }
- public override string node_name () { return "NodeName"; }
+ public bool invalid { get; set; default = true; }
+ public override string node_name () { return "NodeName"; }
}
class Configuration : ObjectModel
{
- public bool invalid { get; set; default = true; }
- public string device { get; set; }
- public override string node_name () { return "Configuration"; }
- public override bool property_use_nick () { return true; }
-
- public Configuration ()
- {
- init_properties (); // initializing properties to be ignored by default
- ignored_serializable_properties.set ("invalid",
- get_class ().find_property("invalid"));
- }
- public override GXml.Node? serialize (GXml.Node node) throws GLib.Error
- {
- var n = default_serialize (node);
- n.add_namespace_attr ("http://www.gnome.org/gxml/0.4", "om");
- return n;
- }
- public override GXml.Node? deserialize (GXml.Node node) throws GLib.Error
- {
- //stdout.printf (@"CONFIGURATOR: Namespaces Check");
- GXml.Node n;
- if (node is Document)
- n = (GXml.Node) (((GXml.Document) node).document_element);
- else
- n = node;
-
- foreach (GXml.Node ns in n.namespace_definitions) {
- //stdout.printf (@"Namespace = $(ns.node_value)");
- if (ns.node_name == "om" && ns.node_value == "http://www.gnome.org/gxml/0.4")
- invalid = false;
- }
- return default_deserialize (node);
- }
+ public bool invalid { get; set; default = true; }
+ public string device { get; set; }
+ public override string node_name () { return "Configuration"; }
+ public override bool property_use_nick () { return true; }
+
+ public Configuration ()
+ {
+ init_properties (); // initializing properties to be ignored by default
+ ignored_serializable_properties.set ("invalid",
+ get_class ().find_property("invalid"));
+ }
+ public override GXml.Node? serialize (GXml.Node node) throws GLib.Error
+ {
+ var n = default_serialize (node);
+ n.add_namespace_attr ("http://www.gnome.org/gxml/0.4", "om");
+ return n;
+ }
+ public override GXml.Node? deserialize (GXml.Node node) throws GLib.Error
+ {
+ //stdout.printf (@"CONFIGURATOR: Namespaces Check");
+ GXml.Node n;
+ if (node is Document)
+ n = (GXml.Node) (((GXml.Document) node).document_element);
+ else
+ n = node;
+
+ foreach (GXml.Node ns in n.namespace_definitions) {
+ //stdout.printf (@"Namespace = $(ns.node_value)");
+ if (ns.node_name == "om" && ns.node_value == "http://www.gnome.org/gxml/0.4")
+ invalid = false;
+ }
+ return default_deserialize (node);
+ }
}
class FakeSerializable : ObjectModel
{
- public string none { get; set; }
+ public string none { get; set; }
}
class UnknownAttribute : ObjectModel
{
- public string name { get; set; }
- public Gee.ArrayList<int> array { get; set; }
- public FakeSerializable fake { get; set; }
+ public string name { get; set; }
+ public Gee.ArrayList<int> array { get; set; }
+ public FakeSerializable fake { get; set; }
}
public enum OptionsEnum
{
- [Description (nick="SelectionOption")]
- SelectBefore,
- HoldOn,
- LeaveHeare,
- NORMAL_OPERATION
+ [Description (nick="SelectionOption")]
+ SelectBefore,
+ HoldOn,
+ LeaveHeare,
+ NORMAL_OPERATION
}
class Options : ObjectModel
{
- public string test { get; set; }
- public OptionsEnum options { get; set; }
+ public string test { get; set; }
+ public OptionsEnum options { get; set; }
}
class SerializableObjectModelTest : GXmlTest
{
- public static void add_tests ()
- {
- Test.add_func ("/gxml/serializable/object_model/serialize/simple_object",
- () => {
- try {
- var computer = new Computer ();
- var doc = new Document ();
- computer.serialize (doc);
- if (doc.document_element.tag_name.down () != "computer") {
- stdout.printf ("ERROR XML_COMPUTER: computer\n");
- assert_not_reached ();
- }
- var m = doc.document_element.get_attribute_node ("manufacturer");
- if (m == null) assert_not_reached ();
- if (m.node_value != "MexicanLaptop, Inc.") {
- stdout.printf ("ERROR XML_COMPUTER: manufacturer\n");
- assert_not_reached ();
- }
- var mod = doc.document_element.get_attribute_node ("model");
- if (mod == null) assert_not_reached ();
- if (mod.node_value != "LQ59678") {
- stdout.printf ("ERROR XML_COMPUTER: model\n");
- assert_not_reached ();
- }
- var c = doc.document_element.get_attribute_node ("cores");
- if (c == null) assert_not_reached ();
- if (c.node_value != "8") {
- stdout.printf ("ERROR XML_COMPUTER: cores val\n");
- assert_not_reached ();
- }
- var g = doc.document_element.get_attribute_node ("ghz");
- if (g == null) assert_not_reached ();
- if (double.parse (g.node_value) != (double) 3.5) {
- stdout.printf ("ERROR XML_COMPUTER: ghz val\n");
- assert_not_reached ();
- }
- }
- catch (GLib.Error e)
- {
- GLib.message (e.message);
- assert_not_reached ();
- }
- }
- );
- Test.add_func ("/gxml/serializable/object_model/deserialize_simple_object",
- () => {
- var computer = new Computer ();
- try {
- var doc = new Document.from_string (XML_COMPUTER_FILE);
- computer.deserialize (doc);
- if (computer.manufacturer != "ThecnicalGroup") {
- stdout.printf (@"ERROR XML_COMPUTER: manufacturer val:
$(computer.manufacturer)\n");
- assert_not_reached ();
- }
- if (computer.model !="YH576G") {
- stdout.printf (@"ERROR XML_COMPUTER: model val: $(computer.model)\n");
- assert_not_reached ();
- }
- if (computer.cores != 4) {
- stdout.printf (@"ERROR XML_COMPUTER: cores val: $(computer.cores)\n");
- assert_not_reached ();
- }
- if (computer.ghz != (float) 2.8) {
- stdout.printf (@"ERROR XML_COMPUTER: ghz val: $(computer.ghz)\n");
- assert_not_reached ();
- }
- } catch (GLib.Error e)
- {
- GLib.message (@"GHz : $(computer.to_string ()) ERROR: $(e.message)");
- assert_not_reached ();
- }
- }
- );
- Test.add_func ("/gxml/serializable/object_model/deserialize_object_contents",
- () => {
- var manual = new Manual ();
- try {
- var doc = new Document.from_string ("""<?xml version="1.0"?>
+ public static void add_tests ()
+ {
+ Test.add_func ("/gxml/serializable/object_model/serialize/simple_object",
+ () => {
+ try {
+ var computer = new Computer ();
+ var doc = new Document ();
+ computer.serialize (doc);
+ if (doc.document_element.tag_name.down () != "computer") {
+ stdout.printf ("ERROR XML_COMPUTER: computer\n");
+ assert_not_reached ();
+ }
+ var m = doc.document_element.get_attribute_node ("manufacturer");
+ if (m == null) assert_not_reached ();
+ if (m.node_value != "MexicanLaptop, Inc.") {
+ stdout.printf ("ERROR XML_COMPUTER: manufacturer\n");
+ assert_not_reached ();
+ }
+ var mod = doc.document_element.get_attribute_node ("model");
+ if (mod == null) assert_not_reached ();
+ if (mod.node_value != "LQ59678") {
+ stdout.printf ("ERROR XML_COMPUTER: model\n");
+ assert_not_reached ();
+ }
+ var c = doc.document_element.get_attribute_node ("cores");
+ if (c == null) assert_not_reached ();
+ if (c.node_value != "8") {
+ stdout.printf ("ERROR XML_COMPUTER: cores val\n");
+ assert_not_reached ();
+ }
+ var g = doc.document_element.get_attribute_node ("ghz");
+ if (g == null) assert_not_reached ();
+ if (double.parse (g.node_value) != (double) 3.5) {
+ stdout.printf ("ERROR XML_COMPUTER: ghz val\n");
+ assert_not_reached ();
+ }
+ }
+ catch (GLib.Error e)
+ {
+ GLib.message (e.message);
+ assert_not_reached ();
+ }
+ }
+ );
+ Test.add_func ("/gxml/serializable/object_model/deserialize_simple_object",
+ () => {
+ var computer = new Computer ();
+ try {
+ var doc = new Document.from_string (XML_COMPUTER_FILE);
+ computer.deserialize (doc);
+ if (computer.manufacturer != "ThecnicalGroup") {
+ stdout.printf (@"ERROR XML_COMPUTER: manufacturer val: $(computer.manufacturer)\n");
+ assert_not_reached ();
+ }
+ if (computer.model !="YH576G") {
+ stdout.printf (@"ERROR XML_COMPUTER: model val: $(computer.model)\n");
+ assert_not_reached ();
+ }
+ if (computer.cores != 4) {
+ stdout.printf (@"ERROR XML_COMPUTER: cores val: $(computer.cores)\n");
+ assert_not_reached ();
+ }
+ if (computer.ghz != (float) 2.8) {
+ stdout.printf (@"ERROR XML_COMPUTER: ghz val: $(computer.ghz)\n");
+ assert_not_reached ();
+ }
+ } catch (GLib.Error e)
+ {
+ GLib.message (@"GHz : $(computer.to_string ()) ERROR: $(e.message)");
+ assert_not_reached ();
+ }
+ }
+ );
+ Test.add_func ("/gxml/serializable/object_model/deserialize_object_contents",
+ () => {
+ var manual = new Manual ();
+ try {
+ var doc = new Document.from_string ("""<?xml version="1.0"?>
<manual document="Specification" pages="3">This is an Specification file</manual>""");
- manual.deserialize (doc);
- if (manual.document != "Specification") {
- stdout.printf (@"ERROR MANUAL: document: $(manual.document)\n");
- assert_not_reached ();
- }
- if (manual.pages != 3) {
- stdout.printf (@"ERROR MANUAL: pages: $(manual.pages)\n");
- assert_not_reached ();
- }
- if (manual.contents != "This is an Specification file") {
- stdout.printf (@"ERROR MANUAL: value: $(manual.contents)\n");
- assert_not_reached ();
- }
- }
- catch (GLib.Error e) {
- GLib.message (@"Error: $(e.message)");
- assert_not_reached ();
- }
- }
- );
- Test.add_func ("/gxml/serializable/object_model/serialize_object_contents",
- () => {
- var doc = new Document ();
- var manual = new Manual ();
- try {
- manual.serialize (doc);
- if (doc.document_element.node_name != "manual") {
- stdout.printf (@"ERROR MANUAL: Element:
$(doc.document_element.node_name)\n");
- assert_not_reached ();
- }
- Element element = doc.document_element;
- serialize_manual_check (element, manual);
- } catch (GLib.Error e) {
- stdout.printf (@"$(e.message)");
- assert_not_reached ();
- }
- }
- );
- Test.add_func ("/gxml/serializable/object_model/deserialize_serializable_properties",
- () => {
- var package = new Package ();
- try {
- var doc = new Document.from_string (XML_PACKAGE_FILE);
- package.deserialize (doc);
- if (package.source != "Mexico/Central") {
- stdout.printf (@"ERROR PACKAGE: source: $(package.source)\n");
- assert_not_reached ();
- }
- if (package.destiny != "Japan") {
- stdout.printf (@"ERROR PACKAGE: destiny: $(package.destiny)\n");
- assert_not_reached ();
- }
- /*if (package.unknown_to_string () != "Unknown Properties: {\n}") {
- stdout.printf (@"ERROR PACKAGE: package unknown properties:
$(package.unknown_to_string ())\n");
- assert_not_reached ();
- }*/
- if (package.manual.document != "Specification") {
- stdout.printf (@"ERROR PACKAGE: manual document:
$(package.manual.document)\n");
- assert_not_reached ();
- }
- if (package.manual.pages != 3) {
- stdout.printf (@"ERROR PACKAGE: manual pages:
$(package.manual.pages)\n");
- assert_not_reached ();
- }
- if (package.manual.contents != "This is an Specification file") {
- stdout.printf (@"ERROR PACKAGE: manual value:
$(package.manual.contents)\n");
- assert_not_reached ();
- }
- if (package.computer.manufacturer != "LanCorop") {
- stdout.printf (@"ERROR PACKAGE: computer manufacturer:
$(package.computer.manufacturer)\n");
- assert_not_reached ();
- }
- if (package.computer.model != "Lap39120") {
- stdout.printf (@"ERROR PACKAGE: computer model:
$(package.computer.model)\n");
- assert_not_reached ();
- }
- if (package.computer.cores != 16) {
- stdout.printf (@"ERROR PACKAGE: computer cores:
$(package.computer.cores)\n");
- assert_not_reached ();
- }
- if (package.computer.ghz != (float) 3.5) {
- stdout.printf (@"ERROR PACKAGE: computer ghz
$(package.computer.ghz)\n");
- assert_not_reached ();
- }
- }
- catch (GLib.Error e) {
- GLib.message (@"Error: $(e.message)");
- assert_not_reached ();
- }
- }
- );
- Test.add_func ("/gxml/serializable/object_model/serialize_serializable_properties",
- () => {
- var doc = new Document ();
- var package = new Package ();
- try {
- package.serialize (doc);
- if (doc.document_element.node_name != "package") {
- stdout.printf (@"ERROR MANUAL: Element:
$(doc.document_element.node_name)\n");
- assert_not_reached ();
- }
- Element element = doc.document_element;
- var source = element.get_attribute_node ("source");
- if (source == null ) assert_not_reached ();
- if (source.node_value != "Mexico") {
- stdout.printf (@"ERROR PACKAGE: source: $(source.node_value)\n");
- assert_not_reached ();
- }
- var destiny = element.get_attribute_node ("destiny");
- if (destiny == null ) assert_not_reached ();
- if (destiny.node_value != "World") {
- stdout.printf (@"ERROR PACKAGE: source: $(destiny.node_value)\n");
- assert_not_reached ();
- }
- }
- catch (GLib.Error e) {
- GLib.message (@"Error: $(e.message)");
- assert_not_reached ();
- }
- }
- );
- Test.add_func ("/gxml/serializable/object_model/deserialize_array_property",
- () => {
- var doc = new Document.from_string (XML_PACKAGE_FILE);
- var package = new Package ();
- try {
- package.deserialize (doc);
- if (package.tags.length != 3) {
- stdout.printf (@"ERROR PACKAGE: tags length: $(package.tags.length)");
- assert_not_reached ();
- }
- if (package.tags.index (0) != "Printer") {
- stdout.printf (@"ERROR PACKAGE: tags index 0: $(package.tags.index
(0))");
- assert_not_reached ();
- }
- if (package.tags.index (1) != "Partner") {
- stdout.printf (@"ERROR PACKAGE: tags index 1: $(package.tags.index
(1))");
- assert_not_reached ();
- }
- if (package.tags.index (2) != "Support") {
- stdout.printf (@"ERROR PACKAGE: tags index 0: $(package.tags.index
(2))");
- assert_not_reached ();
- }
- }
- catch (GLib.Error e) {
- GLib.message (@"Error: $(e.message)");
- assert_not_reached ();
- }
- }
- );
- Test.add_func ("/gxml/serializable/object_model/serialize_array_property",
- () => {
- var doc = new Document ();
- var package = new Package ();
- package.tags.append_val ("Computer");
- package.tags.append_val ("Customer");
- package.tags.append_val ("Sale");
- try {
- package.serialize (doc);
- //stdout.printf (@"$(doc)");
- if (doc.document_element.node_name != "package")
- assert_not_reached ();
- Element element = doc.document_element;
- bool com = false;
- bool cus = false;
- bool sal = false;
- foreach (GXml.Node n in element.child_nodes) {
- //stdout.printf (@"Found Element: $(n.node_name)");
- if (n.node_name == "tag") {
- //stdout.printf (@"Found: $(n.node_name)");
- if (((Element) n).content == "Computer")
- com = true;
- if (((Element) n).content == "Customer")
- cus = true;
- if (((Element) n).content == "Sale")
- sal = true;
- }
- }
- if (!com) {
- stdout.printf (@"ERROR PACKAGE tag Computer not found!");
- assert_not_reached ();
- }
- if (!cus) {
- stdout.printf (@"ERROR PACKAGE tag Customer not found!");
- assert_not_reached ();
- }
- if (!sal) {
- stdout.printf (@"ERROR PACKAGE tag Sale not found!");
- assert_not_reached ();
- }
- }
- catch (GLib.Error e) {
- GLib.message (@"Error: $(e.message)");
- assert_not_reached ();
- }
- }
- );
- Test.add_func ("/gxml/serializable/object_model/serialize_property_nick",
- () => {
- var doc = new Document ();
- var monitor = new Monitor ();
- try {
- monitor.resolution = "1204x720";
- monitor.ac_power = 120;
- monitor.dc_power = 125;
- monitor.serialize (doc);
- //stdout.printf (@"DOC: [$(doc)]");
- if (doc.document_element == null) {
- stdout.printf ("ERROR MONITOR: No root Element");
- assert_not_reached ();
- }
- Element element = doc.document_element;
- if (element.node_name != "monitor") {
- stdout.printf (@"ERROR MONITOR: root Element $(element.node_name)");
- assert_not_reached ();
- }
- var ac = element.get_attribute_node ("AcPower");
- if (ac == null) {
- stdout.printf (@"ERROR MONITOR: attribute AcPower not found");
- assert_not_reached ();
- }
- if (ac.node_value != "120") {
- stdout.printf (@"ERROR MONITOR: AcPower value $(ac.node_value)");
- assert_not_reached ();
- }
- var dc = element.get_attribute_node ("DcPower");
- if (dc == null) {
- stdout.printf (@"ERROR MONITOR: attribute DcPower not found");
- assert_not_reached ();
- }
- if (dc.node_value != "125") {
- stdout.printf (@"ERROR MONITOR: AcPower value $(dc.node_value)");
- assert_not_reached ();
- }
- var r = element.get_attribute_node ("resolution");
- if (r == null) {
- stdout.printf (@"ERROR MONITOR: attribute resolution not found");
- assert_not_reached ();
- }
- if (r.node_value != "1204x720") {
- stdout.printf (@"ERROR MONITOR: resolution value $(r.node_value)");
- assert_not_reached ();
- }
- }
- catch (GLib.Error e) {
- stdout.printf (@"Error: $(e.message)");
- assert_not_reached ();
- }
- }
- );
- Test.add_func ("/gxml/serializable/object_model/override_transform_to_string",
- () => {
- var cpu = new Cpu ();
- cpu.ghz = (float) 3.85;
- cpu.piles.add (1);
- cpu.piles.add (2);
- cpu.piles.add (3);
- var doc = new Document ();
- try {
- cpu.serialize (doc);
- //stdout.printf (@"$doc");
- if (doc.document_element == null) {
- stdout.printf (@"ERROR CPU: no root element");
- assert_not_reached ();
- }
- if (doc.document_element.node_name != "cpu") {
- stdout.printf (@"ERROR CPU: root element
$(doc.document_element.node_name)");
- assert_not_reached ();
- }
- var ghz = doc.document_element.get_attribute_node ("ghz");
- if (ghz == null) {
- stdout.printf (@"ERROR CPU: no attribute ghz");
- assert_not_reached ();
- }
- if (ghz.node_value != "3.85") {
- stdout.printf (@"ERROR CPU: ghz '$(ghz.node_value)'");
- assert_not_reached ();
- }
- var p = doc.document_element.get_attribute_node ("piles");
- if (p == null) {
- stdout.printf (@"ERROR CPU: no attribute piles");
- assert_not_reached ();
- }
- if (p.node_value != "1,2,3") {
- stdout.printf (@"ERROR CPU: piles '$(p.node_value)'");
- assert_not_reached ();
- }
- }
- catch (GLib.Error e) {
- stdout.printf (@"Error: $(e.message)");
- assert_not_reached ();
- }
- });
- Test.add_func ("/gxml/serializable/object_model/override_transform_from_string",
- () => {
- var cpu = new Cpu ();
- var doc = new Document.from_string (XML_CPU_FILE);
- try {
- cpu.deserialize (doc);
- //stdout.printf (@"$doc");
- if (cpu.ghz != (float) 3.85) {
- stdout.printf (@"ERROR CPU: ghz '$(cpu.ghz)'");
- assert_not_reached ();
- }
- if (cpu.piles.size != 3) {
- stdout.printf (@"ERROR CPU: piles size '$(cpu.piles.size)'");
- assert_not_reached ();
- }
- if (!cpu.piles.contains (1)) {
- stdout.printf (@"ERROR CPU: piles contains 1 '$(cpu.piles_to_string
())'");
- assert_not_reached ();
- }
- if (!cpu.piles.contains (2)) {
- stdout.printf (@"ERROR CPU: piles contains 2 '$(cpu.piles_to_string
())'");
- assert_not_reached ();
- }
- if (!cpu.piles.contains (3)) {
- stdout.printf (@"ERROR CPU: piles contains 3 '$(cpu.piles_to_string
())'");
- assert_not_reached ();
- }
- }
- catch (GLib.Error e) {
- stdout.printf (@"Error: $(e.message)");
- assert_not_reached ();
- }
- });
- Test.add_func ("/gxml/serializable/object_model/override_serialize",
- () => {
- var doc = new Document ();
- var configuration = new Configuration ();
- configuration.device = "Controller";
- try {
- configuration.serialize (doc);
- //stdout.printf (@"DOC: $doc");
- if (doc.document_element == null) {
- stdout.printf ("DOC: No root element");
- assert_not_reached ();
- }
- Element element = doc.document_element;
- if (element.node_name != "Configuration") {
- stdout.printf (@"CONFIGURATION: Bad node name: $(element.node_name)");
- assert_not_reached ();
- }
- bool found = false;
- foreach (GXml.Node n in element.namespace_definitions)
- {
- if (n.node_name == "om" && n.node_value ==
"http://www.gnome.org/gxml/0.4")
- found = true;
- }
- if (!found) {
- stdout.printf (@"CONFIGURATION: No namespace found:");
- foreach (GXml.Node n in element.namespace_definitions) {
- stdout.printf (@"CONFIGURATION: Defined Namespace:
$(n.node_name):$(n.node_value)");
- }
- assert_not_reached ();
- }
- }
- catch (GLib.Error e) {
- stdout.printf (@"Error: $(e.message)");
- assert_not_reached ();
- }
- });
- Test.add_func ("/gxml/serializable/object_model/override_deserialize",
- () => {
- var doc = new Document.from_string ("""<?xml version="1.0"?>
+ manual.deserialize (doc);
+ if (manual.document != "Specification") {
+ stdout.printf (@"ERROR MANUAL: document: $(manual.document)\n");
+ assert_not_reached ();
+ }
+ if (manual.pages != 3) {
+ stdout.printf (@"ERROR MANUAL: pages: $(manual.pages)\n");
+ assert_not_reached ();
+ }
+ if (manual.contents != "This is an Specification file") {
+ stdout.printf (@"ERROR MANUAL: value: $(manual.contents)\n");
+ assert_not_reached ();
+ }
+ }
+ catch (GLib.Error e) {
+ GLib.message (@"Error: $(e.message)");
+ assert_not_reached ();
+ }
+ }
+ );
+ Test.add_func ("/gxml/serializable/object_model/serialize_object_contents",
+ () => {
+ var doc = new Document ();
+ var manual = new Manual ();
+ try {
+ manual.serialize (doc);
+ if (doc.document_element.node_name != "manual") {
+ stdout.printf (@"ERROR MANUAL: Element: $(doc.document_element.node_name)\n");
+ assert_not_reached ();
+ }
+ Element element = doc.document_element;
+ serialize_manual_check (element, manual);
+ } catch (GLib.Error e) {
+ stdout.printf (@"$(e.message)");
+ assert_not_reached ();
+ }
+ }
+ );
+ Test.add_func ("/gxml/serializable/object_model/deserialize_serializable_properties",
+ () => {
+ var package = new Package ();
+ try {
+ var doc = new Document.from_string (XML_PACKAGE_FILE);
+ package.deserialize (doc);
+ if (package.source != "Mexico/Central") {
+ stdout.printf (@"ERROR PACKAGE: source: $(package.source)\n");
+ assert_not_reached ();
+ }
+ if (package.destiny != "Japan") {
+ stdout.printf (@"ERROR PACKAGE: destiny: $(package.destiny)\n");
+ assert_not_reached ();
+ }
+ /*if (package.unknown_to_string () != "Unknown Properties: {\n}") {
+ stdout.printf (@"ERROR PACKAGE: package unknown properties: $(package.unknown_to_string ())\n");
+ assert_not_reached ();
+ }*/
+ if (package.manual.document != "Specification") {
+ stdout.printf (@"ERROR PACKAGE: manual document: $(package.manual.document)\n");
+ assert_not_reached ();
+ }
+ if (package.manual.pages != 3) {
+ stdout.printf (@"ERROR PACKAGE: manual pages: $(package.manual.pages)\n");
+ assert_not_reached ();
+ }
+ if (package.manual.contents != "This is an Specification file") {
+ stdout.printf (@"ERROR PACKAGE: manual value: $(package.manual.contents)\n");
+ assert_not_reached ();
+ }
+ if (package.computer.manufacturer != "LanCorop") {
+ stdout.printf (@"ERROR PACKAGE: computer manufacturer: $(package.computer.manufacturer)\n");
+ assert_not_reached ();
+ }
+ if (package.computer.model != "Lap39120") {
+ stdout.printf (@"ERROR PACKAGE: computer model: $(package.computer.model)\n");
+ assert_not_reached ();
+ }
+ if (package.computer.cores != 16) {
+ stdout.printf (@"ERROR PACKAGE: computer cores: $(package.computer.cores)\n");
+ assert_not_reached ();
+ }
+ if (package.computer.ghz != (float) 3.5) {
+ stdout.printf (@"ERROR PACKAGE: computer ghz $(package.computer.ghz)\n");
+ assert_not_reached ();
+ }
+ }
+ catch (GLib.Error e) {
+ GLib.message (@"Error: $(e.message)");
+ assert_not_reached ();
+ }
+ }
+ );
+ Test.add_func ("/gxml/serializable/object_model/serialize_serializable_properties",
+ () => {
+ var doc = new Document ();
+ var package = new Package ();
+ try {
+ package.serialize (doc);
+ if (doc.document_element.node_name != "package") {
+ stdout.printf (@"ERROR MANUAL: Element: $(doc.document_element.node_name)\n");
+ assert_not_reached ();
+ }
+ Element element = doc.document_element;
+ var source = element.get_attribute_node ("source");
+ if (source == null ) assert_not_reached ();
+ if (source.node_value != "Mexico") {
+ stdout.printf (@"ERROR PACKAGE: source: $(source.node_value)\n");
+ assert_not_reached ();
+ }
+ var destiny = element.get_attribute_node ("destiny");
+ if (destiny == null ) assert_not_reached ();
+ if (destiny.node_value != "World") {
+ stdout.printf (@"ERROR PACKAGE: source: $(destiny.node_value)\n");
+ assert_not_reached ();
+ }
+ }
+ catch (GLib.Error e) {
+ GLib.message (@"Error: $(e.message)");
+ assert_not_reached ();
+ }
+ }
+ );
+ Test.add_func ("/gxml/serializable/object_model/deserialize_array_property",
+ () => {
+ var doc = new Document.from_string (XML_PACKAGE_FILE);
+ var package = new Package ();
+ try {
+ package.deserialize (doc);
+ if (package.tags.length != 3) {
+ stdout.printf (@"ERROR PACKAGE: tags length: $(package.tags.length)");
+ assert_not_reached ();
+ }
+ if (package.tags.index (0) != "Printer") {
+ stdout.printf (@"ERROR PACKAGE: tags index 0: $(package.tags.index (0))");
+ assert_not_reached ();
+ }
+ if (package.tags.index (1) != "Partner") {
+ stdout.printf (@"ERROR PACKAGE: tags index 1: $(package.tags.index (1))");
+ assert_not_reached ();
+ }
+ if (package.tags.index (2) != "Support") {
+ stdout.printf (@"ERROR PACKAGE: tags index 0: $(package.tags.index (2))");
+ assert_not_reached ();
+ }
+ }
+ catch (GLib.Error e) {
+ GLib.message (@"Error: $(e.message)");
+ assert_not_reached ();
+ }
+ }
+ );
+ Test.add_func ("/gxml/serializable/object_model/serialize_array_property",
+ () => {
+ var doc = new Document ();
+ var package = new Package ();
+ package.tags.append_val ("Computer");
+ package.tags.append_val ("Customer");
+ package.tags.append_val ("Sale");
+ try {
+ package.serialize (doc);
+ //stdout.printf (@"$(doc)");
+ if (doc.document_element.node_name != "package")
+ assert_not_reached ();
+ Element element = doc.document_element;
+ bool com = false;
+ bool cus = false;
+ bool sal = false;
+ foreach (GXml.Node n in element.child_nodes) {
+ //stdout.printf (@"Found Element: $(n.node_name)");
+ if (n.node_name == "tag") {
+ //stdout.printf (@"Found: $(n.node_name)");
+ if (((Element) n).content == "Computer")
+ com = true;
+ if (((Element) n).content == "Customer")
+ cus = true;
+ if (((Element) n).content == "Sale")
+ sal = true;
+ }
+ }
+ if (!com) {
+ stdout.printf (@"ERROR PACKAGE tag Computer not found!");
+ assert_not_reached ();
+ }
+ if (!cus) {
+ stdout.printf (@"ERROR PACKAGE tag Customer not found!");
+ assert_not_reached ();
+ }
+ if (!sal) {
+ stdout.printf (@"ERROR PACKAGE tag Sale not found!");
+ assert_not_reached ();
+ }
+ }
+ catch (GLib.Error e) {
+ GLib.message (@"Error: $(e.message)");
+ assert_not_reached ();
+ }
+ }
+ );
+ Test.add_func ("/gxml/serializable/object_model/serialize_property_nick",
+ () => {
+ var doc = new Document ();
+ var monitor = new Monitor ();
+ try {
+ monitor.resolution = "1204x720";
+ monitor.ac_power = 120;
+ monitor.dc_power = 125;
+ monitor.serialize (doc);
+ //stdout.printf (@"DOC: [$(doc)]");
+ if (doc.document_element == null) {
+ stdout.printf ("ERROR MONITOR: No root Element");
+ assert_not_reached ();
+ }
+ Element element = doc.document_element;
+ if (element.node_name != "monitor") {
+ stdout.printf (@"ERROR MONITOR: root Element $(element.node_name)");
+ assert_not_reached ();
+ }
+ var ac = element.get_attribute_node ("AcPower");
+ if (ac == null) {
+ stdout.printf (@"ERROR MONITOR: attribute AcPower not found");
+ assert_not_reached ();
+ }
+ if (ac.node_value != "120") {
+ stdout.printf (@"ERROR MONITOR: AcPower value $(ac.node_value)");
+ assert_not_reached ();
+ }
+ var dc = element.get_attribute_node ("DcPower");
+ if (dc == null) {
+ stdout.printf (@"ERROR MONITOR: attribute DcPower not found");
+ assert_not_reached ();
+ }
+ if (dc.node_value != "125") {
+ stdout.printf (@"ERROR MONITOR: AcPower value $(dc.node_value)");
+ assert_not_reached ();
+ }
+ var r = element.get_attribute_node ("resolution");
+ if (r == null) {
+ stdout.printf (@"ERROR MONITOR: attribute resolution not found");
+ assert_not_reached ();
+ }
+ if (r.node_value != "1204x720") {
+ stdout.printf (@"ERROR MONITOR: resolution value $(r.node_value)");
+ assert_not_reached ();
+ }
+ }
+ catch (GLib.Error e) {
+ stdout.printf (@"Error: $(e.message)");
+ assert_not_reached ();
+ }
+ }
+ );
+ Test.add_func ("/gxml/serializable/object_model/override_transform_to_string",
+ () => {
+ var cpu = new Cpu ();
+ cpu.ghz = (float) 3.85;
+ cpu.piles.add (1);
+ cpu.piles.add (2);
+ cpu.piles.add (3);
+ var doc = new Document ();
+ try {
+ cpu.serialize (doc);
+ //stdout.printf (@"$doc");
+ if (doc.document_element == null) {
+ stdout.printf (@"ERROR CPU: no root element");
+ assert_not_reached ();
+ }
+ if (doc.document_element.node_name != "cpu") {
+ stdout.printf (@"ERROR CPU: root element $(doc.document_element.node_name)");
+ assert_not_reached ();
+ }
+ var ghz = doc.document_element.get_attribute_node ("ghz");
+ if (ghz == null) {
+ stdout.printf (@"ERROR CPU: no attribute ghz");
+ assert_not_reached ();
+ }
+ if (ghz.node_value != "3.85") {
+ stdout.printf (@"ERROR CPU: ghz '$(ghz.node_value)'");
+ assert_not_reached ();
+ }
+ var p = doc.document_element.get_attribute_node ("piles");
+ if (p == null) {
+ stdout.printf (@"ERROR CPU: no attribute piles");
+ assert_not_reached ();
+ }
+ if (p.node_value != "1,2,3") {
+ stdout.printf (@"ERROR CPU: piles '$(p.node_value)'");
+ assert_not_reached ();
+ }
+ }
+ catch (GLib.Error e) {
+ stdout.printf (@"Error: $(e.message)");
+ assert_not_reached ();
+ }
+ });
+ Test.add_func ("/gxml/serializable/object_model/override_transform_from_string",
+ () => {
+ var cpu = new Cpu ();
+ var doc = new Document.from_string (XML_CPU_FILE);
+ try {
+ cpu.deserialize (doc);
+ //stdout.printf (@"$doc");
+ if (cpu.ghz != (float) 3.85) {
+ stdout.printf (@"ERROR CPU: ghz '$(cpu.ghz)'");
+ assert_not_reached ();
+ }
+ if (cpu.piles.size != 3) {
+ stdout.printf (@"ERROR CPU: piles size '$(cpu.piles.size)'");
+ assert_not_reached ();
+ }
+ if (!cpu.piles.contains (1)) {
+ stdout.printf (@"ERROR CPU: piles contains 1 '$(cpu.piles_to_string ())'");
+ assert_not_reached ();
+ }
+ if (!cpu.piles.contains (2)) {
+ stdout.printf (@"ERROR CPU: piles contains 2 '$(cpu.piles_to_string ())'");
+ assert_not_reached ();
+ }
+ if (!cpu.piles.contains (3)) {
+ stdout.printf (@"ERROR CPU: piles contains 3 '$(cpu.piles_to_string ())'");
+ assert_not_reached ();
+ }
+ }
+ catch (GLib.Error e) {
+ stdout.printf (@"Error: $(e.message)");
+ assert_not_reached ();
+ }
+ });
+ Test.add_func ("/gxml/serializable/object_model/override_serialize",
+ () => {
+ var doc = new Document ();
+ var configuration = new Configuration ();
+ configuration.device = "Controller";
+ try {
+ configuration.serialize (doc);
+ //stdout.printf (@"DOC: $doc");
+ if (doc.document_element == null) {
+ stdout.printf ("DOC: No root element");
+ assert_not_reached ();
+ }
+ Element element = doc.document_element;
+ if (element.node_name != "Configuration") {
+ stdout.printf (@"CONFIGURATION: Bad node name: $(element.node_name)");
+ assert_not_reached ();
+ }
+ bool found = false;
+ foreach (GXml.Node n in element.namespace_definitions)
+ {
+ if (n.node_name == "om" && n.node_value == "http://www.gnome.org/gxml/0.4")
+ found = true;
+ }
+ if (!found) {
+ stdout.printf (@"CONFIGURATION: No namespace found:");
+ foreach (GXml.Node n in element.namespace_definitions) {
+ stdout.printf (@"CONFIGURATION: Defined Namespace: $(n.node_name):$(n.node_value)");
+ }
+ assert_not_reached ();
+ }
+ }
+ catch (GLib.Error e) {
+ stdout.printf (@"Error: $(e.message)");
+ assert_not_reached ();
+ }
+ });
+ Test.add_func ("/gxml/serializable/object_model/override_deserialize",
+ () => {
+ var doc = new Document.from_string ("""<?xml version="1.0"?>
<Configuration xmlns:om="http://www.gnome.org/gxml/0.4" device="Sampler"/>""");
- var configuration = new Configuration ();
- try {
- //stdout.printf (@"$doc");
- configuration.deserialize (doc);
- if (configuration.invalid == true) {
- stdout.printf ("CONFIGURATION: deserialize is INVALID\n");
- foreach (GXml.Node n in doc.document_element.namespace_definitions) {
- stdout.printf (@"CONFIGURATION: namespace:
$(n.node_value)\n");
- }
- assert_not_reached ();
- }
- }
- catch (GLib.Error e) {
- stdout.printf (@"Error: $(e.message)");
- assert_not_reached ();
- }
- });
- Test.add_func ("/gxml/serializable/object_model/custome_node_name",
- () => {
- var doc = new Document.from_string ("""<?xml version="1.0"?><NodeName />""");
- var nodename = new NodeName ();
- try {
- nodename.deserialize (doc);
- }
- catch (GLib.Error e) {
- stdout.printf (@"Error: $(e.message)");
- assert_not_reached ();
- }
- });
- Test.add_func ("/gxml/serializable/object_model/no_serialize_null_property",
- () => {
- var doc = new Document();
- var unknown_property = new UnknownAttribute (); // name is set to null
- try {
- unknown_property.serialize (doc);
- //stdout.printf (@"DOCUMENT: $doc"); assert_not_reached ();
- var name = doc.document_element.get_attribute_node ("name");
- if (name != null) {
- stdout.printf (@"ERROR: NULL ATTRIBUTE SERIALIZATION: name found
$(name.node_name)");
- assert_not_reached ();
- }
- var array = doc.document_element.get_attribute_node ("array");
- if (array != null) {
- stdout.printf (@"ERROR: NULL ATTRIBUTE SERIALIZATION: array found
$(array.node_name)");
- assert_not_reached ();
- }
- if (doc.document_element.has_child_nodes ()) {
- stdout.printf (@"ERROR: NULL ATTRIBUTE SERIALIZATION: Nodes found
$(doc.document_element.has_child_nodes ())");
- assert_not_reached ();
- }
- }
- catch (GLib.Error e) {
- stdout.printf (@"Error: $(e.message)");
- assert_not_reached ();
- }
- });
- Test.add_func ("/gxml/serializable/object_model/unknown_property",
- () => {
- var doc = new Document.from_string ("""<?xml version="1.0"?>
- <UnknownAttribute ignore="true" ignore2="test">
- <UnknownNode toignore = "true" />
- </UnknownAttribute>""");
- var unknown_property = new UnknownAttribute ();
- try {
- unknown_property.deserialize (doc);
- if (unknown_property.unknown_serializable_property.size () != 3) {
- stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: size
$(unknown_property.unknown_serializable_property.size ().to_string ())\n");
- foreach (string s in
unknown_property.unknown_serializable_property.get_keys ()) {
- stdout.printf (@"Saved unknown property: $(s)\n");
- }
- assert_not_reached ();
- }
- if (!unknown_property.unknown_serializable_property.contains ("ignore")) {
- stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: ignore not found");
- assert_not_reached ();
- }
- var ignore = unknown_property.unknown_serializable_property.get ("ignore");
- if (!(ignore is Attr)) {
- stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: ignore is not an
GXml.Attr");
- assert_not_reached ();
- }
- if (!unknown_property.unknown_serializable_property.contains ("ignore2")) {
- stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: ignore not found");
- assert_not_reached ();
- }
- var ignore2 = unknown_property.unknown_serializable_property.get ("ignore2");
- if (!(ignore2 is Attr)) {
- stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: ignore2 is not an
GXml.Attr");
- assert_not_reached ();
- }
- if (!unknown_property.unknown_serializable_property.contains ("UnknownNode"))
{
- stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: node UnknownNode not
found");
- assert_not_reached ();
- }var unknown_node = unknown_property.unknown_serializable_property.get
("UnknownNode");
- if (!(unknown_node is Element)) {
- stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: unknown node is not an
GXml.Element");
- assert_not_reached ();
- }
- }
- catch (GLib.Error e) {
- stdout.printf (@"Error: $(e.message)");
- assert_not_reached ();
- }
- });
- Test.add_func ("/gxml/serializable/object_model/deserialize_unknown_property",
- () => {
- var doc = new Document.from_string ("""<?xml version="1.0"?>
- <UnknownAttribute ignore="true" ignore2="test">
- <UnknownNode direction = "fordward">
- SECOND FAKE TEXT
- </UnknownNode>
- FAKE TEXT
- </UnknownAttribute>""");
- var unknown_property = new UnknownAttribute ();
- try {
- unknown_property.deserialize (doc);
- var doc2 = new Document ();
- unknown_property.serialize (doc2);
- if (doc2.document_element == null) {
- stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: SERIALIZATION: No Root
Element");
- assert_not_reached ();
- }
- Element element = doc2.document_element;
- if (element.node_name.down () != "unknownattribute") {
- stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: SERIALIZATION: Root
Element Bad name $(element.node_name.down ())");
- assert_not_reached ();
- }
- var ignore = element.get_attribute_node ("ignore");
- if (ignore == null) {
- stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: SERIALIZATION: No
attribute ignore");
- assert_not_reached ();
- }
- if (ignore.node_value != "true") {
- stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: SERIALIZATION: Attribute
ignore bad value $(ignore.node_value)");
- assert_not_reached ();
- }
- var ignore2 = element.get_attribute_node ("ignore2");
- if (ignore2 == null) {
- stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: SERIALIZATION: No
attribute ignore");
- assert_not_reached ();
- }
- if (ignore2.node_value != "test") {
- stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: SERIALIZATION: Attribute
ignore2 bad value $(ignore2.node_value)");
- assert_not_reached ();
- }
- if (!element.has_child_nodes ()) {
- stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: SERIALIZATION: No child
nodes");
- assert_not_reached ();
- }
- // Consider that Element content text (actually none) is considered a
GXml.Node
- if (element.child_nodes.length != 2) {
- stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: SERIALIZATION: Too
many child nodes $(element.child_nodes.length)");
- assert_not_reached ();
- }
- bool found = false;
- foreach (GXml.Node n in element.child_nodes) {
- if (n.node_name == "UnknownNode") {
- found = true;
- var direction = ((Element) n).get_attribute_node
("direction");
- if (direction == null) {
- stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE:
SERIALIZATION: UnknownNode No attribute direction");
- assert_not_reached ();
- }
- if (direction.node_value != "fordward") {
- stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE:
SERIALIZATION: UnknownNode attribute direction bad value $(direction.node_value)");
- assert_not_reached ();
- }
- }
- }
- if (!found) {
- stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: SERIALIZATION: UnknownNode
No not found");
- assert_not_reached ();
- }
- // TODO: serialized_xml_node_value have more text than expected, may be a bug
in Document.to_string ()
- if (!unknown_property.serialized_xml_node_value.contains ("FAKE TEXT")) {
- stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: SERIALIZATION: Bad
UnknownAttribute node's content text $(unknown_property.serialized_xml_node_value)");
- assert_not_reached ();
- }
- }
- catch (GLib.Error e) {
- stdout.printf (@"Error: $(e.message)");
- assert_not_reached ();
- }
- });
- Test.add_func ("/gxml/serializable/object_model/enumeration",
- () => {
- var e = new Options ();
- try {
- e.test = "t1";
- e.options = OptionsEnum.SelectBefore;
- string s = Enumeration.get_string (typeof (OptionsEnum), e.options);
- if (s != "OPTIONS_ENUM_SelectBefore") {
- stdout.printf (@"ERROR: Bad Enum stringification: $(s)");
- assert_not_reached ();
- }
- s = Enumeration.get_nick (typeof (OptionsEnum), e.options);
- if (s != "selectbefore") {
- stdout.printf (@"ERROR: Bad Enum nick name: $(s)");
- assert_not_reached ();
- }
- s = Enumeration.get_nick (typeof (OptionsEnum),OptionsEnum.NORMAL_OPERATION);
- if (s != "normal-operation") {
- stdout.printf (@"ERROR: Bad Enum nick name: $(s)");
- assert_not_reached ();
- }
- s = Enumeration.get_nick_camelcase (typeof
(OptionsEnum),OptionsEnum.NORMAL_OPERATION);
- if (s != "NormalOperation") {
- stdout.printf (@"ERROR: Bad Enum nick name: $(s)");
- assert_not_reached ();
- }
- try {
- Enumeration.parse (typeof (OptionsEnum), "selectbefore");
- }
- catch (GLib.Error e) {
- stdout.printf (@"ERROR PARSING selectbefore: $(e.message)");
- assert_not_reached ();
- }
- try {
- Enumeration.parse (typeof (OptionsEnum), "normal-operation");
- }
- catch (GLib.Error e) {
- stdout.printf (@"ERROR PARSING normal-operation: $(e.message)");
- assert_not_reached ();
- }
- try {
- Enumeration.parse (typeof (OptionsEnum), "NormalOperation");
- }
- catch (GLib.Error e) {
- stdout.printf (@"ERROR PARSING NormalOperation: $(e.message)");
- assert_not_reached ();
- }
- var env = Enumeration.parse (typeof (OptionsEnum), "NormalOperation");
- Value v = Value (typeof (int));
- v.set_int (env.value);
- e.options = (OptionsEnum) v.get_int ();
- if (e.options != OptionsEnum.NORMAL_OPERATION) {
- stdout.printf (@"ERROR: setting NormalOperation: $(e.options)");
- assert_not_reached ();
- }
- }
- catch (GLib.Error e) {
- stdout.printf (@"Error: $(e.message)");
- assert_not_reached ();
- }
- });
- Test.add_func ("/gxml/serializable/object_model/enumeration-serialize",
- () => {
- var doc = new Document ();
- var options = new Options ();
- options.options = OptionsEnum.NORMAL_OPERATION;
- try {
- options.serialize (doc);
- if (doc.document_element == null) {
- stdout.printf (@"ERROR: No root node found");
- assert_not_reached ();
- }
- if (doc.document_element.node_name != "options") {
- stdout.printf (@"ERROR: bad root name:\n$(doc)");
- assert_not_reached ();
- }
- Element element = doc.document_element;
- var op = element.get_attribute_node ("options");
- if (op == null) {
- stdout.printf (@"ERROR: attribute options not found:\n$(doc)");
- assert_not_reached ();
- }
- if (op.node_value != "NormalOperation") {
- stdout.printf (@"ERROR: attribute options value invalid:
$(op.node_value)\n$(doc)");
- assert_not_reached ();
- }
- options.options = (OptionsEnum) (-1); // invaliding this property. Avoids
serialize it.
- var doc2 = new Document ();
- options.serialize (doc2);
- var opts = doc2.document_element.get_attribute_node ("options");
- if (opts != null) {
- stdout.printf (@"ERROR: attribute options must not be
present:\n$(doc)");
- assert_not_reached ();
- }
- }
- catch (GLib.Error e) {
- stdout.printf (@"Error: $(e.message)");
- assert_not_reached ();
- }
- });
- Test.add_func ("/gxml/serializable/object_model/enumeration-deserialize",
- () => {
- var options = new Options ();
- try {
- var doc = new Document.from_string ("""<?xml version="1.0"?>
+ var configuration = new Configuration ();
+ try {
+ //stdout.printf (@"$doc");
+ configuration.deserialize (doc);
+ if (configuration.invalid == true) {
+ stdout.printf ("CONFIGURATION: deserialize is INVALID\n");
+ foreach (GXml.Node n in doc.document_element.namespace_definitions) {
+ stdout.printf (@"CONFIGURATION: namespace: $(n.node_value)\n");
+ }
+ assert_not_reached ();
+ }
+ }
+ catch (GLib.Error e) {
+ stdout.printf (@"Error: $(e.message)");
+ assert_not_reached ();
+ }
+ });
+ Test.add_func ("/gxml/serializable/object_model/custome_node_name",
+ () => {
+ var doc = new Document.from_string ("""<?xml version="1.0"?><NodeName />""");
+ var nodename = new NodeName ();
+ try {
+ nodename.deserialize (doc);
+ }
+ catch (GLib.Error e) {
+ stdout.printf (@"Error: $(e.message)");
+ assert_not_reached ();
+ }
+ });
+ Test.add_func ("/gxml/serializable/object_model/no_serialize_null_property",
+ () => {
+ var doc = new Document();
+ var unknown_property = new UnknownAttribute (); // name is set to null
+ try {
+ unknown_property.serialize (doc);
+ //stdout.printf (@"DOCUMENT: $doc"); assert_not_reached ();
+ var name = doc.document_element.get_attribute_node ("name");
+ if (name != null) {
+ stdout.printf (@"ERROR: NULL ATTRIBUTE SERIALIZATION: name found $(name.node_name)");
+ assert_not_reached ();
+ }
+ var array = doc.document_element.get_attribute_node ("array");
+ if (array != null) {
+ stdout.printf (@"ERROR: NULL ATTRIBUTE SERIALIZATION: array found $(array.node_name)");
+ assert_not_reached ();
+ }
+ if (doc.document_element.has_child_nodes ()) {
+ stdout.printf (@"ERROR: NULL ATTRIBUTE SERIALIZATION: Nodes found
$(doc.document_element.has_child_nodes ())");
+ assert_not_reached ();
+ }
+ }
+ catch (GLib.Error e) {
+ stdout.printf (@"Error: $(e.message)");
+ assert_not_reached ();
+ }
+ });
+ Test.add_func ("/gxml/serializable/object_model/unknown_property",
+ () => {
+ var doc = new Document.from_string ("""<?xml version="1.0"?>
+ <UnknownAttribute ignore="true" ignore2="test">
+ <UnknownNode toignore = "true" />
+ </UnknownAttribute>""");
+ var unknown_property = new UnknownAttribute ();
+ try {
+ unknown_property.deserialize (doc);
+ if (unknown_property.unknown_serializable_property.size () != 3) {
+ stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: size
$(unknown_property.unknown_serializable_property.size ().to_string ())\n");
+ foreach (string s in unknown_property.unknown_serializable_property.get_keys ()) {
+ stdout.printf (@"Saved unknown property: $(s)\n");
+ }
+ assert_not_reached ();
+ }
+ if (!unknown_property.unknown_serializable_property.contains ("ignore")) {
+ stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: ignore not found");
+ assert_not_reached ();
+ }
+ var ignore = unknown_property.unknown_serializable_property.get ("ignore");
+ if (!(ignore is Attr)) {
+ stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: ignore is not an GXml.Attr");
+ assert_not_reached ();
+ }
+ if (!unknown_property.unknown_serializable_property.contains ("ignore2")) {
+ stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: ignore not found");
+ assert_not_reached ();
+ }
+ var ignore2 = unknown_property.unknown_serializable_property.get ("ignore2");
+ if (!(ignore2 is Attr)) {
+ stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: ignore2 is not an GXml.Attr");
+ assert_not_reached ();
+ }
+ if (!unknown_property.unknown_serializable_property.contains ("UnknownNode")) {
+ stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: node UnknownNode not found");
+ assert_not_reached ();
+ }var unknown_node = unknown_property.unknown_serializable_property.get ("UnknownNode");
+ if (!(unknown_node is Element)) {
+ stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: unknown node is not an GXml.Element");
+ assert_not_reached ();
+ }
+ }
+ catch (GLib.Error e) {
+ stdout.printf (@"Error: $(e.message)");
+ assert_not_reached ();
+ }
+ });
+ Test.add_func ("/gxml/serializable/object_model/deserialize_unknown_property",
+ () => {
+ var doc = new Document.from_string ("""<?xml version="1.0"?>
+ <UnknownAttribute ignore="true" ignore2="test">
+ <UnknownNode direction = "fordward">
+ SECOND FAKE TEXT
+ </UnknownNode>
+ FAKE TEXT
+ </UnknownAttribute>""");
+ var unknown_property = new UnknownAttribute ();
+ try {
+ unknown_property.deserialize (doc);
+ var doc2 = new Document ();
+ unknown_property.serialize (doc2);
+ if (doc2.document_element == null) {
+ stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: SERIALIZATION: No Root Element");
+ assert_not_reached ();
+ }
+ Element element = doc2.document_element;
+ if (element.node_name.down () != "unknownattribute") {
+ stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: SERIALIZATION: Root Element Bad name
$(element.node_name.down ())");
+ assert_not_reached ();
+ }
+ var ignore = element.get_attribute_node ("ignore");
+ if (ignore == null) {
+ stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: SERIALIZATION: No attribute ignore");
+ assert_not_reached ();
+ }
+ if (ignore.node_value != "true") {
+ stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: SERIALIZATION: Attribute ignore bad value
$(ignore.node_value)");
+ assert_not_reached ();
+ }
+ var ignore2 = element.get_attribute_node ("ignore2");
+ if (ignore2 == null) {
+ stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: SERIALIZATION: No attribute ignore");
+ assert_not_reached ();
+ }
+ if (ignore2.node_value != "test") {
+ stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: SERIALIZATION: Attribute ignore2 bad value
$(ignore2.node_value)");
+ assert_not_reached ();
+ }
+ if (!element.has_child_nodes ()) {
+ stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: SERIALIZATION: No child nodes");
+ assert_not_reached ();
+ }
+ // Consider that Element content text (actually none) is considered a GXml.Node
+ if (element.child_nodes.length != 2) {
+ stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: SERIALIZATION: Too many child nodes
$(element.child_nodes.length)");
+ assert_not_reached ();
+ }
+ bool found = false;
+ foreach (GXml.Node n in element.child_nodes) {
+ if (n.node_name == "UnknownNode") {
+ found = true;
+ var direction = ((Element) n).get_attribute_node ("direction");
+ if (direction == null) {
+ stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: SERIALIZATION: UnknownNode No attribute direction");
+ assert_not_reached ();
+ }
+ if (direction.node_value != "fordward") {
+ stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: SERIALIZATION: UnknownNode attribute direction bad
value $(direction.node_value)");
+ assert_not_reached ();
+ }
+ }
+ }
+ if (!found) {
+ stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: SERIALIZATION: UnknownNode No not found");
+ assert_not_reached ();
+ }
+ // TODO: serialized_xml_node_value have more text than expected, may be a bug in Document.to_string
()
+ if (!unknown_property.serialized_xml_node_value.contains ("FAKE TEXT")) {
+ stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: SERIALIZATION: Bad UnknownAttribute node's content text
$(unknown_property.serialized_xml_node_value)");
+ assert_not_reached ();
+ }
+ }
+ catch (GLib.Error e) {
+ stdout.printf (@"Error: $(e.message)");
+ assert_not_reached ();
+ }
+ });
+ Test.add_func ("/gxml/serializable/object_model/enumeration",
+ () => {
+ var e = new Options ();
+ try {
+ e.test = "t1";
+ e.options = OptionsEnum.SelectBefore;
+ string s = Enumeration.get_string (typeof (OptionsEnum), e.options);
+ if (s != "OPTIONS_ENUM_SelectBefore") {
+ stdout.printf (@"ERROR: Bad Enum stringification: $(s)");
+ assert_not_reached ();
+ }
+ s = Enumeration.get_nick (typeof (OptionsEnum), e.options);
+ if (s != "selectbefore") {
+ stdout.printf (@"ERROR: Bad Enum nick name: $(s)");
+ assert_not_reached ();
+ }
+ s = Enumeration.get_nick (typeof (OptionsEnum),OptionsEnum.NORMAL_OPERATION);
+ if (s != "normal-operation") {
+ stdout.printf (@"ERROR: Bad Enum nick name: $(s)");
+ assert_not_reached ();
+ }
+ s = Enumeration.get_nick_camelcase (typeof (OptionsEnum),OptionsEnum.NORMAL_OPERATION);
+ if (s != "NormalOperation") {
+ stdout.printf (@"ERROR: Bad Enum nick name: $(s)");
+ assert_not_reached ();
+ }
+ try {
+ Enumeration.parse (typeof (OptionsEnum), "selectbefore");
+ }
+ catch (GLib.Error e) {
+ stdout.printf (@"ERROR PARSING selectbefore: $(e.message)");
+ assert_not_reached ();
+ }
+ try {
+ Enumeration.parse (typeof (OptionsEnum), "normal-operation");
+ }
+ catch (GLib.Error e) {
+ stdout.printf (@"ERROR PARSING normal-operation: $(e.message)");
+ assert_not_reached ();
+ }
+ try {
+ Enumeration.parse (typeof (OptionsEnum), "NormalOperation");
+ }
+ catch (GLib.Error e) {
+ stdout.printf (@"ERROR PARSING NormalOperation: $(e.message)");
+ assert_not_reached ();
+ }
+ var env = Enumeration.parse (typeof (OptionsEnum), "NormalOperation");
+ Value v = Value (typeof (int));
+ v.set_int (env.value);
+ e.options = (OptionsEnum) v.get_int ();
+ if (e.options != OptionsEnum.NORMAL_OPERATION) {
+ stdout.printf (@"ERROR: setting NormalOperation: $(e.options)");
+ assert_not_reached ();
+ }
+ }
+ catch (GLib.Error e) {
+ stdout.printf (@"Error: $(e.message)");
+ assert_not_reached ();
+ }
+ });
+ Test.add_func ("/gxml/serializable/object_model/enumeration-serialize",
+ () => {
+ var doc = new Document ();
+ var options = new Options ();
+ options.options = OptionsEnum.NORMAL_OPERATION;
+ try {
+ options.serialize (doc);
+ if (doc.document_element == null) {
+ stdout.printf (@"ERROR: No root node found");
+ assert_not_reached ();
+ }
+ if (doc.document_element.node_name != "options") {
+ stdout.printf (@"ERROR: bad root name:\n$(doc)");
+ assert_not_reached ();
+ }
+ Element element = doc.document_element;
+ var op = element.get_attribute_node ("options");
+ if (op == null) {
+ stdout.printf (@"ERROR: attribute options not found:\n$(doc)");
+ assert_not_reached ();
+ }
+ if (op.node_value != "NormalOperation") {
+ stdout.printf (@"ERROR: attribute options value invalid: $(op.node_value)\n$(doc)");
+ assert_not_reached ();
+ }
+ options.options = (OptionsEnum) (-1); // invaliding this property. Avoids serialize it.
+ var doc2 = new Document ();
+ options.serialize (doc2);
+ var opts = doc2.document_element.get_attribute_node ("options");
+ if (opts != null) {
+ stdout.printf (@"ERROR: attribute options must not be present:\n$(doc)");
+ assert_not_reached ();
+ }
+ }
+ catch (GLib.Error e) {
+ stdout.printf (@"Error: $(e.message)");
+ assert_not_reached ();
+ }
+ });
+ Test.add_func ("/gxml/serializable/object_model/enumeration-deserialize",
+ () => {
+ var options = new Options ();
+ try {
+ var doc = new Document.from_string ("""<?xml version="1.0"?>
<options options="NormalOperation"/>""");
- options.deserialize (doc);
- if (options.options != OptionsEnum.NORMAL_OPERATION) {
- stdout.printf (@"ERROR: Bad value to options property:
$(options.options)\n$(doc)");
- assert_not_reached ();
- }
- var doc2 = new Document.from_string ("""<?xml version="1.0"?>
+ options.deserialize (doc);
+ if (options.options != OptionsEnum.NORMAL_OPERATION) {
+ stdout.printf (@"ERROR: Bad value to options property: $(options.options)\n$(doc)");
+ assert_not_reached ();
+ }
+ var doc2 = new Document.from_string ("""<?xml version="1.0"?>
<options options="normal-operation"/>""");
- options.deserialize (doc2);
- if (options.options != OptionsEnum.NORMAL_OPERATION) {
- stdout.printf (@"ERROR: Bad value to options property:
$(options.options)\n$(doc2)");
- assert_not_reached ();
- }
- var doc3 = new Document.from_string ("""<?xml version="1.0"?>
+ options.deserialize (doc2);
+ if (options.options != OptionsEnum.NORMAL_OPERATION) {
+ stdout.printf (@"ERROR: Bad value to options property: $(options.options)\n$(doc2)");
+ assert_not_reached ();
+ }
+ var doc3 = new Document.from_string ("""<?xml version="1.0"?>
<options options="selectbefore"/>""");
- options.deserialize (doc3);
- if (options.options != OptionsEnum.SelectBefore) {
- stdout.printf (@"ERROR: Bad value to options property:
$(options.options)\n$(doc3)");
- assert_not_reached ();
- }
- var doc4 = new Document.from_string ("""<?xml version="1.0"?>
+ options.deserialize (doc3);
+ if (options.options != OptionsEnum.SelectBefore) {
+ stdout.printf (@"ERROR: Bad value to options property: $(options.options)\n$(doc3)");
+ assert_not_reached ();
+ }
+ var doc4 = new Document.from_string ("""<?xml version="1.0"?>
<options options="OPTIONS_ENUM_SelectBefore"/>""");
- options.deserialize (doc4);
- if (options.options != OptionsEnum.SelectBefore) {
- stdout.printf (@"ERROR: Bad value to options property:
$(options.options)\n$(doc4)");
- assert_not_reached ();
- }
- var doc5 = new Document.from_string ("""<?xml version="1.0"?>
+ options.deserialize (doc4);
+ if (options.options != OptionsEnum.SelectBefore) {
+ stdout.printf (@"ERROR: Bad value to options property: $(options.options)\n$(doc4)");
+ assert_not_reached ();
+ }
+ var doc5 = new Document.from_string ("""<?xml version="1.0"?>
<options options="SelectBefore"/>""");
- options.deserialize (doc5);
- if (options.options != OptionsEnum.SelectBefore) {
- stdout.printf (@"ERROR: Bad value to options property:
$(options.options)\n$(doc5)");
- assert_not_reached ();
- }
- var doc6 = new Document.from_string ("""<?xml version="1.0"?>
+ options.deserialize (doc5);
+ if (options.options != OptionsEnum.SelectBefore) {
+ stdout.printf (@"ERROR: Bad value to options property: $(options.options)\n$(doc5)");
+ assert_not_reached ();
+ }
+ var doc6 = new Document.from_string ("""<?xml version="1.0"?>
<options options="SELECTBEFORE"/>""");
- options.deserialize (doc6);
- if (options.options != OptionsEnum.SelectBefore) {
- stdout.printf (@"ERROR: Bad value to options property:
$(options.options)\n$(doc6)");
- assert_not_reached ();
- }
- var doc7 = new Document.from_string ("""<?xml version="1.0"?>
+ options.deserialize (doc6);
+ if (options.options != OptionsEnum.SelectBefore) {
+ stdout.printf (@"ERROR: Bad value to options property: $(options.options)\n$(doc6)");
+ assert_not_reached ();
+ }
+ var doc7 = new Document.from_string ("""<?xml version="1.0"?>
<options options="NORMAL_OPERATION"/>""");
- options.deserialize (doc7);
- if (options.options != OptionsEnum.SelectBefore) {
- stdout.printf (@"ERROR: Bad value to options property:
$(options.options)\n$(doc7)");
- assert_not_reached ();
- }
- var op2 = new Options ();
- var doc8 = new Document.from_string ("""<?xml version="1.0"?>
+ options.deserialize (doc7);
+ if (options.options != OptionsEnum.SelectBefore) {
+ stdout.printf (@"ERROR: Bad value to options property: $(options.options)\n$(doc7)");
+ assert_not_reached ();
+ }
+ var op2 = new Options ();
+ var doc8 = new Document.from_string ("""<?xml version="1.0"?>
<options options="INVALID"/>""");
- op2.deserialize (doc8);
- if (op2.options != OptionsEnum.SelectBefore) {
- stdout.printf (@"ERROR: Bad value to options property:
$(op2.options)\n$(doc8)");
- assert_not_reached ();
- }
- }
- catch (GLib.Error e) {
- stdout.printf (@"Error: $(e.message)");
- assert_not_reached ();
- }
- });
- }
- static void serialize_manual_check (Element element, Manual manual)
- {
- var document = element.get_attribute_node ("document");
- if (document == null) assert_not_reached ();
- if (document.node_value != manual.document) {
- stdout.printf (@"ERROR MANUAL: document: $(document.node_value)\n");
- assert_not_reached ();
- }
- var pages = element.get_attribute_node ("pages");
- if (pages == null) assert_not_reached ();
- if (int.parse (pages.node_value) != manual.pages) {
- stdout.printf (@"ERROR MANUAL: pages: $(pages.node_value)\n");
- assert_not_reached ();
- }
- if (element.content != manual.contents) {
- stdout.printf (@"ERROR MANUAL: content: $(element.content)\n");
- assert_not_reached ();
- }
- }
+ op2.deserialize (doc8);
+ if (op2.options != OptionsEnum.SelectBefore) {
+ stdout.printf (@"ERROR: Bad value to options property: $(op2.options)\n$(doc8)");
+ assert_not_reached ();
+ }
+ }
+ catch (GLib.Error e) {
+ stdout.printf (@"Error: $(e.message)");
+ assert_not_reached ();
+ }
+ });
+ }
+ static void serialize_manual_check (Element element, Manual manual)
+ {
+ var document = element.get_attribute_node ("document");
+ if (document == null) assert_not_reached ();
+ if (document.node_value != manual.document) {
+ stdout.printf (@"ERROR MANUAL: document: $(document.node_value)\n");
+ assert_not_reached ();
+ }
+ var pages = element.get_attribute_node ("pages");
+ if (pages == null) assert_not_reached ();
+ if (int.parse (pages.node_value) != manual.pages) {
+ stdout.printf (@"ERROR MANUAL: pages: $(pages.node_value)\n");
+ assert_not_reached ();
+ }
+ if (element.content != manual.contents) {
+ stdout.printf (@"ERROR MANUAL: content: $(element.content)\n");
+ assert_not_reached ();
+ }
+ }
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]