source: trunk/sources/HeuristicLab.Persistence/3.3/Core/DeSerializer.cs @ 1625

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

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

File size: 5.7 KB
Line 
1using System.Collections.Generic;
2using System;
3using HeuristicLab.Persistence.Interfaces;
4using HeuristicLab.Persistence.Core.Tokens;
5
6namespace HeuristicLab.Persistence.Core {
7
8  public class Deserializer {
9
10    class Midwife {
11
12      public int? Id { get; private set; }
13      public bool MetaMode { get; set; }
14      public object Obj { get; private set; }
15
16      private List<Tag> metaInfo;
17      private List<Tag> customValues;
18      private Type type;
19      private IDecomposer decomposer;
20
21      public Midwife(object value) {
22        this.Obj = value;
23      }
24
25      public Midwife(Type type, IDecomposer decomposer, int? id) {
26        this.type = type;
27        this.decomposer = decomposer;
28        this.Id = id;
29        MetaMode = false;
30        metaInfo = new List<Tag>();
31        customValues = new List<Tag>();
32      }
33
34      public void CreateInstance() {
35        if (Obj != null)
36          throw new PersistenceException("object already instantiated");
37        Obj = decomposer.CreateInstance(type, metaInfo);
38      }
39
40      public void AddValue(string name, object value) {
41        if (MetaMode) {
42          metaInfo.Add(new Tag(name, value));
43        } else {
44          customValues.Add(new Tag(name, value));
45        }
46      }
47
48      public void Populate() {
49        decomposer.Populate(Obj, customValues, type);
50      }
51    }
52
53    private readonly Dictionary<int, object> id2obj;
54    private readonly Dictionary<Type, object> serializerMapping;
55    private readonly Stack<Midwife> parentStack;
56    private readonly Dictionary<int, Type> typeIds;
57
58    public Deserializer(
59      IEnumerable<TypeMapping> typeCache) {
60      id2obj = new Dictionary<int, object>();
61      parentStack = new Stack<Midwife>();
62      typeIds = new Dictionary<int, Type>();
63      serializerMapping = CreateSerializers(typeCache);
64    }
65
66    private Dictionary<Type, object> CreateSerializers(IEnumerable<TypeMapping> typeCache) {
67      try {
68        var map = new Dictionary<Type, object>();
69        foreach (var typeMapping in typeCache) {
70          Type type = Type.GetType(typeMapping.TypeName, true);
71          typeIds.Add(typeMapping.Id, type);
72          Type serializerType = Type.GetType(typeMapping.Serializer, true);
73          map.Add(type, Activator.CreateInstance(serializerType, true));
74        }
75        return map;
76      } catch (Exception e) {
77        throw new PersistenceException(
78          "The serialization type cache could not be loaded.\r\n" +
79          "This usualy happens when you are missing an Assembly/Plugin.", e);
80      }     
81    }
82
83    public object Deserialize(IEnumerable<ISerializationToken> tokens) {
84      foreach (ISerializationToken token in tokens) {
85        Type t = token.GetType();
86        if (t == typeof(BeginToken)) {
87          CompositeStartHandler((BeginToken)token);
88        } else if (t == typeof(EndToken)) {
89          CompositeEndHandler((EndToken)token);
90        } else if (t == typeof(PrimitiveToken)) {
91          PrimitiveHandler((PrimitiveToken)token);
92        } else if (t == typeof(ReferenceToken)) {
93          ReferenceHandler((ReferenceToken)token);
94        } else if (t == typeof(NullReferenceToken)) {
95          NullHandler((NullReferenceToken)token);
96        } else if (t == typeof(MetaInfoBeginToken)) {
97          MetaInfoBegin((MetaInfoBeginToken)token);
98        } else if (t == typeof(MetaInfoEndToken)) {
99          MetaInfoEnd((MetaInfoEndToken)token);
100        } else {
101          throw new PersistenceException("invalid token type");
102        }
103      }
104      return parentStack.Pop().Obj;
105    }
106
107    private void CompositeStartHandler(BeginToken token) {
108      Type type = typeIds[(int)token.TypeId];
109      IDecomposer decomposer = null;
110      if (serializerMapping.ContainsKey(type))
111        decomposer = serializerMapping[type] as IDecomposer;
112      if (decomposer == null)
113        throw new PersistenceException(String.Format(
114          "No suitable method for deserialization of type \"{0}\" found.",
115          type.VersionInvariantName()));
116      parentStack.Push(new Midwife(type, decomposer, token.Id));
117    }
118
119    private void CompositeEndHandler(EndToken token) {
120      Type type = typeIds[(int)token.TypeId];
121      Midwife midwife = parentStack.Pop();
122      if (midwife.Obj == null)
123        CreateInstance(midwife);
124      midwife.Populate();
125      SetValue(token.Name, midwife.Obj);
126    }
127
128    private void PrimitiveHandler(PrimitiveToken token) {
129      Type type = typeIds[(int)token.TypeId];
130      object value = ((IFormatter)serializerMapping[type]).Parse(token.SerialData);
131      if (token.Id != null)
132        id2obj[(int)token.Id] = value;
133      SetValue(token.Name, value);
134    }
135
136    private void ReferenceHandler(ReferenceToken token) {
137      object referredObject = id2obj[token.Id];
138      SetValue(token.Name, referredObject);
139    }
140
141    private void NullHandler(NullReferenceToken token) {
142      SetValue(token.Name, null);
143    }
144
145    private void MetaInfoBegin(MetaInfoBeginToken token) {
146      parentStack.Peek().MetaMode = true;
147    }
148
149    private void MetaInfoEnd(MetaInfoEndToken token) {
150      Midwife m = parentStack.Peek();
151      m.MetaMode = false;
152      CreateInstance(m);
153    }
154
155    private void CreateInstance(Midwife m) {
156      m.CreateInstance();
157      if (m.Id != null)
158        id2obj.Add((int)m.Id, m.Obj);
159    }
160
161    private void SetValue(string name, object value) {
162      if (parentStack.Count == 0) {
163        parentStack.Push(new Midwife(value));
164      } else {
165        Midwife m = parentStack.Peek();
166        if (m.MetaMode == false && m.Obj == null)
167          CreateInstance(m);
168        m.AddValue(name, value);
169      }
170    }
171  }
172}
Note: See TracBrowser for help on using the repository browser.