Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/PersistenceManager.cs @ 725

Last change on this file since 725 was 687, checked in by gkronber, 16 years ago

fixed #312 (File is not closed when open fails (with an Exception))

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      using(FileStream stream = File.Create(filename)) {
68        Save(instance, stream);
69        stream.Close();
70      }
71    }
72    public static void Save(IStorable instance, Stream stream) {
73      XmlDocument document = PersistenceManager.CreateXmlDocument();
74
75      document.AppendChild(Persist(instance, document, new Dictionary<Guid, IStorable>()));
76      document.Save(stream);
77    }
78    public static IStorable Load(string filename) {
79      using(FileStream stream = File.OpenRead(filename)) {
80        IStorable storable = Load(stream);
81        stream.Close();
82        return storable;
83      }
84    }
85    public static IStorable Load(Stream stream) {
86      XmlDocument doc = new XmlDocument();
87      doc.Load(stream);
88      return PersistenceManager.Restore(doc.ChildNodes[1], new Dictionary<Guid, IStorable>());
89    }
90
91    public static IStorable RestoreFromGZip(byte[] serializedStorable) {
92      GZipStream stream = new GZipStream(new MemoryStream(serializedStorable), CompressionMode.Decompress);
93      return Load(stream);
94    }
95
96    public static byte[] SaveToGZip(IStorable storable) {
97      MemoryStream memStream = new MemoryStream();
98      GZipStream stream = new GZipStream(memStream, CompressionMode.Compress, true);
99      Save(storable, stream);
100      stream.Close();
101      return memStream.ToArray();
102    }
103
104    public static string BuildTypeString(Type type) {
105      string assembly = type.Assembly.FullName;
106      assembly = assembly.Substring(0, assembly.IndexOf(", "));
107
108      StringBuilder builder = new StringBuilder();
109      builder.Append(type.Namespace);
110      builder.Append(".");
111      builder.Append(type.Name);
112      Type[] args = type.GetGenericArguments();
113      if (args.Length > 0) {
114        builder.Append("[[");
115        builder.Append(BuildTypeString(args[0]));
116        builder.Append("]");
117        for (int i = 1; i < args.Length; i++) {
118          builder.Append(",[");
119          builder.Append(BuildTypeString(args[i]));
120          builder.Append("]");
121        }
122        builder.Append("]");
123      }
124      builder.Append(", ");
125      builder.Append(assembly);
126      return builder.ToString();
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.