#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Text;
using System.Text.RegularExpressions;
using HeuristicLab.Persistence.Core;
namespace HeuristicLab.Persistence.Default.Xml.Primitive {
///
/// Serializes a string to XML by embedding into a CDATA block.
///
public sealed class String2XmlSerializer : PrimitiveXmlSerializerBase {
///
/// Formats the specified string.
///
/// The string.
/// An XmlString that embeds the string s in a CDATA section.
public override XmlString Format(string s) {
StringBuilder sb = new StringBuilder();
sb.Append("", "]]]]>"));
sb.Append("]]>");
s = special.Replace(sb.ToString(), m => ToBase64Tag(m.Value));
return new XmlString(s);
}
private static readonly Regex re = new Regex(@"))*)\]\]>|([^<]*)", RegexOptions.Singleline);
private static readonly Regex special = new Regex(@"[\x00-\x08\x0b\x0c\x0e-\x1f]+", RegexOptions.Singleline);
private static string ToBase64Tag(string s) {
return new StringBuilder()
.Append("]]>")
.Append(Convert.ToBase64String(Encoding.ASCII.GetBytes(s)))
.Append("
/// Parses the specified XmlString into a string.
///
/// The XMLString.
/// The plain string contained in the XML CDATA section.
public override string Parse(XmlString x) {
StringBuilder sb = new StringBuilder();
foreach (Match m in re.Matches(x.Data)) {
if (m.Groups[1].Success)
sb.Append(m.Groups[1].Value);
else if (m.Groups[2].Success) {
sb.Append(Encoding.ASCII.GetString(Convert.FromBase64String(m.Groups[2].Value)));
}
}
string result = sb.ToString();
if (result.Length == 0 && x.Data.Length > 0 && !x.Data.Equals(""))
throw new PersistenceException("Invalid CDATA section during string parsing.");
return result;
}
}
}