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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using System.Xml;
|
---|
26 | using System.IO;
|
---|
27 |
|
---|
28 | namespace 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 | return Persist(instance.GetType().Name, instance, document, persistedObjects);
|
---|
37 | }
|
---|
38 | public static XmlNode Persist(string name, IStorable instance, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
39 | if (persistedObjects.ContainsKey(instance.Guid)) {
|
---|
40 | XmlNode node = document.CreateNode(XmlNodeType.Element, name, null);
|
---|
41 | XmlAttribute guidAttribute = document.CreateAttribute("GUID");
|
---|
42 | guidAttribute.Value = instance.Guid.ToString();
|
---|
43 | node.Attributes.Append(guidAttribute);
|
---|
44 | return node;
|
---|
45 | } else {
|
---|
46 | persistedObjects.Add(instance.Guid, instance);
|
---|
47 | XmlNode node = instance.GetXmlNode(name, document, persistedObjects);
|
---|
48 | return node;
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public static IStorable Restore(XmlReader reader, string elementName, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
53 | reader = reader.ReadSubtree();
|
---|
54 | reader.Read();
|
---|
55 | if(reader.Name != elementName) throw new XmlException("Expected: \"" + elementName + "\" found: \"" + reader.Name+"\"");
|
---|
56 | IStorable result = RestoreElement(reader, restoredObjects);
|
---|
57 | reader.Read();
|
---|
58 | reader.Close();
|
---|
59 | return result;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public static IStorable Restore(XmlReader reader, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
63 | reader = reader.ReadSubtree();
|
---|
64 | reader.Read();
|
---|
65 | IStorable result = RestoreElement(reader, restoredObjects);
|
---|
66 | reader.Read();
|
---|
67 | reader.Close();
|
---|
68 | return result;
|
---|
69 | }
|
---|
70 | private static IStorable RestoreElement(XmlReader reader, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
71 | Guid guid = new Guid(reader["GUID"]);
|
---|
72 | if(restoredObjects.ContainsKey(guid)) {
|
---|
73 | return restoredObjects[guid];
|
---|
74 | } else {
|
---|
75 | Type type = Type.GetType(reader["Type"], true);
|
---|
76 | IStorable instance = (IStorable)Activator.CreateInstance(type);
|
---|
77 | restoredObjects.Add(guid, instance);
|
---|
78 | instance.Populate(reader, restoredObjects);
|
---|
79 | return instance;
|
---|
80 | }
|
---|
81 | }
|
---|
82 | public static void Save(IStorable instance, string filename) {
|
---|
83 | FileStream stream = File.Create(filename);
|
---|
84 | Save(instance, stream);
|
---|
85 | stream.Close();
|
---|
86 | }
|
---|
87 | public static void Save(IStorable instance, Stream stream) {
|
---|
88 | XmlDocument document = PersistenceManager.CreateXmlDocument();
|
---|
89 | document.AppendChild(Persist(instance, document, new Dictionary<Guid, IStorable>()));
|
---|
90 | document.Save(stream);
|
---|
91 | }
|
---|
92 | public static IStorable Load(string filename) {
|
---|
93 | FileStream stream = File.OpenRead(filename);
|
---|
94 | IStorable storable = Load(stream);
|
---|
95 | stream.Close();
|
---|
96 | return storable;
|
---|
97 | }
|
---|
98 | public static IStorable Load(Stream stream) {
|
---|
99 | XmlReaderSettings settings = new XmlReaderSettings();
|
---|
100 | settings.IgnoreComments = true;
|
---|
101 | settings.IgnoreWhitespace = true;
|
---|
102 | XmlReader reader = XmlReader.Create(stream, settings);
|
---|
103 | reader.MoveToContent(); // read "<?xml version="1.0"?/>
|
---|
104 | return PersistenceManager.RestoreElement(reader, new Dictionary<Guid, IStorable>());
|
---|
105 | }
|
---|
106 |
|
---|
107 | public static string BuildTypeString(Type type) {
|
---|
108 | string assembly = type.Assembly.FullName;
|
---|
109 | assembly = assembly.Split(new string[] { ", " }, StringSplitOptions.None)[0];
|
---|
110 |
|
---|
111 | StringBuilder builder = new StringBuilder();
|
---|
112 | builder.Append(type.Namespace);
|
---|
113 | builder.Append(".");
|
---|
114 | builder.Append(type.Name);
|
---|
115 | Type[] args = type.GetGenericArguments();
|
---|
116 | if (args.Length > 0) {
|
---|
117 | builder.Append("[[");
|
---|
118 | builder.Append(BuildTypeString(args[0]));
|
---|
119 | builder.Append("]");
|
---|
120 | for (int i = 1; i < args.Length; i++) {
|
---|
121 | builder.Append(",[");
|
---|
122 | builder.Append(BuildTypeString(args[i]));
|
---|
123 | builder.Append("]");
|
---|
124 | }
|
---|
125 | builder.Append("]");
|
---|
126 | }
|
---|
127 | builder.Append(", ");
|
---|
128 | builder.Append(assembly);
|
---|
129 | return builder.ToString();
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|