summaryrefslogtreecommitdiffstats
path: root/gxml/GomProperty.vala
blob: b999391f4cc593de8389f030fa4179c0cd3f3ad5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* -*- 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. If object is instantiated it is
 * written, if not is just ingnored.
 */
public interface GXml.GomProperty : Object
{
  /**
   * Attribute's value in the parent {@link DomElement} using a string.
   *
   * Implementation should take care to validate value before to set or
   * parse from XML document.
   */
  public abstract string? value { owned get; set; }
  /**
   * Takes a string and check if it is a valid value for property
   */
  public abstract bool validate_value (string val);
}

/**
 * Base class for properties implementing {@link GomProperty} interface.
 */
public abstract class GXml.GomBaseProperty : Object, GXml.GomProperty {
  /**
   * {@inheritDoc}
   */
  public abstract string? value { owned get; set; }
  /**
   * Takes a string and check if it can be valid for this property.
   */
  public virtual bool validate_value (string val) { return true; }
}

/**
 * Convenient class to handle {@link GomElement}'s attributes
 * using validated string using Regular Expressions.
 */
public class GXml.GomString : GomBaseProperty {
  protected string _value = "";
  public override string? value {
    owned get {
      return _value;
    }
    set {
      if (validate_value (value))
        _value = value;
    }
  }
}

/**
 * Convenient class to handle a {@link GomElement}'s attribute
 * using a list of pre-defined and unmutable values.
 */
public class GXml.GomArrayString : GomBaseProperty {
  protected string _value = "";
  protected string[] _values = null;
  public unowned string[] get_values () {
    return _values;
  }
  /**
   * Convenient method to initialize array of values from an array of strings.
   * Values are taken and should not be freed after call initialization.
   */
  public void initialize_strings (owned string[] strs) {
    if (strs.length == 0) return;
    _values = strs;
  }
  /**
   * Returns true if current value in attribute is included
   * in the array of values.
   */
  public bool is_valid_value () {
    if (_values == null) return true;
    foreach (string s in _values) {
      if (s == value) return true;
    }
    return false;
  }
  /**
   * Select one string from array at index:
   */
  public void select (int index) {
    if (index < 0 || index > _values.length) return;
    value = _values[index];
  }
  /**
   * Check if string is in array
   */
  public bool search (string str) {
    if (_values == null) return false;
    for (int i = 0; i < _values.length; i++) {
      if (_values[i] == str) return true;
    }
    return false;
  }
  /**
   * {inheritDoc}
   */
  public override string? value {
    owned get {
      return _value;
    }
    set {
      if (validate_value (value))
        _value = value;
    }
  }
}


/**
 * Convenient class to handle a {@link GomElement}'s attribute
 * using a list of pre-defined and unmutable values, taken from
 * an {@link IXsdSimpleType} definition
 */
public class GXml.GomXsdArrayString : GomArrayString {
  protected GLib.File _source = null;
  protected string _simple_type = null;
  /**
   * Name of {@link IXsdSimpleType} to use as source.
   */
  public string simple_type {
    get { return _simple_type; }
    set { _simple_type = value; }
  }
  /**
   * A {@link GLib.File} source to read from, simple type definitions in
   * an XSD file type.
   */
  public GLib.File source {
    get { return _source; }
    set {
      if (!value.query_exists ()) return;
      _source = value;
    }
  }
  /**
   * Load list of strings from a {@link GLib.File}, parsing using an
   * {@link GomXsdSchema} object and searching for {@link IXsdSimpleType}
   * definition with name {@link simple_type}.
   */
  public void load () throws GLib.Error {
#if DEBUG
          message ("Initializing enumerations: ");
#endif
    if (_source == null) return;
    if (!_source.query_exists ()) return;
    if (_simple_type == null) return;
    var xsd = new GomXsdSchema ();
    xsd.read_from_file (_source);
    if (xsd.simple_type_definitions == null) return;
    if (xsd.simple_type_definitions.length == 0) return;
#if DEBUG
    message ("Searching SimpleType: "+_simple_type);
    message ("SimpleType definitions: "+xsd.simple_type_definitions.length.to_string ());
#endif
    for (int i = 0; i < xsd.simple_type_definitions.length; i++) {
      var st = xsd.simple_type_definitions.get_item (i) as GomXsdSimpleType;
#if DEBUG
          message ("Item SimpleType %i is Null %s: ".printf (i, (st == null).to_string ()));
#endif
      if (st == null) continue;
#if DEBUG
          message ("Item SimpleType %i name is Null %s: ".printf (i, (st.name == null).to_string ()));
#endif
      if (st.name == null) continue;
#if DEBUG
          message ("Checking SimpleType: "+st.name);
#endif
      if (_simple_type.down () == st.name.down ()) {
        if (st.restriction == null) continue;
        if (st.restriction.enumerations == null) continue;
        if (st.restriction.enumerations.length == 0) continue;
        string[] vals = {};
        for (int j = 0; j < st.restriction.enumerations.length; j++) {
          var en = st.restriction.enumerations.get_item (j) as GomXsdTypeRestrictionEnumeration;
          if (en == null) continue;
          if (en.value == null) continue;
#if DEBUG
          message ("Enumeration to add: "+en.value);
#endif
          vals += en.value;
        }
        initialize_strings (vals);
      }
    }
  }
}

/**
 * Convenient class to handle {@link GomElement}'s attributes
 * using double pressition floats as sources of values.
 *
 * Property is represented as a string.
 */
public class GXml.GomDouble : GomBaseProperty {
  protected double _value = 0.0;
  public override string? value {
    owned get {
      string s = "%."+decimals.to_string ()+"f";
      return s.printf (_value);
    }
    set {
      _value = double.parse (value);
    }
  }
  /**
   * Set number of decimals to write out as {@link GomElement}'s property.
   * Default is 4.
   */
  public uint decimals { get; set; default = 4; }
  /**
   * Retrive current value.
   */
  public double get_double () { return _value; }
  /**
   * Sets current value.
   */
  public void set_double (double value) { _value = value; }
}

/**
 * Convenient class to handle {@link GomElement}'s attributes
 * using floats as sources of values.
 *
 * Property is represented as a string.
 */
public class GXml.GomFloat : GomDouble {
  /**
   * Retrive current value.
   */
  public float get_float () { return (float) _value; }
  /**
   * Sets current value.
   */
  public void set_float (float value) { _value = value; }
}


/**
 * Convenient class to handle {@link GomElement}'s attributes
 * using a integers as sources of values.
 *
 * Property is represented as a string.
 */
public class GXml.GomInt : GomBaseProperty {
  protected int _value = 0;
  public override string? value {
    owned get {
      return _value.to_string ();
    }
    set {
      _value = (int) double.parse (value);
    }
  }
  /**
   * Retrive current value.
   */
  public int get_integer () { return _value; }
  /**
   * Sets current value.
   */
  public void set_integer (int value) { _value = value; }
}

/**
 * Convenient class to handle {@link GomElement}'s attributes
 * using a boolean ('true' and 'false') as sources of values.
 *
 * Property is represented as a string, using 'true' or 'false'.
 */
public class GXml.GomBoolean : GomBaseProperty {
  protected bool _value = false;
  public override string? value {
    owned get {
      return _value.to_string ();
    }
    set {
      _value = bool.parse (value);
    }
  }
  /**
   * Retrive current value.
   */
  public bool get_boolean () { return _value; }
  /**
   * Sets current value.
   */
  public void set_boolean (bool value) { _value = value; }
}

/**
 * Convenient class to handle {@link GomElement}'s attributes
 * using a {@link GLib.Type.ENUM} as a source of values.
 *
 * Enumeration is represented as a string, using its name, independent of
 * value possition in enumeration.
 */
public class GXml.GomEnum : GomBaseProperty {
  protected int _value = 0;
  protected Type _enum_type;
  public override 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"));
      }
    }
  }
  /**
   * Enum type used by property.
   */
  public Type enum_type {
    get { return _enum_type; }
    construct set { _enum_type = value; }
  }
  /**
   * Convenient method to initialize internal enum type.
   */
  public void initialize_enum (GLib.Type enum_type) {
    _enum_type = enum_type;
  }
  /**
   * Retrive current value.
   */
  public int get_enum () { return (int) _value; }
  /**
   * Sets current value.
   */
  public void set_enum (int value) { _value = value; }
}

/**
 * Convenient class to handle {@link GomElement}'s attributes
 * using a {@link GLib.Date} as sources of values.
 *
 * Property is represented as a string using a %Y-%m-%d format
 */
public class GXml.GomDate : GomBaseProperty {
  protected Date _value = Date ();
  public override string? value {
    owned get {
      if (!_value.valid ()) return null;
      char[] fr = new char[100];
      _value.strftime (fr, "%Y-%m-%d");
      return (string) fr;
    }
    set {
      _value = Date ();
      if ("-" in value) {
        string[] dp = value.split ("-");
        if (dp.length == 3) {
          int y = int.parse (dp[0]);
          int m = int.parse (dp[1]);
          int d = int.parse (dp[2]);
          _value.set_dmy ((DateDay) d, (DateMonth) m, (DateYear) y);
          if (!_value.valid ())
            warning (_("Invalid Date for property: "+value));
        } else
          warning (_("Invalid format for Date property: "+value));
      } else {
        _value.set_parse (value);
      }
      if (!_value.valid ())
        warning (_("Invalid Date for property: "+value));
    }
  }
  /**
   * Retrives current value.
   */
  public Date get_date () { return _value; }
  /**
   * Sets current value.
   */
  public void set_date (Date date) { _value = date; }
}

/**
 * Convenient class to handle {@link GomElement}'s attributes
 * using a {@link GLib.DateTime} as sources of values.
 *
 * Timestamp is considered in local time.
 *
 * Property is represented as a string using a {@link GomDateTime.format}
 * and {@link GLib.DateTime.format} method. If {@link GomDateTime.format}
 * is not set '%FT%T' format is used by default.
 */
public class GXml.GomDateTime : GomBaseProperty {
  protected DateTime _value = null;
  public string format { get; set; }
  public override string? value {
    owned get {
      if (_value == null) return null;
      string s = format;
      if (s == null)
        s = "%FT%T";
      return _value.format (s);
    }
    set {
      var tv = TimeVal ();
      if (tv.from_iso8601 (value)) {
        _value = new DateTime.from_timeval_local (tv);
      } else
        warning (_("Invalid timestamp for property: "+value));
    }
  }
  /**
   * Retrives current value.
   */
  public DateTime get_datetime () { return _value; }
  /**
   * Sets current value.
   */
  public void set_datetime (DateTime dt) { _value = dt; }
}