Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1428


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

Refactoring of Generator interface. (#506)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/New Persistence Exploration/Persistence/Persistence/Default/Xml/XmlGenerator.cs

    r1363 r1428  
    1616    public const string TYPE = "TYPE";
    1717  }
    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> {
    2043   
    21     delegate string Formatter(ISerializationToken token);
    22 
    23     private readonly Dictionary<Type, Formatter> formatters;
    2444    private int depth;
    2545
    2646    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                      };
    3447      depth = 0;
    3548    }
     
    6679    }
    6780
    68     public string Format(ISerializationToken token) {
    69       return formatters[token.GetType()](token);
    70     }
    71 
    7281    private string Prefix {
    7382      get { return new string(' ', depth * 2); }
    7483    }
    7584
    76     private string FormatBegin(ISerializationToken token) {     
    77       BeginToken beginToken = (BeginToken)token;
     85    protected override string Format(BeginToken beginToken) {           
    7886      var attributes = new Dictionary<string, object> {
    7987        {"name", beginToken.Name},
     
    8694    }
    8795
    88     private string FormatEnd(ISerializationToken token) {     
     96    protected override string Format(EndToken endToken) {     
    8997      depth -= 1;
    9098      return Prefix + "</" + XmlStrings.COMPOSITE + ">\n";
    9199    }
    92100
    93     private string FormatPrimitive(ISerializationToken token) {
    94       PrimitiveToken dataToken = (PrimitiveToken)token;
     101    protected override string Format(PrimitiveToken dataToken) {     
    95102      var attributes =
    96103        new Dictionary<string, object> {
     
    103110    }
    104111
    105     private string FormatReference(ISerializationToken token) {
    106       ReferenceToken refToken = (ReferenceToken) token;
     112    protected override string Format(ReferenceToken refToken) {     
    107113      var attributes = new Dictionary<string, object> {
    108114        {"ref", refToken.Id},
     
    111117    }
    112118
    113     private string FormatNullReference(ISerializationToken token) {
    114       NullReferenceToken nullRefToken = (NullReferenceToken)token;
     119    protected override string Format(NullReferenceToken nullRefToken) {     
    115120      var attributes = new Dictionary<string, object>{
    116121        {"name", nullRefToken.Name}};     
Note: See TracChangeset for help on using the changeset viewer.