[3742] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System.Xml;
|
---|
[1454] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System;
|
---|
| 25 | using System.Collections;
|
---|
| 26 | using System.IO;
|
---|
| 27 | using HeuristicLab.Persistence.Core;
|
---|
| 28 | using HeuristicLab.Persistence.Interfaces;
|
---|
[1466] | 29 | using ICSharpCode.SharpZipLib.Zip;
|
---|
[1556] | 30 | using HeuristicLab.Persistence.Core.Tokens;
|
---|
[3005] | 31 | using System.IO.Compression;
|
---|
[1454] | 32 |
|
---|
| 33 | namespace HeuristicLab.Persistence.Default.Xml {
|
---|
| 34 |
|
---|
[3028] | 35 | /// <summary>
|
---|
| 36 | /// Main entry point of persistence loading from XML. Use the static
|
---|
| 37 | /// methods to load from a file or stream.
|
---|
| 38 | /// </summary>
|
---|
[1454] | 39 | public class XmlParser : IEnumerable<ISerializationToken> {
|
---|
| 40 |
|
---|
[3293] | 41 | private readonly XmlTextReader reader;
|
---|
[1454] | 42 | private delegate IEnumerator<ISerializationToken> Handler();
|
---|
| 43 | private readonly Dictionary<string, Handler> handlers;
|
---|
| 44 |
|
---|
[3028] | 45 | /// <summary>
|
---|
| 46 | /// Initializes a new instance of the <see cref="XmlParser"/> class.
|
---|
| 47 | /// </summary>
|
---|
| 48 | /// <param name="input">The input.</param>
|
---|
[1454] | 49 | public XmlParser(TextReader input) {
|
---|
[3293] | 50 | reader = new XmlTextReader(input);
|
---|
| 51 | reader.WhitespaceHandling = WhitespaceHandling.All;
|
---|
| 52 | reader.Normalization = false;
|
---|
[1454] | 53 | handlers = new Dictionary<string, Handler> {
|
---|
[1612] | 54 | {XmlStringConstants.PRIMITIVE, ParsePrimitive},
|
---|
| 55 | {XmlStringConstants.COMPOSITE, ParseComposite},
|
---|
| 56 | {XmlStringConstants.REFERENCE, ParseReference},
|
---|
| 57 | {XmlStringConstants.NULL, ParseNull},
|
---|
| 58 | {XmlStringConstants.METAINFO, ParseMetaInfo},
|
---|
[3005] | 59 | {XmlStringConstants.TYPE, ParseTypeInfo},
|
---|
[1454] | 60 | };
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[3028] | 63 | /// <summary>
|
---|
| 64 | /// Returns an enumerator that iterates through the serialization tokens.
|
---|
| 65 | /// </summary>
|
---|
| 66 | /// <returns>
|
---|
| 67 | /// An that can be used to iterate through the collection of serialization tokens.
|
---|
| 68 | /// </returns>
|
---|
[1454] | 69 | public IEnumerator<ISerializationToken> GetEnumerator() {
|
---|
| 70 | while (reader.Read()) {
|
---|
| 71 | if (!reader.IsStartElement()) {
|
---|
| 72 | break;
|
---|
| 73 | }
|
---|
| 74 | IEnumerator<ISerializationToken> iterator;
|
---|
| 75 | try {
|
---|
| 76 | iterator = handlers[reader.Name].Invoke();
|
---|
[1616] | 77 | } catch (KeyNotFoundException) {
|
---|
[1625] | 78 | throw new PersistenceException(String.Format(
|
---|
| 79 | "Invalid XML tag \"{0}\" in persistence file.",
|
---|
[1454] | 80 | reader.Name));
|
---|
| 81 | }
|
---|
| 82 | while (iterator.MoveNext()) {
|
---|
| 83 | yield return iterator.Current;
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[3028] | 88 | /// <summary>
|
---|
| 89 | /// Returns an enumerator that iterates through the serialization tokens.
|
---|
| 90 | /// </summary>
|
---|
| 91 | /// <returns>
|
---|
| 92 | /// An that can be used to iterate through the collection of serialization tokens.
|
---|
| 93 | /// </returns>
|
---|
| 94 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
| 95 | return GetEnumerator();
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[1454] | 98 | private IEnumerator<ISerializationToken> ParsePrimitive() {
|
---|
| 99 | int? id = null;
|
---|
| 100 | string idString = reader.GetAttribute("id");
|
---|
| 101 | if (idString != null)
|
---|
| 102 | id = int.Parse(idString);
|
---|
[1616] | 103 | string name = reader.GetAttribute("name");
|
---|
| 104 | int typeId = int.Parse(reader.GetAttribute("typeId"));
|
---|
[3005] | 105 | string typeName = reader.GetAttribute("typeName");
|
---|
| 106 | string serializer = reader.GetAttribute("serializer");
|
---|
| 107 | if (typeName != null)
|
---|
| 108 | yield return new TypeToken(typeId, typeName, serializer);
|
---|
[1616] | 109 | XmlReader inner = reader.ReadSubtree();
|
---|
| 110 | inner.Read();
|
---|
| 111 | string xml = inner.ReadInnerXml();
|
---|
[1703] | 112 | inner.Close();
|
---|
[1616] | 113 | yield return new PrimitiveToken(name, typeId, id, new XmlString(xml));
|
---|
[1454] | 114 | }
|
---|
| 115 |
|
---|
| 116 | private IEnumerator<ISerializationToken> ParseComposite() {
|
---|
[1566] | 117 | string name = reader.GetAttribute("name");
|
---|
[1454] | 118 | string idString = reader.GetAttribute("id");
|
---|
| 119 | int? id = null;
|
---|
| 120 | if (idString != null)
|
---|
| 121 | id = int.Parse(idString);
|
---|
[1564] | 122 | int typeId = int.Parse(reader.GetAttribute("typeId"));
|
---|
[3005] | 123 | string typeName = reader.GetAttribute("typeName");
|
---|
| 124 | string serializer = reader.GetAttribute("serializer");
|
---|
| 125 | if (typeName != null)
|
---|
| 126 | yield return new TypeToken(typeId, typeName, serializer);
|
---|
[1454] | 127 | yield return new BeginToken(name, typeId, id);
|
---|
| 128 | IEnumerator<ISerializationToken> iterator = GetEnumerator();
|
---|
| 129 | while (iterator.MoveNext())
|
---|
| 130 | yield return iterator.Current;
|
---|
| 131 | yield return new EndToken(name, typeId, id);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | private IEnumerator<ISerializationToken> ParseReference() {
|
---|
| 135 | yield return new ReferenceToken(
|
---|
| 136 | reader.GetAttribute("name"),
|
---|
| 137 | int.Parse(reader.GetAttribute("ref")));
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | private IEnumerator<ISerializationToken> ParseNull() {
|
---|
| 141 | yield return new NullReferenceToken(reader.GetAttribute("name"));
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[1553] | 144 | private IEnumerator<ISerializationToken> ParseMetaInfo() {
|
---|
| 145 | yield return new MetaInfoBeginToken();
|
---|
| 146 | IEnumerator<ISerializationToken> iterator = GetEnumerator();
|
---|
| 147 | while (iterator.MoveNext())
|
---|
| 148 | yield return iterator.Current;
|
---|
| 149 | yield return new MetaInfoEndToken();
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[3005] | 152 | private IEnumerator<ISerializationToken> ParseTypeInfo() {
|
---|
| 153 | yield return new TypeToken(
|
---|
| 154 | int.Parse(reader.GetAttribute("id")),
|
---|
| 155 | reader.GetAttribute("typeName"),
|
---|
| 156 | reader.GetAttribute("serializer"));
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[3028] | 159 | /// <summary>
|
---|
| 160 | /// Parses the type cache.
|
---|
| 161 | /// </summary>
|
---|
| 162 | /// <param name="reader">The reader.</param>
|
---|
| 163 | /// <returns>A list of type mapping entries.</returns>
|
---|
[1454] | 164 | public static List<TypeMapping> ParseTypeCache(TextReader reader) {
|
---|
[1625] | 165 | try {
|
---|
| 166 | var typeCache = new List<TypeMapping>();
|
---|
| 167 | XmlReader xmlReader = XmlReader.Create(reader);
|
---|
| 168 | while (xmlReader.Read()) {
|
---|
| 169 | if (xmlReader.Name == XmlStringConstants.TYPE) {
|
---|
| 170 | typeCache.Add(new TypeMapping(
|
---|
| 171 | int.Parse(xmlReader.GetAttribute("id")),
|
---|
| 172 | xmlReader.GetAttribute("typeName"),
|
---|
| 173 | xmlReader.GetAttribute("serializer")));
|
---|
| 174 | }
|
---|
[1454] | 175 | }
|
---|
[1625] | 176 | return typeCache;
|
---|
[1823] | 177 | } catch (PersistenceException) {
|
---|
[1625] | 178 | throw;
|
---|
| 179 | } catch (Exception e) {
|
---|
| 180 | throw new PersistenceException("Unexpected exception during type cache parsing.", e);
|
---|
[1454] | 181 | }
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[3028] | 184 | /// <summary>
|
---|
| 185 | /// Deserializes an object from the specified filename.
|
---|
| 186 | /// </summary>
|
---|
| 187 | /// <param name="filename">The filename.</param>
|
---|
| 188 | /// <returns>A fresh object instance</returns>
|
---|
[1734] | 189 | public static object Deserialize(string filename) {
|
---|
[3913] | 190 | TimeSpan start = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime;
|
---|
| 191 | try {
|
---|
| 192 | using (ZipFile file = new ZipFile(filename)) {
|
---|
| 193 | return Deserialize(file);
|
---|
| 194 | }
|
---|
| 195 | } finally {
|
---|
| 196 | TimeSpan end = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime;
|
---|
| 197 | Tracing.Logger.Info(string.Format(
|
---|
| 198 | "deserialization of {0} took {1} seconds",
|
---|
| 199 | filename, (end - start).TotalSeconds));
|
---|
[2874] | 200 | }
|
---|
[1734] | 201 | }
|
---|
| 202 |
|
---|
[3028] | 203 | /// <summary>
|
---|
| 204 | /// Deserializes the specified filename.
|
---|
| 205 | /// </summary>
|
---|
| 206 | /// <typeparam name="T">object type expected from the serialized file</typeparam>
|
---|
| 207 | /// <param name="filename">The filename.</param>
|
---|
| 208 | /// <returns>A fresh object of type T</returns>
|
---|
| 209 | public static T Deserialize<T>(string filename) {
|
---|
| 210 | return (T)Deserialize(filename);
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 |
|
---|
| 214 | /// <summary>
|
---|
| 215 | /// Deserializes an object from the specified stream.
|
---|
| 216 | /// </summary>
|
---|
| 217 | /// <param name="stream">The stream.</param>
|
---|
| 218 | /// <returns>A fresh object instance.</returns>
|
---|
[1734] | 219 | public static object Deserialize(Stream stream) {
|
---|
[3005] | 220 | try {
|
---|
| 221 | using (StreamReader reader = new StreamReader(new GZipStream(stream, CompressionMode.Decompress))) {
|
---|
| 222 | XmlParser parser = new XmlParser(reader);
|
---|
| 223 | Deserializer deserializer = new Deserializer(new TypeMapping[] { });
|
---|
| 224 | return deserializer.Deserialize(parser);
|
---|
| 225 | }
|
---|
| 226 | } catch (PersistenceException) {
|
---|
| 227 | throw;
|
---|
| 228 | } catch (Exception x) {
|
---|
| 229 | throw new PersistenceException("Unexpected exception during deserialization", x);
|
---|
[2874] | 230 | }
|
---|
[1734] | 231 | }
|
---|
| 232 |
|
---|
[3028] | 233 | /// <summary>
|
---|
| 234 | /// Deserializes an object from the specified stream.
|
---|
| 235 | /// </summary>
|
---|
| 236 | /// <typeparam name="T">object type expected from the serialized stream</typeparam>
|
---|
| 237 | /// <param name="stream">The stream.</param>
|
---|
| 238 | /// <returns>A fresh object instance.</returns>
|
---|
| 239 | public static T Deserialize<T>(Stream stream) {
|
---|
| 240 | return (T)Deserialize(stream);
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[1734] | 243 | private static object Deserialize(ZipFile zipFile) {
|
---|
[1779] | 244 | try {
|
---|
[3005] | 245 | ZipEntry typecache = zipFile.GetEntry("typecache.xml");
|
---|
| 246 | if (typecache == null)
|
---|
| 247 | throw new PersistenceException("file does not contain typecache.xml");
|
---|
| 248 | Deserializer deSerializer = new Deserializer(ParseTypeCache(new StreamReader(zipFile.GetInputStream(typecache))));
|
---|
| 249 | ZipEntry data = zipFile.GetEntry("data.xml");
|
---|
| 250 | if (data == null)
|
---|
| 251 | throw new PersistenceException("file does not contain data.xml");
|
---|
[1625] | 252 | XmlParser parser = new XmlParser(
|
---|
[3005] | 253 | new StreamReader(zipFile.GetInputStream(data)));
|
---|
[1625] | 254 | object result = deSerializer.Deserialize(parser);
|
---|
| 255 | zipFile.Close();
|
---|
| 256 | return result;
|
---|
[3005] | 257 | } catch (PersistenceException) {
|
---|
[1625] | 258 | throw;
|
---|
| 259 | } catch (Exception e) {
|
---|
| 260 | throw new PersistenceException("Unexpected exception during deserialization", e);
|
---|
| 261 | }
|
---|
[1454] | 262 | }
|
---|
[1566] | 263 | }
|
---|
[1564] | 264 | } |
---|