Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2640: Updated year of copyrights in license headers

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