Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/NumberArray2XmlFormatterBase.cs @ 1652

Last change on this file since 1652 was 1625, checked in by epitzer, 16 years ago

Added PersistenceException used consistently for all error conditions in the persistence framework (#548)

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