Free cookie consent management tool by TermsFeed Policy Generator

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

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

Cleanup DeSerializer code. (#506)

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