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 void Persist(IStorable instance, XmlWriter writer, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
36 | Persist(instance.GetType().Name, instance, writer, persistedObjects);
|
---|
37 | }
|
---|
38 | public static void Persist(string name, IStorable instance, XmlWriter writer, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
39 | if (persistedObjects.ContainsKey(instance.Guid)) {
|
---|
40 | writer.WriteStartElement(name, null);
|
---|
41 | writer.WriteAttributeString("GUID", instance.Guid.ToString());
|
---|
42 | writer.WriteEndElement();
|
---|
43 |
|
---|
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 | writer.WriteStartElement(name);
|
---|
52 | instance.Persist(name, writer, persistedObjects);
|
---|
53 | writer.WriteEndElement();
|
---|
54 | //XmlNode node = instance.GetXmlNode(name, document, persistedObjects);
|
---|
55 | //return node;
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public static IStorable Restore(XmlReader reader, string elementName, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
60 | reader = reader.ReadSubtree();
|
---|
61 | reader.Read();
|
---|
62 | if(reader.Name != elementName) throw new XmlException("Expected: \"" + elementName + "\" found: \"" + reader.Name+"\"");
|
---|
63 | IStorable result = RestoreElement(reader, restoredObjects);
|
---|
64 | reader.Read();
|
---|
65 | reader.Close();
|
---|
66 | return result;
|
---|
67 | }
|
---|
68 |
|
---|
69 | public static IStorable Restore(XmlReader reader, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
70 | reader = reader.ReadSubtree();
|
---|
71 | reader.Read();
|
---|
72 | IStorable result = RestoreElement(reader, restoredObjects);
|
---|
73 | reader.Read();
|
---|
74 | reader.Close();
|
---|
75 | return result;
|
---|
76 | }
|
---|
77 | private static IStorable RestoreElement(XmlReader reader, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
78 | Guid guid = new Guid(reader["GUID"]);
|
---|
79 | if(restoredObjects.ContainsKey(guid)) {
|
---|
80 | return restoredObjects[guid];
|
---|
81 | } else {
|
---|
82 | Type type = Type.GetType(reader["Type"], true);
|
---|
83 | IStorable instance = (IStorable)Activator.CreateInstance(type);
|
---|
84 | restoredObjects.Add(guid, instance);
|
---|
85 | instance.Populate(reader, restoredObjects);
|
---|
86 | return instance;
|
---|
87 | }
|
---|
88 | }
|
---|
89 | public static void Save(IStorable instance, string filename) {
|
---|
90 | FileStream stream = File.Create(filename);
|
---|
91 | Save(instance, stream);
|
---|
92 | stream.Close();
|
---|
93 | }
|
---|
94 | public static void Save(IStorable instance, Stream stream) {
|
---|
95 | XmlTextWriter writer = new XmlTextWriter(stream, null);
|
---|
96 | writer.Formatting = Formatting.Indented;
|
---|
97 | writer.WriteProcessingInstruction("xml", "version=\"1.0\"");
|
---|
98 | Persist(instance, writer, new Dictionary<Guid, IStorable>());
|
---|
99 | writer.Flush();
|
---|
100 | writer.Close();
|
---|
101 | }
|
---|
102 | public static IStorable Load(string filename) {
|
---|
103 | FileStream stream = File.OpenRead(filename);
|
---|
104 | IStorable storable = Load(stream);
|
---|
105 | stream.Close();
|
---|
106 | return storable;
|
---|
107 | }
|
---|
108 | public static IStorable Load(Stream stream) {
|
---|
109 | XmlReaderSettings settings = new XmlReaderSettings();
|
---|
110 | settings.IgnoreComments = true;
|
---|
111 | settings.IgnoreWhitespace = true;
|
---|
112 | XmlReader reader = XmlReader.Create(stream, settings);
|
---|
113 | reader.MoveToContent(); // read "<?xml version="1.0"?/>
|
---|
114 | return PersistenceManager.RestoreElement(reader, new Dictionary<Guid, IStorable>());
|
---|
115 | }
|
---|
116 |
|
---|
117 | public static string BuildTypeString(Type type) {
|
---|
118 | string assembly = type.Assembly.FullName;
|
---|
119 | assembly = assembly.Split(new string[] { ", " }, StringSplitOptions.None)[0];
|
---|
120 |
|
---|
121 | StringBuilder builder = new StringBuilder();
|
---|
122 | builder.Append(type.Namespace);
|
---|
123 | builder.Append(".");
|
---|
124 | builder.Append(type.Name);
|
---|
125 | Type[] args = type.GetGenericArguments();
|
---|
126 | if (args.Length > 0) {
|
---|
127 | builder.Append("[[");
|
---|
128 | builder.Append(BuildTypeString(args[0]));
|
---|
129 | builder.Append("]");
|
---|
130 | for (int i = 1; i < args.Length; i++) {
|
---|
131 | builder.Append(",[");
|
---|
132 | builder.Append(BuildTypeString(args[i]));
|
---|
133 | builder.Append("]");
|
---|
134 | }
|
---|
135 | builder.Append("]");
|
---|
136 | }
|
---|
137 | builder.Append(", ");
|
---|
138 | builder.Append(assembly);
|
---|
139 | return builder.ToString();
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|