Exemple #1
0
        internal void WriteRawWithSurrogateChecking(string text)
        {
            if (text == null)
            {
                return;
            }
            if (cacheAttrValue)
            {
                attrValue.Append(text);
            }

            int  len = text.Length;
            int  i   = 0;
            char ch  = (char)0;

            for (;;)
            {
                unsafe
                {
                    while (i < len &&
                           ((xmlCharType.charProperties[ch = text[i]] & XmlCharType.fCharData) != 0 ||
                            ch < 0x20))
                    {
                        i++;
                    }
                }
                if (i == len)
                {
                    break;
                }
                if (XmlCharType.IsHighSurrogate(ch))
                {
                    if (i + 1 < len)
                    {
                        char lowChar = text[i + 1];
                        if (XmlCharType.IsLowSurrogate(lowChar))
                        {
                            i += 2;
                            continue;
                        }
                        else
                        {
                            throw XmlConvertEx.CreateInvalidSurrogatePairException(lowChar, ch);
                        }
                    }
                    throw new ArgumentException(SR.Xml_InvalidSurrogateMissingLowChar);
                }
                else if (XmlCharType.IsLowSurrogate(ch))
                {
                    throw XmlConvertEx.CreateInvalidHighSurrogateCharException(ch);
                }
                else
                {
                    i++;
                }
            }

            textWriter.Write(text);
            return;
        }
Exemple #2
0
        public override void WriteString(string text)
        {
            VerifyState(Method.WriteString);
            XmlConvertEx.VerifyCharData(text, ExceptionType.ArgumentException);
            XmlNode node = _document.CreateTextNode(text);

            AddChild(node, _write);
        }
Exemple #3
0
        public override void WriteProcessingInstruction(string name, string text)
        {
            VerifyState(Method.WriteProcessingInstruction);
            XmlConvertEx.VerifyCharData(text, ExceptionType.ArgumentException);
            XmlNode node = _document.CreateProcessingInstruction(name, text);

            AddChild(node, _write);
        }
        public override void WriteCData(string text)
        {
            VerifyState(Method.WriteCData);
            XmlConvertEx.VerifyCharData(text, ExceptionType.ArgumentException);
            XmlNode node = document.CreateCDataSection(text);

            AddChild(node, write);
        }
Exemple #5
0
 public override void WriteWhitespace(string text)
 {
     VerifyState(Method.WriteWhitespace);
     XmlConvertEx.VerifyCharData(text, ExceptionType.ArgumentException);
     if (_document.PreserveWhitespace)
     {
         XmlNode node = _document.CreateWhitespace(text);
         AddChild(node, _write);
     }
 }
Exemple #6
0
        internal void WriteSurrogateChar(char lowChar, char highChar)
        {
            if (!XmlCharType.IsLowSurrogate(lowChar) ||
                !XmlCharType.IsHighSurrogate(highChar))
            {
                throw XmlConvertEx.CreateInvalidSurrogatePairException(lowChar, highChar);
            }

            textWriter.Write(highChar);
            textWriter.Write(lowChar);
        }
Exemple #7
0
        internal void WriteSurrogateCharEntity(char lowChar, char highChar)
        {
            if (!XmlCharType.IsLowSurrogate(lowChar) ||
                !XmlCharType.IsHighSurrogate(highChar))
            {
                throw XmlConvertEx.CreateInvalidSurrogatePairException(lowChar, highChar);
            }
            int surrogateChar = XmlCharType.CombineSurrogateChar(lowChar, highChar);

            if (cacheAttrValue)
            {
                attrValue.Append(highChar);
                attrValue.Append(lowChar);
            }

            textWriter.Write("&#x");
            textWriter.Write(surrogateChar.ToString("X", NumberFormatInfo.InvariantInfo));
            textWriter.Write(';');
        }
Exemple #8
0
        public override string ToString()
        {
            XmlNodeType nodeType = _node.NodeType;
            string      result   = nodeType.ToString();

            switch (nodeType)
            {
            case XmlNodeType.Element:
            case XmlNodeType.EntityReference:
                result += ", Name=\"" + _node.Name + "\"";
                break;

            case XmlNodeType.Attribute:
            case XmlNodeType.ProcessingInstruction:
                result += ", Name=\"" + _node.Name + "\", Value=\"" + XmlConvertEx.EscapeValueForDebuggerDisplay(_node.Value) + "\"";
                break;

            case XmlNodeType.Text:
            case XmlNodeType.CDATA:
            case XmlNodeType.Comment:
            case XmlNodeType.Whitespace:
            case XmlNodeType.SignificantWhitespace:
            case XmlNodeType.XmlDeclaration:
                result += ", Value=\"" + XmlConvertEx.EscapeValueForDebuggerDisplay(_node.Value) + "\"";
                break;

            case XmlNodeType.DocumentType:
                XmlDocumentType documentType = (XmlDocumentType)_node;
                result += ", Name=\"" + documentType.Name + "\", SYSTEM=\"" + documentType.SystemId + "\", PUBLIC=\"" + documentType.PublicId + "\", Value=\"" + XmlConvertEx.EscapeValueForDebuggerDisplay(documentType.InternalSubset) + "\"";
                break;

            default:
                break;
            }
            return(result);
        }
Exemple #9
0
        internal void Write(string text)
        {
            if (text == null)
            {
                return;
            }

            if (cacheAttrValue)
            {
                attrValue.Append(text);
            }

            // scan through the string to see if there are any characters to be escaped
            int  len      = text.Length;
            int  i        = 0;
            int  startPos = 0;
            char ch       = (char)0;

            for (;;)
            {
                unsafe
                {
                    while (i < len && (xmlCharType.charProperties[ch = text[i]] & XmlCharType.fAttrValue) != 0)
                    {
                        i++;
                    }
                }
                if (i == len)
                {
                    // reached the end of the string -> write it whole out
                    textWriter.Write(text);
                    return;
                }
                if (inAttribute)
                {
                    if (ch == 0x9)
                    {
                        i++;
                        continue;
                    }
                }
                else
                {
                    if (ch == 0x9 || ch == 0xA || ch == 0xD || ch == '"' || ch == '\'')
                    {
                        i++;
                        continue;
                    }
                }
                // some character that needs to be escaped is found:
                break;
            }

            char[] helperBuffer = new char[256];
            for (;;)
            {
                if (startPos < i)
                {
                    WriteStringFragment(text, startPos, i - startPos, helperBuffer);
                }
                if (i == len)
                {
                    break;
                }

                switch (ch)
                {
                case (char)0x9:
                    textWriter.Write(ch);
                    break;

                case (char)0xA:
                case (char)0xD:
                    if (inAttribute)
                    {
                        WriteCharEntityImpl(ch);
                    }
                    else
                    {
                        textWriter.Write(ch);
                    }
                    break;

                case '<':
                    WriteEntityRefImpl("lt");
                    break;

                case '>':
                    WriteEntityRefImpl("gt");
                    break;

                case '&':
                    WriteEntityRefImpl("amp");
                    break;

                case '\'':
                    if (inAttribute && quoteChar == ch)
                    {
                        WriteEntityRefImpl("apos");
                    }
                    else
                    {
                        textWriter.Write('\'');
                    }
                    break;

                case '"':
                    if (inAttribute && quoteChar == ch)
                    {
                        WriteEntityRefImpl("quot");
                    }
                    else
                    {
                        textWriter.Write('"');
                    }
                    break;

                default:
                    if (XmlCharType.IsHighSurrogate(ch))
                    {
                        if (i + 1 < len)
                        {
                            WriteSurrogateChar(text[++i], ch);
                        }
                        else
                        {
                            throw XmlConvertEx.CreateInvalidSurrogatePairException(text[i], ch);
                        }
                    }
                    else if (XmlCharType.IsLowSurrogate(ch))
                    {
                        throw XmlConvertEx.CreateInvalidHighSurrogateCharException(ch);
                    }
                    else
                    {
                        Debug.Assert((ch < 0x20 && !xmlCharType.IsWhiteSpace(ch)) || (ch > 0xFFFD));
                        WriteCharEntityImpl(ch);
                    }
                    break;
                }
                i++;
                startPos = i;
                unsafe
                {
                    while (i < len && (xmlCharType.charProperties[ch = text[i]] & XmlCharType.fAttrValue) != 0)
                    {
                        i++;
                    }
                }
            }
        }
Exemple #10
0
        internal void Write(char[] array, int offset, int count)
        {
            if (null == array)
            {
                throw new ArgumentNullException("array");
            }

            if (0 > offset)
            {
                throw new ArgumentOutOfRangeException("offset");
            }

            if (0 > count)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            if (count > array.Length - offset)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            if (cacheAttrValue)
            {
                attrValue.Append(array, offset, count);
            }

            int  endPos = offset + count;
            int  i      = offset;
            char ch     = (char)0;

            for (;;)
            {
                int startPos = i;
                unsafe
                {
                    while (i < endPos && (xmlCharType.charProperties[ch = array[i]] & XmlCharType.fAttrValue) != 0)
                    {
                        i++;
                    }
                }

                if (startPos < i)
                {
                    textWriter.Write(array, startPos, i - startPos);
                }
                if (i == endPos)
                {
                    break;
                }

                switch (ch)
                {
                case (char)0x9:
                    textWriter.Write(ch);
                    break;

                case (char)0xA:
                case (char)0xD:
                    if (inAttribute)
                    {
                        WriteCharEntityImpl(ch);
                    }
                    else
                    {
                        textWriter.Write(ch);
                    }
                    break;

                case '<':
                    WriteEntityRefImpl("lt");
                    break;

                case '>':
                    WriteEntityRefImpl("gt");
                    break;

                case '&':
                    WriteEntityRefImpl("amp");
                    break;

                case '\'':
                    if (inAttribute && quoteChar == ch)
                    {
                        WriteEntityRefImpl("apos");
                    }
                    else
                    {
                        textWriter.Write('\'');
                    }
                    break;

                case '"':
                    if (inAttribute && quoteChar == ch)
                    {
                        WriteEntityRefImpl("quot");
                    }
                    else
                    {
                        textWriter.Write('"');
                    }
                    break;

                default:
                    if (XmlCharType.IsHighSurrogate(ch))
                    {
                        if (i + 1 < endPos)
                        {
                            WriteSurrogateChar(array[++i], ch);
                        }
                        else
                        {
                            throw new ArgumentException(SR.Xml_SurrogatePairSplit);
                        }
                    }
                    else if (XmlCharType.IsLowSurrogate(ch))
                    {
                        throw XmlConvertEx.CreateInvalidHighSurrogateCharException(ch);
                    }
                    else
                    {
                        Debug.Assert((ch < 0x20 && !xmlCharType.IsWhiteSpace(ch)) || (ch > 0xFFFD));
                        WriteCharEntityImpl(ch);
                    }
                    break;
                }
                i++;
            }
        }
            // This method trims whitespaces from the beginning and the end of the string and cached writer events
            internal void Trim()
            {
                // if only one string value -> trim the write spaces directly
                if (_singleStringValue != null)
                {
                    _singleStringValue = XmlConvertEx.TrimString(_singleStringValue);
                    return;
                }

                // trim the string in StringBuilder
                string valBefore = _stringValue.ToString();
                string valAfter  = XmlConvertEx.TrimString(valBefore);

                if (valBefore != valAfter)
                {
                    _stringValue = new StringBuilder(valAfter);
                }

                // trim the beginning of the recorded writer events
                XmlCharType xmlCharType = XmlCharType.Instance;

                int i = _firstItem;

                while (i == _firstItem && i <= _lastItem)
                {
                    Item item = _items[i];
                    switch (item.type)
                    {
                    case ItemType.Whitespace:
                        _firstItem++;
                        break;

                    case ItemType.String:
                    case ItemType.Raw:
                    case ItemType.ValueString:
                        item.data = XmlConvertEx.TrimStringStart((string)item.data);
                        if (((string)item.data).Length == 0)
                        {
                            // no characters left -> move the firstItem index to exclude it from the Replay
                            _firstItem++;
                        }
                        break;

                    case ItemType.StringChars:
                    case ItemType.RawChars:
                        BufferChunk bufChunk = (BufferChunk)item.data;
                        int         endIndex = bufChunk.index + bufChunk.count;
                        while (bufChunk.index < endIndex && xmlCharType.IsWhiteSpace(bufChunk.buffer[bufChunk.index]))
                        {
                            bufChunk.index++;
                            bufChunk.count--;
                        }
                        if (bufChunk.index == endIndex)
                        {
                            // no characters left -> move the firstItem index to exclude it from the Replay
                            _firstItem++;
                        }
                        break;
                    }
                    i++;
                }

                // trim the end of the recorded writer events
                i = _lastItem;
                while (i == _lastItem && i >= _firstItem)
                {
                    Item item = _items[i];
                    switch (item.type)
                    {
                    case ItemType.Whitespace:
                        _lastItem--;
                        break;

                    case ItemType.String:
                    case ItemType.Raw:
                    case ItemType.ValueString:
                        item.data = XmlConvertEx.TrimStringEnd((string)item.data);
                        if (((string)item.data).Length == 0)
                        {
                            // no characters left -> move the lastItem index to exclude it from the Replay
                            _lastItem--;
                        }
                        break;

                    case ItemType.StringChars:
                    case ItemType.RawChars:
                        BufferChunk bufChunk = (BufferChunk)item.data;
                        while (bufChunk.count > 0 && xmlCharType.IsWhiteSpace(bufChunk.buffer[bufChunk.index + bufChunk.count - 1]))
                        {
                            bufChunk.count--;
                        }
                        if (bufChunk.count == 0)
                        {
                            // no characters left -> move the lastItem index to exclude it from the Replay
                            _lastItem--;
                        }
                        break;
                    }
                    i--;
                }
            }