1 | using System;
|
---|
2 | using System.Collections;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Text;
|
---|
5 | using System.Globalization;
|
---|
6 | namespace Persistence {
|
---|
7 |
|
---|
8 | public interface IFormat {
|
---|
9 | string Name { get; }
|
---|
10 | }
|
---|
11 |
|
---|
12 | public interface IFormatter {
|
---|
13 | Type Type { get; }
|
---|
14 | IFormat Format { get; }
|
---|
15 | object Serialize(object o);
|
---|
16 | object DeSerialize(object o);
|
---|
17 | }
|
---|
18 |
|
---|
19 | public class XmlFormat : IFormat {
|
---|
20 | public string Name { get { return "XML"; } }
|
---|
21 | public static readonly XmlFormat Instance = new XmlFormat();
|
---|
22 | }
|
---|
23 |
|
---|
24 | public class String2XmlFormatter : IFormatter {
|
---|
25 | public Type Type { get { return typeof(string); } }
|
---|
26 | public IFormat Format { get { return XmlFormat.Instance; } }
|
---|
27 | public object Serialize(object o) {
|
---|
28 | return "<![CDATA[" +
|
---|
29 | ((string)o).Replace("]]>", "]]]]><![CDATA[>") +
|
---|
30 | "]]>";
|
---|
31 | }
|
---|
32 | public object DeSerialize(object o) {
|
---|
33 | StringBuilder sb = new StringBuilder();
|
---|
34 | foreach (string s in ((string)o).Split(
|
---|
35 | new[] { "<![CDATA[", "]]>" },
|
---|
36 | StringSplitOptions.RemoveEmptyEntries)) {
|
---|
37 | sb.Append(s);
|
---|
38 | }
|
---|
39 | return sb.ToString();
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public class Int2XmlFormatter : IFormatter {
|
---|
44 | public Type Type { get { return typeof(int); } }
|
---|
45 | public IFormat Format { get { return XmlFormat.Instance; } }
|
---|
46 | public object Serialize(object o) {
|
---|
47 | return ((int)o).ToString();
|
---|
48 | }
|
---|
49 | public object DeSerialize(object o) {
|
---|
50 | return int.Parse((string)o);
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | public class Double2XmlFormatter : IFormatter {
|
---|
55 | public Type Type { get { return typeof(double); } }
|
---|
56 | public IFormat Format { get { return XmlFormat.Instance; } }
|
---|
57 | public object Serialize(object o) {
|
---|
58 | return ((double)o).ToString("r", CultureInfo.InvariantCulture);
|
---|
59 | }
|
---|
60 | public object DeSerialize(object o) {
|
---|
61 | return double.Parse((string)o, CultureInfo.InvariantCulture);
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | public class Boolean2XmlFormatter : IFormatter {
|
---|
66 | public Type Type { get { return typeof(bool); } }
|
---|
67 | public IFormat Format { get { return XmlFormat.Instance; } }
|
---|
68 | public object Serialize(object o) {
|
---|
69 | return ((bool)o).ToString();
|
---|
70 | }
|
---|
71 | public object DeSerialize(object o) {
|
---|
72 | return bool.Parse((string)o);
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | public class DateTime2XmlFormatter : IFormatter {
|
---|
77 | public Type Type { get { return typeof(DateTime); } }
|
---|
78 | public IFormat Format { get { return XmlFormat.Instance; } }
|
---|
79 | public object Serialize(object o) {
|
---|
80 | return ((DateTime)o).Ticks.ToString();
|
---|
81 | }
|
---|
82 | public object DeSerialize(object o) {
|
---|
83 | return new DateTime(long.Parse((string)o));
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | public abstract class NumberArray2XmlFormatter : IFormatter {
|
---|
88 | public abstract Type Type { get; }
|
---|
89 | public IFormat Format { get { return XmlFormat.Instance; } }
|
---|
90 | protected virtual string Separator { get { return ";"; } }
|
---|
91 | protected abstract string formatValue(object o);
|
---|
92 | protected abstract object parseValue(string o);
|
---|
93 | public object Serialize(object obj) {
|
---|
94 | Array a = (Array)obj;
|
---|
95 | StringBuilder sb = new StringBuilder();
|
---|
96 | sb.Append(a.Rank);
|
---|
97 | for (int i = 0; i < a.Rank; i++) {
|
---|
98 | sb.Append(Separator);
|
---|
99 | sb.Append(a.GetLength(i));
|
---|
100 | }
|
---|
101 | foreach (object o in a) {
|
---|
102 | sb.Append(Separator);
|
---|
103 | sb.Append(formatValue(o));
|
---|
104 | }
|
---|
105 | return sb.ToString();
|
---|
106 | }
|
---|
107 | public object DeSerialize(object o) {
|
---|
108 | IEnumerator values =
|
---|
109 | ((string) o)
|
---|
110 | .Split(new[] {Separator},
|
---|
111 | StringSplitOptions.RemoveEmptyEntries).GetEnumerator();
|
---|
112 | values.MoveNext();
|
---|
113 | int rank = int.Parse((string)values.Current);
|
---|
114 | int[] lengths = new int[rank];
|
---|
115 | for ( int i = 0; i<rank; i++ ) {
|
---|
116 | values.MoveNext();
|
---|
117 | lengths[i] = int.Parse((string)values.Current);
|
---|
118 | }
|
---|
119 | Array a = Array.CreateInstance(this.Type.GetElementType(), lengths);
|
---|
120 | int[] positions = new int[rank];
|
---|
121 | while ( values.MoveNext() ) {
|
---|
122 | a.SetValue(parseValue((string)values.Current), positions);
|
---|
123 | positions[0] += 1;
|
---|
124 | for ( int i = 0; i<rank-1; i++) {
|
---|
125 | if (positions[i] >= lengths[i]) {
|
---|
126 | positions[i] = 0;
|
---|
127 | positions[i + 1] += 1;
|
---|
128 | } else {
|
---|
129 | break;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 | return a;
|
---|
134 | }
|
---|
135 | }
|
---|
136 | public class IntArray2XmlFormatter : NumberArray2XmlFormatter {
|
---|
137 | public override Type Type { get { return typeof(int[]); } }
|
---|
138 | protected override string formatValue(object o) { return o.ToString(); }
|
---|
139 | protected override object parseValue(string o) { return int.Parse(o); }
|
---|
140 | }
|
---|
141 | public class Int2DArray2XmlFormatter : IntArray2XmlFormatter {
|
---|
142 | public override Type Type { get { return typeof (int[,]); } }
|
---|
143 | }
|
---|
144 | public class Int3DArray2XmlFormatter : IntArray2XmlFormatter {
|
---|
145 | public override Type Type { get { return typeof(int[,,]); } }
|
---|
146 | }
|
---|
147 | public class DoubleArray2XmlFormatter : NumberArray2XmlFormatter {
|
---|
148 | public override Type Type { get { return typeof(double[]); } }
|
---|
149 | protected override string formatValue(object o) { return ((double) o).ToString("r"); }
|
---|
150 | protected override object parseValue(string o) { return double.Parse(o); }
|
---|
151 | }
|
---|
152 | public class Double2DArray2XmlFormatter : DoubleArray2XmlFormatter {
|
---|
153 | public override Type Type { get { return typeof(double[,]); } }
|
---|
154 | }
|
---|
155 | public class Double3DArray2XmlFormatter : DoubleArray2XmlFormatter {
|
---|
156 | public override Type Type { get { return typeof(double[,,]); } }
|
---|
157 | }
|
---|
158 |
|
---|
159 | public abstract class NumberEnumeration2XmlFormatter : IFormatter {
|
---|
160 | public abstract Type Type { get; }
|
---|
161 | public IFormat Format { get { return XmlFormat.Instance; } }
|
---|
162 | protected virtual string Separator { get { return ";"; } }
|
---|
163 | protected abstract void Add(IEnumerable enumeration, object o);
|
---|
164 | protected abstract object Instantiate();
|
---|
165 | protected abstract string formatValue(object o);
|
---|
166 | protected abstract object parseValue(string o);
|
---|
167 | public object Serialize(object o) {
|
---|
168 | StringBuilder sb = new StringBuilder();
|
---|
169 | foreach (var value in (IEnumerable) o) {
|
---|
170 | sb.Append(formatValue(value));
|
---|
171 | sb.Append(Separator);
|
---|
172 | }
|
---|
173 | return sb.ToString();
|
---|
174 | }
|
---|
175 | public object DeSerialize(object o) {
|
---|
176 | IEnumerable enumeration = (IEnumerable) Instantiate();
|
---|
177 | string[] values = ((string) o).Split(new[] {Separator}, StringSplitOptions.RemoveEmptyEntries);
|
---|
178 | foreach ( var value in values ) {
|
---|
179 | Add(enumeration, parseValue(value));
|
---|
180 | }
|
---|
181 | return enumeration;
|
---|
182 | }
|
---|
183 | }
|
---|
184 | public class IntList2XmlFormatter : NumberEnumeration2XmlFormatter {
|
---|
185 | public override Type Type { get { return typeof(List<int>); } }
|
---|
186 | protected override void Add(IEnumerable enumeration, object o) { ((List<int>)enumeration).Add((int)o); }
|
---|
187 | protected override object Instantiate() { return new List<int>(); }
|
---|
188 | protected override string formatValue(object o) { return o.ToString(); }
|
---|
189 | protected override object parseValue(string o) { return int.Parse(o); }
|
---|
190 | }
|
---|
191 | public class DoubleList2XmlFormatter : NumberEnumeration2XmlFormatter {
|
---|
192 | public override Type Type { get { return typeof(List<double>); } }
|
---|
193 | protected override void Add(IEnumerable enumeration, object o) { ((List<double>)enumeration).Add((int)o); }
|
---|
194 | protected override object Instantiate() { return new List<double>(); }
|
---|
195 | protected override string formatValue(object o) { return ((double) o).ToString("r"); }
|
---|
196 | protected override object parseValue(string o) { return double.Parse(o); }
|
---|
197 | }
|
---|
198 | } |
---|