Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/27/14 10:13:24 (10 years ago)
Author:
epitzer
Message:

#2096 introduce base 64 sections to deal with special characters within CDATA

File:
1 edited

Legend:

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

    r9456 r10895  
    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();
Note: See TracChangeset for help on using the changeset viewer.