Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs @ 12623

Last change on this file since 12623 was 12009, checked in by ascheibe, 10 years ago

#2212 updated copyright year

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