1 | using System.Xml;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System;
|
---|
4 | using System.Collections;
|
---|
5 | using System.IO;
|
---|
6 | using HeuristicLab.Persistence.Core;
|
---|
7 | using HeuristicLab.Persistence.Interfaces;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Persistence.Default.Xml {
|
---|
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 | {XmlStrings.PRIMITIVE, ParsePrimitive},
|
---|
26 | {XmlStrings.COMPOSITE, ParseComposite},
|
---|
27 | {XmlStrings.REFERENCE, ParseReference},
|
---|
28 | {XmlStrings.NULL, ParseNull}
|
---|
29 | };
|
---|
30 | }
|
---|
31 |
|
---|
32 | public IEnumerator<ISerializationToken> GetEnumerator() {
|
---|
33 | while (reader.Read()) {
|
---|
34 | if (!reader.IsStartElement()) {
|
---|
35 | break;
|
---|
36 | }
|
---|
37 | IEnumerator<ISerializationToken> iterator;
|
---|
38 | try {
|
---|
39 | iterator = handlers[reader.Name].Invoke();
|
---|
40 | }
|
---|
41 | catch (KeyNotFoundException) {
|
---|
42 | throw new InvalidOperationException(String.Format(
|
---|
43 | "No handler for XML tag \"{0}\" installed",
|
---|
44 | reader.Name));
|
---|
45 | }
|
---|
46 | while (iterator.MoveNext()) {
|
---|
47 | yield return iterator.Current;
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | private IEnumerator<ISerializationToken> ParsePrimitive() {
|
---|
53 | int? id = null;
|
---|
54 | string idString = reader.GetAttribute("id");
|
---|
55 | if (idString != null)
|
---|
56 | id = int.Parse(idString);
|
---|
57 | yield return new PrimitiveToken(
|
---|
58 | reader.GetAttribute("name"),
|
---|
59 | int.Parse(reader.GetAttribute("typeId")),
|
---|
60 | reader.ReadString(),
|
---|
61 | id);
|
---|
62 | }
|
---|
63 |
|
---|
64 | private IEnumerator<ISerializationToken> ParseComposite() {
|
---|
65 | string name = reader.GetAttribute("name");
|
---|
66 | string idString = reader.GetAttribute("id");
|
---|
67 | int? id = null;
|
---|
68 | int? typeId = null;
|
---|
69 | if (idString != null)
|
---|
70 | id = int.Parse(idString);
|
---|
71 | typeId = int.Parse(reader.GetAttribute("typeId"));
|
---|
72 | yield return new BeginToken(name, typeId, id);
|
---|
73 | IEnumerator<ISerializationToken> iterator = GetEnumerator();
|
---|
74 | while (iterator.MoveNext())
|
---|
75 | yield return iterator.Current;
|
---|
76 | yield return new EndToken(name, typeId, id);
|
---|
77 | }
|
---|
78 |
|
---|
79 | private IEnumerator<ISerializationToken> ParseReference() {
|
---|
80 | yield return new ReferenceToken(
|
---|
81 | reader.GetAttribute("name"),
|
---|
82 | int.Parse(reader.GetAttribute("ref")));
|
---|
83 | }
|
---|
84 |
|
---|
85 | private IEnumerator<ISerializationToken> ParseNull() {
|
---|
86 | yield return new NullReferenceToken(reader.GetAttribute("name"));
|
---|
87 | }
|
---|
88 |
|
---|
89 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
90 | return GetEnumerator();
|
---|
91 | }
|
---|
92 |
|
---|
93 | public static List<TypeMapping> ParseTypeCache(TextReader reader) {
|
---|
94 | var typeCache = new List<TypeMapping>();
|
---|
95 | XmlReader xmlReader = XmlReader.Create(reader);
|
---|
96 | while ( xmlReader.Read() ) {
|
---|
97 | if (xmlReader.Name == XmlStrings.TYPE) {
|
---|
98 | typeCache.Add(new TypeMapping(
|
---|
99 | int.Parse(xmlReader.GetAttribute("id")),
|
---|
100 | xmlReader.GetAttribute("typeName"),
|
---|
101 | xmlReader.GetAttribute("serializer")));
|
---|
102 | }
|
---|
103 | }
|
---|
104 | return typeCache;
|
---|
105 | }
|
---|
106 |
|
---|
107 | public static object DeSerialize(string basename) {
|
---|
108 | DeSerializer deSerializer = new DeSerializer(
|
---|
109 | ParseTypeCache(new StreamReader(basename + "-types.xml")));
|
---|
110 | XmlParser parser = new XmlParser(new StreamReader(basename + ".xml"));
|
---|
111 | return deSerializer.DeSerialize(parser);
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|