[3742] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3742] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
[4068] | 22 | using System;
|
---|
[3742] | 23 | using System.Collections;
|
---|
[1542] | 24 | using System.Text;
|
---|
[17097] | 25 | using HEAL.Attic;
|
---|
[4068] | 26 | using HeuristicLab.Persistence.Auxiliary;
|
---|
[1542] | 27 |
|
---|
| 28 | namespace HeuristicLab.Persistence.Default.Xml.Compact {
|
---|
[1566] | 29 |
|
---|
[17097] | 30 | [StorableType("B4D23353-9322-4005-BAF2-AB3034DEE34A")]
|
---|
[3036] | 31 | internal abstract class NumberEnumeration2XmlSerializerBase<T> : CompactXmlSerializerBase<T> where T : IEnumerable {
|
---|
[1542] | 32 |
|
---|
[3937] | 33 | protected virtual char Separator { get { return ';'; } }
|
---|
[1542] | 34 | protected abstract void Add(IEnumerable enumeration, object o);
|
---|
[1564] | 35 | protected abstract IEnumerable Instantiate();
|
---|
[1542] | 36 | protected abstract string FormatValue(object o);
|
---|
| 37 | protected abstract object ParseValue(string o);
|
---|
| 38 |
|
---|
[1564] | 39 | public override XmlString Format(T t) {
|
---|
[1542] | 40 | StringBuilder sb = new StringBuilder();
|
---|
[1564] | 41 | foreach (var value in (IEnumerable)t) {
|
---|
[1542] | 42 | sb.Append(FormatValue(value));
|
---|
| 43 | sb.Append(Separator);
|
---|
| 44 | }
|
---|
[1564] | 45 | return new XmlString(sb.ToString());
|
---|
[1542] | 46 | }
|
---|
| 47 |
|
---|
[1564] | 48 | public override T Parse(XmlString x) {
|
---|
[1625] | 49 | try {
|
---|
| 50 | IEnumerable enumeration = Instantiate();
|
---|
[3937] | 51 | foreach (var value in x.Data.EnumerateSplit(Separator)) {
|
---|
[1625] | 52 | Add(enumeration, ParseValue(value));
|
---|
| 53 | }
|
---|
| 54 | return (T)enumeration;
|
---|
[4068] | 55 | }
|
---|
| 56 | catch (InvalidCastException e) {
|
---|
[1625] | 57 | throw new PersistenceException("Invalid element data during reconstruction of number enumerable.", e);
|
---|
[4068] | 58 | }
|
---|
| 59 | catch (OverflowException e) {
|
---|
[1625] | 60 | throw new PersistenceException("Overflow during element parsing while trying to reconstruct number enumerable.", e);
|
---|
[1853] | 61 | }
|
---|
[1542] | 62 | }
|
---|
| 63 | }
|
---|
[1566] | 64 |
|
---|
[1542] | 65 | } |
---|