Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 314 was 267, checked in by gkronber, 16 years ago

using string.substring(0..n) instead of string.split()[0] because it is more light-weight

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