Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.Core/PersistenceManager.cs @ 9674

Last change on this file since 9674 was 402, checked in by gkronber, 16 years ago

moved GZip persistence into the PersistenceManager in HeuristicLab.Core because compression is needed in several plugins (CEDMA and Grid)

File size: 4.9 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;
28
29namespace HeuristicLab.Core {
30  public static class PersistenceManager {
31    public static XmlDocument CreateXmlDocument() {
32      XmlDocument document = new XmlDocument();
33      document.AppendChild(document.CreateXmlDeclaration("1.0", null, null));
34      return document;
35    }
36    public static XmlNode Persist(IStorable instance, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
37      string name = instance.GetType().Name;
38      name = name.Replace('`', '_');
39      return Persist(name, instance, document, persistedObjects);
40    }
41    public static XmlNode Persist(string name, IStorable instance, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
42      if (persistedObjects.ContainsKey(instance.Guid)) {
43        XmlNode node = document.CreateNode(XmlNodeType.Element, name, null);
44        XmlAttribute guidAttribute = document.CreateAttribute("GUID");
45        guidAttribute.Value = instance.Guid.ToString();
46        node.Attributes.Append(guidAttribute);
47        return node;
48      } else {
49        persistedObjects.Add(instance.Guid, instance);
50        XmlNode node = instance.GetXmlNode(name, document, persistedObjects);
51        return node;
52      }
53    }
54    public static IStorable Restore(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
55      Guid guid = new Guid(node.Attributes["GUID"].Value);
56      if (restoredObjects.ContainsKey(guid)) {
57        return restoredObjects[guid];
58      } else {
59        Type type = Type.GetType(node.Attributes["Type"].Value, true);
60        IStorable instance = (IStorable)Activator.CreateInstance(type);
61        restoredObjects.Add(guid, instance);
62        instance.Populate(node, restoredObjects);
63        return instance;
64      }
65    }
66    public static void Save(IStorable instance, string filename) {
67      FileStream stream = File.Create(filename);
68      Save(instance, stream);
69      stream.Close();
70    }
71    public static void Save(IStorable instance, Stream stream) {
72      XmlDocument document = PersistenceManager.CreateXmlDocument();
73
74      document.AppendChild(Persist(instance, document, new Dictionary<Guid, IStorable>()));
75      document.Save(stream);
76    }
77    public static IStorable Load(string filename) {
78      FileStream stream = File.OpenRead(filename);
79      IStorable storable = Load(stream);
80      stream.Close();
81      return storable;
82    }
83    public static IStorable Load(Stream stream) {
84      XmlDocument doc = new XmlDocument();
85      doc.Load(stream);
86      return PersistenceManager.Restore(doc.ChildNodes[1], new Dictionary<Guid, IStorable>());
87    }
88
89    public static IStorable RestoreFromGZip(byte[] serializedStorable) {
90      GZipStream stream = new GZipStream(new MemoryStream(serializedStorable), CompressionMode.Decompress);
91      return Load(stream);
92    }
93
94    public static byte[] SaveToGZip(IStorable storable) {
95      MemoryStream memStream = new MemoryStream();
96      GZipStream stream = new GZipStream(memStream, CompressionMode.Compress, true);
97      Save(storable, stream);
98      stream.Close();
99      return memStream.ToArray();
100    }
101
102    public static string BuildTypeString(Type type) {
103      string assembly = type.Assembly.FullName;
104      assembly = assembly.Substring(0, assembly.IndexOf(", "));
105
106      StringBuilder builder = new StringBuilder();
107      builder.Append(type.Namespace);
108      builder.Append(".");
109      builder.Append(type.Name);
110      Type[] args = type.GetGenericArguments();
111      if (args.Length > 0) {
112        builder.Append("[[");
113        builder.Append(BuildTypeString(args[0]));
114        builder.Append("]");
115        for (int i = 1; i < args.Length; i++) {
116          builder.Append(",[");
117          builder.Append(BuildTypeString(args[i]));
118          builder.Append("]");
119        }
120        builder.Append("]");
121      }
122      builder.Append(", ");
123      builder.Append(assembly);
124      return builder.ToString();
125    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.