#region License Information
/* HeuristicLab
* Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.IO.Compression;
using HeuristicLab.PluginInfrastructure;
using HeuristicLab.Persistence.Default.Xml;
using HeuristicLab.Persistence.Core;
namespace HeuristicLab.Core {
///
/// Static class for serializing and deserializing objects.
///
public static class PersistenceManager {
///
/// Saves the specified in the specified file through creating an
/// .
///
/// The object that should be saved.
/// The name of the file where the should be saved.
public static void Save(IStorable instance, string filename) {
XmlGenerator.Serialize(instance, filename, 0);
}
public static void SaveCompressed(IStorable instance, string filename) {
XmlGenerator.Serialize(instance, filename, 9);
}
///
/// Saves the specified in the specified
/// through creating an .
///
/// The object that should be saved.
/// The (file) stream where the object should be saved.
public static void Save(IStorable instance, Stream stream) {
XmlGenerator.Serialize(instance, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat()));
}
///
/// Loads an object from a file with the specified .
///
/// The object must be saved as an .
/// Calls .
/// The filename of the file where the data is saved.
/// The loaded object.
public static IStorable Load(string filename) {
return (IStorable)XmlParser.Deserialize(filename);
}
///
/// Loads an object from the specified .
///
/// The object must be saved as an .
/// Calls .
/// The stream from where to load the data.
/// The loaded object.
public static IStorable Load(Stream stream) {
return (IStorable)XmlParser.Deserialize(stream);
}
///
/// Builds a meaningful string for the given with the namespace information,
/// all its arguments, the assembly name...
///
/// The type for which a string should be created.
/// A string value of this type containing different additional information.
public static string BuildTypeString(Type type) {
string assembly = type.Assembly.FullName;
assembly = assembly.Substring(0, assembly.IndexOf(", "));
StringBuilder builder = new StringBuilder();
builder.Append(type.Namespace);
builder.Append(".");
builder.Append(type.Name);
Type[] args = type.GetGenericArguments();
if (args.Length > 0) {
builder.Append("[[");
builder.Append(BuildTypeString(args[0]));
builder.Append("]");
for (int i = 1; i < args.Length; i++) {
builder.Append(",[");
builder.Append(BuildTypeString(args[i]));
builder.Append("]");
}
builder.Append("]");
}
builder.Append(", ");
builder.Append(assembly);
return builder.ToString();
}
}
}