Changeset 1570
- Timestamp:
- 04/16/09 16:10:39 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/String2XmlFormatter.cs
r1566 r1570 1 1 using System; 2 using System.Text;3 2 using HeuristicLab.Persistence.Core; 4 3 using HeuristicLab.Persistence.Interfaces; 5 using System.Xml; 4 using System.Text; 5 6 6 7 7 namespace HeuristicLab.Persistence.Default.Xml.Primitive { … … 10 10 public class String2XmlFormatter : FormatterBase<string, XmlString> { 11 11 12 public override XmlString Format(string s) { 12 public override XmlString Format(string s) { 13 13 StringBuilder sb = new StringBuilder(); 14 14 sb.Append("<![CDATA["); … … 20 20 private static readonly string[] separators = new string[] { "<![CDATA[", "]]>" }; 21 21 22 public override string Parse(XmlString x) { 22 public override string Parse(XmlString x) { 23 23 StringBuilder sb = new StringBuilder(); 24 24 foreach (string s in x.Data.Split(separators, -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs
r1566 r1570 25 25 26 26 private int depth; 27 private int Depth { 28 get { 29 return depth; 30 } 31 set { 32 depth = value; 33 prefix = new string(' ', depth * 2); 34 } 35 } 36 37 private string prefix; 38 27 39 28 40 public XmlGenerator() { 29 depth = 0;41 Depth = 0; 30 42 } 31 43 32 44 private enum NodeType { Start, End, Inline } ; 33 45 34 private static string FormatNode(string name, 35 Dictionary<string, object> attributes, 36 NodeType type) { 37 return FormatNode(name, attributes, type, " "); 38 } 39 40 private static string FormatNode(string name, 41 Dictionary<string, object> attributes, 42 NodeType type, string space) { 43 StringBuilder sb = new StringBuilder(); 44 sb.Append('<'); 45 if (type == NodeType.End) 46 sb.Append('/'); 46 private static void AddXmlTagContent(StringBuilder sb, string name, Dictionary<string, object> attributes) { 47 47 sb.Append(name); 48 48 foreach (var attribute in attributes) { 49 49 if (attribute.Value != null && !string.IsNullOrEmpty(attribute.Value.ToString())) { 50 sb.Append( space);50 sb.Append(' '); 51 51 sb.Append(attribute.Key); 52 52 sb.Append("=\""); … … 55 55 } 56 56 } 57 if (type == NodeType.Inline) 58 sb.Append('/'); 57 } 58 59 private static void AddXmlStartTag(StringBuilder sb, string name, Dictionary<string, object> attributes) { 60 sb.Append('<'); 61 AddXmlTagContent(sb, name, attributes); 62 sb.Append('>'); 63 } 64 65 private static void AddXmlInlineTag(StringBuilder sb, string name, Dictionary<string, object> attributes) { 66 sb.Append('<'); 67 AddXmlTagContent(sb, name, attributes); 68 sb.Append("/>"); 69 } 70 71 private static void AddXmlEndTag(StringBuilder sb, string name) { 72 sb.Append("</"); 73 sb.Append(name); 59 74 sb.Append(">"); 75 } 76 77 private string CreateNodeStart(string name, Dictionary<string, object> attributes) { 78 StringBuilder sb = new StringBuilder(); 79 sb.Append(prefix); 80 Depth += 1; 81 AddXmlStartTag(sb, name, attributes); 82 sb.Append("\r\n"); 60 83 return sb.ToString(); 61 84 } 62 85 63 private string Prefix { 64 get { return new string(' ', depth * 2); } 86 private string CreateNodeStart(string name) { 87 return CreateNodeStart(name, new Dictionary<string, object>()); 88 } 89 90 private string CreateNodeEnd(string name) { 91 Depth -= 1; 92 StringBuilder sb = new StringBuilder(); 93 sb.Append(prefix); 94 AddXmlEndTag(sb, name); 95 sb.Append("\r\n"); 96 return sb.ToString(); 97 } 98 99 private string CreateNode(string name, Dictionary<string, object> attributes) { 100 StringBuilder sb = new StringBuilder(); 101 sb.Append(prefix); 102 AddXmlInlineTag(sb, name, attributes); 103 sb.Append("\r\n"); 104 return sb.ToString(); 105 } 106 107 private string CreateNode(string name, Dictionary<string, object> attributes, string content) { 108 StringBuilder sb = new StringBuilder(); 109 sb.Append(prefix); 110 AddXmlStartTag(sb, name, attributes); 111 sb.Append(content); 112 sb.Append("</").Append(name).Append(">\r\n"); 113 return sb.ToString(); 65 114 } 66 115 67 116 protected override string Format(BeginToken beginToken) { 68 var attributes = new Dictionary<string, object> { 69 {"name", beginToken.Name}, 70 {"typeId", beginToken.TypeId}, 71 {"id", beginToken.Id}}; 72 string result = Prefix + 73 FormatNode(XmlStrings.COMPOSITE, attributes, NodeType.Start) + "\r\n"; 74 depth += 1; 75 return result; 117 return CreateNodeStart( 118 XmlStrings.COMPOSITE, 119 new Dictionary<string, object> { 120 {"name", beginToken.Name}, 121 {"typeId", beginToken.TypeId}, 122 {"id", beginToken.Id}}); 76 123 } 77 124 78 125 protected override string Format(EndToken endToken) { 79 depth -= 1; 80 return Prefix + "</" + XmlStrings.COMPOSITE + ">\r\n"; 126 return CreateNodeEnd(XmlStrings.COMPOSITE); 81 127 } 82 128 83 129 protected override string Format(PrimitiveToken dataToken) { 84 var attributes =130 return CreateNode(XmlStrings.PRIMITIVE, 85 131 new Dictionary<string, object> { 86 132 {"typeId", dataToken.TypeId}, 87 133 {"name", dataToken.Name}, 88 {"id", dataToken.Id}}; 89 return Prefix + 90 FormatNode(XmlStrings.PRIMITIVE, attributes, NodeType.Start) + 91 ((XmlString)dataToken.SerialData).Data + "</" + XmlStrings.PRIMITIVE + ">\r\n"; 134 {"id", dataToken.Id}}, 135 ((XmlString)dataToken.SerialData).Data); 92 136 } 93 137 94 138 protected override string Format(ReferenceToken refToken) { 95 var attributes = new Dictionary<string, object> {96 {"ref", refToken.Id},97 {"name", refToken.Name}};98 return Prefix + FormatNode(XmlStrings.REFERENCE, attributes, NodeType.Inline) + "\r\n";139 return CreateNode(XmlStrings.REFERENCE, 140 new Dictionary<string, object> { 141 {"ref", refToken.Id}, 142 {"name", refToken.Name}}); 99 143 } 100 144 101 145 protected override string Format(NullReferenceToken nullRefToken) { 102 var attributes = new Dictionary<string, object>{103 {"name", nullRefToken.Name}};104 return Prefix + FormatNode(XmlStrings.NULL, attributes, NodeType.Inline) + "\r\n";146 return CreateNode(XmlStrings.NULL, 147 new Dictionary<string, object>{ 148 {"name", nullRefToken.Name}}); 105 149 } 106 150 107 151 protected override string Format(MetaInfoBeginToken metaInfoBeginToken) { 108 string result = Prefix + "<" + XmlStrings.METAINFO + ">\r\n"; 109 depth += 1; 110 return result; 152 return CreateNodeStart(XmlStrings.METAINFO); 111 153 } 112 154 113 155 protected override string Format(MetaInfoEndToken metaInfoEndToken) { 114 depth -= 1; 115 return Prefix + "</" + XmlStrings.METAINFO + ">\r\n"; 156 return CreateNodeEnd(XmlStrings.METAINFO); 116 157 } 117 158 118 159 public IEnumerable<string> Format(List<TypeMapping> typeCache) { 119 yield return "<" + XmlStrings.TYPECACHE + ">";160 yield return CreateNodeStart(XmlStrings.TYPECACHE); 120 161 foreach (var mapping in typeCache) 121 yield return " " + FormatNode(162 yield return CreateNode( 122 163 XmlStrings.TYPE, 123 mapping.GetDict(), 124 NodeType.Inline, 125 "\r\n "); 126 yield return "</" + XmlStrings.TYPECACHE + ">"; 164 mapping.GetDict()); 165 yield return CreateNodeEnd(XmlStrings.TYPECACHE); 127 166 } 128 167 … … 142 181 string line = generator.Format(token); 143 182 writer.Write(line); 144 logger.Debug(line );183 logger.Debug(line.TrimEnd()); 145 184 } 146 185 writer.Flush(); … … 149 188 foreach (string line in generator.Format(serializer.TypeCache)) { 150 189 writer.WriteLine(line); 151 logger.Debug(line );190 logger.Debug(line.TrimEnd()); 152 191 } 153 192 writer.Flush();
Note: See TracChangeset
for help on using the changeset viewer.