Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Persistence/3.3/Default/DebugString/DebugStringGenerator.cs @ 17732

Last change on this file since 17732 was 16057, checked in by jkarder, 6 years ago

#2839:

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