Free cookie consent management tool by TermsFeed Policy Generator

source: branches/New Persistence Exploration/Persistence/Persistence/XmlFormatter.cs @ 1357

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

Pluginification and major refactoring. (#506)

File size: 4.1 KB
Line 
1using System.Collections.Generic;
2using System;
3using System.Text;
4using HeuristicLab.Persistence.Interfaces;
5
6namespace HeuristicLab.Persistence {
7  public class XmlFormatter {
8
9    delegate string Formatter(ISerializationToken token);
10
11    private readonly Dictionary<Type, Formatter> formatters;
12    private int depth;
13
14    public XmlFormatter() {
15      formatters = new Dictionary<Type, Formatter>{
16                       {typeof (BeginToken), FormatBegin},
17                       {typeof (EndToken), FormatEnd},
18                       {typeof (PrimitiveToken), FormatPrimitive},
19                       {typeof (ReferenceToken), FormatReference},
20                       {typeof (NullReferenceToken), FormatNullReference}
21                     };
22      depth = 0;
23    }
24
25    private enum NodeType { Start, End, Inline } ;
26
27    private static string FormatNode(string name, Dictionary<string, string> attributes, NodeType type) {
28      StringBuilder sb = new StringBuilder();
29      sb.Append('<');
30      if (type == NodeType.End)
31        sb.Append('/');
32      sb.Append(name);     
33      foreach (var attribute in attributes) {
34        sb.Append(' ');
35        sb.Append(attribute.Key);
36        sb.Append("=\"");
37        sb.Append(attribute.Value);
38        sb.Append('"');
39      }
40      if (type == NodeType.Inline)
41        sb.Append('/');
42      sb.Append(">");
43      return sb.ToString();     
44    }
45
46    public string Format(ISerializationToken token) {
47      return formatters[token.GetType()](token);
48    }
49
50    private string Prefix {
51      get { return new string(' ', depth * 2); }
52    }
53
54    private string FormatBegin(ISerializationToken token) {     
55      BeginToken beginToken = (BeginToken)token;
56      var attributes = new Dictionary<string, string> {{"name", beginToken.Name}};
57      if ( beginToken.TypeId != null )
58        attributes.Add("typeId", beginToken.TypeId.ToString());
59      if ( beginToken.Id != null )
60        attributes.Add("id", beginToken.Id.ToString());                                           
61      string result = Prefix +
62                      FormatNode("COMPOSITE", attributes, NodeType.Start) + "\n";
63      depth += 1;
64      return result;
65    }
66
67    private string FormatEnd(ISerializationToken token) {     
68      depth -= 1;
69      return Prefix + "</COMPOSITE>\n";
70    }
71
72    private string FormatPrimitive(ISerializationToken token) {
73      PrimitiveToken dataToken = (PrimitiveToken)token;
74      Dictionary<string, string> attributes =
75        new Dictionary<string, string> {
76            {"typeId", dataToken.TypeId.ToString()}};
77      if ( !string.IsNullOrEmpty(dataToken.Name) )
78        attributes.Add("name", dataToken.Name);
79      if ( dataToken.Id != null )
80        attributes.Add("id", dataToken.Id.ToString());
81      return Prefix +
82        FormatNode("PRIMITIVE", attributes, NodeType.Start) +
83        dataToken.SerialData + "</PRIMITIVE>\n";     
84    }
85
86    private string FormatReference(ISerializationToken token) {
87      ReferenceToken refToken = (ReferenceToken) token;
88      Dictionary<string, string> attributes =
89        new Dictionary<string, string> {{"ref", refToken.Id.ToString()}};
90      if ( refToken.Name != null )
91        attributes.Add("name", refToken.Name);
92      return Prefix + FormatNode("REFERENCE", attributes, NodeType.Inline) + "\n"; 
93    }
94
95    private string FormatNullReference(ISerializationToken token) {
96      NullReferenceToken nullRefToken = (NullReferenceToken)token;
97      Dictionary<string, string> attributes = new Dictionary<string, string>();
98      if (nullRefToken.Name != null)
99        attributes.Add("name", nullRefToken.Name);
100      return Prefix + FormatNode("NULL", attributes, NodeType.Inline) + "\n";
101    }
102
103    public IEnumerable<string> Format(Dictionary<string, int> typeCache) {
104      yield return "<TYPECACHE>";
105      foreach ( var pair in typeCache ) {
106        yield return String.Format("  <TYPE id=\"{0}\" name=\"{1}\"/>",
107                                   pair.Value, pair.Key);       
108      }
109      yield return "</TYPECACHE>";
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.