- Timestamp:
- 03/12/09 12:19:32 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/New Persistence Exploration/Persistence/Persistence/XmlFormatter.cs
r1323 r1330 1 1 using System.Collections.Generic; 2 2 using System; 3 using System.Xml; 4 using System.Text; 3 5 namespace Persistence { 4 6 public class XmlFormatter { … … 13 15 {typeof (BeginToken), FormatBegin}, 14 16 {typeof (EndToken), FormatEnd}, 15 {typeof (PrimitiveToken), Format Data},17 {typeof (PrimitiveToken), FormatPrimitive}, 16 18 {typeof (ReferenceToken), FormatReference}, 17 19 {typeof (NullReferenceToken), FormatNullReference} 18 20 }; 19 21 depth = 0; 22 } 23 24 private enum NodeType { Start, End, Inline } ; 25 26 private static string FormatNode(string name, Dictionary<string, string> attributes, NodeType type) { 27 StringBuilder sb = new StringBuilder(); 28 sb.Append('<'); 29 if (type == NodeType.End) 30 sb.Append('/'); 31 sb.Append(name); 32 foreach (var attribute in attributes) { 33 sb.Append(' '); 34 sb.Append(attribute.Key); 35 sb.Append("=\""); 36 sb.Append(attribute.Value); 37 sb.Append('"'); 38 } 39 if (type == NodeType.Inline) 40 sb.Append('/'); 41 sb.Append(">"); 42 return sb.ToString(); 20 43 } 21 44 … … 30 53 private string FormatBegin(ISerializationToken token) { 31 54 BeginToken beginToken = (BeginToken)token; 32 string result = 33 String.Format("{0}<COMPOSITE name=\"{1}\" type=\"{2}\" id=\"{3}\">\n", 34 Prefix, beginToken.Accessor.Name, beginToken.Accessor.Get().GetType(), beginToken.Id); 55 string result = Prefix + 56 FormatNode("COMPOSITE", 57 new Dictionary<string, string> 58 { 59 {"name", beginToken.Accessor.Name}, 60 {"type", beginToken.Accessor.Get().GetType().ToString()}, 61 {"id", beginToken.Id.ToString()} 62 }, NodeType.Start) + "\n"; 35 63 depth += 1; 36 64 return result; 37 65 } 38 66 39 private string FormatEnd(ISerializationToken token) { 67 private string FormatEnd(ISerializationToken token) { 40 68 depth -= 1; 41 69 return Prefix + "</COMPOSITE>\n"; 42 70 } 43 71 44 private string Format Data(ISerializationToken token) {72 private string FormatPrimitive(ISerializationToken token) { 45 73 PrimitiveToken dataToken = (PrimitiveToken)token; 46 return String.Format("{0}<PRIMITIVE name=\"{1}\" type=\"{2}\">{3}</PRIMITIVE>\n", 47 Prefix, dataToken.accessor.Name, dataToken.accessor.Get().GetType(), dataToken.Data); 74 Dictionary<string, string> attributes = 75 new Dictionary<string, string> { 76 {"type", dataToken.Accessor.Get().GetType().ToString()}}; 77 if ( dataToken.Accessor.Name != "" ) 78 attributes.Add("name", dataToken.Accessor.Name); 79 if ( dataToken.Id != null ) 80 attributes.Add("id", dataToken.Id.ToString()); 81 return Prefix + 82 FormatNode("PRIMITIVE", attributes, NodeType.Start) + 83 dataToken.Data + "</PRIMITIVE>\n"; 48 84 } 49 85 50 86 private string FormatReference(ISerializationToken token) { 51 ReferenceToken refToken = (ReferenceToken)token; 52 return String.Format("{0}<REFERENCE name=\"{1}\" ref=\"{2}\"/>\n", 53 Prefix, refToken.Name, refToken.Id); 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"; 54 93 } 55 94 56 95 private string FormatNullReference(ISerializationToken token) { 57 96 NullReferenceToken nullRefToken = (NullReferenceToken)token; 58 return String.Format("{0}<NULL name=\"{1}\"/>\n", 59 Prefix, nullRefToken.Name); 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"; 60 101 } 61 102 }
Note: See TracChangeset
for help on using the changeset viewer.