Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1463 was 1454, checked in by epitzer, 16 years ago

merge persistence exploration branch into trunk as HL plugin. (#506)

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