Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Persistence/3.3/Default/Xml/Compact/NumberArray2XmlSerializerBase.cs @ 4539

Last change on this file since 4539 was 1853, checked in by epitzer, 15 years ago

Fix EmptyStorableClass attributes. (#603)

File size: 3.5 KB
Line 
1using System.Collections;
2using System.Text;
3using HeuristicLab.Persistence.Interfaces;
4using System;
5using HeuristicLab.Persistence.Core;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7
8namespace HeuristicLab.Persistence.Default.Xml.Compact {
9
10  [EmptyStorableClass]
11  public abstract class NumberArray2XmlSerializerBase<T> : CompactXmlSerializerBase<T> where T : class {
12
13    protected virtual string Separator { get { return ";"; } }
14    protected abstract string FormatValue(object o);
15    protected abstract object ParseValue(string o);
16
17    public override XmlString Format(T t) {
18      Array a = (Array)(object)t;
19      int[] lengths = new int[a.Rank];
20      int[] lowerBounds = new int[a.Rank];
21      StringBuilder sb = new StringBuilder();
22      sb.Append(a.Rank);
23      for (int i = 0; i < a.Rank; i++) {
24        sb.Append(Separator);
25        sb.Append(a.GetLength(i));
26        lengths[i] = a.GetLength(i);
27      }
28      for (int i = 0; i < a.Rank; i++) {
29        sb.Append(Separator);
30        sb.Append(a.GetLowerBound(i));
31        lowerBounds[i] = a.GetLowerBound(i);
32      }
33      int[] positions = (int[])lowerBounds.Clone();
34      while (positions[a.Rank - 1] < lengths[a.Rank - 1] + lowerBounds[a.Rank - 1]) {
35        sb.Append(Separator);
36        sb.Append(FormatValue(a.GetValue(positions)));
37        positions[0] += 1;
38        for (int i = 0; i < a.Rank - 1; i++) {
39          if (positions[i] >= lengths[i] + lowerBounds[i]) {
40            positions[i] = lowerBounds[i];
41            positions[i + 1] += 1;
42          } else {
43            break;
44          }
45        }
46      }
47      return new XmlString(sb.ToString());
48    }
49
50    public override T Parse(XmlString x) {
51      try {
52        IEnumerator values =
53          x.Data.Split(new[] { Separator },
54          StringSplitOptions.RemoveEmptyEntries).GetEnumerator();
55        values.MoveNext();
56        int rank = int.Parse((string)values.Current);
57        int[] lengths = new int[rank];
58        for (int i = 0; i < rank; i++) {
59          values.MoveNext();
60          lengths[i] = int.Parse((string)values.Current);
61        }
62        int[] lowerBounds = new int[rank];
63        for (int i = 0; i < rank; i++) {
64          values.MoveNext();
65          lowerBounds[i] = int.Parse((string)values.Current);
66        }
67        Array a = Array.CreateInstance(this.SourceType.GetElementType(), lengths, lowerBounds);
68        int[] positions = (int[])lowerBounds.Clone();
69        while (values.MoveNext()) {
70          a.SetValue(ParseValue((string)values.Current), positions);
71          positions[0] += 1;
72          for (int i = 0; i < rank - 1; i++) {
73            if (positions[i] >= lengths[i]) {
74              positions[i] = 0;
75              positions[i + 1] += 1;
76            } else {
77              break;
78            }
79          }
80        }
81        if (positions[rank - 1] != lowerBounds[rank - 1] + lengths[rank - 1])
82          throw new PersistenceException("Insufficient number of elements while trying to fill number array.");
83        return (T)(object)a;
84      } catch (InvalidOperationException e) {
85        throw new PersistenceException("Insufficient information to rebuild number array.", e);
86      } catch (InvalidCastException e) {
87        throw new PersistenceException("Invalid element data or meta data to reconstruct number array.", e);
88      } catch (OverflowException e) {
89        throw new PersistenceException("Overflow during element parsing while trying to reconstruct number array.", e);
90      }
91    }
92  }
93
94}
Note: See TracBrowser for help on using the repository browser.