#region License Information
/* HeuristicLab
* Copyright (C) 2002-2014 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.Text;
using HeuristicLab.Persistence.Core;
using HeuristicLab.Persistence.Core.Tokens;
using HeuristicLab.Persistence.Interfaces;
namespace HeuristicLab.Persistence.Default.DebugString {
///
/// Generate a string that recursively describes an object graph.
///
public class DebugStringGenerator : GeneratorBase {
private bool isSepReq;
private readonly bool showRefs;
///
/// Initializes a new instance of the class.
///
public DebugStringGenerator() : this(true) { }
///
/// Initializes a new instance of the class.
///
/// if set to true show references.
public DebugStringGenerator(bool showRefs) {
isSepReq = false;
this.showRefs = showRefs;
}
///
/// Formats the specified begin token.
///
/// The begin token.
/// The token in serialized form.
protected override string Format(BeginToken beginToken) {
StringBuilder sb = new StringBuilder();
if (isSepReq)
sb.Append(", ");
if (!string.IsNullOrEmpty(beginToken.Name)) {
sb.Append(beginToken.Name);
if (beginToken.Id != null && showRefs) {
sb.Append('[');
sb.Append(beginToken.Id);
sb.Append(']');
}
}
sb.Append("(");
isSepReq = false;
return sb.ToString();
}
///
/// Formats the specified end token.
///
/// The end token.
/// The token in serialized form.
protected override string Format(EndToken endToken) {
isSepReq = true;
return ")";
}
///
/// Formats the specified primitive token.
///
/// The primitive token.
/// The token in serialized form.
protected override string Format(PrimitiveToken primitiveToken) {
StringBuilder sb = new StringBuilder();
if (isSepReq)
sb.Append(", ");
if (!string.IsNullOrEmpty(primitiveToken.Name)) {
sb.Append(primitiveToken.Name);
if (primitiveToken.Id != null && showRefs) {
sb.Append('[');
sb.Append(primitiveToken.Id);
sb.Append(']');
}
sb.Append('=');
}
sb.Append(((DebugString)primitiveToken.SerialData).Data);
isSepReq = true;
return sb.ToString();
}
///
/// Formats the specified reference token.
///
/// The reference token.
/// The token in serialized form.
protected override string Format(ReferenceToken referenceToken) {
StringBuilder sb = new StringBuilder();
if (isSepReq)
sb.Append(", ");
if (!string.IsNullOrEmpty(referenceToken.Name)) {
sb.Append(referenceToken.Name);
sb.Append('=');
}
sb.Append('{');
sb.Append(referenceToken.Id);
sb.Append('}');
isSepReq = true;
return sb.ToString();
}
///
/// Formats the specified null reference token.
///
/// The null reference token.
/// The token in serialized form.
protected override string Format(NullReferenceToken nullReferenceToken) {
StringBuilder sb = new StringBuilder();
if (isSepReq)
sb.Append(", ");
if (!string.IsNullOrEmpty(nullReferenceToken.Name)) {
sb.Append(nullReferenceToken.Name);
sb.Append('=');
}
sb.Append("");
isSepReq = true;
return sb.ToString();
}
///
/// Formats the specified meta info begin token.
///
/// The meta info begin token.
/// The token in serialized form.
protected override string Format(MetaInfoBeginToken metaInfoBeginToken) {
return "[";
}
///
/// Formats the specified meta info end token.
///
/// The meta info end token.
/// The token in serialized form.
protected override string Format(MetaInfoEndToken metaInfoEndToken) {
return "]";
}
///
/// Formats the specified type token.
///
/// The type token.
/// The token in serialized form.
protected override string Format(TypeToken typeToken) {
return string.Empty;
}
///
/// Serializes the specified object.
///
/// The object.
/// A string representation of the complete object graph
public static string Serialize(object o) {
return Serialize(o, ConfigurationService.Instance.GetDefaultConfig(new DebugStringFormat()));
}
///
/// Serializes the specified object.
///
/// The object.
/// The persistence configuration.
/// A string representation of the complete object graph.
public static string Serialize(object o, Configuration configuration) {
Serializer s = new Serializer(o, configuration);
DebugStringGenerator generator = new DebugStringGenerator();
StringBuilder sb = new StringBuilder();
foreach (ISerializationToken token in s) {
sb.Append(generator.Format(token));
}
return sb.ToString();
}
}
}