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