Free cookie consent management tool by TermsFeed Policy Generator

source: branches/New Persistence Exploration/Persistence/Persistence/Default/Xml/XmlGenerator.cs @ 1447

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

refactoring and code clean up. (#9999)

File size: 5.5 KB
Line 
1using System.Collections.Generic;
2using System;
3using System.Text;
4using HeuristicLab.Persistence.Interfaces;
5using HeuristicLab.Persistence.Core;
6using System.IO;
7
8namespace HeuristicLab.Persistence.Default.Xml {
9
10  struct XmlStrings {
11    public const string PRIMITIVE = "PRIMITVE";
12    public const string COMPOSITE = "COMPOSITE";
13    public const string REFERENCE = "REFERENCE";
14    public const string NULL = "NULL";
15    public const string TYPECACHE = "TYPCACHE";
16    public const string TYPE = "TYPE";
17  }
18
19  public abstract class Generator<T> {
20    public T Format(ISerializationToken token) {
21      Type type = token.GetType();
22      if (type == typeof(BeginToken))
23        return Format((BeginToken)token);
24      if (type == typeof(EndToken))
25        return Format((EndToken)token);
26      if (type == typeof(PrimitiveToken))
27        return Format((PrimitiveToken)token);
28      if (type == typeof(ReferenceToken))
29        return Format((ReferenceToken)token);
30      if (type == typeof(NullReferenceToken))
31        return Format((NullReferenceToken)token);
32      throw new ApplicationException("Invalid token of type " + type.FullName);
33    }
34    protected abstract T Format(BeginToken beginToken);
35    protected abstract T Format(EndToken endToken);
36    protected abstract T Format(PrimitiveToken primitiveToken);
37    protected abstract T Format(ReferenceToken referenceToken);
38    protected abstract T Format(NullReferenceToken nullReferenceToken);
39  }
40
41  public class XmlGenerator : Generator<string> {
42   
43    private int depth;
44
45    public XmlGenerator() {
46      depth = 0;
47    }
48
49    private enum NodeType { Start, End, Inline } ;
50
51    private static string FormatNode(string name,
52        Dictionary<string, object> attributes,
53        NodeType type) {
54      return FormatNode(name, attributes, type, " ");
55    }
56
57    private static string FormatNode(string name,
58        Dictionary<string, object> attributes,
59        NodeType type, string space) {
60      StringBuilder sb = new StringBuilder();
61      sb.Append('<');
62      if (type == NodeType.End)
63        sb.Append('/');
64      sb.Append(name);     
65      foreach (var attribute in attributes) {
66        if (attribute.Value != null && !string.IsNullOrEmpty(attribute.Value.ToString())) {
67          sb.Append(space);
68          sb.Append(attribute.Key);
69          sb.Append("=\"");
70          sb.Append(attribute.Value);
71          sb.Append('"');
72        }
73      }
74      if (type == NodeType.Inline)
75        sb.Append('/');
76      sb.Append(">");
77      return sb.ToString();     
78    }
79
80    private string Prefix {
81      get { return new string(' ', depth * 2); }
82    }
83
84    protected override string Format(BeginToken beginToken) {           
85      var attributes = new Dictionary<string, object> {
86        {"name", beginToken.Name},
87        {"typeId", beginToken.TypeId},
88        {"id", beginToken.Id}};
89      string result = Prefix +
90                      FormatNode(XmlStrings.COMPOSITE, attributes, NodeType.Start) + "\n";
91      depth += 1;
92      return result;
93    }
94
95    protected override string Format(EndToken endToken) {     
96      depth -= 1;
97      return Prefix + "</" + XmlStrings.COMPOSITE + ">\n";
98    }
99
100    protected override string Format(PrimitiveToken dataToken) {     
101      var attributes =
102        new Dictionary<string, object> {
103            {"typeId", dataToken.TypeId},
104            {"name", dataToken.Name},
105            {"id", dataToken.Id}};
106      return Prefix +
107        FormatNode(XmlStrings.PRIMITIVE, attributes, NodeType.Start) +
108        dataToken.SerialData + "</" + XmlStrings.PRIMITIVE + ">\n";     
109    }
110
111    protected override string Format(ReferenceToken refToken) {     
112      var attributes = new Dictionary<string, object> {
113        {"ref", refToken.Id},
114        {"name", refToken.Name}};                                       
115      return Prefix + FormatNode(XmlStrings.REFERENCE, attributes, NodeType.Inline) + "\n"; 
116    }
117
118    protected override string Format(NullReferenceToken nullRefToken) {     
119      var attributes = new Dictionary<string, object>{
120        {"name", nullRefToken.Name}};     
121      return Prefix + FormatNode(XmlStrings.NULL, attributes, NodeType.Inline) + "\n";
122    }
123
124    public IEnumerable<string> Format(List<TypeMapping> typeCache) {
125      yield return "<" + XmlStrings.TYPECACHE + ">";
126      foreach (var mapping in typeCache)
127        yield return "  " + FormatNode(
128          XmlStrings.TYPE,
129          mapping.GetDict(),
130          NodeType.Inline,
131          "\n    ");
132      yield return "</" + XmlStrings.TYPECACHE + ">";
133    }
134
135    public static void Serialize(object o, string basename) {     
136      Serialize(o, basename, ConfigurationService.Instance.GetDefaultConfig(XmlFormat.Instance));
137    }
138
139    public static void Serialize(object o, string basename, Configuration configuration) {
140      Serializer s = new Serializer(o, configuration);
141      XmlGenerator xmlGenerator = new XmlGenerator();
142      StreamWriter writer = new StreamWriter(basename + ".xml");
143      foreach (ISerializationToken token in s) {
144        string line = xmlGenerator.Format(token);
145        writer.Write(line);
146        Console.Out.Write(line);
147      }
148      writer.Close();
149      writer = new StreamWriter(basename + "-types.xml");
150      foreach (string line in xmlGenerator.Format(s.TypeCache)) {
151        writer.WriteLine(line);
152        Console.Out.WriteLine(line);
153      }
154      writer.Close();     
155    }
156
157  }
158}
Note: See TracBrowser for help on using the repository browser.