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 | using ICSharpCode.SharpZipLib.Zip;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.Persistence.Default.Xml {
|
---|
11 |
|
---|
12 | public class XmlParser : IEnumerable<ISerializationToken> {
|
---|
13 |
|
---|
14 | private readonly XmlReader reader;
|
---|
15 | private delegate IEnumerator<ISerializationToken> Handler();
|
---|
16 | private readonly Dictionary<string, Handler> handlers;
|
---|
17 |
|
---|
18 | public XmlParser(TextReader input) {
|
---|
19 | XmlReaderSettings settings = new XmlReaderSettings {
|
---|
20 | ConformanceLevel = ConformanceLevel.Document,
|
---|
21 | IgnoreWhitespace = true,
|
---|
22 | IgnoreComments = true
|
---|
23 | };
|
---|
24 | reader = XmlReader.Create(input, settings);
|
---|
25 | handlers = new Dictionary<string, Handler> {
|
---|
26 | {XmlStrings.PRIMITIVE, ParsePrimitive},
|
---|
27 | {XmlStrings.COMPOSITE, ParseComposite},
|
---|
28 | {XmlStrings.REFERENCE, ParseReference},
|
---|
29 | {XmlStrings.NULL, ParseNull}
|
---|
30 | };
|
---|
31 | }
|
---|
32 |
|
---|
33 | public IEnumerator<ISerializationToken> GetEnumerator() {
|
---|
34 | while (reader.Read()) {
|
---|
35 | if (!reader.IsStartElement()) {
|
---|
36 | break;
|
---|
37 | }
|
---|
38 | IEnumerator<ISerializationToken> iterator;
|
---|
39 | try {
|
---|
40 | iterator = handlers[reader.Name].Invoke();
|
---|
41 | }
|
---|
42 | catch (KeyNotFoundException) {
|
---|
43 | throw new InvalidOperationException(String.Format(
|
---|
44 | "No handler for XML tag \"{0}\" installed",
|
---|
45 | reader.Name));
|
---|
46 | }
|
---|
47 | while (iterator.MoveNext()) {
|
---|
48 | yield return iterator.Current;
|
---|
49 | }
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | private IEnumerator<ISerializationToken> ParsePrimitive() {
|
---|
54 | int? id = null;
|
---|
55 | string idString = reader.GetAttribute("id");
|
---|
56 | if (idString != null)
|
---|
57 | id = int.Parse(idString);
|
---|
58 | yield return new PrimitiveToken(
|
---|
59 | reader.GetAttribute("name"),
|
---|
60 | int.Parse(reader.GetAttribute("typeId")),
|
---|
61 | reader.ReadString(),
|
---|
62 | id);
|
---|
63 | }
|
---|
64 |
|
---|
65 | private IEnumerator<ISerializationToken> ParseComposite() {
|
---|
66 | string name = reader.GetAttribute("name");
|
---|
67 | string idString = reader.GetAttribute("id");
|
---|
68 | int? id = null;
|
---|
69 | int? typeId = null;
|
---|
70 | if (idString != null)
|
---|
71 | id = int.Parse(idString);
|
---|
72 | typeId = int.Parse(reader.GetAttribute("typeId"));
|
---|
73 | yield return new BeginToken(name, typeId, id);
|
---|
74 | IEnumerator<ISerializationToken> iterator = GetEnumerator();
|
---|
75 | while (iterator.MoveNext())
|
---|
76 | yield return iterator.Current;
|
---|
77 | yield return new EndToken(name, typeId, id);
|
---|
78 | }
|
---|
79 |
|
---|
80 | private IEnumerator<ISerializationToken> ParseReference() {
|
---|
81 | yield return new ReferenceToken(
|
---|
82 | reader.GetAttribute("name"),
|
---|
83 | int.Parse(reader.GetAttribute("ref")));
|
---|
84 | }
|
---|
85 |
|
---|
86 | private IEnumerator<ISerializationToken> ParseNull() {
|
---|
87 | yield return new NullReferenceToken(reader.GetAttribute("name"));
|
---|
88 | }
|
---|
89 |
|
---|
90 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
91 | return GetEnumerator();
|
---|
92 | }
|
---|
93 |
|
---|
94 | public static List<TypeMapping> ParseTypeCache(TextReader reader) {
|
---|
95 | var typeCache = new List<TypeMapping>();
|
---|
96 | XmlReader xmlReader = XmlReader.Create(reader);
|
---|
97 | while ( xmlReader.Read() ) {
|
---|
98 | if (xmlReader.Name == XmlStrings.TYPE) {
|
---|
99 | typeCache.Add(new TypeMapping(
|
---|
100 | int.Parse(xmlReader.GetAttribute("id")),
|
---|
101 | xmlReader.GetAttribute("typeName"),
|
---|
102 | xmlReader.GetAttribute("serializer")));
|
---|
103 | }
|
---|
104 | }
|
---|
105 | return typeCache;
|
---|
106 | }
|
---|
107 |
|
---|
108 | public static object DeSerialize(string filename) {
|
---|
109 | ZipFile zipFile = new ZipFile(filename);
|
---|
110 | DeSerializer deSerializer = new DeSerializer(
|
---|
111 | ParseTypeCache(
|
---|
112 | new StreamReader(
|
---|
113 | zipFile.GetInputStream(zipFile.GetEntry("typecache.xml")))));
|
---|
114 | XmlParser parser = new XmlParser(
|
---|
115 | new StreamReader(zipFile.GetInputStream(zipFile.GetEntry("data.xml"))));
|
---|
116 | return deSerializer.DeSerialize(parser);
|
---|
117 | }
|
---|
118 | }
|
---|
119 | }
|
---|