- Timestamp:
- 03/12/09 12:19:32 (16 years ago)
- Location:
- branches/New Persistence Exploration/Persistence/Persistence
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/New Persistence Exploration/Persistence/Persistence/DeSerializer.cs
r1329 r1330 135 135 Primitive primitive = (Primitive)token; 136 136 object value = primitiveSerializers[primitive.Type].DeSerialize(primitive.SerializedValue); 137 this.SetValue(primitive.Name, value); 137 if ( ! value.GetType().IsValueType ) 138 id2obj[(int)primitive.Id] = value; 139 SetValue(primitive.Name, value); 138 140 } 139 141 private void ReferenceHandler(IParseToken token) { -
branches/New Persistence Exploration/Persistence/Persistence/PrimitiveSerializers.cs
r1315 r1330 1 1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 2 4 using System.Text; 3 5 using System.Globalization; … … 20 22 StringBuilder sb = new StringBuilder(); 21 23 foreach (string s in ((string)o).Split( 22 new string[] { "<![CDATA[", "]]>" },24 new[] { "<![CDATA[", "]]>" }, 23 25 StringSplitOptions.RemoveEmptyEntries)) { 24 26 sb.Append(s); … … 27 29 } 28 30 } 31 29 32 public class Int2XMLSerializer : IPrimitiveSerializer { 30 33 public Type Type { get { return typeof(int); } } … … 36 39 } 37 40 } 41 38 42 public class Double2XmlSerializer : IPrimitiveSerializer { 39 43 public Type Type { get { return typeof(double); } } … … 45 49 } 46 50 } 51 47 52 public class Boolean2XmlSerializer : IPrimitiveSerializer { 48 53 public Type Type { get { return typeof(bool); } } … … 54 59 } 55 60 } 61 56 62 public class DateTime2XmlSerializer : IPrimitiveSerializer { 57 63 public Type Type { get { return typeof(DateTime); } } … … 63 69 } 64 70 } 71 72 public abstract class NumberArray2XmlSerializer : IPrimitiveSerializer { 73 public abstract Type Type { get; } 74 protected virtual string Separator { get { return ";"; } } 75 protected abstract string formatValue(object o); 76 protected abstract object parseValue(string o); 77 public object Serialize(object obj) { 78 Array a = (Array)obj; 79 StringBuilder sb = new StringBuilder(); 80 sb.Append(a.Rank); 81 for (int i = 0; i < a.Rank; i++) { 82 sb.Append(Separator); 83 sb.Append(a.GetLength(i)); 84 } 85 foreach (object o in a) { 86 sb.Append(Separator); 87 sb.Append(formatValue(o)); 88 } 89 return sb.ToString(); 90 } 91 public object DeSerialize(object o) { 92 IEnumerator values = 93 ((string) o) 94 .Split(new[] {Separator}, 95 StringSplitOptions.RemoveEmptyEntries).GetEnumerator(); 96 values.MoveNext(); 97 int rank = int.Parse((string)values.Current); 98 int[] lengths = new int[rank]; 99 for ( int i = 0; i<rank; i++ ) { 100 values.MoveNext(); 101 lengths[i] = int.Parse((string)values.Current); 102 } 103 Array a = Array.CreateInstance(this.Type.GetElementType(), lengths); 104 int[] positions = new int[rank]; 105 while ( values.MoveNext() ) { 106 a.SetValue(parseValue((string)values.Current), positions); 107 positions[0] += 1; 108 for ( int i = 0; i<rank-1; i++) { 109 if (positions[i] >= lengths[i]) { 110 positions[i] = 0; 111 positions[i + 1] += 1; 112 } else { 113 break; 114 } 115 } 116 } 117 return a; 118 } 119 } 120 public class IntArray2XmlSerializer : NumberArray2XmlSerializer { 121 public override Type Type { get { return typeof(int[]); } } 122 protected override string formatValue(object o) { return o.ToString(); } 123 protected override object parseValue(string o) { return int.Parse(o); } 124 } 125 public class Int2DArray2XmlSerializer : IntArray2XmlSerializer { 126 public override Type Type { get { return typeof (int[,]); } } 127 } 128 public class Int3DArray2XmlSerializer : IntArray2XmlSerializer { 129 public override Type Type { get { return typeof(int[,,]); } } 130 } 131 public class DoubleArray2XmlSerializer : NumberArray2XmlSerializer { 132 public override Type Type { get { return typeof(double[]); } } 133 protected override string formatValue(object o) { return ((double) o).ToString("r"); } 134 protected override object parseValue(string o) { return double.Parse(o); } 135 } 136 public class Double2DArray2XmlSerializer : DoubleArray2XmlSerializer { 137 public override Type Type { get { return typeof(double[,]); } } 138 } 139 public class Double3DArray2XmlSerializer : DoubleArray2XmlSerializer { 140 public override Type Type { get { return typeof(double[,,]); } } 141 } 142 143 public abstract class NumberEnumeration2XmlSerializer : IPrimitiveSerializer { 144 public abstract Type Type { get; } 145 protected virtual string Separator { get { return ";"; } } 146 protected abstract void Add(IEnumerable enumeration, object o); 147 protected abstract object Instantiate(); 148 protected abstract string formatValue(object o); 149 protected abstract object parseValue(string o); 150 public object Serialize(object o) { 151 StringBuilder sb = new StringBuilder(); 152 foreach (var value in (IEnumerable) o) { 153 sb.Append(formatValue(value)); 154 sb.Append(Separator); 155 } 156 return sb.ToString(); 157 } 158 public object DeSerialize(object o) { 159 IEnumerable enumeration = (IEnumerable) Instantiate(); 160 string[] values = ((string) o).Split(new[] {Separator}, StringSplitOptions.RemoveEmptyEntries); 161 foreach ( var value in values ) { 162 Add(enumeration, parseValue(value)); 163 } 164 return enumeration; 165 } 166 } 167 public class IntList2XmlSerializer : NumberEnumeration2XmlSerializer { 168 public override Type Type { get { return typeof(List<int>); } } 169 protected override void Add(IEnumerable enumeration, object o) { ((List<int>)enumeration).Add((int)o); } 170 protected override object Instantiate() { return new List<int>(); } 171 protected override string formatValue(object o) { return o.ToString(); } 172 protected override object parseValue(string o) { return int.Parse(o); } 173 } 65 174 } -
branches/New Persistence Exploration/Persistence/Persistence/Serializer.cs
r1323 r1330 52 52 if (value == null) { 53 53 yield return new NullReferenceToken(accessor.Name); 54 } else if (primitiveSerializers.ContainsKey(value.GetType())) {55 yield return new PrimitiveToken(accessor, primitiveSerializers[value.GetType()].Serialize(value));56 54 } else if (obj2id.ContainsKey(value)) { 57 55 yield return new ReferenceToken(accessor.Name, obj2id[value]); 56 } else if (primitiveSerializers.ContainsKey(value.GetType())) { 57 int? id = null; 58 if ( ! value.GetType().IsValueType ) { 59 id = obj2id.Count; 60 obj2id.Add(value, (int)id); 61 } 62 yield return new PrimitiveToken( 63 accessor, 64 primitiveSerializers[value.GetType()].Serialize(value), 65 id); 58 66 } else { 59 67 int id = obj2id.Count; -
branches/New Persistence Exploration/Persistence/Persistence/StorableAttribute.cs
r1324 r1330 232 232 if (!propertyInfo.CanRead || !propertyInfo.CanWrite) { 233 233 throw new NotSupportedException( 234 "Storable properties must implement both a Get and a Set accessor. ");234 "Storable properties must implement both a Get and a Set Accessor. "); 235 235 } 236 236 getter = () => propertyInfo.GetValue(obj, null); … … 255 255 } 256 256 257 public DataMemberAccessor(object o) { 258 Name = ""; 257 public DataMemberAccessor(object o) { 259 258 Type = o.GetType(); 260 259 DefaultValue = null; -
branches/New Persistence Exploration/Persistence/Persistence/Tokens.cs
r1280 r1330 7 7 } 8 8 public class BeginToken : ISerializationToken { 9 public DataMemberAccessor Accessor;10 public int Id;9 public readonly DataMemberAccessor Accessor; 10 public readonly int Id; 11 11 public BeginToken(DataMemberAccessor accessor, int id) { 12 this.Accessor = accessor;13 this.Id = id;12 Accessor = accessor; 13 Id = id; 14 14 } 15 15 } 16 16 public class EndToken : ISerializationToken { 17 public DataMemberAccessor Accessor;18 public int Id;17 public readonly DataMemberAccessor Accessor; 18 public readonly int Id; 19 19 public EndToken(DataMemberAccessor accessor, int id) { 20 this.Accessor = accessor;21 this.Id = id;20 Accessor = accessor; 21 Id = id; 22 22 } 23 23 } 24 24 public class PrimitiveToken : ISerializationToken { 25 public DataMemberAccessor accessor; 26 public object Data; 27 public PrimitiveToken(DataMemberAccessor accessor, object data) { 28 this.accessor = accessor; 29 this.Data = data; 25 public readonly DataMemberAccessor Accessor; 26 public readonly object Data; 27 public readonly int? Id; 28 public PrimitiveToken(DataMemberAccessor accessor, object data, int? id) { 29 Accessor = accessor; 30 Data = data; 31 Id = id; 30 32 } 31 33 } 32 34 public class ReferenceToken : ISerializationToken { 33 public string Name;34 public int Id;35 public readonly string Name; 36 public readonly int Id; 35 37 public ReferenceToken(string name, int id) { 36 this.Name = name;37 this.Id = id;38 Name = name; 39 Id = id; 38 40 } 39 41 } 40 42 public class NullReferenceToken : ISerializationToken { 41 public string Name;43 public readonly string Name; 42 44 public NullReferenceToken(string name) { 43 this.Name = name;45 Name = name; 44 46 } 45 47 } … … 51 53 52 54 public class CompositeStart : IParseToken { 53 public string Name;54 public Type Type;55 public int Id;55 public readonly string Name; 56 public readonly Type Type; 57 public readonly int Id; 56 58 public CompositeStart(string name, Type type, int id) { 57 this.Name = name;58 this.Type = type;59 this.Id = id;59 Name = name; 60 Type = type; 61 Id = id; 60 62 } 61 63 } 62 64 public class CompositeEnd : IParseToken { 63 public string Name;64 public Type Type;65 public int Id;65 public readonly string Name; 66 public readonly Type Type; 67 public readonly int Id; 66 68 public CompositeEnd(string name, Type type, int id) { 67 this.Name = name;68 this.Type = type;69 this.Id = id;69 Name = name; 70 Type = type; 71 Id = id; 70 72 } 71 73 } 72 74 public class Primitive : IParseToken { 73 public string Name; 74 public Type Type; 75 public string SerializedValue; 76 public Primitive(string name, Type type, string serilaizedValue) { 77 this.Name = name; 78 this.Type = type; 79 this.SerializedValue = serilaizedValue; 75 public readonly string Name; 76 public readonly Type Type; 77 public readonly string SerializedValue; 78 public readonly int? Id; 79 public Primitive(string name, Type type, string serilaizedValue, int? id) { 80 Name = name; 81 Type = type; 82 SerializedValue = serilaizedValue; 83 Id = id; 80 84 } 81 85 } 82 86 public class Reference : IParseToken { 83 public string Name;84 public int Id;87 public readonly string Name; 88 public readonly int Id; 85 89 public Reference(string name, int id) { 86 this.Name = name;87 this.Id = id;90 Name = name; 91 Id = id; 88 92 } 89 93 } 90 94 public class Null : IParseToken { 91 public string Name;95 public readonly string Name; 92 96 public Null(string name) { 93 this.Name = name;97 Name = name; 94 98 } 95 99 } -
branches/New Persistence Exploration/Persistence/Persistence/XmlFormatter.cs
r1323 r1330 1 1 using System.Collections.Generic; 2 2 using System; 3 using System.Xml; 4 using System.Text; 3 5 namespace Persistence { 4 6 public class XmlFormatter { … … 13 15 {typeof (BeginToken), FormatBegin}, 14 16 {typeof (EndToken), FormatEnd}, 15 {typeof (PrimitiveToken), Format Data},17 {typeof (PrimitiveToken), FormatPrimitive}, 16 18 {typeof (ReferenceToken), FormatReference}, 17 19 {typeof (NullReferenceToken), FormatNullReference} 18 20 }; 19 21 depth = 0; 22 } 23 24 private enum NodeType { Start, End, Inline } ; 25 26 private static string FormatNode(string name, Dictionary<string, string> attributes, NodeType type) { 27 StringBuilder sb = new StringBuilder(); 28 sb.Append('<'); 29 if (type == NodeType.End) 30 sb.Append('/'); 31 sb.Append(name); 32 foreach (var attribute in attributes) { 33 sb.Append(' '); 34 sb.Append(attribute.Key); 35 sb.Append("=\""); 36 sb.Append(attribute.Value); 37 sb.Append('"'); 38 } 39 if (type == NodeType.Inline) 40 sb.Append('/'); 41 sb.Append(">"); 42 return sb.ToString(); 20 43 } 21 44 … … 30 53 private string FormatBegin(ISerializationToken token) { 31 54 BeginToken beginToken = (BeginToken)token; 32 string result = 33 String.Format("{0}<COMPOSITE name=\"{1}\" type=\"{2}\" id=\"{3}\">\n", 34 Prefix, beginToken.Accessor.Name, beginToken.Accessor.Get().GetType(), beginToken.Id); 55 string result = Prefix + 56 FormatNode("COMPOSITE", 57 new Dictionary<string, string> 58 { 59 {"name", beginToken.Accessor.Name}, 60 {"type", beginToken.Accessor.Get().GetType().ToString()}, 61 {"id", beginToken.Id.ToString()} 62 }, NodeType.Start) + "\n"; 35 63 depth += 1; 36 64 return result; 37 65 } 38 66 39 private string FormatEnd(ISerializationToken token) { 67 private string FormatEnd(ISerializationToken token) { 40 68 depth -= 1; 41 69 return Prefix + "</COMPOSITE>\n"; 42 70 } 43 71 44 private string Format Data(ISerializationToken token) {72 private string FormatPrimitive(ISerializationToken token) { 45 73 PrimitiveToken dataToken = (PrimitiveToken)token; 46 return String.Format("{0}<PRIMITIVE name=\"{1}\" type=\"{2}\">{3}</PRIMITIVE>\n", 47 Prefix, dataToken.accessor.Name, dataToken.accessor.Get().GetType(), dataToken.Data); 74 Dictionary<string, string> attributes = 75 new Dictionary<string, string> { 76 {"type", dataToken.Accessor.Get().GetType().ToString()}}; 77 if ( dataToken.Accessor.Name != "" ) 78 attributes.Add("name", dataToken.Accessor.Name); 79 if ( dataToken.Id != null ) 80 attributes.Add("id", dataToken.Id.ToString()); 81 return Prefix + 82 FormatNode("PRIMITIVE", attributes, NodeType.Start) + 83 dataToken.Data + "</PRIMITIVE>\n"; 48 84 } 49 85 50 86 private string FormatReference(ISerializationToken token) { 51 ReferenceToken refToken = (ReferenceToken)token; 52 return String.Format("{0}<REFERENCE name=\"{1}\" ref=\"{2}\"/>\n", 53 Prefix, refToken.Name, refToken.Id); 87 ReferenceToken refToken = (ReferenceToken) token; 88 Dictionary<string, string> attributes = 89 new Dictionary<string, string> {{"ref", refToken.Id.ToString()}}; 90 if ( refToken.Name != null ) 91 attributes.Add("name", refToken.Name); 92 return Prefix + FormatNode("REFERENCE", attributes, NodeType.Inline) + "\n"; 54 93 } 55 94 56 95 private string FormatNullReference(ISerializationToken token) { 57 96 NullReferenceToken nullRefToken = (NullReferenceToken)token; 58 return String.Format("{0}<NULL name=\"{1}\"/>\n", 59 Prefix, nullRefToken.Name); 97 Dictionary<string, string> attributes = new Dictionary<string, string>(); 98 if (nullRefToken.Name != null) 99 attributes.Add("name", nullRefToken.Name); 100 return Prefix + FormatNode("NULL", attributes, NodeType.Inline) + "\n"; 60 101 } 61 102 } -
branches/New Persistence Exploration/Persistence/Persistence/XmlParser.cs
r1280 r1330 8 8 public class XmlParser : IEnumerable<IParseToken> { 9 9 10 private XmlReader reader;10 private readonly XmlReader reader; 11 11 private delegate IEnumerator<IParseToken> Handler(); 12 private Dictionary<string, Handler> handlers;12 private readonly Dictionary<string, Handler> handlers; 13 13 14 public XmlParser(StreamReader input) { 15 XmlReaderSettings settings = new XmlReaderSettings(); 16 settings.ConformanceLevel = ConformanceLevel.Document; 17 settings.IgnoreWhitespace = true; 18 settings.IgnoreComments = true; 19 this.reader = XmlReader.Create(input, settings); 20 this.handlers = new Dictionary<string, Handler>(); 21 this.handlers.Add("PRIMITIVE", new Handler(ParsePrimitive)); 22 this.handlers.Add("COMPOSITE", new Handler(ParseComposite)); 23 this.handlers.Add("REFERENCE", new Handler(ParseReference)); 24 this.handlers.Add("NULL", new Handler(ParseNull)); 14 public XmlParser(TextReader input) { 15 XmlReaderSettings settings = new XmlReaderSettings { 16 ConformanceLevel = ConformanceLevel.Document, 17 IgnoreWhitespace = true, 18 IgnoreComments = true 19 }; 20 reader = XmlReader.Create(input, settings); 21 handlers = new Dictionary<string, Handler> { 22 {"PRIMITIVE", ParsePrimitive}, 23 {"COMPOSITE", ParseComposite}, 24 {"REFERENCE", ParseReference}, 25 {"NULL", ParseNull} 26 }; 25 27 } 26 28 public IEnumerator<IParseToken> GetEnumerator() { 27 while ( this.reader.Read()) {29 while (reader.Read()) { 28 30 if (!reader.IsStartElement()) { 29 31 break; … … 44 46 } 45 47 private IEnumerator<IParseToken> ParsePrimitive() { 48 int? id = null; 49 string idString = reader.GetAttribute("id"); 50 if (idString != null) 51 id = int.Parse(idString); 46 52 yield return new Primitive( 47 this.reader.GetAttribute("name"), 48 Type.GetType(this.reader.GetAttribute("type")), 49 this.reader.ReadString()); 53 reader.GetAttribute("name"), 54 Type.GetType(reader.GetAttribute("type")), 55 reader.ReadString(), 56 id); 50 57 } 51 58 private IEnumerator<IParseToken> ParseComposite() { 52 string name = this.reader.GetAttribute("name");53 Type type = Type.GetType( this.reader.GetAttribute("type"));54 int id = int.Parse( this.reader.GetAttribute("id"));59 string name = reader.GetAttribute("name"); 60 Type type = Type.GetType(reader.GetAttribute("type")); 61 int id = int.Parse(reader.GetAttribute("id")); 55 62 yield return new CompositeStart(name, type, id); 56 IEnumerator<IParseToken> iterator = this.GetEnumerator();63 IEnumerator<IParseToken> iterator = GetEnumerator(); 57 64 while (iterator.MoveNext()) 58 65 yield return iterator.Current; … … 61 68 private IEnumerator<IParseToken> ParseReference() { 62 69 yield return new Reference( 63 this.reader.GetAttribute("name"),64 int.Parse( this.reader.GetAttribute("ref")));70 reader.GetAttribute("name"), 71 int.Parse(reader.GetAttribute("ref"))); 65 72 } 66 73 private IEnumerator<IParseToken> ParseNull() { 67 yield return new Null( this.reader.GetAttribute("name"));74 yield return new Null(reader.GetAttribute("name")); 68 75 } 69 76 IEnumerator IEnumerable.GetEnumerator() { 70 return this.GetEnumerator();77 return GetEnumerator(); 71 78 } 72 79 }
Note: See TracChangeset
for help on using the changeset viewer.