Skip to main content

Replace invalid XML characters in text string.

using System;
using System.Xml;
using System.Text.RegularExpressions;

namespace log4net.Util
{
    /// <summary>
    /// Utility class for transforming XML strings.
    /// </summary>
    public static class Transform
    {
        private const string CdataEnd = "]]>";
        private const string CdataUnescapableToken = "]]";

        /// <summary>
        /// Characters illegal in XML 1.0.
        /// </summary>
        private static readonly Regex InvalidChars = new Regex(@"[^\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD]", RegexOptions.Compiled);

        /// <summary>
        /// Replace invalid XML characters in text string.
        /// </summary>
        /// <param name="textData">the XML text input string</param>
        /// <param name="mask">the string to use in place of invalid characters. This defaults to a ?. Set it to the empty string to simply remove offending characters.</param>
        /// <returns>A string that does not contain invalid XML characters.</returns>
        /// <remarks>
        /// <para>
        /// Certain Unicode code points are not allowed in the XML InfoSet, for
        /// details see: <a href="http://www.w3.org/TR/REC-xml/#charsets">http://www.w3.org/TR/REC-xml/#charsets</a>.
        /// </para>
        /// <para>
        /// This method replaces any illegal characters in the input string
        /// with the mask string specified.
        /// </para>
        /// </remarks>
        public static string MaskXmlInvalidCharacters(string textData, string mask = "?")
        {
            return InvalidChars.Replace(textData, mask);
        }

        /// <summary>
        /// Write a string to an <see cref="XmlWriter"/>.
        /// The text is escaped either using XML escape entities, or CDATA sections.
        /// </summary>
        /// <param name="writer">the <see cref="XmlWriter"/> to write to.</param>
        /// <param name="textData">the string to write.</param>
        /// <param name="invalidCharReplacement">The string that will be used to replace non-XML compliant characters. This defaults to a ?. Set it to the empty string to simply remove offending characters.</param>
        public static void WriteEscapedXmlString(XmlWriter writer, string textData, string invalidCharReplacement = "?")
        {
            var stringData = MaskXmlInvalidCharacters(textData, invalidCharReplacement);
            // Write either escaped text or CDATA sections

            var weightCData = 12 * (1 + CountSubstrings(stringData, CdataEnd));
            var weightStringEscapes = 3 * (CountSubstrings(stringData, "<") + CountSubstrings(stringData, ">")) + 4 * CountSubstrings(stringData, "&");

            if (weightStringEscapes <= weightCData)
            {
                // Write string using string escapes
                writer.WriteString(stringData);
            }
            else
            {
                // Write string using CDATA section

                var end = stringData.IndexOf(CdataEnd, StringComparison.Ordinal);

                if (end < 0)
                {
                    writer.WriteCData(stringData);
                }
                else
                {
                    var start = 0;
                    while (end > -1)
                    {
                        writer.WriteCData(stringData.Substring(start, end - start));
                        if (end == stringData.Length - 3)
                        {
                            start = stringData.Length;
                            writer.WriteString(CdataEnd);
                            break;
                        }

                        writer.WriteString(CdataUnescapableToken);
                        start = end + 2;
                        end = stringData.IndexOf(CdataEnd, start, StringComparison.Ordinal);
                    }

                    if (start < stringData.Length)
                    {
                        writer.WriteCData(stringData.Substring(start));
                    }
                }
            }
        }

        /// <summary>
        /// Count the number of times that the substring occurs in the text.
        /// </summary>
        /// <param name="text">the text to search</param>
        /// <param name="substring">the substring to find</param>
        /// <returns>the number of times the substring occurs in the text</returns>
        /// <remarks>
        /// <para>
        /// The substring is assumed to be non repeating within itself.
        /// </para>
        /// </remarks>
        private static int CountSubstrings(string text, string substring)
        {
            var length = text.Length;
            if (length == 0)
            {
                return 0;
            }

            var substringLength = substring.Length;
            if (substringLength == 0)
            {
                return 0;
            }

            int count = 0, offset = 0;
            while (offset < length)
            {
                var index = text.IndexOf(substring, offset, StringComparison.Ordinal);
                if (index == -1)
                {
                    break;
                }

                count++;
                offset = index + substringLength;
            }

            return count;
        }
    }
}

// ---------------------------------------------------
// Tests
// ---------------------------------------------------

using log4net.Util;
using NUnit.Framework;

namespace log4net.Tests.Util
{
    [TestFixture]
    public class TransformTest
    {
        [Test]
        public void MaskXmlInvalidCharactersAllowsJapaneseCharacters()
        {
            string kome = "\u203B";
            Assert.AreEqual(kome, Transform.MaskXmlInvalidCharacters(kome, "?"));
        }

        [Test]
        public void MaskXmlInvalidCharactersMasks0Char()
        {
            string c = "\u0000";
            Assert.AreEqual("?", Transform.MaskXmlInvalidCharacters(c, "?"));
        }
    }
}