[1454] | 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;
|
---|
[1466] | 8 | using ICSharpCode.SharpZipLib.Zip;
|
---|
[1556] | 9 | using HeuristicLab.Persistence.Core.Tokens;
|
---|
[1454] | 10 |
|
---|
| 11 | namespace HeuristicLab.Persistence.Default.Xml {
|
---|
| 12 |
|
---|
| 13 | public class XmlParser : IEnumerable<ISerializationToken> {
|
---|
| 14 |
|
---|
| 15 | private readonly XmlReader reader;
|
---|
| 16 | private delegate IEnumerator<ISerializationToken> Handler();
|
---|
| 17 | private readonly Dictionary<string, Handler> handlers;
|
---|
| 18 |
|
---|
| 19 | public XmlParser(TextReader input) {
|
---|
| 20 | XmlReaderSettings settings = new XmlReaderSettings {
|
---|
[1566] | 21 | ConformanceLevel = ConformanceLevel.Document,
|
---|
| 22 | IgnoreWhitespace = true,
|
---|
| 23 | IgnoreComments = true
|
---|
| 24 | };
|
---|
[1454] | 25 | reader = XmlReader.Create(input, settings);
|
---|
| 26 | handlers = new Dictionary<string, Handler> {
|
---|
[1612] | 27 | {XmlStringConstants.PRIMITIVE, ParsePrimitive},
|
---|
| 28 | {XmlStringConstants.COMPOSITE, ParseComposite},
|
---|
| 29 | {XmlStringConstants.REFERENCE, ParseReference},
|
---|
| 30 | {XmlStringConstants.NULL, ParseNull},
|
---|
| 31 | {XmlStringConstants.METAINFO, ParseMetaInfo},
|
---|
[1454] | 32 | };
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | public IEnumerator<ISerializationToken> GetEnumerator() {
|
---|
| 36 | while (reader.Read()) {
|
---|
| 37 | if (!reader.IsStartElement()) {
|
---|
| 38 | break;
|
---|
| 39 | }
|
---|
| 40 | IEnumerator<ISerializationToken> iterator;
|
---|
| 41 | try {
|
---|
| 42 | iterator = handlers[reader.Name].Invoke();
|
---|
[1616] | 43 | } catch (KeyNotFoundException) {
|
---|
[1625] | 44 | throw new PersistenceException(String.Format(
|
---|
| 45 | "Invalid XML tag \"{0}\" in persistence file.",
|
---|
[1454] | 46 | reader.Name));
|
---|
| 47 | }
|
---|
| 48 | while (iterator.MoveNext()) {
|
---|
| 49 | yield return iterator.Current;
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | private IEnumerator<ISerializationToken> ParsePrimitive() {
|
---|
| 55 | int? id = null;
|
---|
| 56 | string idString = reader.GetAttribute("id");
|
---|
| 57 | if (idString != null)
|
---|
| 58 | id = int.Parse(idString);
|
---|
[1616] | 59 | string name = reader.GetAttribute("name");
|
---|
| 60 | int typeId = int.Parse(reader.GetAttribute("typeId"));
|
---|
| 61 | XmlReader inner = reader.ReadSubtree();
|
---|
| 62 | inner.Read();
|
---|
| 63 | string xml = inner.ReadInnerXml();
|
---|
[1703] | 64 | inner.Close();
|
---|
[1616] | 65 | yield return new PrimitiveToken(name, typeId, id, new XmlString(xml));
|
---|
[1454] | 66 | }
|
---|
| 67 |
|
---|
| 68 | private IEnumerator<ISerializationToken> ParseComposite() {
|
---|
[1566] | 69 | string name = reader.GetAttribute("name");
|
---|
[1454] | 70 | string idString = reader.GetAttribute("id");
|
---|
| 71 | int? id = null;
|
---|
| 72 | if (idString != null)
|
---|
| 73 | id = int.Parse(idString);
|
---|
[1564] | 74 | int typeId = int.Parse(reader.GetAttribute("typeId"));
|
---|
[1454] | 75 | yield return new BeginToken(name, typeId, id);
|
---|
| 76 | IEnumerator<ISerializationToken> iterator = GetEnumerator();
|
---|
| 77 | while (iterator.MoveNext())
|
---|
| 78 | yield return iterator.Current;
|
---|
| 79 | yield return new EndToken(name, typeId, id);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | private IEnumerator<ISerializationToken> ParseReference() {
|
---|
| 83 | yield return new ReferenceToken(
|
---|
| 84 | reader.GetAttribute("name"),
|
---|
| 85 | int.Parse(reader.GetAttribute("ref")));
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | private IEnumerator<ISerializationToken> ParseNull() {
|
---|
| 89 | yield return new NullReferenceToken(reader.GetAttribute("name"));
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[1553] | 92 | private IEnumerator<ISerializationToken> ParseMetaInfo() {
|
---|
| 93 | yield return new MetaInfoBeginToken();
|
---|
| 94 | IEnumerator<ISerializationToken> iterator = GetEnumerator();
|
---|
| 95 | while (iterator.MoveNext())
|
---|
| 96 | yield return iterator.Current;
|
---|
| 97 | yield return new MetaInfoEndToken();
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[1454] | 100 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
| 101 | return GetEnumerator();
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | public static List<TypeMapping> ParseTypeCache(TextReader reader) {
|
---|
[1625] | 105 | try {
|
---|
| 106 | var typeCache = new List<TypeMapping>();
|
---|
| 107 | XmlReader xmlReader = XmlReader.Create(reader);
|
---|
| 108 | while (xmlReader.Read()) {
|
---|
| 109 | if (xmlReader.Name == XmlStringConstants.TYPE) {
|
---|
| 110 | typeCache.Add(new TypeMapping(
|
---|
| 111 | int.Parse(xmlReader.GetAttribute("id")),
|
---|
| 112 | xmlReader.GetAttribute("typeName"),
|
---|
| 113 | xmlReader.GetAttribute("serializer")));
|
---|
| 114 | }
|
---|
[1454] | 115 | }
|
---|
[1625] | 116 | return typeCache;
|
---|
[1823] | 117 | } catch (PersistenceException) {
|
---|
[1625] | 118 | throw;
|
---|
| 119 | } catch (Exception e) {
|
---|
| 120 | throw new PersistenceException("Unexpected exception during type cache parsing.", e);
|
---|
[1454] | 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[1734] | 124 | public static object Deserialize(string filename) {
|
---|
| 125 | return Deserialize(new ZipFile(filename));
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | public static object Deserialize(Stream stream) {
|
---|
| 129 | return Deserialize(new ZipFile(stream));
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | private static object Deserialize(ZipFile zipFile) {
|
---|
[1779] | 133 | try {
|
---|
[1625] | 134 | Deserializer deSerializer = new Deserializer(
|
---|
| 135 | ParseTypeCache(
|
---|
| 136 | new StreamReader(
|
---|
| 137 | zipFile.GetInputStream(zipFile.GetEntry("typecache.xml")))));
|
---|
| 138 | XmlParser parser = new XmlParser(
|
---|
| 139 | new StreamReader(zipFile.GetInputStream(zipFile.GetEntry("data.xml"))));
|
---|
| 140 | object result = deSerializer.Deserialize(parser);
|
---|
| 141 | zipFile.Close();
|
---|
| 142 | return result;
|
---|
| 143 | } catch (PersistenceException e) {
|
---|
| 144 | throw;
|
---|
| 145 | } catch (Exception e) {
|
---|
| 146 | throw new PersistenceException("Unexpected exception during deserialization", e);
|
---|
| 147 | }
|
---|
[1454] | 148 | }
|
---|
[1566] | 149 | }
|
---|
[1564] | 150 | } |
---|