예제 #1
0
        public void ReadContent()
        {
            string name;

            if (IsWhitespace(Peek()))
            {
                if (buffer.Length == 0)
                {
                    isWhitespace = true;
                }
                HandleWhitespaces();
            }
            if (Peek() == '<')
            {
                Read();
                switch (Peek())
                {
                case '!':                 // declarations
                    Read();
                    if (Peek() == '[')
                    {
                        Read();
                        if (ReadName() != "CDATA")
                        {
                            throw Error("Invalid declaration markup");
                        }
                        Expect('[');
                        ReadCDATASection();
                        return;
                    }
                    else if (Peek() == '-')
                    {
                        ReadComment();
                        return;
                    }
                    else if (ReadName() != "DOCTYPE")
                    {
                        throw Error("Invalid declaration markup.");
                    }
                    else
                    {
                        throw Error("This parser does not support document type.");
                    }

                case '?':                 // PIs
                    HandleBufferedContent();
                    Read();
                    name = ReadName();
                    SkipWhitespaces();
                    string text = String.Empty;
                    if (Peek() != '?')
                    {
                        while (true)
                        {
                            text += ReadUntil('?', false);
                            if (Peek() == '>')
                            {
                                break;
                            }
                            text += "?";
                        }
                    }
                    handler.OnProcessingInstruction(
                        name, text);
                    Expect('>');
                    return;

                case '/':                 // end tags
                    HandleBufferedContent();
                    if (elementNames.Count == 0)
                    {
                        throw UnexpectedEndError();
                    }
                    Read();
                    name = ReadName();
                    SkipWhitespaces();
                    string expected = (string)elementNames.Pop();
                    xmlSpaces.Pop();
                    if (xmlSpaces.Count > 0)
                    {
                        xmlSpace = (string)xmlSpaces.Peek();
                    }
                    else
                    {
                        xmlSpace = null;
                    }
                    if (name != expected)
                    {
                        throw Error(String.Format("End tag mismatch: expected {0} but found {1}", expected, name));
                    }
                    handler.OnEndElement(name);
                    Expect('>');
                    return;

                default:                 // start tags (including empty tags)
                    HandleBufferedContent();
                    name = ReadName();
                    while (Peek() != '>' && Peek() != '/')
                    {
                        ReadAttribute(attributes);
                    }
                    handler.OnStartElement(name, attributes);
                    attributes.Clear();
                    SkipWhitespaces();
                    if (Peek() == '/')
                    {
                        Read();
                        handler.OnEndElement(name);
                    }
                    else
                    {
                        elementNames.Push(name);
                        xmlSpaces.Push(xmlSpace);
                    }
                    Expect('>');
                    return;
                }
            }
            else
            {
                ReadCharacters();
            }
        }