Free cookie consent management tool by TermsFeed Policy Generator

source: branches/New Persistence Exploration/Persistence/Persistence/XmlParser.cs @ 1357

Last change on this file since 1357 was 1357, checked in by epitzer, 15 years ago

Pluginification and major refactoring. (#506)

File size: 3.5 KB
Line 
1using System.Xml;
2using System.Collections.Generic;
3using System;
4using System.Collections;
5using System.IO;
6using HeuristicLab.Persistence.Interfaces;
7
8namespace HeuristicLab.Persistence {
9
10
11  public class XmlParser : IEnumerable<ISerializationToken> {
12
13    private readonly XmlReader reader;
14    private delegate IEnumerator<ISerializationToken> Handler();
15    private readonly Dictionary<string, Handler> handlers;
16
17    public XmlParser(TextReader input) {
18      XmlReaderSettings settings = new XmlReaderSettings {
19                                       ConformanceLevel = ConformanceLevel.Document,
20                                       IgnoreWhitespace = true,
21                                       IgnoreComments = true
22                                     };
23      reader = XmlReader.Create(input, settings);
24      handlers = new Dictionary<string, Handler> {
25                     {"PRIMITIVE", ParsePrimitive},
26                     {"COMPOSITE", ParseComposite},
27                     {"REFERENCE", ParseReference},
28                     {"NULL", ParseNull}
29                   };
30    }
31    public IEnumerator<ISerializationToken> GetEnumerator() {
32      while (reader.Read()) {
33        if (!reader.IsStartElement()) {
34          break;
35        }
36        IEnumerator<ISerializationToken> iterator;
37        try {
38          iterator = handlers[reader.Name].Invoke();
39        }
40        catch (KeyNotFoundException) {
41          throw new InvalidOperationException(String.Format(
42            "No handler for XML tag \"{0}\" installed",
43            reader.Name));
44        }
45        while (iterator.MoveNext()) {
46          yield return iterator.Current;
47        }
48      }
49    }
50    private IEnumerator<ISerializationToken> ParsePrimitive() {
51      int? id = null;
52      string idString = reader.GetAttribute("id");
53      if (idString != null)
54        id = int.Parse(idString);
55      yield return new PrimitiveToken(
56        reader.GetAttribute("name"),
57        int.Parse(reader.GetAttribute("typeId")),
58        reader.ReadString(),
59        id);
60    }
61    private IEnumerator<ISerializationToken> ParseComposite() {
62      string name = reader.GetAttribute("name");           
63      string idString = reader.GetAttribute("id");
64      int? id = null;
65      int? typeId = null;
66      if (idString != null)
67        id = int.Parse(idString);
68        typeId = int.Parse(reader.GetAttribute("typeId"));
69      yield return new BeginToken(name, typeId, id);
70      IEnumerator<ISerializationToken> iterator = GetEnumerator();
71      while (iterator.MoveNext())
72        yield return iterator.Current;
73      yield return new EndToken(name, typeId, id);
74    }
75    private IEnumerator<ISerializationToken> ParseReference() {
76      yield return new ReferenceToken(
77        reader.GetAttribute("name"),
78        int.Parse(reader.GetAttribute("ref")));
79    }
80    private IEnumerator<ISerializationToken> ParseNull() {
81      yield return new NullReferenceToken(reader.GetAttribute("name"));
82    }
83    IEnumerator IEnumerable.GetEnumerator() {
84      return GetEnumerator();
85    }
86    public static Dictionary<string, int> ParseTypeCache(TextReader reader) {
87      Dictionary<string, int> typeCache = new Dictionary<string, int>();
88      XmlReader xmlReader = XmlReader.Create(reader);
89      while ( xmlReader.Read() ) {
90        if (xmlReader.Name == "TYPE") {         
91          typeCache.Add(xmlReader.GetAttribute("name")
92          , int.Parse(xmlReader.GetAttribute("id")));
93        }
94      }
95      return typeCache;
96    }
97  } 
98}
Note: See TracBrowser for help on using the repository browser.