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