Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/DebugString/DebugStringGenerator.cs @ 3390

Last change on this file since 3390 was 3036, checked in by epitzer, 15 years ago

make most serializers internal and complete API documentation (#548)

File size: 6.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4using HeuristicLab.Persistence.Interfaces;
5using HeuristicLab.Persistence.Default.Xml;
6using HeuristicLab.Persistence.Core;
7using HeuristicLab.Persistence.Core.Tokens;
8
9namespace HeuristicLab.Persistence.Default.DebugString {
10
11  /// <summary>
12  /// Generate a string that recursively describes an object graph.
13  /// </summary>
14  public class DebugStringGenerator : GeneratorBase<string> {
15
16    private bool isSepReq;
17    private readonly bool showRefs;
18
19    /// <summary>
20    /// Initializes a new instance of the <see cref="DebugStringGenerator"/> class.
21    /// </summary>
22    public DebugStringGenerator() : this(true) { }
23
24    /// <summary>
25    /// Initializes a new instance of the <see cref="DebugStringGenerator"/> class.
26    /// </summary>
27    /// <param name="showRefs">if set to <c>true</c> show references.</param>
28    public DebugStringGenerator(bool showRefs) {
29      isSepReq = false;
30      this.showRefs = showRefs;
31    }
32
33    /// <summary>
34    /// Formats the specified begin token.
35    /// </summary>
36    /// <param name="beginToken">The begin token.</param>
37    /// <returns>The token in serialized form.</returns>
38    protected override string Format(BeginToken beginToken) {
39      StringBuilder sb = new StringBuilder();
40      if (isSepReq)
41        sb.Append(", ");
42      if (!string.IsNullOrEmpty(beginToken.Name)) {
43        sb.Append(beginToken.Name);
44        if (beginToken.Id != null && showRefs) {
45          sb.Append('[');
46          sb.Append(beginToken.Id);
47          sb.Append(']');
48        }
49      }
50      sb.Append("(");
51      isSepReq = false;
52      return sb.ToString();
53    }
54
55    /// <summary>
56    /// Formats the specified end token.
57    /// </summary>
58    /// <param name="endToken">The end token.</param>
59    /// <returns>The token in serialized form.</returns>
60    protected override string Format(EndToken endToken) {
61      isSepReq = true;
62      return ")";
63    }
64
65    /// <summary>
66    /// Formats the specified primitive token.
67    /// </summary>
68    /// <param name="primitiveToken">The primitive token.</param>
69    /// <returns>The token in serialized form.</returns>
70    protected override string Format(PrimitiveToken primitiveToken) {
71      StringBuilder sb = new StringBuilder();
72      if (isSepReq)
73        sb.Append(", ");
74      if (!string.IsNullOrEmpty(primitiveToken.Name)) {
75        sb.Append(primitiveToken.Name);
76        if (primitiveToken.Id != null && showRefs) {
77          sb.Append('[');
78          sb.Append(primitiveToken.Id);
79          sb.Append(']');
80        }
81        sb.Append('=');
82      }
83      sb.Append(((DebugString)primitiveToken.SerialData).Data);
84      isSepReq = true;
85      return sb.ToString();
86    }
87
88    /// <summary>
89    /// Formats the specified reference token.
90    /// </summary>
91    /// <param name="referenceToken">The reference token.</param>
92    /// <returns>The token in serialized form.</returns>
93    protected override string Format(ReferenceToken referenceToken) {
94      StringBuilder sb = new StringBuilder();
95      if (isSepReq)
96        sb.Append(", ");
97      if (!string.IsNullOrEmpty(referenceToken.Name)) {
98        sb.Append(referenceToken.Name);
99        sb.Append('=');
100      }
101      sb.Append('{');
102      sb.Append(referenceToken.Id);
103      sb.Append('}');
104      isSepReq = true;
105      return sb.ToString();
106    }
107
108    /// <summary>
109    /// Formats the specified null reference token.
110    /// </summary>
111    /// <param name="nullReferenceToken">The null reference token.</param>
112    /// <returns>The token in serialized form.</returns>
113    protected override string Format(NullReferenceToken nullReferenceToken) {
114      StringBuilder sb = new StringBuilder();
115      if (isSepReq)
116        sb.Append(", ");
117      if (!string.IsNullOrEmpty(nullReferenceToken.Name)) {
118        sb.Append(nullReferenceToken.Name);
119        sb.Append('=');
120      }
121      sb.Append("<null>");
122      isSepReq = true;
123      return sb.ToString();
124    }
125
126    /// <summary>
127    /// Formats the specified meta info begin token.
128    /// </summary>
129    /// <param name="metaInfoBeginToken">The meta info begin token.</param>
130    /// <returns>The token in serialized form.</returns>
131    protected override string Format(MetaInfoBeginToken metaInfoBeginToken) {
132      return "[";
133    }
134
135    /// <summary>
136    /// Formats the specified meta info end token.
137    /// </summary>
138    /// <param name="metaInfoEndToken">The meta info end token.</param>
139    /// <returns>The token in serialized form.</returns>
140    protected override string Format(MetaInfoEndToken metaInfoEndToken) {
141      return "]";
142    }
143
144    /// <summary>
145    /// Formats the specified type token.
146    /// </summary>
147    /// <param name="typeToken">The type token.</param>
148    /// <returns>The token in serialized form.</returns>
149    protected override string Format(TypeToken typeToken) {
150      return string.Empty;
151    }
152
153    /// <summary>
154    /// Serializes the specified object.
155    /// </summary>
156    /// <param name="o">The object.</param>
157    /// <returns>A string representation of the complete object graph</returns>
158    public static string Serialize(object o) {
159      return Serialize(o, ConfigurationService.Instance.GetDefaultConfig(new DebugStringFormat()));
160    }
161
162    /// <summary>
163    /// Serializes the specified object.
164    /// </summary>
165    /// <param name="o">The object.</param>
166    /// <param name="configuration">The persistence configuration.</param>
167    /// <returns>A string representation of the complete object graph.</returns>
168    public static string Serialize(object o, Configuration configuration) {
169      Serializer s = new Serializer(o, configuration);
170      DebugStringGenerator generator = new DebugStringGenerator();
171      StringBuilder sb = new StringBuilder();
172      foreach (ISerializationToken token in s) {
173        sb.Append(generator.Format(token));
174      }
175      return sb.ToString();
176    }
177  }
178}
Note: See TracBrowser for help on using the repository browser.