Changeset 1566 for trunk/sources/HeuristicLab.Persistence/3.3/Default
- Timestamp:
- 04/16/09 12:58:14 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.Persistence/3.3/Default
- Files:
-
- 35 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/ArrayDecomposer.cs
r1563 r1566 5 5 6 6 namespace HeuristicLab.Persistence.Default.Decomposers { 7 7 8 8 [EmptyStorableClass] 9 9 public class ArrayDecomposer : IDecomposer { … … 20 20 Array a = (Array)obj; 21 21 yield return new Tag("rank", a.Rank); 22 for (int i = 0; i < a.Rank; i++) { 22 for (int i = 0; i < a.Rank; i++) { 23 23 yield return new Tag("length_" + i, a.GetLength(i)); 24 24 } 25 for (int i = 0; i < a.Rank; i++) { 25 for (int i = 0; i < a.Rank; i++) { 26 26 yield return new Tag("lowerBound_" + i, a.GetLowerBound(i)); 27 } 27 } 28 28 } 29 29 30 30 public IEnumerable<Tag> Decompose(object array) { 31 Array a = (Array)array; 31 Array a = (Array)array; 32 32 int[] lengths = new int[a.Rank]; 33 33 int[] lowerBounds = new int[a.Rank]; … … 67 67 lowerBounds[i] = (int)e.Current.Value; 68 68 } 69 return Array.CreateInstance(t.GetElementType(), lengths, lowerBounds); 69 return Array.CreateInstance(t.GetElementType(), lengths, lowerBounds); 70 70 } 71 71 72 public void Populate(object instance, IEnumerable<Tag> elements, Type t) { 72 public void Populate(object instance, IEnumerable<Tag> elements, Type t) { 73 73 Array a = (Array)instance; 74 74 int[] lengths = new int[a.Rank]; … … 79 79 for (int i = 0; i < a.Rank; i++) { 80 80 lowerBounds[i] = a.GetLowerBound(i); 81 } 81 } 82 82 int[] positions = (int[])lowerBounds.Clone(); 83 83 IEnumerator<Tag> e = elements.GetEnumerator(); … … 86 86 a.SetValue(e.Current.Value, currentPositions); 87 87 positions[0] += 1; 88 for (int i = 0; i < a.Rank -1; i++) {89 if (positions[i] >= lengths[i] +lowerBounds[i]) {88 for (int i = 0; i < a.Rank - 1; i++) { 89 if (positions[i] >= lengths[i] + lowerBounds[i]) { 90 90 positions[i] = lowerBounds[i]; 91 91 positions[i + 1] += 1; … … 97 97 } 98 98 } 99 99 100 100 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/DictionaryDecomposer.cs
r1563 r1566 5 5 using System.Collections.Generic; 6 6 7 namespace HeuristicLab.Persistence.Default.Decomposers { 8 7 namespace HeuristicLab.Persistence.Default.Decomposers { 8 9 9 [EmptyStorableClass] 10 10 public class DictionaryDecomposer : IDecomposer { … … 16 16 17 17 public bool CanDecompose(Type type) { 18 return type.GetInterface(typeof(IDictionary).FullName) != null; 18 return type.GetInterface(typeof(IDictionary).FullName) != null; 19 19 } 20 20 … … 24 24 25 25 public IEnumerable<Tag> Decompose(object o) { 26 IDictionary dict = (IDictionary)o; 27 foreach ( 26 IDictionary dict = (IDictionary)o; 27 foreach (DictionaryEntry entry in dict) { 28 28 yield return new Tag("key", entry.Key); 29 29 yield return new Tag("value", entry.Value); … … 43 43 Tag value = iter.Current; 44 44 dict.Add(key.Value, value.Value); 45 } 45 } 46 46 } 47 47 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/EnumDecomposer.cs
r1563 r1566 5 5 6 6 namespace HeuristicLab.Persistence.Default.Decomposers { 7 7 8 8 [EmptyStorableClass] 9 9 public class EnumDecomposer : IDecomposer { … … 30 30 return Enum.Parse(t, (string)it.Current.Value); 31 31 } 32 33 public void Populate(object instance, IEnumerable<Tag> elements, Type t) { 32 33 public void Populate(object instance, IEnumerable<Tag> elements, Type t) { 34 34 } 35 35 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/EnumerableDecomposer.cs
r1563 r1566 6 6 using System.Collections.Generic; 7 7 8 namespace HeuristicLab.Persistence.Default.Decomposers { 8 namespace HeuristicLab.Persistence.Default.Decomposers { 9 9 10 10 [EmptyStorableClass] -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/KeyValuePairDecomposer.cs
r1563 r1566 7 7 8 8 namespace HeuristicLab.Persistence.Default.Decomposers { 9 9 10 10 [EmptyStorableClass] 11 11 public class KeyValuePairDecomposer : IDecomposer { … … 18 18 public bool CanDecompose(Type type) { 19 19 return type.IsGenericType && 20 type.GetGenericTypeDefinition() == 21 typeof 20 type.GetGenericTypeDefinition() == 21 typeof(KeyValuePair<int, int>).GetGenericTypeDefinition(); 22 22 } 23 23 … … 38 38 public void Populate(object instance, IEnumerable<Tag> o, Type t) { 39 39 IEnumerator<Tag> iter = o.GetEnumerator(); 40 iter.MoveNext(); 40 iter.MoveNext(); 41 41 t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) 42 42 .Single(fi => fi.Name == "key").SetValue(instance, iter.Current.Value); 43 43 iter.MoveNext(); 44 44 t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) 45 .Single(fi => fi.Name == "value").SetValue(instance, iter.Current.Value); 45 .Single(fi => fi.Name == "value").SetValue(instance, iter.Current.Value); 46 46 } 47 47 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/StorableDecomposer.cs
r1563 r1566 34 34 } 35 35 36 public void Populate(object instance, IEnumerable<Tag> objects, Type type) { 37 var memberDict = new Dictionary<string, Tag>(); 38 IEnumerator<Tag> iter = objects.GetEnumerator(); 36 public void Populate(object instance, IEnumerable<Tag> objects, Type type) { 37 var memberDict = new Dictionary<string, Tag>(); 38 IEnumerator<Tag> iter = objects.GetEnumerator(); 39 39 while (iter.MoveNext()) { 40 40 memberDict.Add(iter.Current.Name, iter.Current); 41 } 41 } 42 42 foreach (var mapping in StorableAttribute.GetStorableAccessors(instance)) { 43 43 if (memberDict.ContainsKey(mapping.Key)) { 44 mapping.Value.Set(memberDict[mapping.Key].Value); 44 mapping.Value.Set(memberDict[mapping.Key].Value); 45 45 } else if (mapping.Value.DefaultValue != null) { 46 46 mapping.Value.Set(mapping.Value.DefaultValue); -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/TypeDecomposer.cs
r1563 r1566 5 5 6 6 namespace HeuristicLab.Persistence.Default.Decomposers { 7 7 8 8 [EmptyStorableClass] 9 9 public class TypeDecomposer : IDecomposer { … … 14 14 15 15 public bool CanDecompose(Type type) { 16 return type == typeof 16 return type == typeof(Type) || 17 17 type.VersionInvariantName() == "System.RuntimeType, mscorlib"; 18 18 } … … 30 30 return Type.GetType((string)typeName.Value); 31 31 } 32 return null; 32 return null; 33 33 } 34 34 35 public void Populate(object instance, IEnumerable<Tag> objects, Type type) { 35 public void Populate(object instance, IEnumerable<Tag> objects, Type type) { 36 36 } 37 37 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/X2StringDecomposer.cs
r1563 r1566 9 9 namespace HeuristicLab.Persistence.Default.Decomposers { 10 10 11 public class Number2StringConverter { 11 public class Number2StringConverter { 12 12 13 13 private static readonly List<Type> numberTypes = … … 27 27 }; 28 28 29 private static readonly Dictionary<Type, MethodInfo> numberParsers; 29 private static readonly Dictionary<Type, MethodInfo> numberParsers; 30 30 31 31 static Number2StringConverter() { 32 32 numberParsers = new Dictionary<Type, MethodInfo>(); 33 foreach ( var type in numberTypes) {33 foreach (var type in numberTypes) { 34 34 numberParsers[type] = type 35 35 .GetMethod("Parse", BindingFlags.Static | BindingFlags.Public, 36 null, new[] { typeof (string)}, null);36 null, new[] { typeof(string) }, null); 37 37 } 38 38 } … … 43 43 44 44 public string Format(object obj) { 45 if (obj.GetType() == typeof(float)) 45 if (obj.GetType() == typeof(float)) 46 46 return ((float)obj).ToString("r", CultureInfo.InvariantCulture); 47 47 if (obj.GetType() == typeof(double)) … … 51 51 return obj.ToString(); 52 52 } 53 53 54 54 public object Parse(string stringValue, Type type) { 55 55 return numberParsers[type] 56 56 .Invoke(null, 57 57 BindingFlags.Static | BindingFlags.PutRefDispProperty, 58 null, new[] { stringValue}, CultureInfo.InvariantCulture);59 } 60 61 } 58 null, new[] { stringValue }, CultureInfo.InvariantCulture); 59 } 60 61 } 62 62 63 63 [EmptyStorableClass] … … 88 88 } 89 89 90 public void Populate(object instance, IEnumerable<Tag> tags, Type type) { 91 } 92 93 } 90 public void Populate(object instance, IEnumerable<Tag> tags, Type type) { 91 } 92 93 } 94 94 95 95 [EmptyStorableClass] … … 99 99 get { return 200; } 100 100 } 101 101 102 102 private static readonly Number2StringConverter numberConverter = 103 new Number2StringConverter(); 103 new Number2StringConverter(); 104 104 105 105 public bool CanDecompose(Type type) { 106 106 return 107 (type.IsArray || type == typeof 107 (type.IsArray || type == typeof(Array)) && 108 108 numberConverter.CanDecompose(type.GetElementType()); 109 109 } … … 146 146 var tagIter = metaInfo.GetEnumerator(); 147 147 tagIter.MoveNext(); 148 var valueIter = ((string) 149 .Split(new[] { ';'}, StringSplitOptions.RemoveEmptyEntries)148 var valueIter = ((string)tagIter.Current.Value) 149 .Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries) 150 150 .GetEnumerator(); 151 151 valueIter.MoveNext(); 152 int rank = int.Parse((string) valueIter.Current);152 int rank = int.Parse((string)valueIter.Current); 153 153 int[] lengths = new int[rank]; 154 int[] lowerBounds = new int[rank]; 154 int[] lowerBounds = new int[rank]; 155 155 for (int i = 0; i < rank; i++) { 156 156 valueIter.MoveNext(); 157 lengths[i] = int.Parse((string) valueIter.Current);158 } 157 lengths[i] = int.Parse((string)valueIter.Current); 158 } 159 159 for (int i = 0; i < rank; i++) { 160 160 valueIter.MoveNext(); 161 lowerBounds[i] = int.Parse((string) valueIter.Current);161 lowerBounds[i] = int.Parse((string)valueIter.Current); 162 162 } 163 163 Type elementType = type.GetElementType(); 164 164 Array a = Array.CreateInstance(elementType, lengths, lowerBounds); 165 int[] positions = (int[]) 165 int[] positions = (int[])lowerBounds.Clone(); 166 166 while (valueIter.MoveNext()) { 167 167 a.SetValue( 168 numberConverter.Parse((string)valueIter.Current, elementType), 168 numberConverter.Parse((string)valueIter.Current, elementType), 169 169 positions); 170 170 positions[0] += 1; 171 for ( int i = 0; i<rank-1; i++) {171 for (int i = 0; i < rank - 1; i++) { 172 172 if (positions[i] >= lengths[i] + lowerBounds[i]) { 173 173 positions[i + 1] += 1; … … 195 195 private static readonly Number2StringConverter numberConverter = 196 196 new Number2StringConverter(); 197 197 198 198 private static readonly Dictionary<Type, Type> interfaceCache = new Dictionary<Type, Type>(); 199 199 … … 212 212 return null; 213 213 } 214 214 215 215 public bool ImplementsGenericEnumerable(Type type) { 216 216 return GetGenericEnumerableInterface(type) != null; … … 225 225 BindingFlags.NonPublic | 226 226 BindingFlags.Instance, 227 null, Type.EmptyTypes, null) != null; 227 null, Type.EmptyTypes, null) != null; 228 228 } 229 229 … … 240 240 public IEnumerable<Tag> Decompose(object obj) { 241 241 Type type = obj.GetType(); 242 Type enumerable = GetGenericEnumerableInterface(type); 243 InterfaceMapping iMap = obj.GetType().GetInterfaceMap(enumerable); 242 Type enumerable = GetGenericEnumerableInterface(type); 243 InterfaceMapping iMap = obj.GetType().GetInterfaceMap(enumerable); 244 244 MethodInfo getEnumeratorMethod = 245 245 iMap.TargetMethods[ … … 247 247 iMap.InterfaceMethods, 248 248 enumerable.GetMethod("GetEnumerator"))]; 249 object[] empty = new object[] { };249 object[] empty = new object[] { }; 250 250 object genericEnumerator = getEnumeratorMethod.Invoke(obj, empty); 251 251 MethodInfo moveNextMethod = genericEnumerator.GetType().GetMethod("MoveNext"); 252 252 PropertyInfo currentProperty = genericEnumerator.GetType().GetProperty("Current"); 253 253 StringBuilder sb = new StringBuilder(); 254 while ( (bool)moveNextMethod.Invoke(genericEnumerator, empty))254 while ((bool)moveNextMethod.Invoke(genericEnumerator, empty)) 255 255 sb.Append( 256 256 numberConverter.Format( … … 265 265 public void Populate(object instance, IEnumerable<Tag> tags, Type type) { 266 266 Type enumerable = GetGenericEnumerableInterface(type); 267 Type elementType = enumerable.GetGenericArguments()[0]; 268 MethodInfo addMethod = type.GetMethod("Add"); 267 Type elementType = enumerable.GetGenericArguments()[0]; 268 MethodInfo addMethod = type.GetMethod("Add"); 269 269 var tagEnumerator = tags.GetEnumerator(); 270 270 tagEnumerator.MoveNext(); 271 string[] stringValues = ((string) 272 .Split(new[] { ';'}, StringSplitOptions.RemoveEmptyEntries);271 string[] stringValues = ((string)tagEnumerator.Current.Value) 272 .Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); 273 273 foreach (var value in stringValues) { 274 addMethod.Invoke(instance, new[] { numberConverter.Parse(value, elementType)});274 addMethod.Invoke(instance, new[] { numberConverter.Parse(value, elementType) }); 275 275 } 276 276 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/ViewOnly/ViewOnlyFormat.cs
r1564 r1566 17 17 18 18 public class ViewOnlyFormat : FormatBase<String> { 19 public override string Name { get { return "ViewOnly"; } } 19 public override string Name { get { return "ViewOnly"; } } 20 20 } 21 21 … … 47 47 [EmptyStorableClass] 48 48 public class Float2ViewFormatter : ValueType2ViewFormatterBase<float> { } 49 49 50 50 public class ViewOnlyGenerator : GeneratorBase<string> { 51 51 … … 58 58 isSepReq = false; 59 59 this.showRefs = showRefs; 60 } 60 } 61 61 62 62 protected override string Format(BeginToken beginToken) { 63 StringBuilder sb = new StringBuilder(); 63 StringBuilder sb = new StringBuilder(); 64 64 if (isSepReq) 65 65 sb.Append(", "); 66 if ( ! string.IsNullOrEmpty(beginToken.Name)) {66 if (!string.IsNullOrEmpty(beginToken.Name)) { 67 67 sb.Append(beginToken.Name); 68 if ( beginToken.Id != null && showRefs) {68 if (beginToken.Id != null && showRefs) { 69 69 sb.Append('['); 70 70 sb.Append(beginToken.Id); 71 71 sb.Append(']'); 72 } 72 } 73 73 } 74 74 sb.Append("("); … … 78 78 79 79 protected override string Format(EndToken endToken) { 80 isSepReq = true; 80 isSepReq = true; 81 81 return ")"; 82 82 } 83 83 84 84 protected override string Format(PrimitiveToken primitiveToken) { 85 StringBuilder sb = new StringBuilder(); 85 StringBuilder sb = new StringBuilder(); 86 86 if (isSepReq) 87 87 sb.Append(", "); 88 if ( ! string.IsNullOrEmpty(primitiveToken.Name)) {88 if (!string.IsNullOrEmpty(primitiveToken.Name)) { 89 89 sb.Append(primitiveToken.Name); 90 if ( primitiveToken.Id != null && showRefs) {90 if (primitiveToken.Id != null && showRefs) { 91 91 sb.Append('['); 92 92 sb.Append(primitiveToken.Id); … … 96 96 } 97 97 sb.Append(((String)primitiveToken.SerialData).Data); 98 isSepReq = true; 99 return sb.ToString(); 98 isSepReq = true; 99 return sb.ToString(); 100 100 } 101 101 … … 104 104 if (isSepReq) 105 105 sb.Append(", "); 106 if ( ! string.IsNullOrEmpty(referenceToken.Name)) {106 if (!string.IsNullOrEmpty(referenceToken.Name)) { 107 107 sb.Append(referenceToken.Name); 108 108 sb.Append('='); … … 111 111 sb.Append(referenceToken.Id); 112 112 sb.Append('}'); 113 isSepReq = true; 113 isSepReq = true; 114 114 return sb.ToString(); 115 115 } … … 141 141 142 142 public static string Serialize(object o, Configuration configuration) { 143 Serializer s = new Serializer(o, configuration); 143 Serializer s = new Serializer(o, configuration); 144 144 ViewOnlyGenerator generator = new ViewOnlyGenerator(); 145 145 StringBuilder sb = new StringBuilder(); 146 146 foreach (ISerializationToken token in s) { 147 sb.Append(generator.Format(token)); 147 sb.Append(generator.Format(token)); 148 148 } 149 149 return sb.ToString(); -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/DoubleArray2XmlFormatters.cs
r1564 r1566 4 4 5 5 namespace HeuristicLab.Persistence.Default.Xml.Compact { 6 6 7 7 public abstract class DoubleArray2XmlFormatterBase<T> : NumberArray2XmlFormatterBase<T> { 8 8 9 9 protected override string FormatValue(object o) { 10 10 return ((double)o).ToString("r", CultureInfo.InvariantCulture); … … 24 24 25 25 [EmptyStorableClass] 26 public class Double3DArray2XmlFormatter : DoubleArray2XmlFormatterBase<double[, ,]> { }27 26 public class Double3DArray2XmlFormatter : DoubleArray2XmlFormatterBase<double[, ,]> { } 27 28 28 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/DoubleList2XmlFormatter.cs
r1564 r1566 8 8 9 9 [EmptyStorableClass] 10 public class DoubleList2XmlFormatter : NumberEnumeration2XmlFormatterBase<List<double>> { 10 public class DoubleList2XmlFormatter : NumberEnumeration2XmlFormatterBase<List<double>> { 11 11 12 12 protected override void Add(IEnumerable enumeration, object o) { -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/IntArray2XmlFormatters.cs
r1564 r1566 3 3 4 4 namespace HeuristicLab.Persistence.Default.Xml.Compact { 5 6 public abstract class IntArray2XmlFormatterBase<T> : NumberArray2XmlFormatterBase<T> { 5 6 public abstract class IntArray2XmlFormatterBase<T> : NumberArray2XmlFormatterBase<T> { 7 7 8 8 protected override string FormatValue(object o) { … … 22 22 23 23 [EmptyStorableClass] 24 public class Int3DArray2XmlFormatter : IntArray2XmlFormatterBase<int[, ,]> { }25 24 public class Int3DArray2XmlFormatter : IntArray2XmlFormatterBase<int[, ,]> { } 25 26 26 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/IntList2XmlFormatter.cs
r1564 r1566 8 8 [EmptyStorableClass] 9 9 public class IntList2XmlFormatter : NumberEnumeration2XmlFormatterBase<List<int>> { 10 10 11 11 protected override void Add(IEnumerable enumeration, object o) { 12 12 ((List<int>)enumeration).Add((int)o); -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/NumberArray2XmlFormatterBase.cs
r1564 r1566 5 5 6 6 namespace HeuristicLab.Persistence.Default.Xml.Compact { 7 7 8 8 public abstract class NumberArray2XmlFormatterBase<T> : FormatterBase<T, XmlString> { 9 9 … … 30 30 } 31 31 int[] positions = (int[])lowerBounds.Clone(); 32 while (positions[a.Rank - 1] < lengths[a.Rank - 1] + lowerBounds[a.Rank - 1]) { 32 while (positions[a.Rank - 1] < lengths[a.Rank - 1] + lowerBounds[a.Rank - 1]) { 33 33 sb.Append(Separator); 34 34 sb.Append(FormatValue(a.GetValue(positions))); … … 42 42 } 43 43 } 44 } 44 } 45 45 return new XmlString(sb.ToString()); 46 46 } … … 80 80 } 81 81 } 82 82 83 83 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/NumberEnumeration2XmlFormatterBase.cs
r1564 r1566 5 5 6 6 namespace HeuristicLab.Persistence.Default.Xml.Compact { 7 7 8 8 public abstract class NumberEnumeration2XmlFormatterBase<T> : FormatterBase<T, XmlString> where T : IEnumerable { 9 9 … … 32 32 } 33 33 } 34 34 35 35 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/Bool2XmlFormatter.cs
r1554 r1566 5 5 using System.Globalization; 6 6 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 9 9 [EmptyStorableClass] 10 10 public class Bool2XmlFormatter : SimpleNumber2XmlFormatterBase<bool> { } 11 11 12 12 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/Byte2XmlFormatter.cs
r1554 r1566 5 5 using System.Globalization; 6 6 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 9 9 [EmptyStorableClass] 10 10 public class Byte2XmlFormatter : SimpleNumber2XmlFormatterBase<byte> { } 11 11 12 12 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/DateTime2XmlFormatter.cs
r1564 r1566 6 6 7 7 [EmptyStorableClass] 8 public class DateTime2XmlFormatter : FormatterBase<DateTime, XmlString> { 9 8 public class DateTime2XmlFormatter : FormatterBase<DateTime, XmlString> { 9 10 10 public override XmlString Format(DateTime dt) { 11 return new XmlString(dt.Ticks.ToString()); 11 return new XmlString(dt.Ticks.ToString()); 12 12 } 13 13 … … 18 18 } 19 19 20 20 21 21 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/Decimal2XmlFormatter.cs
r1554 r1566 5 5 using System.Globalization; 6 6 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 9 9 [EmptyStorableClass] 10 10 public class Decimal2XmlFormatter : DecimalNumber2XmlFormatterBase<decimal> { } 11 11 12 12 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/DecimalNumber2XmlFormatterBase.cs
r1564 r1566 5 5 using System.Globalization; 6 6 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 9 9 public abstract class DecimalNumber2XmlFormatterBase<T> : FormatterBase<T, XmlString> { 10 10 11 11 private static MethodInfo ToStringMethod = typeof(T) 12 12 .GetMethod( … … 30 30 return new XmlString((string)ToStringMethod.Invoke(t, new object[] { "r", CultureInfo.InvariantCulture })); 31 31 } 32 public override T Parse(XmlString x) { 32 public override T Parse(XmlString x) { 33 33 return (T)ParseMethod.Invoke(null, new object[] { x.Data, CultureInfo.InvariantCulture }); 34 34 } 35 } 35 } 36 36 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/Double2XmlFormatter.cs
r1554 r1566 5 5 using System.Globalization; 6 6 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 9 9 [EmptyStorableClass] 10 10 public class Double2XmlFormatter : DecimalNumber2XmlFormatterBase<double> { } 11 11 12 12 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/Float2XmlFormatter.cs
r1554 r1566 5 5 using System.Globalization; 6 6 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 9 9 [EmptyStorableClass] 10 10 public class Float2XmlFormatter : DecimalNumber2XmlFormatterBase<float> { } 11 11 12 12 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/Int2XmlFormatter.cs
r1554 r1566 5 5 using System.Globalization; 6 6 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 8 9 9 [EmptyStorableClass] 10 10 public class Int2XmlFormatter : SimpleNumber2XmlFormatterBase<int> { } 11 11 12 12 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/Long2XmlFormatter.cs
r1554 r1566 5 5 using System.Globalization; 6 6 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 8 9 9 [EmptyStorableClass] 10 10 public class Long2XmlFormatter : SimpleNumber2XmlFormatterBase<long> { } 11 11 12 12 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/SByte2XmlFormatter.cs
r1554 r1566 5 5 using System.Globalization; 6 6 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 9 9 [EmptyStorableClass] 10 10 public class Sbyte2XmlFormatter : SimpleNumber2XmlFormatterBase<sbyte> { } 11 11 12 12 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/Short2XmlFormatter.cs
r1554 r1566 5 5 using System.Globalization; 6 6 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 9 9 [EmptyStorableClass] 10 10 public class Short2XmlFormatter : SimpleNumber2XmlFormatterBase<short> { } 11 11 12 12 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/SimpleNumber2XmlFormatterBase.cs
r1564 r1566 5 5 using System.Globalization; 6 6 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 8 9 9 public abstract class SimpleNumber2XmlFormatterBase<T> : FormatterBase<T, XmlString> { … … 22 22 } 23 23 public override T Parse(XmlString x) { 24 return (T)ParseMethod.Invoke(null, new[] { x.Data }); 24 return (T)ParseMethod.Invoke(null, new[] { x.Data }); 25 25 } 26 } 26 } 27 27 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/String2XmlFormatter.cs
r1564 r1566 9 9 [EmptyStorableClass] 10 10 public class String2XmlFormatter : FormatterBase<string, XmlString> { 11 11 12 12 public override XmlString Format(string s) { 13 13 StringBuilder sb = new StringBuilder(); … … 15 15 sb.Append(s.Replace("]]>", "]]]]><![CDATA[>")); 16 16 sb.Append("]]>"); 17 return new XmlString(sb.ToString()); 17 return new XmlString(sb.ToString()); 18 18 } 19 19 … … 22 22 public override string Parse(XmlString x) { 23 23 StringBuilder sb = new StringBuilder(); 24 foreach (string s in x.Data.Split(separators, 24 foreach (string s in x.Data.Split(separators, 25 25 StringSplitOptions.RemoveEmptyEntries)) { 26 26 sb.Append(s); … … 28 28 return sb.ToString(); 29 29 } 30 } 30 } 31 31 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/UInt2XmlFormatter.cs
r1554 r1566 5 5 using System.Globalization; 6 6 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 9 9 [EmptyStorableClass] 10 10 public class UInt2XmlFormatter : SimpleNumber2XmlFormatterBase<uint> { } 11 11 12 12 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/ULong2XmlFormatter.cs
r1554 r1566 5 5 using System.Globalization; 6 6 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 9 9 [EmptyStorableClass] 10 public class ULong2XmlFormatter : SimpleNumber2XmlFormatterBase<ulong> { } 11 10 public class ULong2XmlFormatter : SimpleNumber2XmlFormatterBase<ulong> { } 11 12 12 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/UShort2XmlFormatter.cs
r1554 r1566 5 5 using System.Globalization; 6 6 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 8 9 9 [EmptyStorableClass] 10 10 public class UShort2XmlFormatter : SimpleNumber2XmlFormatterBase<ushort> { } 11 11 12 12 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlFormat.cs
r1564 r1566 6 6 [EmptyStorableClass] 7 7 public class XmlFormat : FormatBase<XmlString> { 8 public override string Name { get { return "XML"; } } 8 public override string Name { get { return "XML"; } } 9 9 } 10 10 -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs
r1564 r1566 21 21 public const string METAINFO = "METAINFO"; 22 22 } 23 23 24 24 public class XmlGenerator : GeneratorBase<string> { 25 25 26 26 private int depth; 27 27 … … 45 45 if (type == NodeType.End) 46 46 sb.Append('/'); 47 sb.Append(name); 47 sb.Append(name); 48 48 foreach (var attribute in attributes) { 49 49 if (attribute.Value != null && !string.IsNullOrEmpty(attribute.Value.ToString())) { … … 58 58 sb.Append('/'); 59 59 sb.Append(">"); 60 return sb.ToString(); 60 return sb.ToString(); 61 61 } 62 62 … … 65 65 } 66 66 67 protected override string Format(BeginToken beginToken) { 67 protected override string Format(BeginToken beginToken) { 68 68 var attributes = new Dictionary<string, object> { 69 69 {"name", beginToken.Name}, … … 76 76 } 77 77 78 protected override string Format(EndToken endToken) { 78 protected override string Format(EndToken endToken) { 79 79 depth -= 1; 80 80 return Prefix + "</" + XmlStrings.COMPOSITE + ">\r\n"; 81 81 } 82 82 83 protected override string Format(PrimitiveToken dataToken) { 83 protected override string Format(PrimitiveToken dataToken) { 84 84 var attributes = 85 85 new Dictionary<string, object> { … … 89 89 return Prefix + 90 90 FormatNode(XmlStrings.PRIMITIVE, attributes, NodeType.Start) + 91 ((XmlString)dataToken.SerialData).Data + "</" + XmlStrings.PRIMITIVE + ">\r\n"; 91 ((XmlString)dataToken.SerialData).Data + "</" + XmlStrings.PRIMITIVE + ">\r\n"; 92 92 } 93 93 94 protected override string Format(ReferenceToken refToken) { 94 protected override string Format(ReferenceToken refToken) { 95 95 var attributes = new Dictionary<string, object> { 96 96 {"ref", refToken.Id}, 97 {"name", refToken.Name}}; 98 return Prefix + FormatNode(XmlStrings.REFERENCE, attributes, NodeType.Inline) + "\r\n"; 97 {"name", refToken.Name}}; 98 return Prefix + FormatNode(XmlStrings.REFERENCE, attributes, NodeType.Inline) + "\r\n"; 99 99 } 100 100 101 protected override string Format(NullReferenceToken nullRefToken) { 101 protected override string Format(NullReferenceToken nullRefToken) { 102 102 var attributes = new Dictionary<string, object>{ 103 {"name", nullRefToken.Name}}; 103 {"name", nullRefToken.Name}}; 104 104 return Prefix + FormatNode(XmlStrings.NULL, attributes, NodeType.Inline) + "\r\n"; 105 105 } … … 127 127 } 128 128 129 public static void Serialize(object o, string filename) { 129 public static void Serialize(object o, string filename) { 130 130 Serialize(o, filename, ConfigurationService.Instance.GetDefaultConfig(new XmlFormat())); 131 131 } … … 135 135 XmlGenerator generator = new XmlGenerator(); 136 136 ZipOutputStream zipStream = new ZipOutputStream(File.Create(filename)); 137 zipStream.SetLevel(9); 138 zipStream.PutNextEntry(new ZipEntry("data.xml")); 137 zipStream.SetLevel(9); 138 zipStream.PutNextEntry(new ZipEntry("data.xml")); 139 139 StreamWriter writer = new StreamWriter(zipStream); 140 ILog logger = Logger.GetDefaultLogger(); 140 ILog logger = Logger.GetDefaultLogger(); 141 141 foreach (ISerializationToken token in serializer) { 142 142 string line = generator.Format(token); … … 151 151 logger.Debug(line); 152 152 } 153 writer.Flush(); 154 zipStream.Close(); 153 writer.Flush(); 154 zipStream.Close(); 155 155 } 156 156 -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlParser.cs
r1564 r1566 19 19 public XmlParser(TextReader input) { 20 20 XmlReaderSettings settings = new XmlReaderSettings { 21 22 23 24 21 ConformanceLevel = ConformanceLevel.Document, 22 IgnoreWhitespace = true, 23 IgnoreComments = true 24 }; 25 25 reader = XmlReader.Create(input, settings); 26 26 handlers = new Dictionary<string, Handler> { … … 66 66 67 67 private IEnumerator<ISerializationToken> ParseComposite() { 68 string name = reader.GetAttribute("name"); 68 string name = reader.GetAttribute("name"); 69 69 string idString = reader.GetAttribute("id"); 70 70 int? id = null; … … 104 104 var typeCache = new List<TypeMapping>(); 105 105 XmlReader xmlReader = XmlReader.Create(reader); 106 while ( xmlReader.Read()) {106 while (xmlReader.Read()) { 107 107 if (xmlReader.Name == XmlStrings.TYPE) { 108 108 typeCache.Add(new TypeMapping( … … 116 116 117 117 public static object DeSerialize(string filename) { 118 ZipFile zipFile = new ZipFile(filename); 118 ZipFile zipFile = new ZipFile(filename); 119 119 Deserializer deSerializer = new Deserializer( 120 120 ParseTypeCache( … … 123 123 XmlParser parser = new XmlParser( 124 124 new StreamReader(zipFile.GetInputStream(zipFile.GetEntry("data.xml")))); 125 return deSerializer.Deserialize(parser); 125 return deSerializer.Deserialize(parser); 126 126 } 127 } 127 } 128 128 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlString.cs
r1564 r1566 3 3 4 4 namespace HeuristicLab.Persistence.Default.Xml { 5 5 6 6 public class XmlString : ISerialData { 7 7
Note: See TracChangeset
for help on using the changeset viewer.