[1542] | 1 | using System.Collections;
|
---|
| 2 | using System.Text;
|
---|
| 3 | using HeuristicLab.Persistence.Interfaces;
|
---|
| 4 | using System;
|
---|
[1625] | 5 | using HeuristicLab.Persistence.Core;
|
---|
[1853] | 6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[1542] | 7 |
|
---|
| 8 | namespace HeuristicLab.Persistence.Default.Xml.Compact {
|
---|
[1566] | 9 |
|
---|
[3017] | 10 | [StorableClass]
|
---|
[3036] | 11 | internal abstract class NumberEnumeration2XmlSerializerBase<T> : CompactXmlSerializerBase<T> where T : IEnumerable {
|
---|
[1542] | 12 |
|
---|
| 13 | protected virtual string Separator { get { return ";"; } }
|
---|
| 14 | protected abstract void Add(IEnumerable enumeration, object o);
|
---|
[1564] | 15 | protected abstract IEnumerable Instantiate();
|
---|
[1542] | 16 | protected abstract string FormatValue(object o);
|
---|
| 17 | protected abstract object ParseValue(string o);
|
---|
| 18 |
|
---|
[1564] | 19 | public override XmlString Format(T t) {
|
---|
[1542] | 20 | StringBuilder sb = new StringBuilder();
|
---|
[1564] | 21 | foreach (var value in (IEnumerable)t) {
|
---|
[1542] | 22 | sb.Append(FormatValue(value));
|
---|
| 23 | sb.Append(Separator);
|
---|
| 24 | }
|
---|
[1564] | 25 | return new XmlString(sb.ToString());
|
---|
[1542] | 26 | }
|
---|
| 27 |
|
---|
[1564] | 28 | public override T Parse(XmlString x) {
|
---|
[1625] | 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);
|
---|
[1853] | 40 | }
|
---|
[1542] | 41 | }
|
---|
| 42 | }
|
---|
[1566] | 43 |
|
---|
[1542] | 44 | } |
---|