Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/12/14 13:26:18 (11 years ago)
Author:
pfleck
Message:
  • Merged trunk into preprocessing branch.
Location:
branches/DataPreprocessing
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/DataPreprocessing

  • branches/DataPreprocessing/HeuristicLab.Persistence

  • branches/DataPreprocessing/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/Char2XmlFormatter.cs

    r9456 r11009  
    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  }
  • branches/DataPreprocessing/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/String2XmlSerializer.cs

    r9456 r11009  
    2020#endregion
    2121
     22using System;
    2223using System.Text;
    2324using System.Text.RegularExpressions;
    2425using HeuristicLab.Persistence.Core;
    25 
    2626
    2727namespace HeuristicLab.Persistence.Default.Xml.Primitive {
     
    4242      sb.Append(s.Replace("]]>", "]]]]><![CDATA[>"));
    4343      sb.Append("]]>");
    44       return new XmlString(sb.ToString());
     44      s = special.Replace(sb.ToString(), m => ToBase64Tag(m.Value));
     45      return new XmlString(s);
    4546    }
    4647
    47     private static Regex re = new Regex(@"<!\[CDATA\[((?:[^]]|\](?!\]>))*)\]\]>", RegexOptions.Singleline);
     48    private static readonly Regex re = new Regex(@"<!\[CDATA\[((?:[^]]|\](?!\]>))*)\]\]>|<Base64>([^<]*)</Base64>", RegexOptions.Singleline);
     49    private static readonly Regex special = new Regex(@"[\x00-\x08\x0b\x0c\x0e-\x1f]+", RegexOptions.Singleline);
     50
     51    private static string ToBase64Tag(string s) {
     52      return new StringBuilder()
     53        .Append("]]><Base64>")
     54        .Append(Convert.ToBase64String(Encoding.ASCII.GetBytes(s)))
     55        .Append("</Base64><![CDATA[")
     56        .ToString();
     57    }
    4858
    4959    /// <summary>
     
    5565      StringBuilder sb = new StringBuilder();
    5666      foreach (Match m in re.Matches(x.Data)) {
    57         sb.Append(m.Groups[1]);
     67        if (m.Groups[1].Success)
     68          sb.Append(m.Groups[1].Value);
     69        else if (m.Groups[2].Success) {
     70          sb.Append(Encoding.ASCII.GetString(Convert.FromBase64String(m.Groups[2].Value)));
     71        }
    5872      }
    5973      string result = sb.ToString();
  • branches/DataPreprocessing/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/System.Drawing/Font2XmlSerializer.cs

    r9988 r11009  
    2828    public override XmlString Format(Font font) {
    2929      return new XmlString(string.Format("{0};{1};{2};{3};{4};{5}",
    30         font.FontFamily.Name,
     30        GetFontFamilyName(font.FontFamily),
    3131        Float2XmlSerializer.FormatG8(font.Size),
    3232        font.Style,
     
    3939      string[] tokens = fontData.Data.Split(';');
    4040      return new Font(
    41         tokens[0],
     41        GetFontFamily(tokens[0]),
    4242        Float2XmlSerializer.ParseG8(tokens[1]),
    4343        (FontStyle)Enum.Parse(typeof(FontStyle), tokens[2]),
     
    4646        bool.Parse(tokens[5]));
    4747    }
     48
     49    public const string GENERIC_MONOSPACE_NAME = "_GenericMonospace";
     50    public const string GENERIC_SANS_SERIF_NAME = "_GenericSansSerif";
     51    public const string GENERIC_SERIF_NAME = "_GenericSerif";
     52
     53    public static FontFamily GetFontFamily(string name) {
     54      if (name == GENERIC_MONOSPACE_NAME) return FontFamily.GenericMonospace;
     55      if (name == GENERIC_SANS_SERIF_NAME) return FontFamily.GenericSansSerif;
     56      if (name == GENERIC_SERIF_NAME) return FontFamily.GenericSerif;
     57      return new FontFamily(name);
     58    }
     59
     60    public static string GetFontFamilyName(FontFamily ff) {
     61      if (ff.Equals(FontFamily.GenericMonospace)) return GENERIC_MONOSPACE_NAME;
     62      if (ff.Equals(FontFamily.GenericSansSerif)) return GENERIC_SANS_SERIF_NAME;
     63      if (ff.Equals(FontFamily.GenericSerif)) return GENERIC_SERIF_NAME;
     64      return ff.Name;
     65    }
     66
    4867  }
    4968}
Note: See TracChangeset for help on using the changeset viewer.