using System;
using System.Collections.Generic;
using System.Text;
using HeuristicLab.Persistence.Interfaces;
using HeuristicLab.Persistence.Default.Xml;
using HeuristicLab.Persistence.Core;
using HeuristicLab.Persistence.Core.Tokens;
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();
}
}
}