1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Persistence.Interfaces;
|
---|
6 | using HeuristicLab.Persistence.Default.Xml;
|
---|
7 | using HeuristicLab.Persistence.Core;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Persistence.Default.ViewOnly {
|
---|
10 |
|
---|
11 | public class ViewOnlyFormat : Format {
|
---|
12 | public override string Name { get { return "ViewOnly"; } }
|
---|
13 | public static ViewOnlyFormat Instance = new ViewOnlyFormat();
|
---|
14 | }
|
---|
15 |
|
---|
16 | public abstract class ValueType2ViewFormatter : IFormatter {
|
---|
17 | public abstract Type Type { get; }
|
---|
18 | public IFormat Format { get { return ViewOnlyFormat.Instance; } }
|
---|
19 | public object DoFormat(object o) { return o; }
|
---|
20 | public object Parse(object o) {
|
---|
21 | throw new NotImplementedException();
|
---|
22 | }
|
---|
23 | }
|
---|
24 |
|
---|
25 | public class String2ViewFormatter : ValueType2ViewFormatter {
|
---|
26 | public override Type Type { get { return typeof(string); } }
|
---|
27 | }
|
---|
28 |
|
---|
29 | public class Bool2ViewFormatter : ValueType2ViewFormatter {
|
---|
30 | public override Type Type { get { return typeof(bool); } }
|
---|
31 | }
|
---|
32 |
|
---|
33 | public class Int2ViewFormatter : ValueType2ViewFormatter {
|
---|
34 | public override Type Type { get { return typeof(int); } }
|
---|
35 | }
|
---|
36 |
|
---|
37 | public class Double2ViewFormatter : ValueType2ViewFormatter {
|
---|
38 | public override Type Type { get { return typeof(double); } }
|
---|
39 | }
|
---|
40 |
|
---|
41 | public class DateTime2ViewFormatter : ValueType2ViewFormatter {
|
---|
42 | public override Type Type { get { return typeof(DateTime); } }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public class Type2ViewFormatter : ValueType2ViewFormatter {
|
---|
46 | public override Type Type { get { return typeof(Type); } }
|
---|
47 | }
|
---|
48 |
|
---|
49 | //public class ToStringDecomposer : IDecomposer {
|
---|
50 | // should not be used by default
|
---|
51 | public class ToStringDecomposer {
|
---|
52 | public bool CanDecompose(Type type) { return true; }
|
---|
53 | public IEnumerable<Tag> DeCompose(object obj) {
|
---|
54 | yield return new Tag(obj.ToString());
|
---|
55 | }
|
---|
56 |
|
---|
57 | public object CreateInstance(Type type) {
|
---|
58 | throw new NotImplementedException();
|
---|
59 | }
|
---|
60 |
|
---|
61 | public object Populate(object instance, IEnumerable<Tag> tags, Type type) {
|
---|
62 | throw new NotImplementedException();
|
---|
63 | }
|
---|
64 |
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | public class ViewOnlyGenerator : Generator<string> {
|
---|
69 |
|
---|
70 | private bool isSepReq;
|
---|
71 | private bool showRefs;
|
---|
72 |
|
---|
73 | public ViewOnlyGenerator() : this(false) { }
|
---|
74 |
|
---|
75 | public ViewOnlyGenerator(bool showRefs) {
|
---|
76 | isSepReq = false;
|
---|
77 | this.showRefs = showRefs;
|
---|
78 | }
|
---|
79 |
|
---|
80 | protected override string Format(BeginToken beginToken) {
|
---|
81 | StringBuilder sb = new StringBuilder();
|
---|
82 | if (isSepReq)
|
---|
83 | sb.Append(", ");
|
---|
84 | if ( ! string.IsNullOrEmpty(beginToken.Name) ) {
|
---|
85 | sb.Append(beginToken.Name);
|
---|
86 | if ( beginToken.Id != null && showRefs ) {
|
---|
87 | sb.Append('[');
|
---|
88 | sb.Append(beginToken.Id);
|
---|
89 | sb.Append(']');
|
---|
90 | }
|
---|
91 | }
|
---|
92 | sb.Append("(");
|
---|
93 | isSepReq = false;
|
---|
94 | return sb.ToString();
|
---|
95 | }
|
---|
96 |
|
---|
97 | protected override string Format(EndToken endToken) {
|
---|
98 | isSepReq = true;
|
---|
99 | return ")";
|
---|
100 | }
|
---|
101 |
|
---|
102 | protected override string Format(PrimitiveToken primitiveToken) {
|
---|
103 | StringBuilder sb = new StringBuilder();
|
---|
104 | if (isSepReq)
|
---|
105 | sb.Append(", ");
|
---|
106 | if ( ! string.IsNullOrEmpty(primitiveToken.Name) ) {
|
---|
107 | sb.Append(primitiveToken.Name);
|
---|
108 | if ( primitiveToken.Id != null && showRefs ) {
|
---|
109 | sb.Append('[');
|
---|
110 | sb.Append(primitiveToken.Id);
|
---|
111 | sb.Append(']');
|
---|
112 | }
|
---|
113 | sb.Append('=');
|
---|
114 | }
|
---|
115 | sb.Append(primitiveToken.SerialData);
|
---|
116 | isSepReq = true;
|
---|
117 | return sb.ToString();
|
---|
118 | }
|
---|
119 |
|
---|
120 | protected override string Format(ReferenceToken referenceToken) {
|
---|
121 | StringBuilder sb = new StringBuilder();
|
---|
122 | if (isSepReq)
|
---|
123 | sb.Append(", ");
|
---|
124 | if ( ! string.IsNullOrEmpty(referenceToken.Name) ) {
|
---|
125 | sb.Append(referenceToken.Name);
|
---|
126 | sb.Append('=');
|
---|
127 | }
|
---|
128 | sb.Append('{');
|
---|
129 | sb.Append(referenceToken.Id);
|
---|
130 | sb.Append('}');
|
---|
131 | isSepReq = true;
|
---|
132 | return sb.ToString();
|
---|
133 | }
|
---|
134 |
|
---|
135 | protected override string Format(NullReferenceToken nullReferenceToken) {
|
---|
136 | StringBuilder sb = new StringBuilder();
|
---|
137 | if (isSepReq)
|
---|
138 | sb.Append(", ");
|
---|
139 | if (!string.IsNullOrEmpty(nullReferenceToken.Name)) {
|
---|
140 | sb.Append(nullReferenceToken.Name);
|
---|
141 | sb.Append('=');
|
---|
142 | }
|
---|
143 | sb.Append("<null>");
|
---|
144 | isSepReq = true;
|
---|
145 | return sb.ToString();
|
---|
146 | }
|
---|
147 |
|
---|
148 | public static string Serialize(object o) {
|
---|
149 | return Serialize(o, ConfigurationService.Instance.GetDefaultConfig(ViewOnlyFormat.Instance));
|
---|
150 | }
|
---|
151 |
|
---|
152 | public static string Serialize(object o, Configuration configuration) {
|
---|
153 | Serializer s = new Serializer(o, configuration);
|
---|
154 | ViewOnlyGenerator generator = new ViewOnlyGenerator();
|
---|
155 | StringBuilder sb = new StringBuilder();
|
---|
156 | foreach (ISerializationToken token in s) {
|
---|
157 | sb.Append(generator.Format(token));
|
---|
158 | }
|
---|
159 | return sb.ToString();
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|