Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/PersistenceManager.cs @ 2321

Last change on this file since 2321 was 1895, checked in by epitzer, 16 years ago

Add SaveCompressed() method to PersistenceManager. (#646)

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Text;
25using System.Xml;
26using System.IO;
27using System.IO.Compression;
28using HeuristicLab.PluginInfrastructure;
29using HeuristicLab.Persistence.Default.Xml;
30using HeuristicLab.Persistence.Core;
31
32namespace HeuristicLab.Core {
33  /// <summary>
34  /// Static class for serializing and deserializing objects.
35  /// </summary>
36  public static class PersistenceManager {
37    /// <summary>
38    /// Saves the specified <paramref name="instance"/> in the specified file through creating an
39    /// <see cref="XmlDocument"/>.
40    /// </summary>
41    /// <param name="instance">The object that should be saved.</param>
42    /// <param name="filename">The name of the file where the <paramref name="object"/> should be saved.</param>
43    public static void Save(IStorable instance, string filename) {
44      XmlGenerator.Serialize(instance, filename, 0);
45    }
46
47    public static void SaveCompressed(IStorable instance, string filename) {
48      XmlGenerator.Serialize(instance, filename, 9);
49    }
50
51    /// <summary>
52    /// Saves the specified <paramref name="instance"/> in the specified <paramref name="stream"/>
53    /// through creating an <see cref="XmlDocument"/>.
54    /// </summary>
55    /// <param name="instance">The object that should be saved.</param>
56    /// <param name="stream">The (file) stream where the object should be saved.</param>
57    public static void Save(IStorable instance, Stream stream) {
58      XmlGenerator.Serialize(instance, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat()));     
59    }
60    /// <summary>
61    /// Loads an object from a file with the specified <paramref name="filename"/>.
62    /// </summary>
63    /// <remarks>The object must be saved as an <see cref="XmlDocument"/>. <br/>
64    /// Calls <see cref="Restore"/>.</remarks>
65    /// <param name="filename">The filename of the file where the data is saved.</param>
66    /// <returns>The loaded object.</returns>
67    public static IStorable Load(string filename) {
68      return (IStorable)XmlParser.Deserialize(filename);
69    }
70    /// <summary>
71    /// Loads an object from the specified <paramref name="stream"/>.
72    /// </summary>
73    /// <remarks>The object must be saved as an <see cref="XmlDocument"/>. <br/>
74    /// Calls <see cref="Restore"/>.</remarks>
75    /// <param name="stream">The stream from where to load the data.</param>
76    /// <returns>The loaded object.</returns>
77    public static IStorable Load(Stream stream) {
78      return (IStorable)XmlParser.Deserialize(stream);
79    }
80
81    /// <summary>
82    /// Builds a meaningful string for the given <paramref name="type"/> with the namespace information,
83    /// all its arguments, the assembly name...
84    /// </summary>
85    /// <param name="type">The type for which a string should be created.</param>
86    /// <returns>A string value of this type containing different additional information.</returns>
87    public static string BuildTypeString(Type type) {
88      string assembly = type.Assembly.FullName;
89      assembly = assembly.Substring(0, assembly.IndexOf(", "));
90
91      StringBuilder builder = new StringBuilder();
92      builder.Append(type.Namespace);
93      builder.Append(".");
94      builder.Append(type.Name);
95      Type[] args = type.GetGenericArguments();
96      if (args.Length > 0) {
97        builder.Append("[[");
98        builder.Append(BuildTypeString(args[0]));
99        builder.Append("]");
100        for (int i = 1; i < args.Length; i++) {
101          builder.Append(",[");
102          builder.Append(BuildTypeString(args[i]));
103          builder.Append("]");
104        }
105        builder.Append("]");
106      }
107      builder.Append(", ");
108      builder.Append(assembly);
109      return builder.ToString();
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.