Ejemplo n.º 1
0
        /// <summary>
        /// In the body state - no doctypes and declarations allowed.
        /// </summary>
        /// <param name="token">The consumed token.</param>
        void InBody(XmlToken token)
        {
            switch (token.Type)
            {
            case XmlTokenType.StartTag:
            {
                var tok = (XmlTagToken)token;
                var tag = doc.CreateElement(tok.Name);
                CurrentNode.AppendChild(tag);

                if (!tok.IsSelfClosing)
                {
                    open.Add(tag);
                }
                else if (open.Count == 0)
                {
                    insert = XmlTreeMode.After;
                }

                for (int i = 0; i < tok.Attributes.Count; i++)
                {
                    tag.SetAttribute(tok.Attributes[i].Key, tok.Attributes[i].Value.Trim());
                }

                break;
            }

            case XmlTokenType.EndTag:
            {
                var tok = (XmlTagToken)token;

                if (CurrentNode.NodeName != tok.Name)
                {
                    throw Errors.Xml(ErrorCode.TagClosingMismatch);
                }

                open.RemoveAt(open.Count - 1);

                if (open.Count == 0)
                {
                    insert = XmlTreeMode.After;
                }

                break;
            }

            case XmlTokenType.ProcessingInstruction:
            case XmlTokenType.Comment:
            {
                InMisc(token);
                break;
            }

            case XmlTokenType.Entity:
            {
                var tok = (XmlEntityToken)token;
                var str = tokenizer.GetEntity(tok);
                CurrentNode.AppendText(str);
                break;
            }

            case XmlTokenType.CData:
            {
                var tok = (XmlCDataToken)token;
                CurrentNode.AppendText(tok.Data);
                break;
            }

            case XmlTokenType.Character:
            {
                var tok = (XmlCharacterToken)token;
                CurrentNode.AppendText(tok.Data);
                break;
            }

            case XmlTokenType.EOF:
            {
                throw Errors.Xml(ErrorCode.EOF);
            }

            case XmlTokenType.DOCTYPE:
            {
                throw Errors.Xml(ErrorCode.XmlDoctypeAfterContent);
            }

            case XmlTokenType.Declaration:
            {
                throw Errors.Xml(ErrorCode.XmlDeclarationMisplaced);
            }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// In the body state - no doctypes and declarations allowed.
        /// </summary>
        /// <param name="token">The consumed token.</param>
        void InBody(XmlToken token)
        {
            switch (token.Type)
            {
            case XmlTokenType.StartTag:
            {
                var tok = (XmlTagToken)token;
                var tag = doc.CreateElement(tok.Name);
                CurrentNode.AppendChild(tag);

                if (!tok.IsSelfClosing)
                {
                    open.Add(tag);
                }

                for (int i = 0; i < tok.Attributes.Count; i++)
                {
                    tag.SetAttribute(tok.Attributes[i].Key, tok.Attributes[i].Value);
                }

                break;
            }

            case XmlTokenType.EndTag:
            {
                if (open.Count == 0)
                {
                    throw new ArgumentException("Unexpected end-tag (no current element).");
                }

                var tok = (XmlTagToken)token;

                if (CurrentNode.NodeName != tok.Name)
                {
                    throw new ArgumentException("Mismatched end-tag.");
                }

                open.RemoveAt(open.Count - 1);
                break;
            }

            case XmlTokenType.Comment:
            {
                var tok = (XmlCommentToken)token;
                var com = doc.CreateComment(tok.Data);
                CurrentNode.AppendChild(com);
                break;
            }

            case XmlTokenType.ProcessingInstruction:
            {
                var tok = (XmlPIToken)token;
                var pi  = doc.CreateProcessingInstruction(tok.Target, tok.Content);
                CurrentNode.AppendChild(pi);
                break;
            }

            case XmlTokenType.Entity:
            {
                var tok = (XmlEntityToken)token;
                var str = tokenizer.GetEntity(tok);
                CurrentNode.AppendText(str);
                break;
            }

            case XmlTokenType.Character:
            {
                var tok = (XmlCharacterToken)token;
                CurrentNode.AppendText(tok.Data);
                break;
            }

            case XmlTokenType.EOF:
            {
                if (open.Count != 0)
                {
                    RaiseErrorOccurred(ErrorCode.EOF);
                    open.RemoveRange(0, open.Count);
                }
                break;
            }

            case XmlTokenType.DOCTYPE:
            {
                RaiseErrorOccurred(ErrorCode.DoctypeUnexpected);
                break;
            }

            case XmlTokenType.Declaration:
            {
                RaiseErrorOccurred(ErrorCode.UndefinedMarkupDeclaration);
                break;
            }
            }
        }