Esempio n. 1
0
        internal System.Text.Encoding GetEncoding()
        {
            StreamTokenReader reader = _input as StreamTokenReader;

            if (reader == null)
            {
                return(null);
            }

            return(reader._in.CurrentEncoding);
        }
Esempio n. 2
0
        internal void ChangeFormat(System.Text.Encoding encoding)
        {
            if (encoding == null)
            {
                return;
            }

            StreamTokenReader reader = _input as StreamTokenReader;

            if (reader == null)
            {
                return;
            }

            Stream stream = reader._in.BaseStream;

            String fakeReadString = new String(new char[reader.NumCharEncountered]);

            stream.Position = reader._in.CurrentEncoding.GetByteCount(fakeReadString);

            _input = new StreamTokenReader(new StreamReader(stream, encoding));
        }
Esempio n. 3
0
        internal void ChangeFormat(System.Text.Encoding encoding)
        {
            if (encoding == null)
            {
                return;
            }

            Contract.Assert(_inSavedCharacter == -1, "There was a lookahead character at the stream change point, that means the parser is changing encodings too late");

            switch (_inTokenSource)
            {
            case TokenSource.UnicodeByteArray:
            case TokenSource.UTF8ByteArray:
            case TokenSource.ASCIIByteArray:
                // these are the ones we can change on the fly

                if (encoding == System.Text.Encoding.Unicode)
                {
                    _inTokenSource = TokenSource.UnicodeByteArray;
                    return;
                }

                if (encoding == System.Text.Encoding.UTF8)
                {
                    _inTokenSource = TokenSource.UTF8ByteArray;
                    return;
                }
#if FEATURE_ASCII
                if (encoding == System.Text.Encoding.ASCII)
                {
                    _inTokenSource = TokenSource.ASCIIByteArray;
                    return;
                }
#endif
                break;

            case TokenSource.String:
            case TokenSource.CharArray:
            case TokenSource.NestedStrings:
                // these are already unicode and encoding changes are moot
                // they can't be further decoded
                return;
            }

            // if we're here it means we don't know how to change
            // to the desired encoding with the memory that we have
            // we'll have to do this the hard way -- that means
            // creating a suitable stream from what we've got

            // this is thankfully the rare case as UTF8 and unicode
            // dominate the scene

            Stream stream = null;

            switch (_inTokenSource)
            {
            case TokenSource.UnicodeByteArray:
            case TokenSource.UTF8ByteArray:
            case TokenSource.ASCIIByteArray:
                stream = new MemoryStream(_inBytes, _inIndex, _inSize - _inIndex);
                break;

            case TokenSource.CharArray:
            case TokenSource.String:
            case TokenSource.NestedStrings:
                Contract.Assert(false, "attempting to change encoding on a non-changable source, should have been prevented earlier");
                return;

            default:
                StreamTokenReader reader = _inTokenReader as StreamTokenReader;

                if (reader == null)
                {
                    Contract.Assert(false, "A new input source type has been added to the Tokenizer but it doesn't support encoding changes");
                    return;
                }

                stream = reader._in.BaseStream;

                Contract.Assert(reader._in.CurrentEncoding != null, "Tokenizer's StreamReader does not have an encoding");

                String fakeReadString = new String(' ', reader.NumCharEncountered);
                stream.Position = reader._in.CurrentEncoding.GetByteCount(fakeReadString);
                break;
            }

            Contract.Assert(stream != null, "The XML stream with new encoding was not properly initialized for kind of input we had");

            // we now have an initialized memory stream based on whatever source we had before
            _inTokenReader = new StreamTokenReader(new StreamReader(stream, encoding));
            _inTokenSource = TokenSource.Other;
        }
Esempio n. 4
0
        internal void ChangeFormat(Encoding encoding)
        {
            Stream baseStream;

            if (encoding != null)
            {
                switch (this._inTokenSource)
                {
                case TokenSource.UnicodeByteArray:
                case TokenSource.UTF8ByteArray:
                case TokenSource.ASCIIByteArray:
                    if (encoding != Encoding.Unicode)
                    {
                        if (encoding == Encoding.UTF8)
                        {
                            this._inTokenSource = TokenSource.UTF8ByteArray;
                            return;
                        }
                        if (encoding != Encoding.ASCII)
                        {
                            goto Label_005B;
                        }
                        this._inTokenSource = TokenSource.ASCIIByteArray;
                        break;
                    }
                    this._inTokenSource = TokenSource.UnicodeByteArray;
                    return;

                case TokenSource.CharArray:
                case TokenSource.String:
                case TokenSource.NestedStrings:
                    break;

                default:
                    goto Label_005B;
                }
            }
            return;

Label_005B:
            baseStream = null;
            switch (this._inTokenSource)
            {
            case TokenSource.UnicodeByteArray:
            case TokenSource.UTF8ByteArray:
            case TokenSource.ASCIIByteArray:
                baseStream = new MemoryStream(this._inBytes, this._inIndex, this._inSize - this._inIndex);
                break;

            case TokenSource.CharArray:
            case TokenSource.String:
            case TokenSource.NestedStrings:
                return;

            default:
            {
                StreamTokenReader reader = this._inTokenReader as StreamTokenReader;
                if (reader == null)
                {
                    return;
                }
                baseStream = reader._in.BaseStream;
                string s = new string(' ', reader.NumCharEncountered);
                baseStream.Position = reader._in.CurrentEncoding.GetByteCount(s);
                break;
            }
            }
            this._inTokenReader = new StreamTokenReader(new StreamReader(baseStream, encoding));
            this._inTokenSource = TokenSource.Other;
        }