Free cookie consent management tool by TermsFeed Policy Generator

Changeset 10960 for trunk


Ignore:
Timestamp:
06/11/14 09:42:05 (10 years ago)
Author:
epitzer
Message:

#2096 add treatment of special characters also to Char2XmlSerializer

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/Char2XmlFormatter.cs

    r9456 r10960  
    2020#endregion
    2121
     22using System;
     23using System.Text;
     24using System.Text.RegularExpressions;
    2225using HeuristicLab.Persistence.Core;
    2326using HeuristicLab.Persistence.Interfaces;
     
    2730  internal sealed class Char2XmlSerializer : PrimitiveSerializerBase<char, XmlString> {
    2831
     32    private static readonly Regex base64Regex = new Regex("<Base64>(.+)</Base64>");
     33
     34    private static bool IsSpecial(char c) {
     35      return c <= 0x1F && c != 0x9 && c != 0xA && c != 0xD;
     36    }
     37
     38    private static string ToBase64String(char c) {
     39      return string.Format("<Base64>{0}</Base64>", Convert.ToBase64String(Encoding.ASCII.GetBytes(new[] {c})));
     40    }
     41
    2942    public override XmlString Format(char c) {
    30       return new XmlString(new string(c, 1));
     43      return new XmlString(IsSpecial(c) ? ToBase64String(c) : new string(c, 1));
    3144    }
    3245
    3346    public override char Parse(XmlString x) {
    34       if (x.Data.Length != 1)
    35         throw new PersistenceException("Invalid character format, XML string length != 1");
    36       return x.Data[0];
     47      if (x.Data.Length <= 1) return x.Data[0];
     48      var m = base64Regex.Match(x.Data);
     49      if (m.Success)
     50        return Encoding.ASCII.GetString(Convert.FromBase64String(m.Groups[1].Value))[0];
     51      throw new PersistenceException("Invalid character format, XML string length != 1");
    3752    }
    3853  }
Note: See TracChangeset for help on using the changeset viewer.