Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1667 was 1667, checked in by epitzer, 15 years ago

Convert persistence of Core plugin to new persistence framework. The target framework has been upgraded from 2.0 to 3.5 and events during persistence are not generated anymore. (#603)

File size: 5.1 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;
30
31namespace HeuristicLab.Core {
32  /// <summary>
33  /// Static class for serializing and deserializing objects.
34  /// </summary>
35  public static class PersistenceManager {
36    /// <summary>
37    /// Saves the specified <paramref name="instance"/> in the specified file through creating an
38    /// <see cref="XmlDocument"/>.
39    /// </summary>
40    /// <param name="instance">The object that should be saved.</param>
41    /// <param name="filename">The name of the file where the <paramref name="object"/> should be saved.</param>
42    public static void Save(IStorable instance, string filename) {
43      XmlGenerator.Serialize(instance, filename);
44    }
45
46    /// <summary>
47    /// Saves the specified <paramref name="instance"/> in the specified <paramref name="stream"/>
48    /// through creating an <see cref="XmlDocument"/>.
49    /// </summary>
50    /// <param name="instance">The object that should be saved.</param>
51    /// <param name="stream">The (file) stream where the object should be saved.</param>
52    public static void Save(IStorable instance, Stream stream) {
53      string tempfile = Path.GetTempFileName();
54      XmlGenerator.Serialize(instance, tempfile);
55      Stream reader = new FileStream(tempfile, FileMode.Open);
56      byte[] buffer = new byte[1024];
57      int bytesRead = 0;
58      do {
59        bytesRead = reader.Read(buffer, 0, buffer.Length);
60        stream.Write(buffer, 0, bytesRead);
61      } while (bytesRead > 0);
62      reader.Close();
63      stream.Close();
64      File.Delete(tempfile);
65    }
66    /// <summary>
67    /// Loads an object from a file with the specified <paramref name="filename"/>.
68    /// </summary>
69    /// <remarks>The object must be saved as an <see cref="XmlDocument"/>. <br/>
70    /// Calls <see cref="Restore"/>.</remarks>
71    /// <param name="filename">The filename of the file where the data is saved.</param>
72    /// <returns>The loaded object.</returns>
73    public static IStorable Load(string filename) {
74      return (IStorable)XmlParser.DeSerialize(filename);
75    }
76    /// <summary>
77    /// Loads an object from the specified <paramref name="stream"/>.
78    /// </summary>
79    /// <remarks>The object must be saved as an <see cref="XmlDocument"/>. <br/>
80    /// Calls <see cref="Restore"/>.</remarks>
81    /// <param name="stream">The stream from where to load the data.</param>
82    /// <returns>The loaded object.</returns>
83    public static IStorable Load(Stream stream) {
84      string tempfile = Path.GetTempFileName();
85      Stream writer = new FileStream(tempfile, FileMode.CreateNew);
86      byte[] buffer = new byte[1024];
87      int bytesRead = 0;
88      do {
89        bytesRead = stream.Read(buffer, 0, buffer.Length);
90        writer.Write(buffer, 0, bytesRead);
91      } while (bytesRead > 0);
92      stream.Close();
93      writer.Close();
94      object o = XmlParser.DeSerialize(tempfile);
95      File.Delete(tempfile);
96      return (IStorable)o;
97    }
98
99    /// <summary>
100    /// Builds a meaningful string for the given <paramref name="type"/> with the namespace information,
101    /// all its arguments, the assembly name...
102    /// </summary>
103    /// <param name="type">The type for which a string should be created.</param>
104    /// <returns>A string value of this type containing different additional information.</returns>
105    public static string BuildTypeString(Type type) {
106      string assembly = type.Assembly.FullName;
107      assembly = assembly.Substring(0, assembly.IndexOf(", "));
108
109      StringBuilder builder = new StringBuilder();
110      builder.Append(type.Namespace);
111      builder.Append(".");
112      builder.Append(type.Name);
113      Type[] args = type.GetGenericArguments();
114      if (args.Length > 0) {
115        builder.Append("[[");
116        builder.Append(BuildTypeString(args[0]));
117        builder.Append("]");
118        for (int i = 1; i < args.Length; i++) {
119          builder.Append(",[");
120          builder.Append(BuildTypeString(args[i]));
121          builder.Append("]");
122        }
123        builder.Append("]");
124      }
125      builder.Append(", ");
126      builder.Append(assembly);
127      return builder.ToString();
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.