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 | using System.IO.Compression;
|
---|
28 | using HeuristicLab.PluginInfrastructure;
|
---|
29 | using HeuristicLab.Persistence.Default.Xml;
|
---|
30 | using HeuristicLab.Persistence.Core;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Core {
|
---|
33 | /// <summary>
|
---|
34 | /// Static class for serializing and deserializing objects.
|
---|
35 | /// </summary>
|
---|
36 | public static class PersistenceManager {
|
---|
37 | /// <summary>
|
---|
38 | /// Saves the specified <paramref name="instance"/> in th 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>
|
---|
43 | public static void Save(IItem instance, string filename) {
|
---|
44 | XmlGenerator.Serialize(instance, filename, 0);
|
---|
45 | }
|
---|
46 |
|
---|
47 | public static void SaveCompressed(IItem instance, string filename) {
|
---|
48 | XmlGenerator.Serialize(instance, filename, 9);
|
---|
49 | }
|
---|
50 |
|
---|
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>
|
---|
57 | public static void Save(IItem instance, Stream stream) {
|
---|
58 | XmlGenerator.Serialize(instance, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat()));
|
---|
59 | }
|
---|
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>
|
---|
67 | public static IItem Load(string filename) {
|
---|
68 | return (IItem)XmlParser.Deserialize(filename);
|
---|
69 | }
|
---|
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>
|
---|
77 | public static IItem Load(Stream stream) {
|
---|
78 | return (IItem)XmlParser.Deserialize(stream);
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|