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 | |
---|
1 | using System;
|
---|
2 | using HeuristicLab.Persistence.Core;
|
---|
3 | using HeuristicLab.Persistence.Interfaces;
|
---|
4 | using System.Text;
|
---|
5 | using System.Text.RegularExpressions;
|
---|
6 | using System.Globalization;
|
---|
7 |
|
---|
8 |
|
---|
9 | namespace 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.