diff --git a/gxml/Makefile.am b/gxml/Makefile.am index a691a978ab7fc6d2d3ed4b41a9356ecd29d81644..25c2e2077f511447132f9860a182220c1477711d 100644 --- a/gxml/Makefile.am +++ b/gxml/Makefile.am @@ -69,6 +69,7 @@ libgxml_la_SOURCES = \ SerializableMapId.vala \ SerializableGeeDualKeyMap.vala \ SerializableMapDualId.vala \ + SerializableGeeArrayList.vala \ $(NULL) gxml.vala.stamp: $(libgxml_la_SOURCES) diff --git a/gxml/SerializableGeeArrayList.vala b/gxml/SerializableGeeArrayList.vala new file mode 100644 index 0000000000000000000000000000000000000000..36c547cae523ec2ed009b5a3e4ea6a62ae1e4f53 --- /dev/null +++ b/gxml/SerializableGeeArrayList.vala @@ -0,0 +1,137 @@ +/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* SerializableGeeTreeModel.vala + * + * Copyright (C) 2013 Daniel Espinosa + * + * 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 . + * + * Authors: + * Daniel Espinosa + */ +using GXml; +using Gee; + +public class GXml.SerializableArrayList : Gee.ArrayList, Serializable +{ + protected ParamSpec[] properties { get; set; } + public GLib.HashTable ignored_serializable_properties { get; protected set; } + public string? serialized_xml_node_value { get; protected set; default=null; } + public GLib.HashTable unknown_serializable_property { get; protected set; } + + public virtual bool property_use_nick () { return false; } + + public virtual string node_name () + { + return ""; + } + public string default_node_name () + { + return get_type().name().down(); + } + + 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 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 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_to_string (GLib.Value val, ref string str) + { + return false; + } + + public virtual GXml.Node? serialize (GXml.Node node) + throws GLib.Error + requires (node is Element) + { + return default_serialize (node); + } + public GXml.Node? default_serialize (GXml.Node node) + throws GLib.Error + requires (node is Element) + { + if (element_type.is_a (typeof (Serializable))) { + foreach (G e in this) { + ((GXml.Serializable) e).serialize (node); + } + } + return node; + } + public virtual GXml.Node? serialize_property (GXml.Element element, + GLib.ParamSpec prop) + throws GLib.Error + { + return default_serialize_property (element, prop); + } + public GXml.Node? default_serialize_property (GXml.Element element, + GLib.ParamSpec prop) + throws GLib.Error + { + return element; + } + public virtual GXml.Node? deserialize (GXml.Node node) + throws GLib.Error + requires (node_name () != null) + { + return default_deserialize (node); + } + public GXml.Node? default_deserialize (GXml.Node node) + throws GLib.Error + requires (element_type.is_a (typeof (GXml.Serializable))) + { + if (node is Element) { + foreach (GXml.Node n in node.child_nodes) { + var obj = (Serializable) Object.new (element_type); + obj.deserialize (n); + add (obj); + } + } + return node; + } + 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 + { + return true; + } +} + diff --git a/test/GXmlTest.vala b/test/GXmlTest.vala index 8811ca5ba8fb1f8582e4860d0cc0c98f7b3277a1..eac03f30f97832d60c46064e6f3bbbd0ec3ae6ec 100644 --- a/test/GXmlTest.vala +++ b/test/GXmlTest.vala @@ -40,6 +40,7 @@ class GXmlTest { SerializableObjectModelTest.add_tests (); SerializableGeeTreeMapTest.add_tests (); SerializableGeeDualKeyMapTest.add_tests (); + SerializableGeeArrayListTest.add_tests (); Test.run (); diff --git a/test/Makefile.am b/test/Makefile.am index 4296024088bbf12c43dbebe8c2ff6e272c9befe1..b98600ea33de709b633173d0c1a2b13302d6d2e6 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -40,6 +40,7 @@ gxml_test_SOURCES = \ SerializableObjectModelTest.vala \ SerializableGeeTreeMapTest.vala \ SerializableGeeDualKeyMapTest.vala \ + SerializableGeeArrayListTest.vala \ $(NULL) gxml_test.vala.stamp: $(gxml_test_SOURCES) diff --git a/test/SerializableGeeArrayListTest.vala b/test/SerializableGeeArrayListTest.vala new file mode 100644 index 0000000000000000000000000000000000000000..6c95504d9fbdb3bc7a0434b45ff8859b3f234d83 --- /dev/null +++ b/test/SerializableGeeArrayListTest.vala @@ -0,0 +1,115 @@ +using GXml; +using Gee; + +class AElement : SerializableObjectModel +{ + public string name { get; set; } + public AElement.named (string name) { this.name = name; } + public override string to_string () { return name; } +} + +class SerializableGeeArrayListTest : GXmlTest +{ + public static void add_tests () + { + Test.add_func ("/gxml/serializable/serializable_array_list/api", + () => { + try { + var c = new SerializableArrayList (); + var o1 = new AElement.named ("Big"); + var o2 = new AElement.named ("Small"); + c.add (o1); + c.add (o2); + bool found1 = false; + bool found2 = false; + foreach (AElement o in c) { + if (o.name == "Big") found1 = true; + if (o.name == "Small") found2 = true; + } + if (!found1) { + stdout.printf (@"Big is not found\n"); + assert_not_reached (); + } + if (!found2) { + stdout.printf (@"Small is not found\n"); + assert_not_reached (); + } + } + catch (GLib.Error e) { + stdout.printf (@"ERROR: $(e.message)"); + } + }); + Test.add_func ("/gxml/serializable/serializable_array_list/serialize", + () => { + try { + var c = new SerializableArrayList (); + var o1 = new AElement.named ("Big"); + var o2 = new AElement.named ("Small"); + c.add (o1); + c.add (o2); + var doc = new Document (); + var root = doc.create_element ("root"); + doc.append_child (root); + c.serialize (root); + if (!root.has_child_nodes ()) { + stdout.printf (@"ERROR: root node have no childs $(doc)\n"); + assert_not_reached (); + } + bool found1 = false; + bool found2 = false; + foreach (GXml.Node n in root.child_nodes) { + if (n is Element && n.node_name == "aelement") { + var name = ((Element) n).get_attribute_node ("name"); + if (name != null) { + if (name.node_value == "Big") found1 = true; + if (name.node_value == "Small") found2 = true; + } + } + } + if (!found1) { + stdout.printf (@"ERROR: Big space node is not found\n"); + assert_not_reached (); + } + if (!found2) { + stdout.printf (@"ERROR: Small space node is not found\n"); + assert_not_reached (); + } + } + catch (GLib.Error e) { + stdout.printf (@"ERROR: $(e.message)"); + assert_not_reached (); + } + }); + Test.add_func ("/gxml/serializable/serializable_array_list/deserialize", + () => { + try { + var doc = new Document.from_string (""" + """); + var c = new SerializableArrayList (); + c.deserialize (doc.document_element); + if (c.size != 2) { + stdout.printf (@"ERROR: incorrect size must be 2 got: $(c.size)\n"); + assert_not_reached (); + } + bool found1 = false; + bool found2 = false; + foreach (AElement o in c) { + if (o.name == "Big") found1 = true; + if (o.name == "Small") found2 = true; + } + if (!found1) { + stdout.printf (@"ERROR: Big key value is not found\n"); + assert_not_reached (); + } + if (!found2) { + stdout.printf (@"ERROR: Small key value is not found\n"); + assert_not_reached (); + } + } + catch (GLib.Error e) { + stdout.printf (@"ERROR: $(e.message)"); + assert_not_reached (); + } + }); + } +}