Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2965_CancelablePersistence/HeuristicLab.Persistence/3.3/Default/Xml/XmlParser.cs @ 16321

Last change on this file since 16321 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 10.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using System.IO;
26using System.IO.Compression;
27using System.Xml;
28using HeuristicLab.Persistence.Core;
29using HeuristicLab.Persistence.Core.Tokens;
30using HeuristicLab.Persistence.Interfaces;
31
32namespace HeuristicLab.Persistence.Default.Xml {
33  /// <summary>
34  /// Type of compression used for the Xml stream or file.
35  /// </summary>
36  public enum CompressionType {
37    GZip,
38    Zip
39  }
40
41  /// <summary>
42  /// Main entry point of persistence loading from XML. Use the static
43  /// methods to load from a file or stream.
44  /// </summary>
45  public class XmlParser : IEnumerable<ISerializationToken> {
46
47    private readonly XmlTextReader reader;
48    private delegate IEnumerator<ISerializationToken> Handler();
49    private readonly Dictionary<string, Handler> handlers;
50
51    /// <summary>
52    /// Initializes a new instance of the <see cref="XmlParser"/> class.
53    /// </summary>
54    /// <param name="input">The input.</param>
55    public XmlParser(TextReader input) {
56      reader = new XmlTextReader(input);
57      reader.WhitespaceHandling = WhitespaceHandling.All;
58      reader.Normalization = false;
59      handlers = new Dictionary<string, Handler> {
60                     {XmlStringConstants.PRIMITIVE, ParsePrimitive},
61                     {XmlStringConstants.COMPOSITE, ParseComposite},
62                     {XmlStringConstants.REFERENCE, ParseReference},
63                     {XmlStringConstants.NULL, ParseNull},
64                     {XmlStringConstants.METAINFO, ParseMetaInfo},
65                     {XmlStringConstants.TYPE, ParseTypeInfo},
66                   };
67    }
68
69    /// <summary>
70    /// Returns an enumerator that iterates through the serialization tokens.
71    /// </summary>
72    /// <returns>
73    /// An that can be used to iterate through the collection of serialization tokens.
74    /// </returns>
75    public IEnumerator<ISerializationToken> GetEnumerator() {
76      while (reader.Read()) {
77        if (!reader.IsStartElement()) {
78          break;
79        }
80        IEnumerator<ISerializationToken> iterator;
81        try {
82          iterator = handlers[reader.Name].Invoke();
83        }
84        catch (KeyNotFoundException) {
85          throw new PersistenceException(String.Format(
86            "Invalid XML tag \"{0}\" in persistence file.",
87            reader.Name));
88        }
89        while (iterator.MoveNext()) {
90          yield return iterator.Current;
91        }
92      }
93    }
94
95    /// <summary>
96    /// Returns an enumerator that iterates through the serialization tokens.
97    /// </summary>
98    /// <returns>
99    /// An that can be used to iterate through the collection of serialization tokens.
100    /// </returns>
101    IEnumerator IEnumerable.GetEnumerator() {
102      return GetEnumerator();
103    }
104
105    private IEnumerator<ISerializationToken> ParsePrimitive() {
106      int? id = null;
107      string idString = reader.GetAttribute("id");
108      if (idString != null)
109        id = int.Parse(idString);
110      string name = reader.GetAttribute("name");
111      int typeId = int.Parse(reader.GetAttribute("typeId"));
112      string typeName = reader.GetAttribute("typeName");
113      string serializer = reader.GetAttribute("serializer");
114      if (typeName != null)
115        yield return new TypeToken(typeId, typeName, serializer);
116      XmlReader inner = reader.ReadSubtree();
117      inner.Read();
118      string xml = inner.ReadInnerXml();
119      inner.Close();
120      yield return new PrimitiveToken(name, typeId, id, new XmlString(xml));
121    }
122
123    private IEnumerator<ISerializationToken> ParseComposite() {
124      string name = reader.GetAttribute("name");
125      string idString = reader.GetAttribute("id");
126      int? id = null;
127      if (idString != null)
128        id = int.Parse(idString);
129      int typeId = int.Parse(reader.GetAttribute("typeId"));
130      string typeName = reader.GetAttribute("typeName");
131      string serializer = reader.GetAttribute("serializer");
132      if (typeName != null)
133        yield return new TypeToken(typeId, typeName, serializer);
134      yield return new BeginToken(name, typeId, id);
135      IEnumerator<ISerializationToken> iterator = GetEnumerator();
136      while (iterator.MoveNext())
137        yield return iterator.Current;
138      yield return new EndToken(name, typeId, id);
139    }
140
141    private IEnumerator<ISerializationToken> ParseReference() {
142      yield return new ReferenceToken(
143        reader.GetAttribute("name"),
144        int.Parse(reader.GetAttribute("ref")));
145    }
146
147    private IEnumerator<ISerializationToken> ParseNull() {
148      yield return new NullReferenceToken(reader.GetAttribute("name"));
149    }
150
151    private IEnumerator<ISerializationToken> ParseMetaInfo() {
152      yield return new MetaInfoBeginToken();
153      IEnumerator<ISerializationToken> iterator = GetEnumerator();
154      while (iterator.MoveNext())
155        yield return iterator.Current;
156      yield return new MetaInfoEndToken();
157    }
158
159    private IEnumerator<ISerializationToken> ParseTypeInfo() {
160      yield return new TypeToken(
161        int.Parse(reader.GetAttribute("id")),
162        reader.GetAttribute("typeName"),
163        reader.GetAttribute("serializer"));
164    }
165
166    /// <summary>
167    /// Parses the type cache.
168    /// </summary>
169    /// <param name="reader">The reader.</param>
170    /// <returns>A list of type mapping entries.</returns>
171    public static List<TypeMapping> ParseTypeCache(TextReader reader) {
172      try {
173        var typeCache = new List<TypeMapping>();
174        XmlReader xmlReader = XmlReader.Create(reader);
175        while (xmlReader.Read()) {
176          if (xmlReader.Name == XmlStringConstants.TYPE) {
177            typeCache.Add(new TypeMapping(
178              int.Parse(xmlReader.GetAttribute("id")),
179              xmlReader.GetAttribute("typeName"),
180              xmlReader.GetAttribute("serializer")));
181          }
182        }
183        return typeCache;
184      }
185      catch (PersistenceException) {
186        throw;
187      }
188      catch (Exception e) {
189        throw new PersistenceException("Unexpected exception during type cache parsing.", e);
190      }
191    }
192
193    /// <summary>
194    /// Deserializes an object from the specified filename.
195    /// </summary>
196    /// <param name="filename">The filename.</param>
197    /// <returns>A fresh object instance</returns>
198    public static object Deserialize(string filename) {
199      TimeSpan start = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime;
200      try {
201        using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) {
202          using (ZipArchive zip = new ZipArchive(fs)) {
203            return Deserialize(zip);
204          }
205        }
206      }
207      finally {
208        TimeSpan end = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime;
209        Tracing.Logger.Info(string.Format(
210          "deserialization of {0} took {1} seconds",
211          filename, (end - start).TotalSeconds));
212      }
213    }
214
215    /// <summary>
216    /// Deserializes the specified filename.
217    /// </summary>
218    /// <typeparam name="T">object type expected from the serialized file</typeparam>
219    /// <param name="filename">The filename.</param>
220    /// <returns>A fresh object of type T</returns>
221    public static T Deserialize<T>(string filename) {
222      return (T)Deserialize(filename);
223    }
224
225    /// <summary>
226    /// Deserializes an object from the specified stream.
227    /// </summary>
228    /// <typeparam name="T">object type expected from the serialized stream</typeparam>
229    /// <param name="stream">The stream.</param>
230    /// <param name="compressionType">Type of compression, default is GZip.</param>
231    /// <returns>A fresh object instance.</returns>
232    public static T Deserialize<T>(Stream stream, CompressionType compressionType = CompressionType.GZip) {
233      return (T)Deserialize(stream, compressionType);
234    }
235
236    /// <summary>
237    /// Deserializes an object from the specified stream.
238    /// </summary>
239    /// <param name="stream">The stream.</param>
240    /// <param name="compressionType">Type of compression, default is GZip.</param>
241    /// <returns>A fresh object instance.</returns>
242    public static object Deserialize(Stream stream, CompressionType compressionType = CompressionType.GZip) {
243      if (compressionType == CompressionType.Zip) {
244        ZipArchive zipFile = new ZipArchive(stream);
245        return Deserialize(zipFile);
246      } else {
247        try {
248          using (StreamReader reader = new StreamReader(new GZipStream(stream, CompressionMode.Decompress))) {
249            XmlParser parser = new XmlParser(reader);
250            Deserializer deserializer = new Deserializer(new TypeMapping[] { });
251            return deserializer.Deserialize(parser);
252          }
253        }
254        catch (PersistenceException) {
255          throw;
256        }
257        catch (Exception x) {
258          throw new PersistenceException("Unexpected exception during deserialization", x);
259        }
260      }
261    }
262
263    private static object Deserialize(ZipArchive zipFile) {
264      try {
265        ZipArchiveEntry typecache = zipFile.GetEntry("typecache.xml");
266        if (typecache == null) throw new PersistenceException("file does not contain typecache.xml");
267        Deserializer deSerializer;
268        using (StreamReader sr = new StreamReader(typecache.Open())) {
269          deSerializer = new Deserializer(ParseTypeCache(sr));
270        }
271
272        ZipArchiveEntry data = zipFile.GetEntry("data.xml");
273        if (data == null) throw new PersistenceException("file does not contain data.xml");
274        object result;
275        using (StreamReader sr = new StreamReader(data.Open())) {
276          XmlParser parser = new XmlParser(sr);
277          result = deSerializer.Deserialize(parser);
278        }
279
280        return result;
281      }
282      catch (PersistenceException) {
283        throw;
284      }
285      catch (Exception e) {
286        throw new PersistenceException("Unexpected exception during deserialization", e);
287      }
288    }
289  }
290}
Note: See TracBrowser for help on using the repository browser.