Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/String2XmlFormatter.cs @ 1683

Last change on this file since 1683 was 1683, checked in by epitzer, 15 years ago

Fix StackDecomposer, reverse collection before serialization (#603)

File size: 1.2 KB
Line 
1using System;
2using HeuristicLab.Persistence.Core;
3using HeuristicLab.Persistence.Interfaces;
4using System.Text;
5using System.Text.RegularExpressions;
6using HeuristicLab.Persistence.Default.Decomposers.Storable;
7using System.Globalization;
8
9
10namespace HeuristicLab.Persistence.Default.Xml.Primitive {
11
12  [EmptyStorableClass]
13  public class String2XmlFormatter : PrimitiveXmlFormatterBase<string> {
14
15    public override XmlString Format(string s) {
16      StringBuilder sb = new StringBuilder();
17      sb.Append("<![CDATA[");
18      sb.Append(s.Replace("]]>", "]]]]><![CDATA[>"));
19      sb.Append("]]>");
20      return new XmlString(sb.ToString());
21    }
22
23    public override string Parse(XmlString x) {
24      StringBuilder sb = new StringBuilder();
25      Regex re = new Regex(@"<!\[CDATA\[((?:[^]]|\](?!\]>))*)\]\]>", RegexOptions.Singleline);
26      foreach (Match m in re.Matches(x.Data)) {
27        sb.Append(m.Groups[1]);
28      }
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.");
32      return sb.ToString();
33    }
34  }
35}
Note: See TracBrowser for help on using the repository browser.