#region License Information /* HeuristicLab * Copyright (C) 2002-2015 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.IO; using HeuristicLab.Persistence.Auxiliary; using HeuristicLab.Persistence.Core; using HeuristicLab.Persistence.Core.Tokens; using HeuristicLab.Persistence.Interfaces; using HeuristicLab.Tracing; namespace HeuristicLab.Persistence.Default.Xml { /// /// Main entry point of persistence to XML. Use the static methods to serialize /// to a file or to a stream. /// public class ReadableXmlGenerator : XmlGenerator { protected Dictionary typeCache = new Dictionary(); /// /// Formats the specified begin token. /// /// The begin token. /// The token in serialized form. protected override string Format(BeginToken beginToken) { var dict = new Dictionary { {"name", beginToken.Name}, {"id", beginToken.Id.ToString()}}; return CreateNodeStart(typeCache[beginToken.TypeId], dict); } /// /// Formats the specified end token. /// /// The end token. /// The token in serialized form. protected override string Format(EndToken endToken) { return CreateNodeEnd(typeCache[endToken.TypeId]); } /// /// Formats the specified data token. /// /// The data token. /// The token in serialized form. protected override string Format(PrimitiveToken dataToken) { var dict = new Dictionary { {"name", dataToken.Name}, {"id", dataToken.Id.ToString()}}; return CreateNode(typeCache[dataToken.TypeId], dict, ((XmlString)dataToken.SerialData).Data); } /// /// Formats the specified ref token. /// /// The ref token. /// The token in serialized form. protected override string Format(ReferenceToken refToken) { return CreateNode(refToken.Id.ToString(), new Dictionary { {"name", refToken.Name}}); } /// /// Formats the specified null ref token. /// /// The null ref token. /// The token in serialized form. protected override string Format(NullReferenceToken nullRefToken) { return CreateNode(XmlStringConstants.NULL, new Dictionary{ {"name", nullRefToken.Name}}); } /// /// Formats the specified meta info begin token. /// /// The meta info begin token. /// The token in serialized form. protected override string Format(MetaInfoBeginToken metaInfoBeginToken) { return CreateNodeStart(XmlStringConstants.METAINFO); } /// /// Formats the specified meta info end token. /// /// The meta info end token. /// The token in serialized form. protected override string Format(MetaInfoEndToken metaInfoEndToken) { return CreateNodeEnd(XmlStringConstants.METAINFO); } /// /// Formats the specified token. /// /// The token. /// The token in serialized form. protected override string Format(TypeToken token) { typeCache[token.Id] = TypeNameParser.Parse(token.TypeName).GetTypeNameInCode(false); return ""; } /// /// Serialize an object into a file. /// The XML configuration is obtained from the ConfigurationService. /// The file is actually a ZIP file. /// Compression level is set to 5 and needed assemblies are not included. /// /// The object. /// The filename. public new static void Serialize(object o, string filename) { Serialize(o, filename, ConfigurationService.Instance.GetConfiguration(new XmlFormat())); } /// /// Serializes the specified object into a file. /// Needed assemblies are not included, ZIP compression level is set to 5. /// /// The object. /// The filename. /// The configuration. public new static void Serialize(object obj, string filename, Configuration config) { try { string tempfile = Path.GetTempFileName(); DateTime start = DateTime.Now; using (FileStream stream = File.Create(tempfile)) { Serialize(obj, stream, config); } Logger.Info(String.Format("easy serialization took {0} seconds", (DateTime.Now - start).TotalSeconds)); File.Copy(tempfile, filename, true); File.Delete(tempfile); } catch (Exception) { Logger.Warn("Exception caught, no data has been written."); throw; } } /// /// Serializes the specified object into a stream using the /// obtained from the . /// /// The object. /// The stream. public static void Serialize(object obj, Stream stream) { Serialize(obj, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat())); } /// /// Serializes the specified object into a stream. /// /// The object. /// The stream. /// The configuration. public static void Serialize(object obj, Stream stream, Configuration config) { try { using (StreamWriter writer = new StreamWriter(stream)) { Serializer serializer = new Serializer(obj, config); serializer.InterleaveTypeInformation = true; ReadableXmlGenerator generator = new ReadableXmlGenerator(); foreach (ISerializationToken token in serializer) { string line = generator.Format(token); writer.Write(line); } writer.Flush(); } } catch (PersistenceException) { throw; } catch (Exception e) { throw new PersistenceException("Unexpected exception during Serialization.", e); } } } }