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
|
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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 | using System.Globalization;
|
---|
8 |
|
---|
9 |
|
---|
10 | namespace HeuristicLab.Persistence.Default.Xml.Primitive {
|
---|
11 |
|
---|
12 | [EmptyStorableClass]
|
---|
13 | public class String2XmlSerializer : PrimitiveXmlSerializerBase<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.