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