Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/ViewOnly/ViewOnlyFormat.cs @ 1556

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

Namespace refactoring. (#548)

File size: 4.8 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.ViewOnly {
10
11  public class ViewOnlyFormat : FormatBase {
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 Float2ViewFormatter : ValueType2ViewFormatter {
50    public override Type Type { get { return typeof(float); } }
51  }
52
53  public class ViewOnlyGenerator : Generator<string> {
54
55    private bool isSepReq;
56    private readonly bool showRefs;
57
58    public ViewOnlyGenerator() : this(false) { }
59
60    public ViewOnlyGenerator(bool showRefs) {
61      isSepReq = false;
62      this.showRefs = showRefs;
63    }   
64
65    protected override string Format(BeginToken beginToken) {
66      StringBuilder sb = new StringBuilder();     
67      if (isSepReq)
68        sb.Append(", ");
69      if ( ! string.IsNullOrEmpty(beginToken.Name) ) {
70        sb.Append(beginToken.Name);
71        if ( beginToken.Id != null && showRefs ) {
72          sb.Append('[');
73          sb.Append(beginToken.Id);
74          sb.Append(']');
75        }       
76      }
77      sb.Append("(");
78      isSepReq = false;
79      return sb.ToString();
80    }
81
82    protected override string Format(EndToken endToken) {
83      isSepReq = true;     
84      return ")";
85    }
86
87    protected override string Format(PrimitiveToken primitiveToken) {
88      StringBuilder sb = new StringBuilder();     
89      if (isSepReq)
90        sb.Append(", ");
91      if ( ! string.IsNullOrEmpty(primitiveToken.Name) ) {
92        sb.Append(primitiveToken.Name);
93        if ( primitiveToken.Id != null && showRefs ) {
94          sb.Append('[');
95          sb.Append(primitiveToken.Id);
96          sb.Append(']');
97        }
98        sb.Append('=');
99      }
100      sb.Append(primitiveToken.SerialData);
101      isSepReq = true;     
102      return sb.ToString(); 
103    }
104
105    protected override string Format(ReferenceToken referenceToken) {
106      StringBuilder sb = new StringBuilder();
107      if (isSepReq)
108        sb.Append(", ");
109      if ( ! string.IsNullOrEmpty(referenceToken.Name) ) {
110        sb.Append(referenceToken.Name);
111        sb.Append('=');
112      }
113      sb.Append('{');
114      sb.Append(referenceToken.Id);
115      sb.Append('}');
116      isSepReq = true;     
117      return sb.ToString();
118    }
119
120    protected override string Format(NullReferenceToken nullReferenceToken) {
121      StringBuilder sb = new StringBuilder();
122      if (isSepReq)
123        sb.Append(", ");
124      if (!string.IsNullOrEmpty(nullReferenceToken.Name)) {
125        sb.Append(nullReferenceToken.Name);
126        sb.Append('=');
127      }
128      sb.Append("<null>");
129      isSepReq = true;
130      return sb.ToString();
131    }
132
133    protected override string Format(MetaInfoBeginToken metaInfoBeginToken) {
134      return "[";
135    }
136
137    protected override string Format(MetaInfoEndToken metaInfoEndToken) {
138      return "]";
139    }
140
141    public static string Serialize(object o) {
142      return Serialize(o, ConfigurationService.Instance.GetDefaultConfig(ViewOnlyFormat.Instance));
143    }
144
145    public static string Serialize(object o, Configuration configuration) {
146      Serializer s = new Serializer(o, configuration);     
147      ViewOnlyGenerator generator = new ViewOnlyGenerator();
148      StringBuilder sb = new StringBuilder();
149      foreach (ISerializationToken token in s) {
150        sb.Append(generator.Format(token));       
151      }
152      return sb.ToString();
153    }
154  }
155}
Note: See TracBrowser for help on using the repository browser.