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
|
Rev | Line | |
---|
[1476] | 1 | using System;
|
---|
| 2 | using HeuristicLab.Persistence.Core;
|
---|
| 3 | using HeuristicLab.Persistence.Interfaces;
|
---|
[1570] | 4 | using System.Text;
|
---|
[1615] | 5 | using System.Text.RegularExpressions;
|
---|
[1823] | 6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[1679] | 7 | using System.Globalization;
|
---|
[1476] | 8 |
|
---|
[1570] | 9 |
|
---|
[1476] | 10 | namespace 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.