1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Text;
|
---|
4 | using HeuristicLab.Persistence.Interfaces;
|
---|
5 | using HeuristicLab.Persistence.Default.Xml;
|
---|
6 | using HeuristicLab.Persistence.Core;
|
---|
7 | using HeuristicLab.Persistence.Core.Tokens;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Persistence.Default.DebugString {
|
---|
10 |
|
---|
11 | public class DebugStringGenerator : GeneratorBase<string> {
|
---|
12 |
|
---|
13 | private bool isSepReq;
|
---|
14 | private readonly bool showRefs;
|
---|
15 |
|
---|
16 | public DebugStringGenerator() : this(true) { }
|
---|
17 |
|
---|
18 | public DebugStringGenerator(bool showRefs) {
|
---|
19 | isSepReq = false;
|
---|
20 | this.showRefs = showRefs;
|
---|
21 | }
|
---|
22 |
|
---|
23 | protected override string Format(BeginToken beginToken) {
|
---|
24 | StringBuilder sb = new StringBuilder();
|
---|
25 | if (isSepReq)
|
---|
26 | sb.Append(", ");
|
---|
27 | if (!string.IsNullOrEmpty(beginToken.Name)) {
|
---|
28 | sb.Append(beginToken.Name);
|
---|
29 | if (beginToken.Id != null && showRefs) {
|
---|
30 | sb.Append('[');
|
---|
31 | sb.Append(beginToken.Id);
|
---|
32 | sb.Append(']');
|
---|
33 | }
|
---|
34 | }
|
---|
35 | sb.Append("(");
|
---|
36 | isSepReq = false;
|
---|
37 | return sb.ToString();
|
---|
38 | }
|
---|
39 |
|
---|
40 | protected override string Format(EndToken endToken) {
|
---|
41 | isSepReq = true;
|
---|
42 | return ")";
|
---|
43 | }
|
---|
44 |
|
---|
45 | protected override string Format(PrimitiveToken primitiveToken) {
|
---|
46 | StringBuilder sb = new StringBuilder();
|
---|
47 | if (isSepReq)
|
---|
48 | sb.Append(", ");
|
---|
49 | if (!string.IsNullOrEmpty(primitiveToken.Name)) {
|
---|
50 | sb.Append(primitiveToken.Name);
|
---|
51 | if (primitiveToken.Id != null && showRefs) {
|
---|
52 | sb.Append('[');
|
---|
53 | sb.Append(primitiveToken.Id);
|
---|
54 | sb.Append(']');
|
---|
55 | }
|
---|
56 | sb.Append('=');
|
---|
57 | }
|
---|
58 | sb.Append(((DebugString)primitiveToken.SerialData).Data);
|
---|
59 | isSepReq = true;
|
---|
60 | return sb.ToString();
|
---|
61 | }
|
---|
62 |
|
---|
63 | protected override string Format(ReferenceToken referenceToken) {
|
---|
64 | StringBuilder sb = new StringBuilder();
|
---|
65 | if (isSepReq)
|
---|
66 | sb.Append(", ");
|
---|
67 | if (!string.IsNullOrEmpty(referenceToken.Name)) {
|
---|
68 | sb.Append(referenceToken.Name);
|
---|
69 | sb.Append('=');
|
---|
70 | }
|
---|
71 | sb.Append('{');
|
---|
72 | sb.Append(referenceToken.Id);
|
---|
73 | sb.Append('}');
|
---|
74 | isSepReq = true;
|
---|
75 | return sb.ToString();
|
---|
76 | }
|
---|
77 |
|
---|
78 | protected override string Format(NullReferenceToken nullReferenceToken) {
|
---|
79 | StringBuilder sb = new StringBuilder();
|
---|
80 | if (isSepReq)
|
---|
81 | sb.Append(", ");
|
---|
82 | if (!string.IsNullOrEmpty(nullReferenceToken.Name)) {
|
---|
83 | sb.Append(nullReferenceToken.Name);
|
---|
84 | sb.Append('=');
|
---|
85 | }
|
---|
86 | sb.Append("<null>");
|
---|
87 | isSepReq = true;
|
---|
88 | return sb.ToString();
|
---|
89 | }
|
---|
90 |
|
---|
91 | protected override string Format(MetaInfoBeginToken metaInfoBeginToken) {
|
---|
92 | return "[";
|
---|
93 | }
|
---|
94 |
|
---|
95 | protected override string Format(MetaInfoEndToken metaInfoEndToken) {
|
---|
96 | return "]";
|
---|
97 | }
|
---|
98 |
|
---|
99 | public static string Serialize(object o) {
|
---|
100 | return Serialize(o, ConfigurationService.Instance.GetDefaultConfig(new DebugStringFormat()));
|
---|
101 | }
|
---|
102 |
|
---|
103 | public static string Serialize(object o, Configuration configuration) {
|
---|
104 | Serializer s = new Serializer(o, configuration);
|
---|
105 | DebugStringGenerator generator = new DebugStringGenerator();
|
---|
106 | StringBuilder sb = new StringBuilder();
|
---|
107 | foreach (ISerializationToken token in s) {
|
---|
108 | sb.Append(generator.Format(token));
|
---|
109 | }
|
---|
110 | return sb.ToString();
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|