[gxml] Added SerializableValueList class as a SerializableProperty implementation



commit 583d7fa460f4395d6375f622f28155e1d6ddc2e0
Author: Daniel Espinosa <esodan gmail com>
Date:   Sat Sep 26 18:22:22 2015 -0700

    Added SerializableValueList class as a SerializableProperty implementation

 gxml/Makefile.am                |    1 +
 gxml/SerializableProperty.vala  |  108 +++++++++++++++++++++++++++++++++++++++
 gxml/SerializableValueList.vala |   77 +++++++++++++++++++++++++++
 3 files changed, 186 insertions(+), 0 deletions(-)
---
diff --git a/gxml/Makefile.am b/gxml/Makefile.am
index 922b914..06e57e5 100644
--- a/gxml/Makefile.am
+++ b/gxml/Makefile.am
@@ -57,6 +57,7 @@ sources = \
        Serializable.vala \
        SerializableProperty.vala \
        SerializableBool.vala \
+       SerializableValueList.vala \
        Enumeration.vala \
        SerializableObjectModel.vala \
        SerializableJson.vala \
diff --git a/gxml/SerializableProperty.vala b/gxml/SerializableProperty.vala
new file mode 100644
index 0000000..8ae1130
--- /dev/null
+++ b/gxml/SerializableProperty.vala
@@ -0,0 +1,108 @@
+/* -*- Mode: vala; indent-tabs-mode: nil; c-basic-offset: 2; tab-width: 2 -*- */
+/* SerializableProperty.vala
+ *
+ * Copyright (C) 2015  Daniel Espinosa <esodan gmail com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *      Daniel Espinosa <esodan gmail com>
+ */
+
+using Gee;
+/**
+ * Represent any property to be added as a { link GXml.Attr} to a { link GXml.Element}
+ *
+ * On implementations of { link GXml.Serializable}, consider to detect if the object to
+ * serialize/deserialize is of kind { link GXml.SerializableProperty} and call
+ * { link GXml.Serializable.serialize_property} instead of { link GXml.Serializable.serialize}
+ * to fill automatically { link GXml.SerializableProperty.serializable_property_name} and
+ * { link GXml.SerializableProperty.serializable_property_value}.
+ */
+public interface GXml.SerializableProperty : Object, Serializable
+{
+  /**
+  * Value to be set to a { link GXml.Attr}, to be added to a { link GXml.Element}
+  */
+  public abstract string get_serializable_property_value ();
+  /**
+  * Set value to be set to a { link GXml.Attr}, to be added to a { link GXml.Element}
+  *
+  * If value is set to @null then the property will be ignored by default and no
+  * property will be set to given { link GXml.Element}
+  */
+  public abstract void set_serializable_property_value (string? val);
+  /**
+   * Name to be set to a { link GXml.Attr}, to be added to a { link GXml.Element}
+   */
+  public abstract string get_serializable_property_name ();
+  /**
+   * Sets name to be set to a { link GXml.Attr}, to be added to a { link GXml.Element}
+   */
+  public abstract void set_serializable_property_name (string name);
+  /**
+   * Default serialization method to add a { link GXml.Attr} to a { link GXml.Element}
+   *
+   * If { link GXml.SerializableProperty.get_serializable_property_value} returns { link null}
+   * given { link GXml.Node} is not modified.
+   *
+   * Implementators should call this method instead of { link Serializable.default_serialize}.
+   */
+  public virtual GXml.Node? default_serializable_property_serialize (GXml.Node node) throws GLib.Error
+    requires (get_serializable_property_name () != null)
+  {
+    if (get_serializable_property_value () == null) return node;
+    if (node is GXml.Attribute && node.name == get_serializable_property_name ()) {
+      ((GXml.Attribute) node).value = get_serializable_property_value ();
+      return node;
+    }
+    ((GXml.Element) node).set_attr (get_serializable_property_name (), get_serializable_property_value ());
+    return node;
+  }
+  /**
+   * Default serialization method to add a { link GXml.Attr} to a { link GXml.Element}
+   *
+   * If { link GXml.SerializableProperty.get_serializable_property_value} returns { link null}
+   * given { link GXml.Node} is not modified.
+   *
+   * Implementators should override { link Serializable.serialize_property} to call
+   * this method on serialization.
+   */
+  public virtual GXml.Node? default_serializable_property_serialize_property (GXml.Node element,
+                                        GLib.ParamSpec prop)
+                                        throws GLib.Error
+  {
+    if (get_serializable_property_value () == null) return element;
+    ((GXml.Element) element).set_attr (prop.name, get_serializable_property_value ());
+    return element;
+  }
+  /**
+   * Tryies to deserialize from a { link GXml.Node} searching a { link GXml.Attr}
+   * with the name returned by { link GXml.SerializableProperty.get_serializable_property_name}
+   */
+  public virtual GXml.Node? default_serializable_property_deserialize (GXml.Node node)
+                                      throws GLib.Error
+    requires (get_serializable_property_name () != null)
+  {
+    GXml.Attribute attr = null;
+    if (node is GXml.Attribute)
+      attr = (GXml.Attribute) node;
+    if (node is GXml.Element)
+      attr = (GXml.Attribute) ((GXml.Element) node).attrs.get (get_serializable_property_name ());
+    if (attr == null) return node;
+    if (attr.name == get_serializable_property_name ())
+      set_serializable_property_value (attr.value);
+    return node;
+  }
+}
\ No newline at end of file
diff --git a/gxml/SerializableValueList.vala b/gxml/SerializableValueList.vala
new file mode 100644
index 0000000..966ddf3
--- /dev/null
+++ b/gxml/SerializableValueList.vala
@@ -0,0 +1,77 @@
+/* -*- Mode: vala; indent-tabs-mode: nil; c-basic-offset: 2; tab-width: 2 -*- */
+/* SerializableValueList.vala
+ *
+ * Copyright (C) 2015  Daniel Espinosa <esodan gmail com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *      Daniel Espinosa <esodan gmail com>
+ */
+
+using Gee;
+/**
+ * Represent any value as string but a list of options by default to select from.
+ * property to be added as a { link GXml.Attr} to a { link GXml.Element}.
+ *
+ * All values are stored in an array to get access to it by its position using
+ * { link SerializableValueList.get_value}.
+ */
+using Gee;
+
+public class GXml.SerializableValueList : SerializableObjectModel, SerializableProperty
+{
+  private string _val = null;
+  private string _name = null;
+  private ArrayList<string> _values = null;
+  public SerializableValueList.with_name (string name) { _name = name; }
+  public void add_value (string val)
+  {
+    if (_values == null) _values = new ArrayList<string> ();
+    _values.add (val);
+  }
+  public string get_value (int index)
+    requires (index >= 0)
+  {
+    if (_values == null) return null;
+    return _values.get (index);
+  }
+  public string[] get_values () { return _values.to_array (); }
+  public string get_serializable_property_value () { return _val; }
+  public void set_serializable_property_value (string? val) { _val = val; }
+  public string get_serializable_property_name () { return _name; }
+  public void set_serializable_property_name (string name) { _name = name; }
+  public override GXml.Node? serialize (GXml.Node node) throws GLib.Error
+  {
+    return default_serializable_property_serialize (node);
+  }
+  public override GXml.Node? serialize_property (GXml.Node element,
+                                        GLib.ParamSpec prop)
+                                        throws GLib.Error
+  {
+    return default_serializable_property_serialize_property (element, prop);
+  }
+  public override GXml.Node? deserialize (GXml.Node node)
+                                      throws GLib.Error
+  {
+    return default_serializable_property_deserialize (node);
+  }
+  public override bool deserialize_property (GXml.Node property_node)
+                                              throws GLib.Error
+  {
+    default_serializable_property_deserialize (property_node);
+    return true;
+  }
+  public override string to_string () { return _val; }
+}
\ No newline at end of file


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