Changeset 1564 for trunk/sources/HeuristicLab.Persistence/3.3/Default
- Timestamp:
- 04/16/09 12:40:41 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.Persistence/3.3/Default
- Files:
-
- 1 added
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Default/ViewOnly/ViewOnlyFormat.cs
r1556 r1564 9 9 namespace HeuristicLab.Persistence.Default.ViewOnly { 10 10 11 public class ViewOnlyFormat : FormatBase { 12 public override string Name { get { return "ViewOnly"; } } 13 public static ViewOnlyFormat Instance = new ViewOnlyFormat(); 11 public class String : ISerialData { 12 public string Data { get; set; } 13 public String(string s) { 14 Data = s; 15 } 14 16 } 15 17 16 public abstract class ValueType2ViewFormatter : IFormatter { 17 public abstract Type Type { get; } 18 public IFormat Format { get { return ViewOnlyFormat.Instance; } } 19 public object DoFormat(object o) { return o; } 20 public object Parse(object o) { 18 public class ViewOnlyFormat : FormatBase<String> { 19 public override string Name { get { return "ViewOnly"; } } 20 } 21 22 public abstract class ValueType2ViewFormatterBase<T> : FormatterBase<T, String> { 23 public override String Format(T o) { return new String(o.ToString()); } 24 public override T Parse(String s) { 21 25 throw new NotImplementedException(); 22 26 } 23 27 } 24 28 25 public class String2ViewFormatter : ValueType2ViewFormatter { 26 public override Type Type { get { return typeof(string); } } 27 } 29 [EmptyStorableClass] 30 public class String2ViewFormatter : ValueType2ViewFormatterBase<string> { } 28 31 29 public class Bool2ViewFormatter : ValueType2ViewFormatter { 30 public override Type Type { get { return typeof(bool); } } 31 } 32 [EmptyStorableClass] 33 public class Bool2ViewFormatter : ValueType2ViewFormatterBase<bool> { } 32 34 33 public class Int2ViewFormatter : ValueType2ViewFormatter { 34 public override Type Type { get { return typeof(int); } } 35 } 35 [EmptyStorableClass] 36 public class Int2ViewFormatter : ValueType2ViewFormatterBase<int> { } 36 37 37 public class Double2ViewFormatter : ValueType2ViewFormatter { 38 public override Type Type { get { return typeof(double); } } 39 } 38 [EmptyStorableClass] 39 public class Double2ViewFormatter : ValueType2ViewFormatterBase<double> { } 40 40 41 public class DateTime2ViewFormatter : ValueType2ViewFormatter { 42 public override Type Type { get { return typeof(DateTime); } } 43 } 41 [EmptyStorableClass] 42 public class DateTime2ViewFormatter : ValueType2ViewFormatterBase<DateTime> { } 44 43 45 public class Type2ViewFormatter : ValueType2ViewFormatter { 46 public override Type Type { get { return typeof(Type); } } 47 } 44 [EmptyStorableClass] 45 public class Type2ViewFormatter : ValueType2ViewFormatterBase<Type> { } 48 46 49 public class Float2ViewFormatter : ValueType2ViewFormatter { 50 public override Type Type { get { return typeof(float); } } 51 } 52 53 public class ViewOnlyGenerator : Generator<string> { 47 [EmptyStorableClass] 48 public class Float2ViewFormatter : ValueType2ViewFormatterBase<float> { } 49 50 public class ViewOnlyGenerator : GeneratorBase<string> { 54 51 55 52 private bool isSepReq; … … 98 95 sb.Append('='); 99 96 } 100 sb.Append( primitiveToken.SerialData);97 sb.Append(((String)primitiveToken.SerialData).Data); 101 98 isSepReq = true; 102 99 return sb.ToString(); … … 140 137 141 138 public static string Serialize(object o) { 142 return Serialize(o, ConfigurationService.Instance.GetDefaultConfig( ViewOnlyFormat.Instance));139 return Serialize(o, ConfigurationService.Instance.GetDefaultConfig(new ViewOnlyFormat())); 143 140 } 144 141 -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/DoubleArray2XmlFormatters.cs
r1542 r1564 4 4 5 5 namespace HeuristicLab.Persistence.Default.Xml.Compact { 6 7 [EmptyStorableClass] 8 public class DoubleArray2XmlFormatter : NumberArray2XmlFormatterBase { 9 10 public override Type Type { 11 get { 12 return typeof(double[]); 13 } 14 } 15 6 7 public abstract class DoubleArray2XmlFormatterBase<T> : NumberArray2XmlFormatterBase<T> { 8 16 9 protected override string FormatValue(object o) { 17 10 return ((double)o).ToString("r", CultureInfo.InvariantCulture); … … 24 17 25 18 [EmptyStorableClass] 26 public class Double2DArray2XmlFormatter : DoubleArray2XmlFormatter { 27 public override Type Type { 28 get { 29 return typeof(double[,]); 30 } 31 } 32 } 19 public class Double1DArray2XmlFormatter : DoubleArray2XmlFormatterBase<double[]> { } 20 33 21 34 22 [EmptyStorableClass] 35 public class Double3DArray2XmlFormatter : DoubleArray2XmlFormatter { 36 public override Type Type { 37 get { 38 return typeof(double[, ,]); 39 } 40 } 41 } 23 public class Double2DArray2XmlFormatter : DoubleArray2XmlFormatterBase<double[,]> { } 24 25 [EmptyStorableClass] 26 public class Double3DArray2XmlFormatter : DoubleArray2XmlFormatterBase<double[,,]> { } 42 27 43 28 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/DoubleList2XmlFormatter.cs
r1542 r1564 8 8 9 9 [EmptyStorableClass] 10 public class DoubleList2XmlFormatter : NumberEnumeration2XmlFormatterBase { 11 12 public override Type Type { 13 get { 14 return typeof(List<double>); 15 } 16 } 10 public class DoubleList2XmlFormatter : NumberEnumeration2XmlFormatterBase<List<double>> { 17 11 18 12 protected override void Add(IEnumerable enumeration, object o) { … … 20 14 } 21 15 22 protected override objectInstantiate() {16 protected override IEnumerable Instantiate() { 23 17 return new List<double>(); 24 18 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/IntArray2XmlFormatters.cs
r1542 r1564 3 3 4 4 namespace HeuristicLab.Persistence.Default.Xml.Compact { 5 6 [EmptyStorableClass] 7 public class IntArray2XmlFormatter : NumberArray2XmlFormatterBase { 8 9 public override Type Type { 10 get { 11 return typeof(int[]); 12 } 13 } 5 6 public abstract class IntArray2XmlFormatterBase<T> : NumberArray2XmlFormatterBase<T> { 14 7 15 8 protected override string FormatValue(object o) { … … 22 15 } 23 16 17 [EmptyStorableClass] 18 public class Int1DArray2XmlFormatter : IntArray2XmlFormatterBase<int[]> { } 24 19 25 20 [EmptyStorableClass] 26 public class Int2DArray2XmlFormatter : IntArray2XmlFormatter { 27 public override Type Type { 28 get { 29 return typeof(int[,]); 30 } 31 } 32 } 33 21 public class Int2DArray2XmlFormatter : IntArray2XmlFormatterBase<int[,]> { } 34 22 35 23 [EmptyStorableClass] 36 public class Int3DArray2XmlFormatter : IntArray2XmlFormatter { 37 public override Type Type { 38 get { 39 return typeof(int[, ,]); 40 } 41 } 42 } 24 public class Int3DArray2XmlFormatter : IntArray2XmlFormatterBase<int[,,]> { } 43 25 44 26 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/IntList2XmlFormatter.cs
r1542 r1564 7 7 8 8 [EmptyStorableClass] 9 public class IntList2XmlFormatter : NumberEnumeration2XmlFormatterBase { 10 11 public override Type Type { 12 get { 13 return typeof(List<int>); 14 } 15 } 16 9 public class IntList2XmlFormatter : NumberEnumeration2XmlFormatterBase<List<int>> { 10 17 11 protected override void Add(IEnumerable enumeration, object o) { 18 12 ((List<int>)enumeration).Add((int)o); 19 13 } 20 14 21 protected override objectInstantiate() {15 protected override IEnumerable Instantiate() { 22 16 return new List<int>(); 23 17 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/NumberArray2XmlFormatterBase.cs
r1542 r1564 6 6 namespace HeuristicLab.Persistence.Default.Xml.Compact { 7 7 8 public abstract class NumberArray2XmlFormatterBase : IFormatter{8 public abstract class NumberArray2XmlFormatterBase<T> : FormatterBase<T, XmlString> { 9 9 10 public abstract Type Type { get; }11 public IFormat Format { get { return XmlFormat.Instance; } }12 10 protected virtual string Separator { get { return ";"; } } 13 11 protected abstract string FormatValue(object o); 14 12 protected abstract object ParseValue(string o); 15 13 16 public object DoFormat(object obj) { 17 Array a = (Array)obj; 14 public override XmlString Format(T t) { 15 object o = (object)t; 16 Array a = (Array)o; 18 17 int[] lengths = new int[a.Rank]; 19 18 int[] lowerBounds = new int[a.Rank]; … … 44 43 } 45 44 } 46 return sb.ToString();45 return new XmlString(sb.ToString()); 47 46 } 48 47 49 public o bject Parse(object o) {48 public override T Parse(XmlString x) { 50 49 IEnumerator values = 51 ((string)o) 52 .Split(new[] { Separator }, 50 x.Data.Split(new[] { Separator }, 53 51 StringSplitOptions.RemoveEmptyEntries).GetEnumerator(); 54 52 values.MoveNext(); … … 64 62 lowerBounds[i] = int.Parse((string)values.Current); 65 63 } 66 Array a = Array.CreateInstance(this. Type.GetElementType(), lengths, lowerBounds);64 Array a = Array.CreateInstance(this.SourceType.GetElementType(), lengths, lowerBounds); 67 65 int[] positions = new int[rank]; 68 66 while (values.MoveNext()) { … … 78 76 } 79 77 } 80 return a; 78 object o = a; 79 return (T)o; 81 80 } 82 81 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/NumberEnumeration2XmlFormatterBase.cs
r1542 r1564 6 6 namespace HeuristicLab.Persistence.Default.Xml.Compact { 7 7 8 public abstract class NumberEnumeration2XmlFormatterBase : IFormatter{8 public abstract class NumberEnumeration2XmlFormatterBase<T> : FormatterBase<T, XmlString> where T : IEnumerable { 9 9 10 public abstract Type Type { get; }11 public IFormat Format { get { return XmlFormat.Instance; } }12 10 protected virtual string Separator { get { return ";"; } } 13 11 protected abstract void Add(IEnumerable enumeration, object o); 14 protected abstract objectInstantiate();12 protected abstract IEnumerable Instantiate(); 15 13 protected abstract string FormatValue(object o); 16 14 protected abstract object ParseValue(string o); 17 15 18 public o bject DoFormat(object o) {16 public override XmlString Format(T t) { 19 17 StringBuilder sb = new StringBuilder(); 20 foreach (var value in (IEnumerable) o) {18 foreach (var value in (IEnumerable)t) { 21 19 sb.Append(FormatValue(value)); 22 20 sb.Append(Separator); 23 21 } 24 return sb.ToString();22 return new XmlString(sb.ToString()); 25 23 } 26 24 27 public o bject Parse(object o) {28 IEnumerable enumeration = (IEnumerable)Instantiate();29 string[] values = ((string)o).Split(new[] { Separator }, StringSplitOptions.RemoveEmptyEntries);25 public override T Parse(XmlString x) { 26 IEnumerable enumeration = Instantiate(); 27 string[] values = x.Data.Split(new[] { Separator }, StringSplitOptions.RemoveEmptyEntries); 30 28 foreach (var value in values) { 31 29 Add(enumeration, ParseValue(value)); 32 30 } 33 return enumeration;31 return (T)enumeration; 34 32 } 35 33 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/DateTime2XmlFormatter.cs
r1454 r1564 6 6 7 7 [EmptyStorableClass] 8 public class DateTime2XmlFormatter : IFormatter { 9 10 public Type Type { get { return typeof(DateTime); } } 11 public IFormat Format { get { return XmlFormat.Instance; } } 12 13 public object DoFormat(object o) { 14 return ((DateTime)o).Ticks.ToString(); 8 public class DateTime2XmlFormatter : FormatterBase<DateTime, XmlString> { 9 10 public override XmlString Format(DateTime dt) { 11 return new XmlString(dt.Ticks.ToString()); 15 12 } 16 13 17 public o bject Parse(object o) {18 return new DateTime(long.Parse( (string)o));14 public override DateTime Parse(XmlString x) { 15 return new DateTime(long.Parse(x.Data)); 19 16 } 20 17 -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/DecimalNumber2XmlFormatterBase.cs
r1554 r1564 7 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 8 9 public abstract class DecimalNumber2XmlFormatterBase<T> : IFormatter { 10 11 public Type Type { get { return typeof(T); } } 12 13 public IFormat Format { get { return XmlFormat.Instance; } } 14 9 public abstract class DecimalNumber2XmlFormatterBase<T> : FormatterBase<T, XmlString> { 10 15 11 private static MethodInfo ToStringMethod = typeof(T) 16 12 .GetMethod( … … 31 27 null); 32 28 33 public o bject DoFormat(object o) {34 return ToStringMethod.Invoke(o, new object[] { "r", CultureInfo.InvariantCulture });29 public override XmlString Format(T t) { 30 return new XmlString((string)ToStringMethod.Invoke(t, new object[] { "r", CultureInfo.InvariantCulture })); 35 31 } 36 public o bject Parse(object o) {37 return ParseMethod.Invoke(null, new[] { o, CultureInfo.InvariantCulture });32 public override T Parse(XmlString x) { 33 return (T)ParseMethod.Invoke(null, new object[] { x.Data, CultureInfo.InvariantCulture }); 38 34 } 39 35 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/SimpleNumber2XmlFormatterBase.cs
r1554 r1564 7 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 8 9 public abstract class SimpleNumber2XmlFormatterBase<T> : IFormatter { 10 11 public Type Type { get { return typeof(T); } } 12 13 public IFormat Format { get { return XmlFormat.Instance; } } 9 public abstract class SimpleNumber2XmlFormatterBase<T> : FormatterBase<T, XmlString> { 14 10 15 11 private static MethodInfo ParseMethod = typeof(T) … … 22 18 null); 23 19 24 public o bject DoFormat(object o) {25 return ((T)o).ToString();20 public override XmlString Format(T t) { 21 return new XmlString(t.ToString()); 26 22 } 27 public o bject Parse(object o) {28 return ParseMethod.Invoke(null, new[] { o});23 public override T Parse(XmlString x) { 24 return (T)ParseMethod.Invoke(null, new[] { x.Data }); 29 25 } 30 26 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/String2XmlFormatter.cs
r1542 r1564 8 8 9 9 [EmptyStorableClass] 10 public class String2XmlFormatter : IFormatter { 11 12 public Type Type { get { return typeof(string); } } 13 public IFormat Format { get { return XmlFormat.Instance; } } 14 15 public object DoFormat(object o) { 16 return "<![CDATA[" + 17 ((string)o).Replace("]]>", "]]]]><![CDATA[>") + 18 "]]>"; 10 public class String2XmlFormatter : FormatterBase<string, XmlString> { 11 12 public override XmlString Format(string s) { 13 StringBuilder sb = new StringBuilder(); 14 sb.Append("<![CDATA["); 15 sb.Append(s.Replace("]]>", "]]]]><![CDATA[>")); 16 sb.Append("]]>"); 17 return new XmlString(sb.ToString()); 19 18 } 20 19 21 public object Parse(object o) { 20 private static readonly string[] separators = new string[] { "<![CDATA[", "]]>" }; 21 22 public override string Parse(XmlString x) { 22 23 StringBuilder sb = new StringBuilder(); 23 foreach (string s in ((string)o).Split( 24 new[] { "<![CDATA[", "]]>" }, 24 foreach (string s in x.Data.Split(separators, 25 25 StringSplitOptions.RemoveEmptyEntries)) { 26 26 sb.Append(s); -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlFormat.cs
r1542 r1564 5 5 6 6 [EmptyStorableClass] 7 public class XmlFormat : FormatBase { 8 public override string Name { get { return "XML"; } } 9 public static readonly XmlFormat Instance = new XmlFormat(); 10 private XmlFormat() { } 7 public class XmlFormat : FormatBase<XmlString> { 8 public override string Name { get { return "XML"; } } 11 9 } 12 10 -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs
r1556 r1564 21 21 public const string METAINFO = "METAINFO"; 22 22 } 23 24 public abstract class Generator<T> { 25 public T Format(ISerializationToken token) { 26 Type type = token.GetType(); 27 if (type == typeof(BeginToken)) 28 return Format((BeginToken)token); 29 if (type == typeof(EndToken)) 30 return Format((EndToken)token); 31 if (type == typeof(PrimitiveToken)) 32 return Format((PrimitiveToken)token); 33 if (type == typeof(ReferenceToken)) 34 return Format((ReferenceToken)token); 35 if (type == typeof(NullReferenceToken)) 36 return Format((NullReferenceToken)token); 37 if (type == typeof(MetaInfoBeginToken)) 38 return Format((MetaInfoBeginToken)token); 39 if (type == typeof(MetaInfoEndToken)) 40 return Format((MetaInfoEndToken)token); 41 throw new ApplicationException("Invalid token of type " + type.FullName); 42 } 43 protected abstract T Format(BeginToken beginToken); 44 protected abstract T Format(EndToken endToken); 45 protected abstract T Format(PrimitiveToken primitiveToken); 46 protected abstract T Format(ReferenceToken referenceToken); 47 protected abstract T Format(NullReferenceToken nullReferenceToken); 48 protected abstract T Format(MetaInfoBeginToken metaInfoBeginToken); 49 protected abstract T Format(MetaInfoEndToken metaInfoEndToken); 50 } 51 52 public class XmlGenerator : Generator<string> { 23 24 public class XmlGenerator : GeneratorBase<string> { 53 25 54 26 private int depth; … … 117 89 return Prefix + 118 90 FormatNode(XmlStrings.PRIMITIVE, attributes, NodeType.Start) + 119 dataToken.SerialData + "</" + XmlStrings.PRIMITIVE + ">\r\n";91 ((XmlString)dataToken.SerialData).Data + "</" + XmlStrings.PRIMITIVE + ">\r\n"; 120 92 } 121 93 … … 134 106 135 107 protected override string Format(MetaInfoBeginToken metaInfoBeginToken) { 136 string result = Prefix + "<" + XmlStrings.METAINFO + "> ";108 string result = Prefix + "<" + XmlStrings.METAINFO + ">\r\n"; 137 109 depth += 1; 138 110 return result; … … 141 113 protected override string Format(MetaInfoEndToken metaInfoEndToken) { 142 114 depth -= 1; 143 return Prefix + "</" + XmlStrings.METAINFO + "> ";115 return Prefix + "</" + XmlStrings.METAINFO + ">\r\n"; 144 116 } 145 117 … … 156 128 157 129 public static void Serialize(object o, string filename) { 158 Serialize(o, filename, ConfigurationService.Instance.GetDefaultConfig( XmlFormat.Instance));130 Serialize(o, filename, ConfigurationService.Instance.GetDefaultConfig(new XmlFormat())); 159 131 } 160 132 -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlParser.cs
r1556 r1564 62 62 int.Parse(reader.GetAttribute("typeId")), 63 63 id, 64 reader.ReadString());64 new XmlString(reader.ReadString())); 65 65 } 66 66 … … 69 69 string idString = reader.GetAttribute("id"); 70 70 int? id = null; 71 int? typeId = null;72 71 if (idString != null) 73 72 id = int.Parse(idString); 74 73 int typeId = int.Parse(reader.GetAttribute("typeId")); 75 74 yield return new BeginToken(name, typeId, id); 76 75 IEnumerator<ISerializationToken> iterator = GetEnumerator();
Note: See TracChangeset
for help on using the changeset viewer.