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 | public static IStorable Restore(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
|
---|
52 | Guid guid = new Guid(node.Attributes["GUID"].Value);
|
---|
53 | if (restoredObjects.ContainsKey(guid)) {
|
---|
54 | return restoredObjects[guid];
|
---|
55 | } else {
|
---|
56 | Type type = Type.GetType(node.Attributes["Type"].Value, true);
|
---|
57 | IStorable instance = (IStorable)Activator.CreateInstance(type);
|
---|
58 | restoredObjects.Add(guid, instance);
|
---|
59 | instance.Populate(node, restoredObjects);
|
---|
60 | return instance;
|
---|
61 | }
|
---|
62 | }
|
---|
63 | public static void Save(IStorable instance, string filename) {
|
---|
64 | Save(instance, File.OpenWrite(filename));
|
---|
65 | }
|
---|
66 | public static void Save(IStorable instance, Stream stream) {
|
---|
67 | XmlDocument document = PersistenceManager.CreateXmlDocument();
|
---|
68 | document.AppendChild(Persist(instance, document, new Dictionary<Guid, IStorable>()));
|
---|
69 | document.Save(stream);
|
---|
70 | }
|
---|
71 | public static IStorable Load(string filename) {
|
---|
72 | return Load(File.OpenRead(filename));
|
---|
73 | }
|
---|
74 | public static IStorable Load(Stream stream) {
|
---|
75 | XmlDocument doc = new XmlDocument();
|
---|
76 | doc.Load(stream);
|
---|
77 | return PersistenceManager.Restore(doc.ChildNodes[1], new Dictionary<Guid, IStorable>());
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|