예제 #1
0
 void BeforeDoctype(XmlToken token)
 {
     if (token.Type == XmlTokenType.DOCTYPE)
     {
         var tok     = (XmlDoctypeToken)token;
         var doctype = new DocumentType();
         doctype.SystemId = tok.SystemIdentifier;
         doctype.PublicId = tok.PublicIdentifier;
         doctype.Name     = tok.Name;
         doc.AppendChild(doctype);
         insert = XmlTreeMode.Body;
     }
     else if (token.Type == XmlTokenType.ProcessingInstruction)
     {
         var tok = (XmlPIToken)token;
         var pi  = doc.CreateProcessingInstruction(tok.Target, tok.Content);
         doc.AppendChild(pi);
     }
     else if (token.Type == XmlTokenType.Comment)
     {
         var tok = (XmlCommentToken)token;
         var com = doc.CreateComment(tok.Data);
         doc.AppendChild(com);
     }
     else if (!token.IsIgnorable)
     {
         insert = XmlTreeMode.Body;
         InBody(token);
     }
 }
예제 #2
0
        /// <summary>
        /// Before any doctype - still in the prolog. No declaration
        /// allowed.
        /// </summary>
        /// <param name="token">The consumed token.</param>
        void BeforeDoctype(XmlToken token)
        {
            switch (token.Type)
            {
            case XmlTokenType.DOCTYPE:
            {
                var tok     = (XmlDoctypeToken)token;
                var doctype = new DocumentType();
                doctype.SystemId        = tok.SystemIdentifier;
                doctype.PublicId        = tok.PublicIdentifier;
                doctype.TypeDefinitions = tokenizer.DTD;
                doctype.Name            = tok.Name;
                doc.AppendChild(doctype);
                insert = XmlTreeMode.Misc;

                if (!tok.IsSystemIdentifierMissing && !standalone)
                {
                    ScanExternalSubset(doctype.SystemId, doctype.TypeDefinitions);
                }

                break;
            }

            default:
            {
                InMisc(token);
                break;
            }
            }
        }
예제 #3
0
        /// <summary>
        /// Before any doctype - still in the prolog. No declaration
        /// allowed.
        /// </summary>
        /// <param name="token">The consumed token.</param>
        void BeforeDoctype(XmlToken token)
        {
            switch (token.Type)
            {
            case XmlTokenType.Doctype:
            {
                var doctypeToken = (XmlDoctypeToken)token;
                var doctypeNode  = new DocumentType(_document, doctypeToken.Name)
                {
                    SystemIdentifier = doctypeToken.SystemIdentifier,
                    PublicIdentifier = doctypeToken.PublicIdentifier
                };
                _document.AppendChild(doctypeNode);
                _currentMode = XmlTreeMode.Misc;

                break;
            }

            default:
            {
                InMisc(token);
                break;
            }
            }
        }
예제 #4
0
        void Initial(XmlToken token)
        {
            if (token.Type == XmlTokenType.Declaration)
            {
                var tok = (XmlDeclarationToken)token;
                standalone = tok.Standalone;
                var ver = 1.0;

                if (!tok.IsEncodingMissing)
                {
                    SetEncoding(tok.Encoding);
                }

                //The declaration token -- Check version
                if (!Double.TryParse(tok.Version, out ver) || ver >= 2.0)
                {
                    throw new ArgumentException("The given version number is not supported.");
                }
            }
            else if (!token.IsIgnorable)
            {
                RaiseErrorOccurred(ErrorCode.UndefinedMarkupDeclaration);
                insert = XmlTreeMode.Prolog;
                BeforeDoctype(token);
            }
        }
예제 #5
0
 /// <summary>
 /// Creates a new instance of the XML parser.
 /// </summary>
 /// <param name="document">The document instance to be filled.</param>
 internal XmlDomBuilder(Document document)
 {
     _tokenizer    = new XmlTokenizer(document.Source, document.Entities);
     _document     = document;
     _standalone   = false;
     _openElements = new List <Element>();
     _currentMode  = XmlTreeMode.Initial;
 }
예제 #6
0
 /// <summary>
 /// Creates a new instance of the XML parser with the specified document.
 /// </summary>
 /// <param name="document">The document instance to be constructed.</param>
 internal XmlDomBuilder(Document document)
 {
     _tokenizer = new XmlTokenizer(document.Source, document.Options.Events);
     _document = document;
     _standalone = false;
     _openElements = new List<Element>();
     _currentMode = XmlTreeMode.Initial;
 }
예제 #7
0
 /// <summary>
 /// Creates a new instance of the XML parser.
 /// </summary>
 /// <param name="document">The document instance to be filled.</param>
 internal XmlDomBuilder(Document document)
 {
     var resolver = document.Options.GetService<IEntityService>() ?? XmlEntityService.Resolver;
     _tokenizer = new XmlTokenizer(document.Source, document.Options.Events, resolver);
     _document = document;
     _standalone = false;
     _openElements = new List<Element>();
     _currentMode = XmlTreeMode.Initial;
 }
예제 #8
0
        /// <summary>
        /// Creates a new instance of the XML parser.
        /// </summary>
        /// <param name="document">The document instance to be filled.</param>
        internal XmlDomBuilder(Document document)
        {
            var resolver = document.Options.GetService <IEntityService>() ?? XmlEntityService.Resolver;

            _tokenizer    = new XmlTokenizer(document.Source, document.Options.Events, resolver);
            _document     = document;
            _standalone   = false;
            _openElements = new List <Element>();
            _currentMode  = XmlTreeMode.Initial;
        }
예제 #9
0
 /// <summary>
 /// Creates a new instance of the XML parser.
 /// </summary>
 /// <param name="document">The document instance to be filled.</param>
 /// <param name="creator">The optional non-standard creator to use.</param>
 internal XmlDomBuilder(Document document, Func<Document, String, String, Element> creator = null)
 {
     var resolver = document.Options.GetProvider<IEntityProvider>() ?? XmlEntityService.Resolver;
     _tokenizer = new XmlTokenizer(document.Source, resolver);
     _document = document;
     _standalone = false;
     _openElements = new List<Element>();
     _currentMode = XmlTreeMode.Initial;
     _creator = creator ?? CreateElement;
 }
예제 #10
0
        /// <summary>
        /// Creates a new instance of the XML parser.
        /// </summary>
        /// <param name="document">The document instance to be filled.</param>
        /// <param name="creator">The optional non-standard creator to use.</param>
        internal XmlDomBuilder(Document document, Func <Document, String, String, Element> creator = null)
        {
            var resolver = document.Options.GetProvider <IEntityProvider>() ?? XmlEntityService.Resolver;

            _tokenizer    = new XmlTokenizer(document.Source, resolver);
            _document     = document;
            _standalone   = false;
            _openElements = new List <Element>();
            _currentMode  = XmlTreeMode.Initial;
            _creator      = creator ?? CreateElement;
        }
예제 #11
0
        /// <summary>
        /// Creates a new instance of the XML parser with the specified document
        /// based on the given source manager.
        /// </summary>
        /// <param name="document">The document instance to be constructed.</param>
        /// <param name="source">The source to use.</param>
        internal XmlParser(XMLDocument document, SourceManager source)
        {
            tokenizer = new XmlTokenizer(source);

            tokenizer.ErrorOccurred += (s, ev) =>
            {
                if (ErrorOccurred != null)
                {
                    ErrorOccurred(this, ev);
                }
            };

            started    = false;
            doc        = document;
            standalone = false;
            open       = new List <Element>();
            insert     = XmlTreeMode.Initial;
        }
예제 #12
0
        /// <summary>
        /// Before any doctype - still in the prolog. No declaration
        /// allowed.
        /// </summary>
        /// <param name="token">The consumed token.</param>
        private void BeforeDoctype(XmlToken token)
        {
            switch (token.Type)
            {
            case XmlTokenType.Doctype:
            {
                var doctypeToken = (XmlDoctypeToken)token;
                var doctypeNode  = _document.Implementation.CreateDocumentType(doctypeToken.Name, doctypeToken.PublicIdentifier, doctypeToken.SystemIdentifier);
                _document.AppendChild(doctypeNode);
                _currentMode = XmlTreeMode.Misc;

                break;
            }

            default:
            {
                InMisc(token);
                break;
            }
            }
        }
예제 #13
0
        void SetEncoding(String charSet)
        {
            if (TextEncoding.IsSupported(charSet))
            {
                var encoding = TextEncoding.Resolve(charSet);

                if (encoding != null)
                {
                    try
                    {
                        _document.Source.CurrentEncoding = encoding;
                    }
                    catch (NotSupportedException)
                    {
                        _currentMode = XmlTreeMode.Initial;
                        _document.ReplaceAll(null, true);
                        _openElements.Clear();
                    }
                }
            }
        }
예제 #14
0
        /// <summary>
        /// In the body state - no doctypes and declarations allowed.
        /// </summary>
        /// <param name="token">The consumed token.</param>
        void InMisc(XmlToken token)
        {
            switch (token.Type)
            {
            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.StartTag:
            {
                insert = XmlTreeMode.Body;
                InBody(token);
                break;
            }

            default:
            {
                if (!token.IsIgnorable)
                {
                    throw Errors.Xml(ErrorCode.XmlMissingRoot);
                }

                break;
            }
            }
        }
예제 #15
0
        /// <summary>
        /// In the body state - no doctypes and declarations allowed.
        /// </summary>
        /// <param name="token">The consumed token.</param>
        void InMisc(XmlToken token)
        {
            switch (token.Type)
            {
            case XmlTokenType.Comment:
            {
                var commenToken = (XmlCommentToken)token;
                var commentNode = _document.CreateComment(commenToken.Data);
                CurrentNode.AppendChild(commentNode);
                break;
            }

            case XmlTokenType.ProcessingInstruction:
            {
                var piToken = (XmlPIToken)token;
                var piNode  = _document.CreateProcessingInstruction(piToken.Target, piToken.Content);
                CurrentNode.AppendChild(piNode);
                break;
            }

            case XmlTokenType.StartTag:
            {
                _currentMode = XmlTreeMode.Body;
                InBody(token);
                break;
            }

            default:
            {
                if (!token.IsIgnorable && !_options.IsSuppressingErrors)
                {
                    throw XmlParseError.XmlMissingRoot.At(token.Position);
                }

                break;
            }
            }
        }
예제 #16
0
        /// <summary>
        /// The initial state. Expects an XML declaration.
        /// </summary>
        /// <param name="token">The consumed token.</param>
        void Initial(XmlToken token)
        {
            if (token.Type == XmlTokenType.Declaration)
            {
                var declarationToken = (XmlDeclarationToken)token;
                _standalone = declarationToken.Standalone;

                if (!declarationToken.IsEncodingMissing)
                {
                    SetEncoding(declarationToken.Encoding);
                }

                if (!CheckVersion(declarationToken.Version) && !_options.IsSuppressingErrors)
                {
                    throw XmlParseError.XmlDeclarationVersionUnsupported.At(token.Position);
                }
            }
            else
            {
                _currentMode = XmlTreeMode.Prolog;
                BeforeDoctype(token);
            }
        }
예제 #17
0
        /// <summary>
        /// The initial state. Expects an XML declaration.
        /// </summary>
        /// <param name="token">The consumed token.</param>
        void Initial(XmlToken token)
        {
            if (token.Type == XmlTokenType.Declaration)
            {
                var tok = (XmlDeclarationToken)token;
                standalone = tok.Standalone;

                if (!tok.IsEncodingMissing)
                {
                    SetEncoding(tok.Encoding);
                }

                if (!CheckVersion(tok.Version))
                {
                    throw Errors.Xml(ErrorCode.XmlDeclarationVersionUnsupported);
                }
            }
            else
            {
                insert = XmlTreeMode.Prolog;
                BeforeDoctype(token);
            }
        }
예제 #18
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 tagToken = (XmlTagToken)token;
                    var element = new XmlElement(_document, tagToken.Name);
                    CurrentNode.AppendChild(element);

                    if (!tagToken.IsSelfClosing)
                    {
                        _openElements.Add(element);
                    }
                    else if (_openElements.Count == 0)
                    {
                        _currentMode = XmlTreeMode.After;
                    }

                    for (var i = 0; i < tagToken.Attributes.Count; i++)
                    {
                        var name = tagToken.Attributes[i].Key;
                        var value = tagToken.Attributes[i].Value.Trim();
                        element.SetAttribute(name, value);
                    }

                    break;
                }
                case XmlTokenType.EndTag:
                {
                    var tagToken = (XmlTagToken)token;

                    if (!CurrentNode.NodeName.Is(tagToken.Name))
                    {
                        if (_options.IsSuppressingErrors)
                        {
                            break;
                        }

                        throw XmlParseError.TagClosingMismatch.At(token.Position);
                    }

                    _openElements.RemoveAt(_openElements.Count - 1);

                    if (_openElements.Count == 0)
                    {
                        _currentMode = XmlTreeMode.After;
                    }

                    break;
                }
                case XmlTokenType.ProcessingInstruction:
                case XmlTokenType.Comment:
                {
                    InMisc(token);
                    break;
                }
                case XmlTokenType.CData:
                {
                    var cdataToken = (XmlCDataToken)token;
                    CurrentNode.AppendText(cdataToken.Data);
                    break;
                }
                case XmlTokenType.Character:
                {
                    var charToken = (XmlCharacterToken)token;
                    CurrentNode.AppendText(charToken.Data);
                    break;
                }
                case XmlTokenType.EndOfFile:
                {
                    if (_options.IsSuppressingErrors)
                    {
                        break;
                    }

                    throw XmlParseError.EOF.At(token.Position);
                }
                case XmlTokenType.Doctype:
                {
                    if (_options.IsSuppressingErrors)
                    {
                        break;
                    }

                    throw XmlParseError.XmlDoctypeAfterContent.At(token.Position);
                }
                case XmlTokenType.Declaration:
                {
                    if (_options.IsSuppressingErrors)
                    {
                        break;
                    }

                    throw XmlParseError.XmlDeclarationMisplaced.At(token.Position);
                }
            }
        }
예제 #19
0
        /// <summary>
        /// In the body state - no doctypes and declarations allowed.
        /// </summary>
        /// <param name="token">The consumed token.</param>
        void InMisc(XmlToken token)
        {
            switch (token.Type)
            {
                case XmlTokenType.Comment:
                {
                    var commenToken = (XmlCommentToken)token;
                    var commentNode = _document.CreateComment(commenToken.Data);
                    CurrentNode.AppendChild(commentNode);
                    break;
                }
                case XmlTokenType.ProcessingInstruction:
                {
                    var piToken = (XmlPIToken)token;
                    var piNode = _document.CreateProcessingInstruction(piToken.Target, piToken.Content);
                    CurrentNode.AppendChild(piNode);
                    break;
                }
                case XmlTokenType.StartTag:
                {
                    _currentMode = XmlTreeMode.Body;
                    InBody(token);
                    break;
                }
                default:
                {
                    if (!token.IsIgnorable && !_options.IsSuppressingErrors)
                    {
                        throw XmlParseError.XmlMissingRoot.At(token.Position);
                    }

                    break;
                }
            }
        }
예제 #20
0
        /// <summary>
        /// Before any doctype - still in the prolog. No declaration
        /// allowed.
        /// </summary>
        /// <param name="token">The consumed token.</param>
        void BeforeDoctype(XmlToken token)
        {
            switch (token.Type)
            {
                case XmlTokenType.Doctype:
                {
                    var doctypeToken = (XmlDoctypeToken)token;
                    var doctypeNode = new DocumentType(_document, doctypeToken.Name)
                    {
                        SystemIdentifier = doctypeToken.SystemIdentifier,
                        PublicIdentifier = doctypeToken.PublicIdentifier
                    };
                    _document.AppendChild(doctypeNode);
                    _currentMode = XmlTreeMode.Misc;

                    break;
                }
                default:
                {
                    InMisc(token);
                    break;
                }
            }
        }
예제 #21
0
        /// <summary>
        /// The initial state. Expects an XML declaration.
        /// </summary>
        /// <param name="token">The consumed token.</param>
        void Initial(XmlToken token)
        {
            if (token.Type == XmlTokenType.Declaration)
            {
                var declarationToken = (XmlDeclarationToken)token;
                _standalone = declarationToken.Standalone;

                if (!declarationToken.IsEncodingMissing)
                {
                    SetEncoding(declarationToken.Encoding);
                }

                if (!CheckVersion(declarationToken.Version) && !_options.IsSuppressingErrors)
                {
                    throw XmlParseError.XmlDeclarationVersionUnsupported.At(token.Position);
                }
            }
            else
            {
                _currentMode = XmlTreeMode.Prolog;
                BeforeDoctype(token);
            }
        }
예제 #22
0
        /// <summary>
        /// In the body state - no doctypes and declarations allowed.
        /// </summary>
        /// <param name="token">The consumed token.</param>
        void InMisc(XmlToken token)
        {
            switch (token.Type)
            {
                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.StartTag:
                {
                    insert = XmlTreeMode.Body;
                    InBody(token);
                    break;
                }
                default:
                {
                    if (!token.IsIgnorable)
                        throw Errors.Xml(ErrorCode.XmlMissingRoot);

                    break;
                }
            }
        }
예제 #23
0
 void BeforeDoctype(XmlToken token)
 {
     if (token.Type == XmlTokenType.DOCTYPE)
     {
         var tok = (XmlDoctypeToken)token;
         var doctype = new DocumentType();
         doctype.SystemId = tok.SystemIdentifier;
         doctype.PublicId = tok.PublicIdentifier;
         doctype.Name = tok.Name;
         doc.AppendChild(doctype);
         insert = XmlTreeMode.Body;
     }
     else if (token.Type == XmlTokenType.ProcessingInstruction)
     {
         var tok = (XmlPIToken)token;
         var pi = doc.CreateProcessingInstruction(tok.Target, tok.Content);
         doc.AppendChild(pi);
     }
     else if (token.Type == XmlTokenType.Comment)
     {
         var tok = (XmlCommentToken)token;
         var com = doc.CreateComment(tok.Data);
         doc.AppendChild(com);
     }
     else if (!token.IsIgnorable)
     {
         insert = XmlTreeMode.Body;
         InBody(token);
     }
 }
예제 #24
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);
                }
            }
        }
예제 #25
0
        /// <summary>
        /// Before any doctype - still in the prolog. No declaration
        /// allowed.
        /// </summary>
        /// <param name="token">The consumed token.</param>
        void BeforeDoctype(XmlToken token)
        {
            switch (token.Type)
            {
                case XmlTokenType.DOCTYPE:
                {
                    var tok = (XmlDoctypeToken)token;
                    var doctype = new DocumentType();
                    doctype.SystemId = tok.SystemIdentifier;
                    doctype.PublicId = tok.PublicIdentifier;
                    doctype.TypeDefinitions = tokenizer.DTD;
                    doctype.Name = tok.Name;
                    doc.AppendChild(doctype);
                    insert = XmlTreeMode.Misc;

                    if (!tok.IsSystemIdentifierMissing && !standalone)
                        ScanExternalSubset(doctype.SystemId, doctype.TypeDefinitions);

                    break;
                }
                default:
                {
                    InMisc(token);
                    break;
                }
            }
        }
예제 #26
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);
            }
            }
        }
예제 #27
0
        /// <summary>
        /// The initial state. Expects an XML declaration.
        /// </summary>
        /// <param name="token">The consumed token.</param>
        void Initial(XmlToken token)
        {
            if (token.Type == XmlTokenType.Declaration)
            {
                var tok = (XmlDeclarationToken)token;
                _standalone = tok.Standalone;

                if (!tok.IsEncodingMissing)
                    SetEncoding(tok.Encoding);

                if (!CheckVersion(tok.Version))
                    throw XmlParseError.XmlDeclarationVersionUnsupported.At(token.Position);
            }
            else
            {
                _currentMode = XmlTreeMode.Prolog;
                BeforeDoctype(token);
            }
        }
예제 #28
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 = new XmlElement(_document, tok.Name);
                    CurrentNode.AppendChild(tag);

                    if (!tok.IsSelfClosing)
                        _openElements.Add(tag);
                    else if(_openElements.Count == 0)
                        _currentMode = 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 XmlParseError.TagClosingMismatch.At(token.Position);

                    _openElements.RemoveAt(_openElements.Count - 1);

                    if (_openElements.Count == 0)
                        _currentMode = XmlTreeMode.After;

                    break;
                }
                case XmlTokenType.ProcessingInstruction:
                case XmlTokenType.Comment:
                {
                    InMisc(token);
                    break;
                }
                case XmlTokenType.Entity:
                {
                    var tok = (XmlEntityToken)token;
                    var str = tok.GetEntity();
                    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.ToString());
                    break;
                }
                case XmlTokenType.EndOfFile:
                {
                    throw XmlParseError.EOF.At(token.Position);
                }
                case XmlTokenType.Doctype:
                {
                    throw XmlParseError.XmlDoctypeAfterContent.At(token.Position);
                }
                case XmlTokenType.Declaration:
                {
                    throw XmlParseError.XmlDeclarationMisplaced.At(token.Position);
                }
            }
        }
예제 #29
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 = new XmlElement(_document, tok.Name);
                CurrentNode.AppendChild(tag);

                if (!tok.IsSelfClosing)
                {
                    _openElements.Add(tag);
                }
                else if (_openElements.Count == 0)
                {
                    _currentMode = 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)
                {
                    if (_options.IsSuppressingErrors)
                    {
                        break;
                    }

                    throw XmlParseError.TagClosingMismatch.At(token.Position);
                }

                _openElements.RemoveAt(_openElements.Count - 1);

                if (_openElements.Count == 0)
                {
                    _currentMode = XmlTreeMode.After;
                }

                break;
            }

            case XmlTokenType.ProcessingInstruction:
            case XmlTokenType.Comment:
            {
                InMisc(token);
                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.EndOfFile:
            {
                if (_options.IsSuppressingErrors)
                {
                    break;
                }

                throw XmlParseError.EOF.At(token.Position);
            }

            case XmlTokenType.Doctype:
            {
                if (_options.IsSuppressingErrors)
                {
                    break;
                }

                throw XmlParseError.XmlDoctypeAfterContent.At(token.Position);
            }

            case XmlTokenType.Declaration:
            {
                if (_options.IsSuppressingErrors)
                {
                    break;
                }

                throw XmlParseError.XmlDeclarationMisplaced.At(token.Position);
            }
            }
        }
예제 #30
0
        /// <summary>
        /// Sets the document's encoding to the given one.
        /// </summary>
        /// <param name="charSet">The encoding to use.</param>
        void SetEncoding(String charSet)
        {
            if (TextEncoding.IsSupported(charSet))
            {
                var encoding = TextEncoding.Resolve(charSet);

                if (encoding != null)
                {
                    try
                    {
                        _document.Source.CurrentEncoding = encoding;
                    }
                    catch (NotSupportedException)
                    {
                        _currentMode = XmlTreeMode.Initial;
                        _document.ReplaceAll(null, true);
                        _openElements.Clear();
                    }
                }
            }
        }
예제 #31
0
        void Initial(XmlToken token)
        {
            if (token.Type == XmlTokenType.Declaration)
            {
                var tok = (XmlDeclarationToken)token;
                standalone = tok.Standalone;
                var ver = 1.0;

                if (!tok.IsEncodingMissing)
                    SetEncoding(tok.Encoding);

                //The declaration token -- Check version
                if (!Double.TryParse(tok.Version, out ver) || ver >= 2.0)
                    throw new ArgumentException("The given version number is not supported.");
            }
            else if (!token.IsIgnorable)
            {
                RaiseErrorOccurred(ErrorCode.UndefinedMarkupDeclaration);
                insert = XmlTreeMode.Prolog;
                BeforeDoctype(token);
            }
        }
예제 #32
0
        /// <summary>
        /// The initial state. Expects an XML declaration.
        /// </summary>
        /// <param name="token">The consumed token.</param>
        void Initial(XmlToken token)
        {
            if (token.Type == XmlTokenType.Declaration)
            {
                var tok = (XmlDeclarationToken)token;
                standalone = tok.Standalone;

                if (!tok.IsEncodingMissing)
                    SetEncoding(tok.Encoding);

                if (!CheckVersion(tok.Version))
                    throw Errors.Xml(ErrorCode.XmlDeclarationVersionUnsupported);
            }
            else
            {
                insert = XmlTreeMode.Prolog;
                BeforeDoctype(token);
            }
        }
예제 #33
0
        /// <summary>
        /// Creates a new instance of the XML parser with the specified document
        /// based on the given source manager.
        /// </summary>
        /// <param name="document">The document instance to be constructed.</param>
        /// <param name="source">The source to use.</param>
        internal XmlParser(XMLDocument document, SourceManager source)
        {
            tokenizer = new XmlTokenizer(source);

            tokenizer.ErrorOccurred += (s, ev) =>
            {
                if (ErrorOccurred != null)
                    ErrorOccurred(this, ev);
            };

            started = false;
            doc = document;
            standalone = false;
            open = new List<Element>();
            insert = XmlTreeMode.Initial;
        }
예제 #34
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 tagToken = (XmlTagToken)token;
                var element  = _creator.Invoke(_document, tagToken.Name, null);
                CurrentNode.AppendChild(element);

                if (!tagToken.IsSelfClosing)
                {
                    _openElements.Add(element);
                }
                else if (_openElements.Count == 0)
                {
                    _currentMode = XmlTreeMode.After;
                }

                for (var i = 0; i < tagToken.Attributes.Count; i++)
                {
                    var name  = tagToken.Attributes[i].Key;
                    var value = tagToken.Attributes[i].Value.Trim();
                    element.SetAttribute(name, value);
                }

                if (_options.OnCreated != null)
                {
                    _options.OnCreated.Invoke(element, tagToken.Position);
                }

                break;
            }

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

                if (!CurrentNode.NodeName.Is(tagToken.Name))
                {
                    if (_options.IsSuppressingErrors)
                    {
                        break;
                    }

                    throw XmlParseError.TagClosingMismatch.At(token.Position);
                }

                _openElements.RemoveAt(_openElements.Count - 1);

                if (_openElements.Count == 0)
                {
                    _currentMode = XmlTreeMode.After;
                }

                break;
            }

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

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

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

            case XmlTokenType.EndOfFile:
            {
                if (_options.IsSuppressingErrors)
                {
                    break;
                }

                throw XmlParseError.EOF.At(token.Position);
            }

            case XmlTokenType.Doctype:
            {
                if (_options.IsSuppressingErrors)
                {
                    break;
                }

                throw XmlParseError.XmlDoctypeAfterContent.At(token.Position);
            }

            case XmlTokenType.Declaration:
            {
                if (_options.IsSuppressingErrors)
                {
                    break;
                }

                throw XmlParseError.XmlDeclarationMisplaced.At(token.Position);
            }
            }
        }