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