Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 771 was 750, checked in by gkronber, 16 years ago

implemented #339 (Persist meta-information about required plugins in all documents)
old documents can still be loaded new documents have a list of necessary plugins at the top XML.

File size: 6.6 KB
RevLine 
[2]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;
[402]27using System.IO.Compression;
[750]28using HeuristicLab.PluginInfrastructure;
[2]29
30namespace HeuristicLab.Core {
31  public static class PersistenceManager {
32    public static XmlDocument CreateXmlDocument() {
33      XmlDocument document = new XmlDocument();
34      document.AppendChild(document.CreateXmlDeclaration("1.0", null, null));
35      return document;
36    }
37    public static XmlNode Persist(IStorable instance, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
[150]38      string name = instance.GetType().Name;
39      name = name.Replace('`', '_');
40      return Persist(name, instance, document, persistedObjects);
[2]41    }
42    public static XmlNode Persist(string name, IStorable instance, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
[750]43      if(persistedObjects.ContainsKey(instance.Guid)) {
[2]44        XmlNode node = document.CreateNode(XmlNodeType.Element, name, null);
45        XmlAttribute guidAttribute = document.CreateAttribute("GUID");
46        guidAttribute.Value = instance.Guid.ToString();
47        node.Attributes.Append(guidAttribute);
48        return node;
49      } else {
50        persistedObjects.Add(instance.Guid, instance);
51        XmlNode node = instance.GetXmlNode(name, document, persistedObjects);
52        return node;
53      }
54    }
[750]55    public static IStorable Restore(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
[2]56      Guid guid = new Guid(node.Attributes["GUID"].Value);
[750]57      if(restoredObjects.ContainsKey(guid)) {
[2]58        return restoredObjects[guid];
59      } else {
60        Type type = Type.GetType(node.Attributes["Type"].Value, true);
61        IStorable instance = (IStorable)Activator.CreateInstance(type);
62        restoredObjects.Add(guid, instance);
63        instance.Populate(node, restoredObjects);
64        return instance;
65      }
66    }
67    public static void Save(IStorable instance, string filename) {
[687]68      using(FileStream stream = File.Create(filename)) {
69        Save(instance, stream);
70        stream.Close();
71      }
[2]72    }
73    public static void Save(IStorable instance, Stream stream) {
74      XmlDocument document = PersistenceManager.CreateXmlDocument();
[750]75      Dictionary<Guid, IStorable> dictionary = new Dictionary<Guid, IStorable>();
76      XmlNode rootNode = document.CreateElement("Root");
77      document.AppendChild(rootNode);
78      XmlNode necessaryPluginsNode = document.CreateElement("NecessaryPlugins");
79      rootNode.AppendChild(necessaryPluginsNode);
80      rootNode.AppendChild(Persist(instance, document, dictionary));
81      // determine the list of necessary plugins for this document
82      DiscoveryService service = new DiscoveryService();
83      List<PluginInfo> plugins = new List<PluginInfo>();
84      foreach(IStorable storeable in dictionary.Values) {
85        PluginInfo pluginInfo = service.GetDeclaringPlugin(storeable.GetType());
86        if(!plugins.Contains(pluginInfo)) plugins.Add(pluginInfo);
87      }
88      foreach(PluginInfo uniquePlugin in plugins) {
89        XmlNode necessaryPluginNode = document.CreateElement("Plugin");
90        XmlAttribute nameAttr = document.CreateAttribute("Name");
91        nameAttr.Value = uniquePlugin.Name;
92        XmlAttribute versionAttr = document.CreateAttribute("Version");
93        versionAttr.Value = uniquePlugin.Version.ToString();
94        necessaryPluginNode.Attributes.Append(nameAttr);
95        necessaryPluginNode.Attributes.Append(versionAttr);
96        necessaryPluginsNode.AppendChild(necessaryPluginNode);
97      }
[2]98      document.Save(stream);
99    }
100    public static IStorable Load(string filename) {
[687]101      using(FileStream stream = File.OpenRead(filename)) {
102        IStorable storable = Load(stream);
103        stream.Close();
104        return storable;
105      }
[2]106    }
107    public static IStorable Load(Stream stream) {
108      XmlDocument doc = new XmlDocument();
109      doc.Load(stream);
[750]110      XmlNode rootNode = doc.ChildNodes[1];
111      if(rootNode.Name == "Root" && rootNode.ChildNodes.Count == 2) {
112        // load documents that have a list of necessary plugins at the top
113        return PersistenceManager.Restore(rootNode.ChildNodes[1], new Dictionary<Guid, IStorable>());
114      } else {
115        // compatibility to load documents without list of necessary plugins
116        return PersistenceManager.Restore(rootNode, new Dictionary<Guid, IStorable>());
117      }
[2]118    }
[40]119
[402]120    public static IStorable RestoreFromGZip(byte[] serializedStorable) {
121      GZipStream stream = new GZipStream(new MemoryStream(serializedStorable), CompressionMode.Decompress);
122      return Load(stream);
123    }
124
125    public static byte[] SaveToGZip(IStorable storable) {
126      MemoryStream memStream = new MemoryStream();
127      GZipStream stream = new GZipStream(memStream, CompressionMode.Compress, true);
128      Save(storable, stream);
129      stream.Close();
130      return memStream.ToArray();
131    }
132
[40]133    public static string BuildTypeString(Type type) {
134      string assembly = type.Assembly.FullName;
[267]135      assembly = assembly.Substring(0, assembly.IndexOf(", "));
[40]136
137      StringBuilder builder = new StringBuilder();
138      builder.Append(type.Namespace);
139      builder.Append(".");
140      builder.Append(type.Name);
141      Type[] args = type.GetGenericArguments();
[750]142      if(args.Length > 0) {
[40]143        builder.Append("[[");
144        builder.Append(BuildTypeString(args[0]));
145        builder.Append("]");
[750]146        for(int i = 1; i < args.Length; i++) {
[40]147          builder.Append(",[");
148          builder.Append(BuildTypeString(args[i]));
149          builder.Append("]");
150        }
151        builder.Append("]");
152      }
153      builder.Append(", ");
154      builder.Append(assembly);
155      return builder.ToString();
156    }
[2]157  }
158}
Note: See TracBrowser for help on using the repository browser.