Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1539 was 1476, checked in by epitzer, 16 years ago

Decomposers for all primitive types except string. (#563)

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