#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 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.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("]]>");
return new XmlString(sb.ToString());
}
private static Regex re = new Regex(@"))*)\]\]>", RegexOptions.Singleline);
///
/// 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)) {
sb.Append(m.Groups[1]);
}
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;
}
}
}