Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/String2XmlSerializer.cs @ 1835

Last change on this file since 1835 was 1823, checked in by epitzer, 16 years ago

Namespace refactoring: rename formatters & decomposers -> primitive and composite serializers. (#603)

File size: 1.2 KB
RevLine 
[1476]1using System;
2using HeuristicLab.Persistence.Core;
3using HeuristicLab.Persistence.Interfaces;
[1570]4using System.Text;
[1615]5using System.Text.RegularExpressions;
[1823]6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[1679]7using System.Globalization;
[1476]8
[1570]9
[1476]10namespace HeuristicLab.Persistence.Default.Xml.Primitive {
11
12  [EmptyStorableClass]
[1823]13  public class String2XmlSerializer : PrimitiveXmlSerializerBase<string> {
[1566]14
[1615]15    public override XmlString Format(string s) {
[1564]16      StringBuilder sb = new StringBuilder();
17      sb.Append("<![CDATA[");
18      sb.Append(s.Replace("]]>", "]]]]><![CDATA[>"));
19      sb.Append("]]>");
[1566]20      return new XmlString(sb.ToString());
[1564]21    }
[1476]22
[1615]23    public override string Parse(XmlString x) {
[1476]24      StringBuilder sb = new StringBuilder();
[1615]25      Regex re = new Regex(@"<!\[CDATA\[((?:[^]]|\](?!\]>))*)\]\]>", RegexOptions.Singleline);
26      foreach (Match m in re.Matches(x.Data)) {
27        sb.Append(m.Groups[1]);
[1476]28      }
[1625]29      string result = sb.ToString();
30      if (result.Length == 0 && x.Data.Length > 0 && !x.Data.Equals("<![CDATA[]]>"))
31        throw new PersistenceException("Invalid CDATA section during string parsing.");
[1476]32      return sb.ToString();
33    }
[1566]34  }
[1476]35}
Note: See TracBrowser for help on using the repository browser.