[gxml] GomProperty: New interface for GObject attributes
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gxml] GomProperty: New interface for GObject attributes
- Date: Mon, 7 Nov 2016 01:41:35 +0000 (UTC)
commit 4b9a5d1d99473a36ffce68abe510b4418bc00438
Author: Daniel Espinosa <esodan gmail com>
Date: Sun Nov 6 19:39:50 2016 -0600
GomProperty: New interface for GObject attributes
GomProperty binds Objects to DomElement's attributes.
Basic types implementations have been added.
gxml/GomProperty.vala | 132 ++++++++++++++++++++++++++++++++++++++++
gxml/Makefile.am | 1 +
test/GomSerializationTest.vala | 51 +++++++++++++++
3 files changed, 184 insertions(+), 0 deletions(-)
---
diff --git a/gxml/GomProperty.vala b/gxml/GomProperty.vala
new file mode 100644
index 0000000..724a898
--- /dev/null
+++ b/gxml/GomProperty.vala
@@ -0,0 +1,132 @@
+/* -*- Mode: vala; indent-tabs-mode: nil; c-basic-offset: 2; tab-width: 2 -*- */
+/*
+ *
+ * Copyright (C) 2016 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>
+ */
+
+
+/**
+ * An interface for {@link GomObject}'s properties translated to
+ * {@link DomElement} attributes.
+ */
+public interface GXml.GomProperty : Object
+{
+ /**
+ * Attribute's name in the parent {@link DomElement}.
+ */
+ public abstract string attribute_name { get; construct set; }
+ /**
+ * Attribute's value in the parent {@link DomElement}.
+ */
+ public abstract string value { owned get; set; }
+}
+
+public class GXml.GomDouble : Object, GomProperty {
+ protected double _value = 0.0;
+ protected string _attribute_name;
+ public string attribute_name {
+ get { return _attribute_name; }
+ construct set { _attribute_name = value;}
+ }
+ public string value {
+ owned get {
+ string s = "%."+decimals.to_string ()+"f";
+ return s.printf (_value);
+ }
+ set {
+ _value = double.parse (value);
+ }
+ }
+ public uint decimals { get; set; default = 4; }
+ public double get_double () { return _value; }
+}
+
+public class GXml.GomFloat : GomDouble {
+ public float get_double () { return (float) _value; }
+}
+
+
+public class GXml.GomInt : Object, GomProperty {
+ protected int _value = 0;
+ protected string _attribute_name;
+ public string attribute_name {
+ get { return _attribute_name; }
+ construct set { _attribute_name = value;}
+ }
+ public string value {
+ owned get {
+ return _value.to_string ();
+ }
+ set {
+ _value = (int) double.parse (value);
+ }
+ }
+ public int get_integer () { return _value; }
+}
+
+public class GXml.GomBoolean : Object, GomProperty {
+ protected bool _value = false;
+ protected string _attribute_name;
+ public string attribute_name {
+ get { return _attribute_name; }
+ construct set { _attribute_name = value;}
+ }
+ public string value {
+ owned get {
+ return _value.to_string ();
+ }
+ set {
+ _value = bool.parse (value);
+ }
+ }
+ public bool get_boolean () { return _value; }
+}
+
+public class GXml.GomEnum : Object, GomProperty {
+ protected int _value = 0;
+ protected string _attribute_name;
+ protected Type _enum_type;
+ public string attribute_name {
+ get { return _attribute_name; }
+ construct set { _attribute_name = value;}
+ }
+ public string value {
+ owned get {
+ string s = "";
+ try {
+ s = Enumeration.get_string (enum_type, _value, true, true);
+ } catch {
+ GLib.warning (_("Error when transform enum to attribute's value"));
+ }
+ return s;
+ }
+ set {
+ try {
+ _value = (int) Enumeration.parse (enum_type, value).value;
+ } catch {
+ GLib.warning (_("Error when transform from attribute string value to enum"));
+ }
+ }
+ }
+ public Type enum_type {
+ get { return _enum_type; }
+ construct set { _enum_type = value; }
+ }
+ public int get_enum () { return (int) _value; }
+}
diff --git a/gxml/Makefile.am b/gxml/Makefile.am
index 2cc4a37..fb901ff 100644
--- a/gxml/Makefile.am
+++ b/gxml/Makefile.am
@@ -89,6 +89,7 @@ sources = \
GomText.vala \
GomObject.vala \
GomCollections.vala \
+ GomProperty.vala \
Parser.vala \
XParser.vala \
$(NULL)
diff --git a/test/GomSerializationTest.vala b/test/GomSerializationTest.vala
index 085fb44..a1398ea 100644
--- a/test/GomSerializationTest.vala
+++ b/test/GomSerializationTest.vala
@@ -105,6 +105,50 @@ class GomSerializationTest : GXmlTest {
return parser.write_string ();
}
}
+ public class Motor : GomElement {
+ public On is_on { get; set; }
+ public Torque torque { get; set; }
+ public Speed speed { get; set; }
+ public Type tension_type { get; set; }
+ public Tension tension { get; set; }
+ construct {
+ _local_name = "Motor";
+ }
+ public string to_string () {
+ var parser = new XParser (this);
+ return parser.write_string ();
+ }
+ public enum Enum {
+ AC,
+ DC
+ }
+ public class On : GomBoolean {
+ construct {
+ _attribute_name = "On";
+ }
+ }
+ public class Torque : GomDouble {
+ construct {
+ _attribute_name = "Torque";
+ }
+ }
+ public class Speed : GomFloat {
+ construct {
+ _attribute_name = "Speed";
+ }
+ }
+ public class Type : GomEnum {
+ construct {
+ _enum_type = typeof (Enum);
+ _attribute_name = "Type";
+ }
+ }
+ public class Tension : GomInt {
+ construct {
+ _attribute_name = "Tension";
+ }
+ }
+ }
public static void add_tests () {
Test.add_func ("/gxml/gom-serialization/write/properties", () => {
var b = new Book ();
@@ -241,6 +285,13 @@ class GomSerializationTest : GXmlTest {
assert ((bs.books.get("Title2") as Book).name == "Title2");
assert ((bs.books.get("Title3") as Book).name == "Title3");
});
+ Test.add_func ("/gxml/gom-serialization/write/gom-property", () => {
+ var c = new Motor ();
+ string s = c.to_string ();
+ assert (s != null);
+ GLib.message ("DOC:"+s);
+ assert ("<Motor/>" in s);
+ });
Test.add_func ("/gxml/gom-serialization/read/properties", () => {
var b = new Book ();
var parser = new XParser (b);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]