Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone3/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/String2XmlSerializer.cs @ 4695

Last change on this file since 4695 was 1893, checked in by epitzer, 16 years ago

Avoid recompiling regex during string parsing and cache storable attribues for faster saving. (#646)

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