Beispiel #1
0
        /// <summary>
        /// Scans the external portion, i.e. the system identifier of the doctype.
        /// </summary>
        /// <param name="url">The url to use.</param>
        /// <param name="typeDefinitions">The type definitions to modify.</param>
        void ScanExternalSubset(String url, DtdContainer typeDefinitions)
        {
            if (Configuration.HasHttpRequester)
            {
                if (!Location.IsAbsolute(url))
                {
                    url = Location.MakeAbsolute(doc.BaseURI, url);
                }

                var http     = Configuration.GetHttpRequester();
                var response = http.Request(new DefaultHttpRequest {
                    Address = new Uri(url)
                });
                var stream    = new SourceManager(response.Content);
                var container = new DtdContainer(typeDefinitions)
                {
                    Url    = url,
                    Parent = doc
                };

                var dtd = new DtdParser(container, stream);
                dtd.IsInternal = false;
                dtd.Parse();
            }
        }
Beispiel #2
0
        public void SubjectsDtd()
        {
            var dtd = @"<!--see Element Type Declarations
  for an explanation of the following syntax-->
<!ELEMENT document
  (title*,subjectID,subjectname,prerequisite?,
  classes,assessment,syllabus,textbooks*)>
<!ELEMENT prerequisite (subjectID,subjectname)>
<!ELEMENT textbooks (author,booktitle)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT subjectID (#PCDATA)>
<!ELEMENT subjectname (#PCDATA)>
<!ELEMENT classes (#PCDATA)>
<!ELEMENT assessment (#PCDATA)>
<!ATTLIST assessment assessment_type (exam | assignment) #IMPLIED>
<!ELEMENT syllabus (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT booktitle (#PCDATA)>";

            var parser = new DtdParser(dtd);

            parser.Parse();

            var result = parser.Result;

            Assert.AreEqual(13, result.Count);
            Assert.AreEqual(1, result.Comments.Count());
            Assert.AreEqual(1, result.Attributes.Count());
            Assert.AreEqual(11, result.Elements.Count());
        }
        private void ParseDocumentType(XmlDocumentType dtNode, bool bUseResolver, XmlResolver resolver)
        {
            this.doc = dtNode.OwnerDocument;
            XmlNameTable        nt         = this.doc.NameTable;
            XmlNamespaceManager mgr        = new XmlNamespaceManager(nt);
            SchemaInfo          schemaInfo = DtdParser.Parse(nt, mgr, dtNode.ParseWithNamespaces, this.doc.BaseURI, dtNode.Name, dtNode.PublicId,
                                                             dtNode.SystemId, dtNode.InternalSubset, bUseResolver, resolver);

            LoadDocumentType(schemaInfo, dtNode);
        }
Beispiel #4
0
 private void ParseDtdFromParserContext()
 {
     if ((this.parserContext.DocTypeName != null) && (this.parserContext.DocTypeName.Length != 0))
     {
         IDtdParser parser = DtdParser.Create();
         XmlTextReaderImpl.DtdParserProxy adapter = new XmlTextReaderImpl.DtdParserProxy(this.coreReaderImpl);
         IDtdInfo newDtdInfo = parser.ParseFreeFloatingDtd(this.parserContext.BaseURI, this.parserContext.DocTypeName, this.parserContext.PublicId, this.parserContext.SystemId, this.parserContext.InternalSubset, adapter);
         this.coreReaderImpl.SetDtdInfo(newDtdInfo);
         this.ValidateDtd();
     }
 }
Beispiel #5
0
#pragma warning disable 618
        // Creates a XmlValidatingReader suitable for parsing InnerXml strings
        private XmlReader CreateInnerXmlReader(String xmlFragment, XmlNodeType nt, XmlParserContext context, XmlDocument doc)
        {
            XmlNodeType contentNT = nt;

            if (contentNT == XmlNodeType.Entity || contentNT == XmlNodeType.EntityReference)
            {
                contentNT = XmlNodeType.Element;
            }

            XmlTextReaderImpl tr = new XmlTextReaderImpl(xmlFragment, contentNT, context);

            tr.XmlValidatingReaderCompatibilityMode = true;
            if (doc.HasSetResolver)
            {
                tr.XmlResolver = doc.GetResolver();
            }
            if (!(doc.ActualLoadingStatus))
            {
                tr.DisableUndeclaredEntityCheck = true;
            }
            Debug.Assert(tr.EntityHandling == EntityHandling.ExpandCharEntities);

            XmlDocumentType dtdNode = doc.DocumentType;

            if (dtdNode != null)
            {
                tr.Namespaces = dtdNode.ParseWithNamespaces;
                if (dtdNode.DtdSchemaInfo != null)
                {
                    tr.SetDtdInfo(dtdNode.DtdSchemaInfo);
                }
                else
                {
                    IDtdParser dtdParser = DtdParser.Create();
                    XmlTextReaderImpl.DtdParserProxy proxy = new XmlTextReaderImpl.DtdParserProxy(tr);

                    IDtdInfo dtdInfo = dtdParser.ParseFreeFloatingDtd(context.BaseURI, context.DocTypeName, context.PublicId, context.SystemId, context.InternalSubset, proxy);

                    // TODO: Change all of XmlDocument to IDtdInfo interfaces
                    dtdNode.DtdSchemaInfo = dtdInfo as SchemaInfo;
                    tr.SetDtdInfo(dtdInfo);
                }
            }

            if (nt == XmlNodeType.Entity || nt == XmlNodeType.EntityReference)
            {
                tr.Read(); //this will skip the first element "wrapper"
                tr.ResolveEntity();
            }
            return(tr);
        }
Beispiel #6
0
//
// Private implementation methods
//

        private void ParseDtdFromParserContext()
        {
            Debug.Assert(parserContext != null);
            Debug.Assert(coreReaderImpl.DtdSchemaInfo == null);

            if (parserContext.DocTypeName == null || parserContext.DocTypeName.Length == 0)
            {
                return;
            }

            coreReaderImpl.DtdSchemaInfo = DtdParser.Parse(coreReaderImpl, parserContext.BaseURI, parserContext.DocTypeName, parserContext.PublicId,
                                                           parserContext.SystemId, parserContext.InternalSubset);
            ValidateDtd();
        }
Beispiel #7
0
        private XmlReader CreateInnerXmlReader(string xmlFragment, XmlNodeType nt, XmlParserContext context, XmlDocument doc)
        {
            XmlNodeType fragType = nt;

            switch (fragType)
            {
            case XmlNodeType.Entity:
            case XmlNodeType.EntityReference:
                fragType = XmlNodeType.Element;
                break;
            }
            XmlTextReaderImpl reader = new XmlTextReaderImpl(xmlFragment, fragType, context)
            {
                XmlValidatingReaderCompatibilityMode = true
            };

            if (doc.HasSetResolver)
            {
                reader.XmlResolver = doc.GetResolver();
            }
            if (!doc.ActualLoadingStatus)
            {
                reader.DisableUndeclaredEntityCheck = true;
            }
            XmlDocumentType documentType = doc.DocumentType;

            if (documentType != null)
            {
                reader.Namespaces = documentType.ParseWithNamespaces;
                if (documentType.DtdSchemaInfo != null)
                {
                    reader.SetDtdInfo(documentType.DtdSchemaInfo);
                }
                else
                {
                    IDtdParser parser = DtdParser.Create();
                    XmlTextReaderImpl.DtdParserProxy adapter = new XmlTextReaderImpl.DtdParserProxy(reader);
                    IDtdInfo newDtdInfo = parser.ParseFreeFloatingDtd(context.BaseURI, context.DocTypeName, context.PublicId, context.SystemId, context.InternalSubset, adapter);
                    documentType.DtdSchemaInfo = newDtdInfo as SchemaInfo;
                    reader.SetDtdInfo(newDtdInfo);
                }
            }
            if ((nt == XmlNodeType.Entity) || (nt == XmlNodeType.EntityReference))
            {
                reader.Read();
                reader.ResolveEntity();
            }
            return(reader);
        }
Beispiel #8
0
        public void TestDtdParser()
        {
            string    dtd    = @"<!ELEMENT NEWSPAPER (ARTICLE+)>
<!ELEMENT ARTICLE (HEADLINE,BYLINE)>
<!ELEMENT HEADLINE (#PCDATA)>
<!ELEMENT BYLINE (#PCDATA)>";
            DtdParser parser = new DtdParser(dtd);
            var       result = parser.Parse();

            Assert.Equal(result.Count, 2);
            Assert.Equal(result[0].Name, "NEWSPAPER");
            Assert.Equal(result[0].Fields["ARTICLE+"], "List<ARTICLE> ARTICLE");
            Assert.Equal(result[1].Name, "ARTICLE");
            Assert.Equal(result[1].Fields["HEADLINE"], "string HEADLINE");
            Assert.Equal(result[1].Fields["BYLINE"], "string BYLINE");
        }
        //
        // Private implementation methods
        //

        private async Task ParseDtdFromParserContextAsync()
        {
            Debug.Assert(_parserContext != null);
            Debug.Assert(_coreReaderImpl.DtdInfo == null);

            if (_parserContext.DocTypeName == null || _parserContext.DocTypeName.Length == 0)
            {
                return;
            }

            IDtdParser dtdParser = DtdParser.Create();

            XmlTextReaderImpl.DtdParserProxy proxy = new XmlTextReaderImpl.DtdParserProxy(_coreReaderImpl);
            IDtdInfo dtdInfo = await dtdParser.ParseFreeFloatingDtdAsync(_parserContext.BaseURI, _parserContext.DocTypeName, _parserContext.PublicId, _parserContext.SystemId, _parserContext.InternalSubset, proxy).ConfigureAwait(false);

            _coreReaderImpl.SetDtdInfo(dtdInfo);

            ValidateDtd();
        }
Beispiel #10
0
        private void ParseDocumentType(XmlDocumentType dtNode, bool bUseResolver, XmlResolver resolver)
        {
            _doc = dtNode.OwnerDocument;
            XmlParserContext  pc = new XmlParserContext(null, new XmlNamespaceManager(_doc.NameTable), null, null, null, null, _doc.BaseURI, string.Empty, XmlSpace.None);
            XmlTextReaderImpl tr = new XmlTextReaderImpl("", XmlNodeType.Element, pc);

            tr.Namespaces = dtNode.ParseWithNamespaces;
            if (bUseResolver)
            {
                tr.XmlResolver = resolver;
            }

            IDtdParser dtdParser = DtdParser.Create();

            XmlTextReaderImpl.DtdParserProxy proxy = new XmlTextReaderImpl.DtdParserProxy(tr);

            IDtdInfo dtdInfo = dtdParser.ParseFreeFloatingDtd(_doc.BaseURI, dtNode.Name, dtNode.PublicId, dtNode.SystemId, dtNode.InternalSubset, proxy);

            LoadDocumentType(dtdInfo, dtNode);
        }
Beispiel #11
0
        private void ParseDocumentType(XmlDocumentType dtNode, bool bUseResolver, XmlResolver resolver)
        {
            this.doc = dtNode.OwnerDocument;
            XmlParserContext  context = new XmlParserContext(null, new XmlNamespaceManager(this.doc.NameTable), null, null, null, null, this.doc.BaseURI, string.Empty, XmlSpace.None);
            XmlTextReaderImpl reader  = new XmlTextReaderImpl("", XmlNodeType.Element, context)
            {
                Namespaces = dtNode.ParseWithNamespaces
            };

            if (bUseResolver)
            {
                reader.XmlResolver = resolver;
            }
            IDtdParser parser = DtdParser.Create();

            XmlTextReaderImpl.DtdParserProxy adapter = new XmlTextReaderImpl.DtdParserProxy(reader);
            IDtdInfo dtdInfo = parser.ParseFreeFloatingDtd(this.doc.BaseURI, dtNode.Name, dtNode.PublicId, dtNode.SystemId, dtNode.InternalSubset, adapter);

            this.LoadDocumentType(dtdInfo, dtNode);
        }
//
// Private implementation methods
//

        private void ParseDtdFromParserContext()
        {
            Debug.Assert(parserContext != null);
            Debug.Assert(coreReaderImpl.DtdInfo == null);

            if (parserContext.DocTypeName == null || parserContext.DocTypeName.Length == 0)
            {
                return;
            }

            IDtdParser dtdParser = DtdParser.Create();

            XmlTextReaderImpl.DtdParserProxy proxy = new XmlTextReaderImpl.DtdParserProxy(coreReaderImpl);
            IDtdInfo dtdInfo = dtdParser.ParseFreeFloatingDtd(parserContext.BaseURI, parserContext.DocTypeName, parserContext.PublicId,
                                                              parserContext.SystemId, parserContext.InternalSubset, proxy);

            coreReaderImpl.SetDtdInfo(dtdInfo);

            ValidateDtd();
        }
Beispiel #13
0
        public static void Main(string[] args)
        {
            if (!CheckArgs(args))
            {
                return;
            }

            try
            {
                string    dtdContent    = File.ReadAllText(_dtdFilePath);
                DtdParser parser        = new DtdParser(dtdContent);
                var       dtdEntityList = parser.Parse();
                if (dtdEntityList == null || dtdEntityList.Count == 0)
                {
                    Console.WriteLine("0 entity was parsed.");
                    return;
                }

                if (!Directory.Exists(_classDir))
                {
                    Directory.CreateDirectory(_classDir);
                }

                foreach (var entity in dtdEntityList)
                {
                    string classFilePath = Path.Combine(_classDir, $"{entity.Name}.cs");
                    File.WriteAllText(classFilePath, entity.ToString());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Convert failed.");
                Console.WriteLine($"{ex.Message}");
            }

            Console.WriteLine($"Convert successfully. All class files were saved into {_classDir}.");
        }
Beispiel #14
0
 // Constructors
 internal DtdParserProxy(XmlTextReaderImpl reader)
 {
     this.reader    = reader;
     this.dtdParser = new DtdParser(this);
 }
 // Constructors
     internal DtdParserProxy( XmlTextReaderImpl reader ) {
         this.reader = reader;
         this.dtdParser = new DtdParser( this );
     }
Beispiel #16
0
        /// <summary>
        /// Called once an &amp; character is being seen.
        /// </summary>
        /// <param name="c">The next character after the &amp; character.</param>
        /// <returns>The entity token.</returns>
        XmlEntityToken CharacterReference(Char c)
        {
            var buffer = Pool.NewStringBuilder();

            if (c == Specification.NUM)
            {
                c = _src.Next;
                var hex = c == 'x' || c == 'X';

                if (hex)
                {
                    c = _src.Next;

                    while (c.IsHex())
                    {
                        buffer.Append(c);
                        c = _src.Next;
                    }
                }
                else
                {
                    while (c.IsDigit())
                    {
                        buffer.Append(c);
                        c = _src.Next;
                    }
                }

                if (buffer.Length > 0 && c == Specification.SC)
                {
                    return new XmlEntityToken {
                               Value = buffer.ToPool(), IsNumeric = true, IsHex = hex
                    }
                }
                ;
            }
            else if (c.IsXmlNameStart())
            {
                do
                {
                    buffer.Append(c);

                    c = _src.Next;
                }while (c.IsXmlName());

                if (c == Specification.SC)
                {
                    return new XmlEntityToken {
                               Value = buffer.ToPool()
                    }
                }
                ;
            }

            buffer.ToPool();
            throw Errors.Xml(ErrorCode.CharacterReferenceNotTerminated);
        }

        #endregion

        #region Tags

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#sec-starttags.
        /// </summary>
        /// <param name="c">The next input character.</param>
        XmlToken TagOpen(Char c)
        {
            if (c == Specification.EM)
            {
                return(MarkupDeclaration(_src.Next));
            }

            if (c == Specification.QM)
            {
                c = _src.Next;

                if (_src.ContinuesWith(Tags.XML, false))
                {
                    _src.Advance(2);

                    return(DeclarationStart(_src.Next));
                }

                return(ProcessingStart(c));
            }

            if (c == Specification.SOLIDUS)
            {
                return(TagEnd(_src.Next));
            }

            if (c.IsXmlNameStart())
            {
                _stringBuffer.Clear();
                _stringBuffer.Append(c);
                return(TagName(_src.Next, XmlToken.OpenTag()));
            }

            throw Errors.Xml(ErrorCode.XmlInvalidStartTag);
        }

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#dt-etag.
        /// </summary>
        /// <param name="c">The next input character.</param>
        XmlToken TagEnd(Char c)
        {
            if (c.IsXmlNameStart())
            {
                _stringBuffer.Clear();

                do
                {
                    _stringBuffer.Append(c);
                    c = _src.Next;
                }while (c.IsXmlName());

                while (c.IsSpaceCharacter())
                {
                    c = _src.Next;
                }

                if (c == Specification.GT)
                {
                    var tag = XmlToken.CloseTag();
                    tag.Name = _stringBuffer.ToString();
                    return(tag);
                }
            }

            if (c == Specification.EOF)
            {
                throw Errors.Xml(ErrorCode.EOF);
            }

            throw Errors.Xml(ErrorCode.XmlInvalidEndTag);
        }

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#NT-Name.
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="tag">The current tag token.</param>
        /// <returns>The emitted token.</returns>
        XmlToken TagName(Char c, XmlTagToken tag)
        {
            while (c.IsXmlName())
            {
                _stringBuffer.Append(c);
                c = _src.Next;
            }

            tag.Name = _stringBuffer.ToString();

            if (c == Specification.EOF)
            {
                throw Errors.Xml(ErrorCode.EOF);
            }

            if (c == Specification.GT)
            {
                return(tag);
            }
            else if (c.IsSpaceCharacter())
            {
                return(AttributeBeforeName(_src.Next, tag));
            }
            else if (c == Specification.SOLIDUS)
            {
                return(TagSelfClosing(_src.Next, tag));
            }

            throw Errors.Xml(ErrorCode.XmlInvalidName);
        }

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#d0e2480.
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="tag">The current tag token.</param>
        XmlToken TagSelfClosing(Char c, XmlTagToken tag)
        {
            tag.IsSelfClosing = true;

            if (c == Specification.GT)
            {
                return(tag);
            }

            if (c == Specification.EOF)
            {
                throw Errors.Xml(ErrorCode.EOF);
            }

            throw Errors.Xml(ErrorCode.XmlInvalidName);
        }

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#dt-markup.
        /// </summary>
        /// <param name="c">The next input character.</param>
        XmlToken MarkupDeclaration(Char c)
        {
            if (_src.ContinuesWith("--"))
            {
                _src.Advance();
                return(CommentStart(_src.Next));
            }
            else if (_src.ContinuesWith(Tags.DOCTYPE, false))
            {
                _src.Advance(6);
                return(Doctype(_src.Next));
            }
            else if (_src.ContinuesWith(CDATA, false))
            {
                _src.Advance(6);
                return(CData(_src.Next));
            }

            throw Errors.Xml(ErrorCode.UndefinedMarkupDeclaration);
        }

        #endregion

        #region XML Declaration

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#NT-XMLDecl.
        /// </summary>
        /// <param name="c">The next input character.</param>
        XmlToken DeclarationStart(Char c)
        {
            if (!c.IsSpaceCharacter())
            {
                _stringBuffer.Clear();
                _stringBuffer.Append(Tags.XML);
                return(ProcessingTarget(c, XmlToken.Processing()));
            }

            do
            {
                c = _src.Next;
            }while (c.IsSpaceCharacter());

            if (_src.ContinuesWith(AttributeNames.VERSION, false))
            {
                _src.Advance(6);
                return(DeclarationVersionAfterName(_src.Next, XmlToken.Declaration()));
            }

            throw Errors.Xml(ErrorCode.XmlDeclarationInvalid);
        }

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#NT-VersionInfo.
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="decl">The current declaration token.</param>
        XmlToken DeclarationVersionAfterName(Char c, XmlDeclarationToken decl)
        {
            while (c.IsSpaceCharacter())
            {
                c = _src.Next;
            }

            if (c == Specification.EQ)
            {
                return(DeclarationVersionBeforeValue(_src.Next, decl));
            }

            throw Errors.Xml(ErrorCode.XmlDeclarationInvalid);
        }

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#NT-VersionInfo.
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="decl">The current declaration token.</param>
        XmlToken DeclarationVersionBeforeValue(Char c, XmlDeclarationToken decl)
        {
            while (c.IsSpaceCharacter())
            {
                c = _src.Next;
            }

            if (c == Specification.DQ || c == Specification.SQ)
            {
                _stringBuffer.Clear();
                return(DeclarationVersionValue(_src.Next, c, decl));
            }

            throw Errors.Xml(ErrorCode.XmlDeclarationInvalid);
        }

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#NT-VersionInfo.
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="q">The quote character.</param>
        /// <param name="decl">The current declaration token.</param>
        XmlToken DeclarationVersionValue(Char c, Char q, XmlDeclarationToken decl)
        {
            while (c != q)
            {
                if (c == Specification.EOF)
                {
                    throw Errors.Xml(ErrorCode.EOF);
                }

                _stringBuffer.Append(c);
                c = _src.Next;
            }

            decl.Version = _stringBuffer.ToString();
            c            = _src.Next;

            if (c.IsSpaceCharacter())
            {
                return(DeclarationAfterVersion(c, decl));
            }

            return(DeclarationEnd(c, decl));
        }

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#NT-VersionNum.
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="decl">The current declaration token.</param>
        XmlToken DeclarationAfterVersion(Char c, XmlDeclarationToken decl)
        {
            while (c.IsSpaceCharacter())
            {
                c = _src.Next;
            }

            if (_src.ContinuesWith(AttributeNames.ENCODING, false))
            {
                _src.Advance(7);
                return(DeclarationEncodingAfterName(_src.Next, decl));
            }
            else if (_src.ContinuesWith(AttributeNames.STANDALONE, false))
            {
                _src.Advance(9);
                return(DeclarationStandaloneAfterName(_src.Next, decl));
            }

            return(DeclarationEnd(c, decl));
        }

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#NT-EncodingDecl.
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="decl">The current declaration token.</param>
        XmlToken DeclarationEncodingAfterName(Char c, XmlDeclarationToken decl)
        {
            while (c.IsSpaceCharacter())
            {
                c = _src.Next;
            }

            if (c == Specification.EQ)
            {
                return(DeclarationEncodingBeforeValue(_src.Next, decl));
            }

            throw Errors.Xml(ErrorCode.XmlDeclarationInvalid);
        }

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#NT-EncodingDecl.
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="decl">The current declaration token.</param>
        XmlToken DeclarationEncodingBeforeValue(Char c, XmlDeclarationToken decl)
        {
            while (c.IsSpaceCharacter())
            {
                c = _src.Next;
            }

            if (c == Specification.DQ || c == Specification.SQ)
            {
                var q = c;
                _stringBuffer.Clear();
                c = _src.Next;

                if (c.IsLetter())
                {
                    return(DeclarationEncodingValue(c, q, decl));
                }
            }

            throw Errors.Xml(ErrorCode.XmlDeclarationInvalid);
        }

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#NT-EncodingDecl.
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="q">The quote character.</param>
        /// <param name="decl">The current declaration token.</param>
        XmlToken DeclarationEncodingValue(Char c, Char q, XmlDeclarationToken decl)
        {
            do
            {
                if (c.IsAlphanumericAscii() || c == Specification.DOT || c == Specification.UNDERSCORE || c == Specification.MINUS)
                {
                    _stringBuffer.Append(c);
                    c = _src.Next;
                }
                else
                {
                    throw Errors.Xml(ErrorCode.XmlDeclarationInvalid);
                }
            }while (c != q);

            decl.Encoding = _stringBuffer.ToString();
            c             = _src.Next;

            if (c.IsSpaceCharacter())
            {
                return(DeclarationAfterEncoding(c, decl));
            }

            return(DeclarationEnd(c, decl));
        }

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#NT-SDDecl.
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="decl">The current declaration token.</param>
        XmlToken DeclarationAfterEncoding(Char c, XmlDeclarationToken decl)
        {
            while (c.IsSpaceCharacter())
            {
                c = _src.Next;
            }

            if (_src.ContinuesWith(AttributeNames.STANDALONE, false))
            {
                _src.Advance(9);
                return(DeclarationStandaloneAfterName(_src.Next, decl));
            }

            return(DeclarationEnd(c, decl));
        }

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#NT-SDDecl.
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="decl">The current declaration token.</param>
        XmlToken DeclarationStandaloneAfterName(Char c, XmlDeclarationToken decl)
        {
            while (c.IsSpaceCharacter())
            {
                c = _src.Next;
            }

            if (c == Specification.EQ)
            {
                return(DeclarationStandaloneBeforeValue(_src.Next, decl));
            }

            throw Errors.Xml(ErrorCode.XmlDeclarationInvalid);
        }

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#NT-SDDecl.
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="decl">The current declaration token.</param>
        XmlToken DeclarationStandaloneBeforeValue(Char c, XmlDeclarationToken decl)
        {
            while (c.IsSpaceCharacter())
            {
                c = _src.Next;
            }

            if (c == Specification.DQ || c == Specification.SQ)
            {
                _stringBuffer.Clear();
                return(DeclarationStandaloneValue(_src.Next, c, decl));
            }

            throw Errors.Xml(ErrorCode.XmlDeclarationInvalid);
        }

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#NT-SDDecl.
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="q">The quote character.</param>
        /// <param name="decl">The current declaration token.</param>
        XmlToken DeclarationStandaloneValue(Char c, Char q, XmlDeclarationToken decl)
        {
            while (c != q)
            {
                if (c == Specification.EOF)
                {
                    throw Errors.Xml(ErrorCode.EOF);
                }

                _stringBuffer.Append(c);
                c = _src.Next;
            }

            var s = _stringBuffer.ToString();

            if (s.Equals(YES))
            {
                decl.Standalone = true;
            }
            else if (s.Equals(NO))
            {
                decl.Standalone = false;
            }
            else
            {
                throw Errors.Xml(ErrorCode.XmlDeclarationInvalid);
            }

            return(DeclarationEnd(_src.Next, decl));
        }

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#NT-XMLDecl.
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="decl">The current declaration token.</param>
        XmlDeclarationToken DeclarationEnd(Char c, XmlDeclarationToken decl)
        {
            while (c.IsSpaceCharacter())
            {
                c = _src.Next;
            }

            if (c != Specification.QM || _src.Next != Specification.GT)
            {
                throw Errors.Xml(ErrorCode.XmlDeclarationInvalid);
            }

            return(decl);
        }

        #endregion

        #region Doctype

        /// <summary>
        /// See 8.2.4.52 DOCTYPE state
        /// </summary>
        /// <param name="c">The next input character.</param>
        XmlToken Doctype(Char c)
        {
            if (c.IsSpaceCharacter())
            {
                return(DoctypeNameBefore(_src.Next));
            }

            throw Errors.Xml(ErrorCode.DoctypeInvalid);
        }

        /// <summary>
        /// See 8.2.4.53 Before DOCTYPE name state
        /// </summary>
        /// <param name="c">The next input character.</param>
        XmlToken DoctypeNameBefore(Char c)
        {
            while (c.IsSpaceCharacter())
            {
                c = _src.Next;
            }

            if (c.IsXmlNameStart())
            {
                _stringBuffer.Clear();
                _stringBuffer.Append(c);
                return(DoctypeName(_src.Next, XmlToken.Doctype()));
            }

            throw Errors.Xml(ErrorCode.DoctypeInvalid);
        }

        /// <summary>
        /// See 8.2.4.54 DOCTYPE name state
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="doctype">The current doctype token.</param>
        /// <returns>The emitted token.</returns>
        XmlToken DoctypeName(Char c, XmlDoctypeToken doctype)
        {
            while (c.IsXmlName())
            {
                _stringBuffer.Append(c);
                c = _src.Next;
            }

            doctype.Name = _stringBuffer.ToString();
            _stringBuffer.Clear();

            if (c == Specification.GT)
            {
                return(doctype);
            }
            else if (c.IsSpaceCharacter())
            {
                return(DoctypeNameAfter(_src.Next, doctype));
            }

            throw Errors.Xml(ErrorCode.DoctypeInvalid);
        }

        /// <summary>
        /// See 8.2.4.55 After DOCTYPE name state
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="doctype">The current doctype token.</param>
        /// <returns>The emitted token.</returns>
        XmlToken DoctypeNameAfter(Char c, XmlDoctypeToken doctype)
        {
            while (c.IsSpaceCharacter())
            {
                c = _src.Next;
            }

            if (c == Specification.GT)
            {
                return(doctype);
            }

            if (_src.ContinuesWith(PUBLIC, false))
            {
                _src.Advance(5);
                return(DoctypePublic(_src.Next, doctype));
            }
            else if (_src.ContinuesWith(SYSTEM, false))
            {
                _src.Advance(5);
                return(DoctypeSystem(_src.Next, doctype));
            }
            else if (c == Specification.SBO)
            {
                _src.Advance();
                ScanInternalSubset(doctype);
                return(DoctypeAfter(_src.Next, doctype));
            }

            throw Errors.Xml(ErrorCode.DoctypeInvalid);
        }

        /// <summary>
        /// See 8.2.4.56 After DOCTYPE public keyword state
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="doctype">The current doctype token.</param>
        /// <returns>The emitted token.</returns>
        XmlToken DoctypePublic(Char c, XmlDoctypeToken doctype)
        {
            if (c.IsSpaceCharacter())
            {
                while (c.IsSpaceCharacter())
                {
                    c = _src.Next;
                }

                if (c == Specification.DQ || c == Specification.SQ)
                {
                    doctype.PublicIdentifier = String.Empty;
                    return(DoctypePublicIdentifierValue(_src.Next, c, doctype));
                }
            }

            throw Errors.Xml(ErrorCode.DoctypeInvalid);
        }

        /// <summary>
        /// See 8.2.4.58 DOCTYPE public identifier (double-quoted) state
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="q">The closing character.</param>
        /// <param name="doctype">The current doctype token.</param>
        /// <returns>The emitted token.</returns>
        XmlToken DoctypePublicIdentifierValue(Char c, Char q, XmlDoctypeToken doctype)
        {
            while (c != q)
            {
                if (!c.IsPubidChar())
                {
                    throw Errors.Xml(ErrorCode.XmlInvalidPubId);
                }

                _stringBuffer.Append(c);
                c = _src.Next;
            }

            doctype.PublicIdentifier = _stringBuffer.ToString();
            _stringBuffer.Clear();
            return(DoctypePublicIdentifierAfter(_src.Next, doctype));
        }

        /// <summary>
        /// See 8.2.4.60 After DOCTYPE public identifier state
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="doctype">The current doctype token.</param>
        /// <returns>The emitted token.</returns>
        XmlToken DoctypePublicIdentifierAfter(Char c, XmlDoctypeToken doctype)
        {
            if (c == Specification.GT)
            {
                return(doctype);
            }
            else if (c.IsSpaceCharacter())
            {
                return(DoctypeBetween(_src.Next, doctype));
            }

            throw Errors.Xml(ErrorCode.DoctypeInvalid);
        }

        /// <summary>
        /// See 8.2.4.61 Between DOCTYPE public and system identifiers state
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="doctype">The current doctype token.</param>
        /// <returns>The emitted token.</returns>
        XmlToken DoctypeBetween(Char c, XmlDoctypeToken doctype)
        {
            while (c.IsSpaceCharacter())
            {
                c = _src.Next;
            }

            if (c == Specification.GT)
            {
                return(doctype);
            }

            if (c == Specification.DQ || c == Specification.SQ)
            {
                doctype.SystemIdentifier = String.Empty;
                return(DoctypeSystemIdentifierValue(_src.Next, c, doctype));
            }

            throw Errors.Xml(ErrorCode.DoctypeInvalid);
        }

        /// <summary>
        /// See 8.2.4.62 After DOCTYPE system keyword state
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="doctype">The current doctype token.</param>
        /// <returns>The emitted token.</returns>
        XmlToken DoctypeSystem(Char c, XmlDoctypeToken doctype)
        {
            if (c.IsSpaceCharacter())
            {
                while (c.IsSpaceCharacter())
                {
                    c = _src.Next;
                }

                if (c == Specification.DQ || c == Specification.SQ)
                {
                    doctype.SystemIdentifier = String.Empty;
                    return(DoctypeSystemIdentifierValue(_src.Next, c, doctype));
                }
            }

            throw Errors.Xml(ErrorCode.DoctypeInvalid);
        }

        /// <summary>
        /// See 8.2.4.64 DOCTYPE system identifier (double-quoted) state
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="q">The quote character.</param>
        /// <param name="doctype">The current doctype token.</param>
        /// <returns>The emitted token.</returns>
        XmlToken DoctypeSystemIdentifierValue(Char c, Char q, XmlDoctypeToken doctype)
        {
            while (c != q)
            {
                if (c == Specification.EOF)
                {
                    throw Errors.Xml(ErrorCode.EOF);
                }

                _stringBuffer.Append(c);
                c = _src.Next;
            }

            doctype.SystemIdentifier = _stringBuffer.ToString();
            _stringBuffer.Clear();
            return(DoctypeSystemIdentifierAfter(_src.Next, doctype));
        }

        /// <summary>
        /// See 8.2.4.66 After DOCTYPE system identifier state
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="doctype">The current doctype token.</param>
        /// <returns>The emitted token.</returns>
        XmlToken DoctypeSystemIdentifierAfter(Char c, XmlDoctypeToken doctype)
        {
            while (c.IsSpaceCharacter())
            {
                c = _src.Next;
            }

            if (c == Specification.SBO)
            {
                _src.Advance();
                ScanInternalSubset(doctype);
                c = _src.Next;
            }

            return(DoctypeAfter(c, doctype));
        }

        /// <summary>
        /// The doctype finalizer.
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="doctype">The current doctype token.</param>
        /// <returns>The emitted token.</returns>
        XmlToken DoctypeAfter(Char c, XmlDoctypeToken doctype)
        {
            while (c.IsSpaceCharacter())
            {
                c = _src.Next;
            }

            if (c == Specification.GT)
            {
                return(doctype);
            }

            throw Errors.Xml(ErrorCode.DoctypeInvalid);
        }

        #endregion

        #region Attributes

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#NT-Attribute.
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="tag">The current tag token.</param>
        XmlToken AttributeBeforeName(Char c, XmlTagToken tag)
        {
            while (c.IsSpaceCharacter())
            {
                c = _src.Next;
            }

            if (c == Specification.SOLIDUS)
            {
                return(TagSelfClosing(_src.Next, tag));
            }
            else if (c == Specification.GT)
            {
                return(tag);
            }
            else if (c == Specification.EOF)
            {
                throw Errors.Xml(ErrorCode.EOF);
            }

            if (c.IsXmlNameStart())
            {
                _stringBuffer.Clear();
                _stringBuffer.Append(c);
                return(AttributeName(_src.Next, tag));
            }

            throw Errors.Xml(ErrorCode.XmlInvalidAttribute);
        }

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#NT-Attribute.
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="tag">The current tag token.</param>
        XmlToken AttributeName(Char c, XmlTagToken tag)
        {
            while (c.IsXmlName())
            {
                _stringBuffer.Append(c);
                c = _src.Next;
            }

            var name = _stringBuffer.ToString();

            if (!String.IsNullOrEmpty(tag.GetAttribute(name)))
            {
                throw Errors.Xml(ErrorCode.XmlUniqueAttribute);
            }

            tag.AddAttribute(name);

            if (c.IsSpaceCharacter())
            {
                do
                {
                    c = _src.Next;
                }while (c.IsSpaceCharacter());
            }

            if (c == Specification.EQ)
            {
                return(AttributeBeforeValue(_src.Next, tag));
            }

            throw Errors.Xml(ErrorCode.XmlInvalidAttribute);
        }

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#NT-Attribute.
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="tag">The current tag token.</param>
        XmlToken AttributeBeforeValue(Char c, XmlTagToken tag)
        {
            while (c.IsSpaceCharacter())
            {
                c = _src.Next;
            }

            if (c == Specification.DQ || c == Specification.SQ)
            {
                _stringBuffer.Clear();
                return(AttributeValue(_src.Next, c, tag));
            }

            throw Errors.Xml(ErrorCode.XmlInvalidAttribute);
        }

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#NT-Attribute.
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="q">The quote character.</param>
        /// <param name="tag">The current tag token.</param>
        XmlToken AttributeValue(Char c, Char q, XmlTagToken tag)
        {
            while (c != q)
            {
                if (c == Specification.EOF)
                {
                    throw Errors.Xml(ErrorCode.EOF);
                }

                if (c == Specification.AMPERSAND)
                {
                    _stringBuffer.Append(GetEntity(CharacterReference(_src.Next)));
                }
                else if (c == Specification.LT)
                {
                    throw Errors.Xml(ErrorCode.XmlLtInAttributeValue);
                }
                else
                {
                    _stringBuffer.Append(c);
                }

                c = _src.Next;
            }

            tag.SetAttributeValue(_stringBuffer.ToString());
            return(AttributeAfterValue(_src.Next, tag));
        }

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#NT-Attribute.
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="tag">The current tag token.</param>
        XmlToken AttributeAfterValue(Char c, XmlTagToken tag)
        {
            if (c.IsSpaceCharacter())
            {
                return(AttributeBeforeName(_src.Next, tag));
            }
            else if (c == Specification.SOLIDUS)
            {
                return(TagSelfClosing(_src.Next, tag));
            }
            else if (c == Specification.GT)
            {
                return(tag);
            }

            throw Errors.Xml(ErrorCode.XmlInvalidAttribute);
        }

        #endregion

        #region Processing Instruction

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#sec-pi.
        /// </summary>
        /// <param name="c">The next input character.</param>
        XmlToken ProcessingStart(Char c)
        {
            if (c.IsXmlNameStart())
            {
                _stringBuffer.Clear();
                _stringBuffer.Append(c);
                return(ProcessingTarget(_src.Next, XmlToken.Processing()));
            }

            throw Errors.Xml(ErrorCode.XmlInvalidPI);
        }

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#sec-pi.
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="pi">The processing instruction token.</param>
        XmlToken ProcessingTarget(Char c, XmlPIToken pi)
        {
            while (c.IsXmlName())
            {
                _stringBuffer.Append(c);
                c = _src.Next;
            }

            pi.Target = _stringBuffer.ToString();
            _stringBuffer.Clear();

            if (String.Compare(pi.Target, Tags.XML, StringComparison.OrdinalIgnoreCase) == 0)
            {
                throw Errors.Xml(ErrorCode.XmlInvalidPI);
            }

            if (c == Specification.QM)
            {
                c = _src.Next;

                if (c == Specification.GT)
                {
                    return(pi);
                }
            }
            else if (c.IsSpaceCharacter())
            {
                return(ProcessingContent(_src.Next, pi));
            }

            throw Errors.Xml(ErrorCode.XmlInvalidPI);
        }

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#sec-pi.
        /// </summary>
        /// <param name="c">The next input character.</param>
        /// <param name="pi">The processing instruction token.</param>
        XmlToken ProcessingContent(Char c, XmlPIToken pi)
        {
            while (c != Specification.EOF)
            {
                if (c == Specification.QM)
                {
                    c = _src.Next;

                    if (c == Specification.GT)
                    {
                        pi.Content = _stringBuffer.ToString();
                        return(pi);
                    }

                    _stringBuffer.Append(Specification.QM);
                }
                else
                {
                    _stringBuffer.Append(c);
                    c = _src.Next;
                }
            }

            throw Errors.Xml(ErrorCode.EOF);
        }

        #endregion

        #region Comments

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#sec-comments.
        /// </summary>
        /// <param name="c">The next input character.</param>
        XmlToken CommentStart(Char c)
        {
            _stringBuffer.Clear();
            return(Comment(c));
        }

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#sec-comments.
        /// </summary>
        /// <param name="c">The next input character.</param>
        XmlToken Comment(Char c)
        {
            while (c.IsXmlChar())
            {
                if (c == Specification.MINUS)
                {
                    return(CommentDash(_src.Next));
                }

                _stringBuffer.Append(c);
                c = _src.Next;
            }

            throw Errors.Xml(ErrorCode.XmlInvalidComment);
        }

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#sec-comments.
        /// </summary>
        /// <param name="c">The next input character.</param>
        XmlToken CommentDash(Char c)
        {
            if (c == Specification.MINUS)
            {
                return(CommentEnd(_src.Next));
            }

            return(Comment(c));
        }

        /// <summary>
        /// More http://www.w3.org/TR/REC-xml/#sec-comments.
        /// </summary>
        /// <param name="c">The next input character.</param>
        XmlToken CommentEnd(Char c)
        {
            if (c == Specification.GT)
            {
                return(XmlToken.Comment(_stringBuffer.ToString()));
            }

            throw Errors.Xml(ErrorCode.XmlInvalidComment);
        }

        #endregion

        #region Helpers

        /// <summary>
        /// Scans the internal subset, i.e. the DTD in [] of the current source.
        /// </summary>
        /// <param name="doctype">The doctype which contains the subset.</param>
        void ScanInternalSubset(XmlDoctypeToken doctype)
        {
            var dtd = new DtdParser(_dtd, _src);

            dtd.IsInternal     = true;
            dtd.ErrorOccurred += (s, e) => RaiseErrorOccurred(s, e);
            dtd.Parse();
            doctype.InternalSubset = dtd.Result.Text;
        }

        #endregion
    }
}
 internal DtdParserProxy( string baseUri, string docTypeName, string publicId, string systemId, 
                          string internalSubset, XmlTextReaderImpl reader ) {
     this.reader = reader;
     this.dtdParser = new DtdParser( baseUri, docTypeName, publicId, systemId, internalSubset, this );
 }
Beispiel #18
0
 internal XmlDtdTokenInfo(XmlScanner scanner, XmlNamespaceManager nsMgr, XmlNodeType type,
     int depth, bool nor) : base(scanner, nsMgr, type, depth, nor) {
     _DtdParser = null;
 }
Beispiel #19
0
 internal XmlDtdTokenInfo(XmlScanner scanner, XmlNamespaceManager nsMgr, XmlNodeType type,
                          int depth, bool nor) : base(scanner, nsMgr, type, depth, nor)
 {
     _DtdParser = null;
 }
Beispiel #20
0
        // All settings remain the same.
        /// <include file='doc\xmltextreader.uex' path='docs/doc[@for="XmlTextReader.ResetState"]/*' />
        public void ResetState() {
           if (XmlNodeType.None != _PartialContentNodeType) {
                    throw new InvalidOperationException(Res.GetString(Res.Xml_InvalidResetStateCall));
           }

            if (ReadState.Initial == ReadState) {
                //noop. we are already at the begining of the document
                return;
            }
            _ReadState = ReadState.Initial;
            _RootCount = 0;

            ResetFieldsCollection();
            _Scanner.Reset();
            _NextFunction = _ParseRootIndex;
            _DtdParser = null;
            _TmpToken  = new XmlNSElementTokenInfo(_Scanner, _NsMgr, XmlNodeType.None, String.Empty,-1, -1, -1, 0,false);
            _CurrentToken = _TmpToken;
            _CantHaveXmlDecl = false;
            _Encoding = _Scanner.Encoding;
            //
            // pop all the pushed elements
            //
            while (Pop() != null)
                ;

        }
Beispiel #21
0
        //
        // functions handle DTDParser, DTD and schema Validation
        //

        private String ParseDtd( XmlScanner scanner ) {
            if (_DtdParser == null)
                _DtdParser = new DtdParser(scanner, (XmlReader)this, this.GetResolver(), _NameTable, _ValidationEventHandler, _CheckNamespaces);
            _DtdParser.Parse();
            return _NameTable.Add(_DtdParser.GetSchemaInfo().DocTypeName.ToString());
        }
Beispiel #22
0
 internal DtdParserProxy(string baseUri, string docTypeName, string publicId, string systemId,
                         string internalSubset, XmlTextReaderImpl reader)
 {
     this.reader    = reader;
     this.dtdParser = new DtdParser(baseUri, docTypeName, publicId, systemId, internalSubset, this);
 }