Changeset 10895 for trunk/sources
- Timestamp:
- 05/27/14 10:13:24 (11 years ago)
- Location:
- trunk/sources
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/String2XmlSerializer.cs
r9456 r10895 20 20 #endregion 21 21 22 using System; 22 23 using System.Text; 23 24 using System.Text.RegularExpressions; 24 25 using HeuristicLab.Persistence.Core; 25 26 26 27 27 namespace HeuristicLab.Persistence.Default.Xml.Primitive { … … 42 42 sb.Append(s.Replace("]]>", "]]]]><![CDATA[>")); 43 43 sb.Append("]]>"); 44 return new XmlString(sb.ToString()); 44 s = special.Replace(sb.ToString(), m => ToBase64Tag(m.Value)); 45 return new XmlString(s); 45 46 } 46 47 47 private static Regex re = new Regex(@"<!\[CDATA\[((?:[^]]|\](?!\]>))*)\]\]>", RegexOptions.Singleline); 48 private static readonly Regex re = new Regex(@"<!\[CDATA\[((?:[^]]|\](?!\]>))*)\]\]>|<Base64>([^<]*)</Base64>", RegexOptions.Singleline); 49 private static readonly Regex special = new Regex(@"[\x00-\x08\x0b\x0c\x0e-\x1f]+", RegexOptions.Singleline); 50 51 private static string ToBase64Tag(string s) { 52 return new StringBuilder() 53 .Append("]]><Base64>") 54 .Append(Convert.ToBase64String(Encoding.ASCII.GetBytes(s))) 55 .Append("</Base64><![CDATA[") 56 .ToString(); 57 } 48 58 49 59 /// <summary> … … 55 65 StringBuilder sb = new StringBuilder(); 56 66 foreach (Match m in re.Matches(x.Data)) { 57 sb.Append(m.Groups[1]); 67 if (m.Groups[1].Success) 68 sb.Append(m.Groups[1].Value); 69 else if (m.Groups[2].Success) { 70 sb.Append(Encoding.ASCII.GetString(Convert.FromBase64String(m.Groups[2].Value))); 71 } 58 72 } 59 73 string result = sb.ToString(); -
trunk/sources/HeuristicLab.Tests/HeuristicLab.Persistence-3.3/UseCases.cs
r9778 r10895 1441 1441 } 1442 1442 1443 [TestMethod] 1444 [TestCategory("Persistence")] 1445 [TestProperty("Time", "short")] 1446 public void TestSpecialCharacters() { 1447 var s = "abc" + "\x15" + "def"; 1448 XmlGenerator.Serialize(s, tempFile); 1449 var newS = XmlParser.Deserialize(tempFile); 1450 Assert.AreEqual(s, newS); 1451 } 1452 1453 [TestMethod] 1454 [TestCategory("Persistence")] 1455 [TestProperty("Time", "short")] 1456 public void TestByteArray() { 1457 var b = new byte[3]; 1458 b[0] = 0; 1459 b[1] = 200; 1460 b[2] = byte.MaxValue; 1461 XmlGenerator.Serialize(b, tempFile); 1462 var newB = (byte[]) XmlParser.Deserialize(tempFile); 1463 CollectionAssert.AreEqual(b, newB); 1464 } 1465 1466 1443 1467 [ClassInitialize] 1444 1468 public static void Initialize(TestContext testContext) {
Note: See TracChangeset
for help on using the changeset viewer.