Changeset 1428
- Timestamp:
- 03/26/09 12:38:32 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/New Persistence Exploration/Persistence/Persistence/Default/Xml/XmlGenerator.cs
r1363 r1428 16 16 public const string TYPE = "TYPE"; 17 17 } 18 19 public class XmlGenerator { 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> { 20 43 21 delegate string Formatter(ISerializationToken token);22 23 private readonly Dictionary<Type, Formatter> formatters;24 44 private int depth; 25 45 26 46 public XmlGenerator() { 27 formatters = new Dictionary<Type, Formatter>{28 {typeof (BeginToken), FormatBegin},29 {typeof (EndToken), FormatEnd},30 {typeof (PrimitiveToken), FormatPrimitive},31 {typeof (ReferenceToken), FormatReference},32 {typeof (NullReferenceToken), FormatNullReference}33 };34 47 depth = 0; 35 48 } … … 66 79 } 67 80 68 public string Format(ISerializationToken token) {69 return formatters[token.GetType()](token);70 }71 72 81 private string Prefix { 73 82 get { return new string(' ', depth * 2); } 74 83 } 75 84 76 private string FormatBegin(ISerializationToken token) { 77 BeginToken beginToken = (BeginToken)token; 85 protected override string Format(BeginToken beginToken) { 78 86 var attributes = new Dictionary<string, object> { 79 87 {"name", beginToken.Name}, … … 86 94 } 87 95 88 pr ivate string FormatEnd(ISerializationToken token) {96 protected override string Format(EndToken endToken) { 89 97 depth -= 1; 90 98 return Prefix + "</" + XmlStrings.COMPOSITE + ">\n"; 91 99 } 92 100 93 private string FormatPrimitive(ISerializationToken token) { 94 PrimitiveToken dataToken = (PrimitiveToken)token; 101 protected override string Format(PrimitiveToken dataToken) { 95 102 var attributes = 96 103 new Dictionary<string, object> { … … 103 110 } 104 111 105 private string FormatReference(ISerializationToken token) { 106 ReferenceToken refToken = (ReferenceToken) token; 112 protected override string Format(ReferenceToken refToken) { 107 113 var attributes = new Dictionary<string, object> { 108 114 {"ref", refToken.Id}, … … 111 117 } 112 118 113 private string FormatNullReference(ISerializationToken token) { 114 NullReferenceToken nullRefToken = (NullReferenceToken)token; 119 protected override string Format(NullReferenceToken nullRefToken) { 115 120 var attributes = new Dictionary<string, object>{ 116 121 {"name", nullRefToken.Name}};
Note: See TracChangeset
for help on using the changeset viewer.