1 | using System.Collections;
|
---|
2 | using System.Text;
|
---|
3 | using HeuristicLab.Persistence.Interfaces;
|
---|
4 | using System;
|
---|
5 | using HeuristicLab.Persistence.Core;
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Persistence.Default.Xml.Compact {
|
---|
9 |
|
---|
10 | [EmptyStorableClass]
|
---|
11 | public abstract class NumberEnumeration2XmlSerializerBase<T> : CompactXmlSerializerBase<T> where T : IEnumerable {
|
---|
12 |
|
---|
13 | protected virtual string Separator { get { return ";"; } }
|
---|
14 | protected abstract void Add(IEnumerable enumeration, object o);
|
---|
15 | protected abstract IEnumerable Instantiate();
|
---|
16 | protected abstract string FormatValue(object o);
|
---|
17 | protected abstract object ParseValue(string o);
|
---|
18 |
|
---|
19 | public override XmlString Format(T t) {
|
---|
20 | StringBuilder sb = new StringBuilder();
|
---|
21 | foreach (var value in (IEnumerable)t) {
|
---|
22 | sb.Append(FormatValue(value));
|
---|
23 | sb.Append(Separator);
|
---|
24 | }
|
---|
25 | return new XmlString(sb.ToString());
|
---|
26 | }
|
---|
27 |
|
---|
28 | public override T Parse(XmlString x) {
|
---|
29 | try {
|
---|
30 | IEnumerable enumeration = Instantiate();
|
---|
31 | string[] values = x.Data.Split(new[] { Separator }, StringSplitOptions.RemoveEmptyEntries);
|
---|
32 | foreach (var value in values) {
|
---|
33 | Add(enumeration, ParseValue(value));
|
---|
34 | }
|
---|
35 | return (T)enumeration;
|
---|
36 | } catch (InvalidCastException e) {
|
---|
37 | throw new PersistenceException("Invalid element data during reconstruction of number enumerable.", e);
|
---|
38 | } catch (OverflowException e) {
|
---|
39 | throw new PersistenceException("Overflow during element parsing while trying to reconstruct number enumerable.", e);
|
---|
40 | }
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | } |
---|