[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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using System.Xml;
|
---|
| 26 | using System.IO;
|
---|
[402] | 27 | using System.IO.Compression;
|
---|
[750] | 28 | using HeuristicLab.PluginInfrastructure;
|
---|
[1667] | 29 | using HeuristicLab.Persistence.Default.Xml;
|
---|
[1734] | 30 | using HeuristicLab.Persistence.Core;
|
---|
[2] | 31 |
|
---|
| 32 | namespace HeuristicLab.Core {
|
---|
[776] | 33 | /// <summary>
|
---|
| 34 | /// Static class for serializing and deserializing objects.
|
---|
| 35 | /// </summary>
|
---|
[2] | 36 | public static class PersistenceManager {
|
---|
[776] | 37 | /// <summary>
|
---|
| 38 | /// Saves the specified <paramref name="instance"/> in the specified file through creating an
|
---|
| 39 | /// <see cref="XmlDocument"/>.
|
---|
| 40 | /// </summary>
|
---|
| 41 | /// <param name="instance">The object that should be saved.</param>
|
---|
| 42 | /// <param name="filename">The name of the file where the <paramref name="object"/> should be saved.</param>
|
---|
[2] | 43 | public static void Save(IStorable instance, string filename) {
|
---|
[1895] | 44 | XmlGenerator.Serialize(instance, filename, 0);
|
---|
[2] | 45 | }
|
---|
[1667] | 46 |
|
---|
[1895] | 47 | public static void SaveCompressed(IStorable instance, string filename) {
|
---|
| 48 | XmlGenerator.Serialize(instance, filename, 9);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[776] | 51 | /// <summary>
|
---|
| 52 | /// Saves the specified <paramref name="instance"/> in the specified <paramref name="stream"/>
|
---|
| 53 | /// through creating an <see cref="XmlDocument"/>.
|
---|
| 54 | /// </summary>
|
---|
| 55 | /// <param name="instance">The object that should be saved.</param>
|
---|
| 56 | /// <param name="stream">The (file) stream where the object should be saved.</param>
|
---|
[2] | 57 | public static void Save(IStorable instance, Stream stream) {
|
---|
[1734] | 58 | XmlGenerator.Serialize(instance, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat()));
|
---|
[2] | 59 | }
|
---|
[776] | 60 | /// <summary>
|
---|
| 61 | /// Loads an object from a file with the specified <paramref name="filename"/>.
|
---|
| 62 | /// </summary>
|
---|
| 63 | /// <remarks>The object must be saved as an <see cref="XmlDocument"/>. <br/>
|
---|
| 64 | /// Calls <see cref="Restore"/>.</remarks>
|
---|
| 65 | /// <param name="filename">The filename of the file where the data is saved.</param>
|
---|
| 66 | /// <returns>The loaded object.</returns>
|
---|
[2] | 67 | public static IStorable Load(string filename) {
|
---|
[1734] | 68 | return (IStorable)XmlParser.Deserialize(filename);
|
---|
[2] | 69 | }
|
---|
[776] | 70 | /// <summary>
|
---|
| 71 | /// Loads an object from the specified <paramref name="stream"/>.
|
---|
| 72 | /// </summary>
|
---|
| 73 | /// <remarks>The object must be saved as an <see cref="XmlDocument"/>. <br/>
|
---|
| 74 | /// Calls <see cref="Restore"/>.</remarks>
|
---|
| 75 | /// <param name="stream">The stream from where to load the data.</param>
|
---|
| 76 | /// <returns>The loaded object.</returns>
|
---|
[2] | 77 | public static IStorable Load(Stream stream) {
|
---|
[1734] | 78 | return (IStorable)XmlParser.Deserialize(stream);
|
---|
[402] | 79 | }
|
---|
| 80 |
|
---|
[776] | 81 | /// <summary>
|
---|
| 82 | /// Builds a meaningful string for the given <paramref name="type"/> with the namespace information,
|
---|
| 83 | /// all its arguments, the assembly name...
|
---|
| 84 | /// </summary>
|
---|
| 85 | /// <param name="type">The type for which a string should be created.</param>
|
---|
| 86 | /// <returns>A string value of this type containing different additional information.</returns>
|
---|
[40] | 87 | public static string BuildTypeString(Type type) {
|
---|
| 88 | string assembly = type.Assembly.FullName;
|
---|
[267] | 89 | assembly = assembly.Substring(0, assembly.IndexOf(", "));
|
---|
[40] | 90 |
|
---|
| 91 | StringBuilder builder = new StringBuilder();
|
---|
| 92 | builder.Append(type.Namespace);
|
---|
| 93 | builder.Append(".");
|
---|
| 94 | builder.Append(type.Name);
|
---|
| 95 | Type[] args = type.GetGenericArguments();
|
---|
[1667] | 96 | if (args.Length > 0) {
|
---|
[40] | 97 | builder.Append("[[");
|
---|
| 98 | builder.Append(BuildTypeString(args[0]));
|
---|
| 99 | builder.Append("]");
|
---|
[1667] | 100 | for (int i = 1; i < args.Length; i++) {
|
---|
[40] | 101 | builder.Append(",[");
|
---|
| 102 | builder.Append(BuildTypeString(args[i]));
|
---|
| 103 | builder.Append("]");
|
---|
| 104 | }
|
---|
| 105 | builder.Append("]");
|
---|
| 106 | }
|
---|
| 107 | builder.Append(", ");
|
---|
| 108 | builder.Append(assembly);
|
---|
| 109 | return builder.ToString();
|
---|
| 110 | }
|
---|
[2] | 111 | }
|
---|
| 112 | }
|
---|