Skip to content

Commit

Permalink
Replacing invalid xml character by unicode escape character (microsof…
Browse files Browse the repository at this point in the history
…t#437)

* Replace invalid xml character by unicode escape character
  • Loading branch information
Faizan2304 authored Feb 7, 2017
1 parent 2f16af1 commit 4f19865
Show file tree
Hide file tree
Showing 17 changed files with 50 additions and 88 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,4 @@ Error Details: {1}:{2}</value>
<data name="TrxLoggerResultsFileOverwriteWarning" xml:space="preserve">
<value>WARNING: Overwriting results file: {0}</value>
</data>
<data name="PlaceHolderForInvalidChar" xml:space="preserve">
<value>?</value>
<comment>Place holder for invalid xml character in trx file</comment>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,6 @@ Podrobnosti o chybě: {1}: {2}</target>
<target state="new">WARNING: Overwriting results file: {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PlaceHolderForInvalidChar">
<source>?</source>
<target state="new">?</target>
<note>Place holder for invalid xml character in trx file</note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,6 @@ Fehlerdetails: {1}: {2}</target>
<target state="new">WARNING: Overwriting results file: {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PlaceHolderForInvalidChar">
<source>?</source>
<target state="new">?</target>
<note>Place holder for invalid xml character in trx file</note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,6 @@ Detalles del error: {1}: {2}</target>
<target state="new">WARNING: Overwriting results file: {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PlaceHolderForInvalidChar">
<source>?</source>
<target state="new">?</target>
<note>Place holder for invalid xml character in trx file</note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,6 @@ Détails de l’erreur : {1} : {2}</target>
<target state="new">WARNING: Overwriting results file: {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PlaceHolderForInvalidChar">
<source>?</source>
<target state="new">?</target>
<note>Place holder for invalid xml character in trx file</note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,6 @@ Dettagli errore: {1}: {2}</target>
<target state="new">WARNING: Overwriting results file: {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PlaceHolderForInvalidChar">
<source>?</source>
<target state="new">?</target>
<note>Place holder for invalid xml character in trx file</note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,6 @@ Error Details: {1}:{2}</source>
<target state="new">WARNING: Overwriting results file: {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PlaceHolderForInvalidChar">
<source>?</source>
<target state="new">?</target>
<note>Place holder for invalid xml character in trx file</note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,6 @@ Error Details: {1}:{2}</source>
<target state="new">WARNING: Overwriting results file: {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PlaceHolderForInvalidChar">
<source>?</source>
<target state="new">?</target>
<note>Place holder for invalid xml character in trx file</note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,6 @@ Szczegóły błędu: {1}: {2}</target>
<target state="new">WARNING: Overwriting results file: {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PlaceHolderForInvalidChar">
<source>?</source>
<target state="new">?</target>
<note>Place holder for invalid xml character in trx file</note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,6 @@ Detalhes do erro: {1}: {2}</target>
<target state="new">WARNING: Overwriting results file: {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PlaceHolderForInvalidChar">
<source>?</source>
<target state="new">?</target>
<note>Place holder for invalid xml character in trx file</note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,6 @@ Error Details: {1}:{2}</source>
<target state="new">WARNING: Overwriting results file: {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PlaceHolderForInvalidChar">
<source>?</source>
<target state="new">?</target>
<note>Place holder for invalid xml character in trx file</note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,6 @@ Hata ayrıntıları: {1}: {2}</target>
<target state="new">WARNING: Overwriting results file: {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PlaceHolderForInvalidChar">
<source>?</source>
<target state="new">?</target>
<note>Place holder for invalid xml character in trx file</note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,6 @@ Error Details: {1}:{2}</source>
<target state="new">WARNING: Overwriting results file: {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PlaceHolderForInvalidChar">
<source>?</source>
<target state="new">?</target>
<note>Place holder for invalid xml character in trx file</note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,6 @@ Error Details: {1}:{2}</source>
<target state="new">WARNING: Overwriting results file: {0}</target>
<note></note>
</trans-unit>
<trans-unit id="PlaceHolderForInvalidChar">
<source>?</source>
<target state="new">?</target>
<note>Place holder for invalid xml character in trx file</note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -700,13 +700,23 @@ private static string RemoveInvalidXmlChar(string str)
// From xml spec (http://www.w3.org/TR/xml/#charsets) valid chars:
// #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]

string invalidChar = @"[^\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD\u10000-\u10FFFF]";
return Regex.Replace(str, invalidChar, TrxLoggerResources.PlaceHolderForInvalidChar);
// we are handling only #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD]
// because C# support unicode character in range \u0000 to \uFFFF
MatchEvaluator evaluator = new MatchEvaluator(ReplaceInvalidCharacterWithUniCodeEscapeSequence);
string invalidChar = @"[^\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD]";
return Regex.Replace(str, invalidChar, evaluator);
}

return str;
}

private static string ReplaceInvalidCharacterWithUniCodeEscapeSequence(Match match)
{
char x = match.Value[0];
return String.Format(@"\u{0:x4}", (ushort)x);

}

private XmlNode EnsureLocationExists(XmlElement xml, string location, string nameSpaceUri)
{
XmlNode node = this.LocationToXmlNode(xml, location);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,66 @@ namespace Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests
{
using Microsoft.TestPlatform.Extensions.TrxLogger.XML;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;

[TestClass]
public class XmlPersistenceTests
{
[TestMethod]
public void SaveObjectShouldRemoveInvalidCharacter()
public void SaveObjectShouldReplaceInvalidCharacter()
{
XmlPersistence xmlPersistence = new XmlPersistence();
var node = xmlPersistence.CreateRootElement("TestRun");

string strWithInvalidCharForXml = "This string has these \0 \v invalid characters";
// we are handling only #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD]
char[] invalidXmlCharacterArray = new char[7];
invalidXmlCharacterArray[0] = (char)0x5;
invalidXmlCharacterArray[1] = (char)0xb;
invalidXmlCharacterArray[2] = (char)0xf;
invalidXmlCharacterArray[3] = (char)0xd800;
invalidXmlCharacterArray[4] = (char)0xdc00;
invalidXmlCharacterArray[5] = (char)0xfffe;
invalidXmlCharacterArray[6] = (char)0x0;

string strWithInvalidCharForXml = new string(invalidXmlCharacterArray);
xmlPersistence.SaveObject(strWithInvalidCharForXml, node, null, "dummy");

string expectedResult = "This string has these ? ? invalid characters";
string expectedResult = "\\u0005\\u000b\\u000f\\ud800\\udc00\\ufffe\\u0000";
Assert.AreEqual(string.Compare(expectedResult, node.InnerXml), 0);
}

[TestMethod]
public void SaveObjectShouldDoesNotRemoveValidCharacter()
public void SaveObjectShouldNotReplaceValidCharacter()
{
XmlPersistence xmlPersistence = new XmlPersistence();
var node = xmlPersistence.CreateRootElement("TestRun");

string strWithInvalidCharForXml = "This string has these \\0 \v invalid characters";
// we are handling only #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD]
char[] validXmlCharacterArray = new char[8];
validXmlCharacterArray[0] = (char)0x9;
validXmlCharacterArray[1] = (char)0xa;
validXmlCharacterArray[2] = (char)0xd;
validXmlCharacterArray[3] = (char)0x20;
validXmlCharacterArray[4] = (char)0xc123;
validXmlCharacterArray[5] = (char)0xe000;
validXmlCharacterArray[6] = (char)0xea12;
validXmlCharacterArray[7] = (char)0xfffd;

xmlPersistence.SaveObject(strWithInvalidCharForXml, node, null, "dummy");
string strWithValidCharForXml = new string(validXmlCharacterArray);

xmlPersistence.SaveObject(strWithValidCharForXml, node, null, "dummy");

string expectedResult = "\t\n\r 섣�";
Assert.AreEqual(string.Compare(expectedResult, node.InnerXml), 0);
}

string expectedResult = "This string has these \\0 ? invalid characters";
[TestMethod]
public void SaveObjectShouldReplaceOnlyInvalidCharacter()
{
XmlPersistence xmlPersistence = new XmlPersistence();
var node = xmlPersistence.CreateRootElement("TestRun");
string strWithInvalidCharForXml = "This string has these \0 \v invalid characters";
xmlPersistence.SaveObject(strWithInvalidCharForXml, node, null, "dummy");
string expectedResult = "This string has these \\u0000 \\u000b invalid characters";
Assert.AreEqual(string.Compare(expectedResult, node.InnerXml), 0);
}
}
Expand Down

0 comments on commit 4f19865

Please sign in to comment.