Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/12/09 12:19:32 (15 years ago)
Author:
epitzer
Message:

More compact XML Formatting (no empty names, referencable primitives, compact arrays & lists). (#506)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/New Persistence Exploration/Persistence/Persistence/XmlFormatter.cs

    r1323 r1330  
    11using System.Collections.Generic;
    22using System;
     3using System.Xml;
     4using System.Text;
    35namespace Persistence {
    46  public class XmlFormatter {
     
    1315                       {typeof (BeginToken), FormatBegin},
    1416                       {typeof (EndToken), FormatEnd},
    15                        {typeof (PrimitiveToken), FormatData},
     17                       {typeof (PrimitiveToken), FormatPrimitive},
    1618                       {typeof (ReferenceToken), FormatReference},
    1719                       {typeof (NullReferenceToken), FormatNullReference}
    1820                     };
    1921      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();     
    2043    }
    2144
     
    3053    private string FormatBegin(ISerializationToken token) {
    3154      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";
    3563      depth += 1;
    3664      return result;
    3765    }
    3866
    39     private string FormatEnd(ISerializationToken token) {
     67    private string FormatEnd(ISerializationToken token) {     
    4068      depth -= 1;
    4169      return Prefix + "</COMPOSITE>\n";
    4270    }
    4371
    44     private string FormatData(ISerializationToken token) {
     72    private string FormatPrimitive(ISerializationToken token) {
    4573      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";     
    4884    }
    4985
    5086    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"; 
    5493    }
    5594
    5695    private string FormatNullReference(ISerializationToken token) {
    5796      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";
    60101    }
    61102  }
Note: See TracChangeset for help on using the changeset viewer.