Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs @ 16565

Last change on this file since 16565 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 19.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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.Generic;
24using System.IO;
25using System.IO.Compression;
26using System.Linq;
27using System.Text;
28using System.Threading;
29using HEAL.Attic;
30using HeuristicLab.Persistence.Core;
31using HeuristicLab.Persistence.Core.Tokens;
32using HeuristicLab.Persistence.Interfaces;
33using HeuristicLab.Tracing;
34
35namespace HeuristicLab.Persistence.Default.Xml {
36
37
38  /// <summary>
39  /// Main entry point of persistence to XML. Use the static methods to serialize
40  /// to a file or to a stream.
41  /// </summary>
42  public class XmlGenerator : GeneratorBase<string> {
43
44    protected int depth;
45    protected int Depth {
46      get {
47        return depth;
48      }
49      set {
50        depth = value;
51        prefix = new string(' ', depth * 2);
52      }
53    }
54
55    protected string prefix;
56
57
58    /// <summary>
59    /// Initializes a new instance of the <see cref="XmlGenerator"/> class.
60    /// </summary>
61    public XmlGenerator() {
62      Depth = 0;
63    }
64
65    protected enum NodeType { Start, End, Inline };
66
67    protected static void AddXmlTagContent(StringBuilder sb, string name, Dictionary<string, string> attributes) {
68      sb.Append(name);
69      foreach (var attribute in attributes) {
70        if (attribute.Value != null && !string.IsNullOrEmpty(attribute.Value.ToString())) {
71          sb.Append(' ');
72          sb.Append(attribute.Key);
73          sb.Append("=\"");
74          sb.Append(attribute.Value);
75          sb.Append('"');
76        }
77      }
78    }
79
80    protected static int AttributeLength(Dictionary<string, string> attributes) {
81      return attributes
82        .Where(kvp => !string.IsNullOrEmpty(kvp.Key) && !string.IsNullOrEmpty(kvp.Value))
83        .Select(kvp => kvp.Key.Length + kvp.Value.Length + 4).Sum();
84    }
85
86    protected static void AddXmlStartTag(StringBuilder sb, string name, Dictionary<string, string> attributes) {
87      sb.Append('<');
88      AddXmlTagContent(sb, name, attributes);
89      sb.Append('>');
90    }
91
92    protected static void AddXmlInlineTag(StringBuilder sb, string name, Dictionary<string, string> attributes) {
93      sb.Append('<');
94      AddXmlTagContent(sb, name, attributes);
95      sb.Append("/>");
96    }
97
98    protected static void AddXmlEndTag(StringBuilder sb, string name) {
99      sb.Append("</");
100      sb.Append(name);
101      sb.Append(">");
102    }
103
104    protected string CreateNodeStart(string name, Dictionary<string, string> attributes) {
105      StringBuilder sb = new StringBuilder(prefix.Length + name.Length + 4
106        + AttributeLength(attributes));
107      sb.Append(prefix);
108      Depth += 1;
109      AddXmlStartTag(sb, name, attributes);
110      sb.Append("\r\n");
111      return sb.ToString();
112    }
113
114    private static Dictionary<string, string> emptyDict = new Dictionary<string, string>();
115    protected string CreateNodeStart(string name) {
116      return CreateNodeStart(name, emptyDict);
117    }
118
119    protected string CreateNodeEnd(string name) {
120      Depth -= 1;
121      StringBuilder sb = new StringBuilder(prefix.Length + name.Length + 5);
122      sb.Append(prefix);
123      AddXmlEndTag(sb, name);
124      sb.Append("\r\n");
125      return sb.ToString();
126    }
127
128    protected string CreateNode(string name, Dictionary<string, string> attributes) {
129      StringBuilder sb = new StringBuilder(prefix.Length + name.Length + 5
130        + AttributeLength(attributes));
131      sb.Append(prefix);
132      AddXmlInlineTag(sb, name, attributes);
133      sb.Append("\r\n");
134      return sb.ToString();
135    }
136
137    protected string CreateNode(string name, Dictionary<string, string> attributes, string content) {
138      StringBuilder sb = new StringBuilder(
139        prefix.Length + name.Length + AttributeLength(attributes) + 2
140        + content.Length + name.Length + 5);
141      sb.Append(prefix);
142      AddXmlStartTag(sb, name, attributes);
143      sb.Append(content);
144      sb.Append("</").Append(name).Append(">\r\n");
145      return sb.ToString();
146    }
147
148    /// <summary>
149    /// Formats the specified begin token.
150    /// </summary>
151    /// <param name="beginToken">The begin token.</param>
152    /// <returns>The token in serialized form.</returns>
153    protected override string Format(BeginToken beginToken) {
154      var dict = new Dictionary<string, string> {
155          {"name", beginToken.Name},
156          {"typeId", beginToken.TypeId.ToString()},
157          {"id", beginToken.Id.ToString()}};
158      AddTypeInfo(beginToken.TypeId, dict);
159      return CreateNodeStart(XmlStringConstants.COMPOSITE, dict);
160
161    }
162
163    protected void AddTypeInfo(int typeId, Dictionary<string, string> dict) {
164      if (lastTypeToken != null) {
165        if (typeId == lastTypeToken.Id) {
166          dict.Add("typeName", lastTypeToken.TypeName);
167          dict.Add("serializer", lastTypeToken.Serializer);
168          lastTypeToken = null;
169        } else {
170          FlushTypeToken();
171        }
172      }
173    }
174
175    /// <summary>
176    /// Formats the specified end token.
177    /// </summary>
178    /// <param name="endToken">The end token.</param>
179    /// <returns>The token in serialized form.</returns>
180    protected override string Format(EndToken endToken) {
181      return CreateNodeEnd(XmlStringConstants.COMPOSITE);
182    }
183
184    /// <summary>
185    /// Formats the specified data token.
186    /// </summary>
187    /// <param name="dataToken">The data token.</param>
188    /// <returns>The token in serialized form.</returns>
189    protected override string Format(PrimitiveToken dataToken) {
190      var dict = new Dictionary<string, string> {
191            {"typeId", dataToken.TypeId.ToString()},
192            {"name", dataToken.Name},
193            {"id", dataToken.Id.ToString()}};
194      AddTypeInfo(dataToken.TypeId, dict);
195      return CreateNode(XmlStringConstants.PRIMITIVE, dict,
196        ((XmlString)dataToken.SerialData).Data);
197    }
198
199    /// <summary>
200    /// Formats the specified ref token.
201    /// </summary>
202    /// <param name="refToken">The ref token.</param>
203    /// <returns>The token in serialized form.</returns>
204    protected override string Format(ReferenceToken refToken) {
205      return CreateNode(XmlStringConstants.REFERENCE,
206        new Dictionary<string, string> {
207          {"ref", refToken.Id.ToString()},
208          {"name", refToken.Name}});
209    }
210
211    /// <summary>
212    /// Formats the specified null ref token.
213    /// </summary>
214    /// <param name="nullRefToken">The null ref token.</param>
215    /// <returns>The token in serialized form.</returns>
216    protected override string Format(NullReferenceToken nullRefToken) {
217      return CreateNode(XmlStringConstants.NULL,
218        new Dictionary<string, string>{
219          {"name", nullRefToken.Name}});
220    }
221
222    /// <summary>
223    /// Formats the specified meta info begin token.
224    /// </summary>
225    /// <param name="metaInfoBeginToken">The meta info begin token.</param>
226    /// <returns>The token in serialized form.</returns>
227    protected override string Format(MetaInfoBeginToken metaInfoBeginToken) {
228      return CreateNodeStart(XmlStringConstants.METAINFO);
229    }
230
231    /// <summary>
232    /// Formats the specified meta info end token.
233    /// </summary>
234    /// <param name="metaInfoEndToken">The meta info end token.</param>
235    /// <returns>The token in serialized form.</returns>
236    protected override string Format(MetaInfoEndToken metaInfoEndToken) {
237      return CreateNodeEnd(XmlStringConstants.METAINFO);
238    }
239
240    protected TypeToken lastTypeToken;
241    /// <summary>
242    /// Formats the specified token.
243    /// </summary>
244    /// <param name="token">The token.</param>
245    /// <returns>The token in serialized form.</returns>
246    protected override string Format(TypeToken token) {
247      lastTypeToken = token;
248      return "";
249    }
250
251    protected string FlushTypeToken() {
252      if (lastTypeToken == null)
253        return "";
254      try {
255        return CreateNode(XmlStringConstants.TYPE,
256          new Dictionary<string, string> {
257          {"id", lastTypeToken.Id.ToString()},
258          {"typeName", lastTypeToken.TypeName },
259          {"serializer", lastTypeToken.Serializer }});
260      } finally {
261        lastTypeToken = null;
262      }
263    }
264
265    /// <summary>
266    /// Formats the specified type cache.
267    /// </summary>
268    /// <param name="typeCache">The type cache.</param>
269    /// <returns>An enumerable of formatted type cache tags.</returns>
270    public IEnumerable<string> Format(List<TypeMapping> typeCache) {
271      yield return CreateNodeStart(XmlStringConstants.TYPECACHE);
272      foreach (var mapping in typeCache)
273        yield return CreateNode(
274          XmlStringConstants.TYPE,
275          mapping.GetDict());
276      yield return CreateNodeEnd(XmlStringConstants.TYPECACHE);
277    }
278
279    /// <summary>
280    /// Serialize an object into a file.
281    /// The XML configuration is obtained from the <c>ConfigurationService</c>.
282    /// The file is actually a ZIP file.
283    /// Compression level is set to 5 and needed assemblies are not included.
284    /// </summary>
285    /// <param name="o">The object.</param>
286    /// <param name="filename">The filename.</param>
287    public static void Serialize(object o, string filename, CancellationToken cancellationToken = default(CancellationToken)) {
288      Serialize(o, filename, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), false, CompressionLevel.Optimal, cancellationToken);
289    }
290
291    /// <summary>
292    /// Serialize an object into a file.
293    /// The XML configuration is obtained from the <c>ConfigurationService</c>.
294    /// Needed assemblies are not included.
295    /// </summary>
296    /// <param name="o">The object.</param>
297    /// <param name="filename">The filename.</param>
298    /// <param name="compression">ZIP file compression level</param>
299    public static void Serialize(object o, string filename, CompressionLevel compression, CancellationToken cancellationToken = default(CancellationToken)) {
300      Serialize(o, filename, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), false, compression, cancellationToken);
301    }
302
303    /// <summary>
304    /// Serializes the specified object into a file.
305    /// Needed assemblies are not included, ZIP compression level is set to 5.
306    /// </summary>
307    /// <param name="obj">The object.</param>
308    /// <param name="filename">The filename.</param>
309    /// <param name="config">The configuration.</param>
310    public static void Serialize(object obj, string filename, Configuration config, CancellationToken cancellationToken = default(CancellationToken)) {
311      Serialize(obj, filename, config, false, CompressionLevel.Optimal, cancellationToken);
312    }
313
314    private static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, CompressionLevel compression, CancellationToken cancellationToken = default(CancellationToken)) {
315      Core.Serializer serializer = new Core.Serializer(obj, config);
316      Serialize(stream, includeAssemblies, compression, serializer, cancellationToken);
317    }
318
319    private static void Serialize(Stream stream, bool includeAssemblies, CompressionLevel compression, Core.Serializer serializer, CancellationToken cancellationToken = default(CancellationToken)) {
320      try {
321        cancellationToken.ThrowIfCancellationRequested();
322        DateTime start = DateTime.Now;
323        serializer.InterleaveTypeInformation = false;
324        XmlGenerator generator = new XmlGenerator();
325        using (ZipArchive zipArchive = new ZipArchive(stream, ZipArchiveMode.Create)) {
326          ZipArchiveEntry entry = zipArchive.CreateEntry("data.xml", compression);
327          using (StreamWriter writer = new StreamWriter(entry.Open())) {
328            foreach (ISerializationToken token in serializer) {
329              cancellationToken.ThrowIfCancellationRequested();
330              string line = generator.Format(token);
331              writer.Write(line);
332            }
333          }
334          entry = zipArchive.CreateEntry("typecache.xml", compression);
335          using (StreamWriter writer = new StreamWriter(entry.Open())) {
336            foreach (string line in generator.Format(serializer.TypeCache)) {
337              writer.Write(line);
338            }
339          }
340          if (includeAssemblies) {
341            foreach (string name in serializer.RequiredFiles) {
342              cancellationToken.ThrowIfCancellationRequested();
343              Uri uri = new Uri(name);
344              if (!uri.IsFile) {
345                Logger.Warn("cannot read non-local files");
346                continue;
347              }
348              entry = zipArchive.CreateEntry(Path.GetFileName(uri.PathAndQuery), compression);
349              using (BinaryWriter bw = new BinaryWriter(entry.Open())) {
350                using (FileStream reader = File.OpenRead(uri.PathAndQuery)) {
351                  byte[] buffer = new byte[1024 * 1024];
352                  while (true) {
353                    cancellationToken.ThrowIfCancellationRequested();
354                    int bytesRead = reader.Read(buffer, 0, 1024 * 1024);
355                    if (bytesRead == 0)
356                      break;
357                    bw.Write(buffer, 0, bytesRead);
358                  }
359                }
360              }
361            }
362          }
363        }
364        Logger.Info(String.Format("serialization took {0} seconds with compression level {1}",
365          (DateTime.Now - start).TotalSeconds, compression));
366      } catch (Exception) {
367        Logger.Warn("Exception caught, no data has been serialized.");
368        throw;
369      }
370    }
371
372    /// <summary>
373    /// Serializes the specified object into a file.
374    /// </summary>
375    /// <param name="obj">The object.</param>
376    /// <param name="filename">The filename.</param>
377    /// <param name="config">The configuration.</param>
378    /// <param name="includeAssemblies">if set to <c>true</c> include needed assemblies.</param>
379    /// <param name="compression">The ZIP compression level.</param>
380    public static void Serialize(object obj, string filename, Configuration config, bool includeAssemblies, CompressionLevel compression, CancellationToken cancellationToken = default(CancellationToken)) {
381      string tempfile = null;
382      try {
383        tempfile = Path.GetTempFileName();
384
385        using (FileStream stream = File.Create(tempfile)) {
386          Serialize(obj, stream, config, includeAssemblies, compression, cancellationToken);
387        }
388        // copy only if needed
389        File.Copy(tempfile, filename, true);
390      } catch (Exception) {
391        Logger.Warn("Exception caught, no data has been written.");
392        throw;
393      } finally {
394        if (tempfile != null && File.Exists(tempfile))
395          File.Delete(tempfile);
396      }
397    }
398
399    /// <summary>
400    /// Serializes the specified object into a stream using the <see cref="XmlFormat"/>
401    /// obtained from the <see cref="ConfigurationService"/>.
402    /// </summary>
403    /// <param name="obj">The object.</param>
404    /// <param name="stream">The stream.</param>
405    /// <param name="compressionType">Type of compression, default is GZip.</param>
406    public static void Serialize(object obj, Stream stream, CompressionType compressionType = CompressionType.GZip, CancellationToken cancellationToken = default(CancellationToken)) {
407      Serialize(obj, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), compressionType, cancellationToken);
408    }
409
410
411    /// <summary>
412    /// Serializes the specified object into a stream.
413    /// </summary>
414    /// <param name="obj">The object.</param>
415    /// <param name="stream">The stream.</param>
416    /// <param name="config">The configuration.</param>
417    /// <param name="compressionType">Type of compression, default is GZip.</param>
418    public static void Serialize(object obj, Stream stream, Configuration config, CompressionType compressionType = CompressionType.GZip, CancellationToken cancellationToken = default(CancellationToken)) {
419      Serialize(obj, stream, config, false, compressionType, cancellationToken);
420    }
421
422    /// <summary>
423    /// Serializes the specified object into a stream.
424    /// </summary>
425    /// <param name="obj">The object.</param>
426    /// <param name="stream">The stream.</param>
427    /// <param name="config">The configuration.</param>
428    /// <param name="includeAssemblies">if set to <c>true</c> include need assemblies.</param>
429    /// <param name="compressionType">Type of compression, default is GZip.</param>
430    public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, CompressionType compressionType = CompressionType.GZip, CancellationToken cancellationToken = default(CancellationToken)) {
431      try {
432        Core.Serializer serializer = new Core.Serializer(obj, config);
433        if (compressionType == CompressionType.Zip) {
434          Serialize(obj, stream, config, includeAssemblies, CompressionLevel.Optimal, cancellationToken);
435        } else {
436          Serialize(stream, serializer, cancellationToken);
437        }
438      } catch (PersistenceException) {
439        throw;
440      } catch (Exception e) {
441        throw new PersistenceException("Unexpected exception during Serialization.", e);
442      }
443    }
444
445    /// <summary>
446    /// Serializes the specified object into a stream.
447    /// </summary>
448    /// <param name="obj">The object.</param>
449    /// <param name="stream">The stream.</param>
450    /// <param name="config">The configuration.</param>
451    /// <param name="includeAssemblies">if set to <c>true</c> include need assemblies.</param>
452    /// <param name="types">The list of all serialized types.</param>
453    /// <param name="compressionType">Type of compression, default is GZip.</param>
454    public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, out IEnumerable<Type> types,
455                                 CompressionType compressionType = CompressionType.GZip, CancellationToken cancellationToken = default(CancellationToken)) {
456      try {
457        Core.Serializer serializer = new Core.Serializer(obj, config);
458        if (compressionType == CompressionType.Zip) {
459          Serialize(stream, includeAssemblies, CompressionLevel.Optimal, serializer, cancellationToken);
460        } else {
461          Serialize(stream, serializer, cancellationToken);
462        }
463        types = serializer.SerializedTypes;
464      } catch (PersistenceException) {
465        throw;
466      } catch (Exception e) {
467        throw new PersistenceException("Unexpected exception during Serialization.", e);
468      }
469    }
470
471    private static void Serialize(Stream stream, Core.Serializer serializer, CancellationToken cancellationToken = default(CancellationToken)) {
472      cancellationToken.ThrowIfCancellationRequested();
473      using (StreamWriter writer = new StreamWriter(new GZipStream(stream, CompressionMode.Compress))) {
474        serializer.InterleaveTypeInformation = true;
475        XmlGenerator generator = new XmlGenerator();
476        foreach (ISerializationToken token in serializer) {
477          cancellationToken.ThrowIfCancellationRequested();
478          string line = generator.Format(token);
479          writer.Write(line);
480        }
481        writer.Flush();
482      }
483    }
484  }
485}
Note: See TracBrowser for help on using the repository browser.