コード例 #1
0
        public InputSource ResolveEntity(string name, string publicId, string baseUri, string systemId)
        {
            string dtdUniqueResourceId;
            Stream dtdStream = LocalXmlUrlResolver.mapUri(new Uri(systemId, UriKind.Absolute), out dtdUniqueResourceId);

            if (!string.IsNullOrEmpty(dtdUniqueResourceId))
            {
                DebugFix.Assert(dtdStream != null);

                TextReader txtReader = new StreamReader(dtdStream, Encoding.UTF8);
                return(new InputSource <TextReader>(txtReader, systemId));

                //return new InputSource<Stream>(dtdStream, systemId);
            }

            return(null);
        }
コード例 #2
0
        protected DocumentMarkupType parseContentDocument_DTD(Project project, XmlDocument xmlDoc, TreeNode parentTreeNode, string filePath, out string dtdUniqueResourceId)
        {
            dtdUniqueResourceId = null;

            DocumentMarkupType docMarkupType = DocumentMarkupType.NA;

            //xmlNode.OwnerDocument
            string dtdID = xmlDoc.DocumentType == null ? string.Empty
            : !string.IsNullOrEmpty(xmlDoc.DocumentType.SystemId) ? xmlDoc.DocumentType.SystemId
            : !string.IsNullOrEmpty(xmlDoc.DocumentType.PublicId) ? xmlDoc.DocumentType.PublicId
            : xmlDoc.DocumentType.Name;

            string rootElemName = xmlDoc.DocumentElement.LocalName;

            if (dtdID == @"html" &&
                string.IsNullOrEmpty(xmlDoc.DocumentType.SystemId) &&
                string.IsNullOrEmpty(xmlDoc.DocumentType.PublicId))
            {
                dtdID         = @"html5";
                docMarkupType = DocumentMarkupType.XHTML5;
                DebugFix.Assert(rootElemName == @"html");
            }
            else if (dtdID.Contains(@"xhtml1")
                     //systemId.Contains(@"xhtml11.dtd")
                     //|| systemId.Contains(@"xhtml1-strict.dtd")
                     //|| systemId.Contains(@"xhtml1-transitional.dtd")
                     )
            {
                dtdID         = @"http://www.w3.org/xhtml-math-svg-flat.dtd";
                docMarkupType = DocumentMarkupType.XHTML;
                DebugFix.Assert(rootElemName == @"html");
            }
            else if (rootElemName == @"dtbook")
            {
                docMarkupType = DocumentMarkupType.DTBOOK;
            }
            else if (rootElemName == @"html")
            {
                dtdID         = @"html5";
                docMarkupType = DocumentMarkupType.XHTML5;
            }

            if (docMarkupType == DocumentMarkupType.NA)
            {
#if DEBUG
                Debugger.Break();
#endif
            }

            if (string.IsNullOrEmpty(dtdID))
            {
                return(docMarkupType);
            }

            if (!string.IsNullOrEmpty(dtdID) && !dtdID.StartsWith(@"http://"))
            {
                dtdID = @"http://www.daisy.org/" + dtdID;
            }

            bool needToLoadDTDManuallyToCheckMixedContentElements = docMarkupType == DocumentMarkupType.XHTML5;
            if (docMarkupType == DocumentMarkupType.DTBOOK)
            {
                XmlNode rootElement = XmlDocumentHelper.GetFirstChildElementOrSelfWithName(xmlDoc, true, "book", null);
                DebugFix.Assert(rootElement != null);
                if (rootElement != null)
                {
                    XmlAttributeCollection attrs = rootElement.Attributes;
                    if (attrs != null)
                    {
                        XmlNode attr = attrs.GetNamedItem("space", XmlReaderWriterHelper.NS_URL_XML);
                        if (attr == null)
                        {
                            attr = attrs.GetNamedItem("xml:space", XmlReaderWriterHelper.NS_URL_XML);
                        }

                        if (attr != null && attr.Value == "preserve")
                        {
                            //Bookshare hack! :(
                            needToLoadDTDManuallyToCheckMixedContentElements = true;
                        }
                    }
                }
            }

            if (!needToLoadDTDManuallyToCheckMixedContentElements)
            {
                return(docMarkupType);
            }

            bool isHTML = docMarkupType == DocumentMarkupType.XHTML || docMarkupType == DocumentMarkupType.XHTML5;

#if ENABLE_DTDSHARP
            Stream dtdStream = LocalXmlUrlResolver.mapUri(new Uri(dtdID, UriKind.Absolute), out dtdUniqueResourceId);

            if (!string.IsNullOrEmpty(dtdUniqueResourceId))
            {
                DebugFix.Assert(dtdStream != null);

                List <string> list;
                m_listOfMixedContentXmlElementNames.TryGetValue(dtdUniqueResourceId, out list);

                if (list == null)
                {
                    if (dtdStream != null)
                    {
                        list = new List <string>();
                        m_listOfMixedContentXmlElementNames.Add(dtdUniqueResourceId, list);

                        initMixedContentXmlElementNamesFromDTD(dtdUniqueResourceId, dtdStream);
                    }
                    else
                    {
#if DEBUG
                        Debugger.Break();
#endif
                    }
                }
                else
                {
                    if (dtdStream != null)
                    {
                        dtdStream.Close();
                    }
                }
            }
            else
            {
#if DEBUG
                Debugger.Break();
#endif
            }
#else
            dtdUniqueResourceId = dtdID;

            List <string> list;
            m_listOfMixedContentXmlElementNames.TryGetValue(dtdUniqueResourceId, out list);

            if (list != null)
            {
                return(docMarkupType);
            }

            list = new List <string>();
            m_listOfMixedContentXmlElementNames.Add(dtdUniqueResourceId, list);

            IXmlReader reader = null;


            //string dll = @"SaxNET.dll";
            ////#if NET40
            ////                            dll = @"\SaxNET_NET4.dll";
            ////#endif
            //string appFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            //string dtdPath = Path.Combine(appFolder, dll);
            //Assembly assembly = Assembly.LoadFrom(dtdPath);
            //                            try
            //                            {
            //                                reader = SaxReaderFactory.CreateReader(assembly, null);
            //                            }
            //                            catch (Exception e)
            //                            {
            //#if DEBUG
            //                                Debugger.Break();
            //#endif
            //                            }

            bool useCSharpSaxImpl = IsRunning64(); // docMarkupType == DocumentMarkupType.XHTML5;
            if (useCSharpSaxImpl)
            {
                reader = new SaxDriver();
            }
            else
            {
                reader = new ExpatReader();
            }

            DebugFix.Assert(reader != null);
            if (reader == null)
            {
                return(docMarkupType);
            }
            //Type readerType = reader.GetType();

            reader.EntityResolver = new SaxEntityResolver();

            SaxErrorHandler errorHandler = new SaxErrorHandler();
            reader.ErrorHandler = errorHandler;


            if (reader is SaxDriver)
            {
                //"namespaces"
                try
                {
                    reader.SetFeature(Constants.NamespacesFeature, true);
                }
                catch (Exception e)
                {
#if DEBUG
                    Debugger.Break();
#endif
                }

                //"namespace-prefixes"
                try
                {
                    reader.SetFeature(Constants.NamespacePrefixesFeature, true);
                }
                catch (Exception e)
                {
#if DEBUG
                    Debugger.Break();
#endif
                }

                //"external-general-entities"
                try
                {
                    reader.SetFeature(Constants.ExternalGeneralFeature, true);
                }
                catch (Exception e)
                {
#if DEBUG
                    Debugger.Break();
#endif
                }

                //"external-parameter-entities"
                try
                {
                    reader.SetFeature(Constants.ExternalParameterFeature, true);
                }
                catch (Exception e)
                {
#if DEBUG
                    Debugger.Break();
#endif
                }

                //"xmlns-uris"
                try
                {
                    reader.SetFeature(Constants.XmlNsUrisFeature, true);
                }
                catch (Exception e)
                {
#if DEBUG
                    Debugger.Break();
#endif
                }

                //"resolve-dtd-uris"
                try
                {
                    reader.SetFeature(Constants.ResolveDtdUrisFeature, true);
                }
                catch (Exception e)
                {
#if DEBUG
                    Debugger.Break();
#endif
                }
            }


            if (reader is ExpatReader)
            {
                // http://xml.org/sax/features/namespaces
                try
                {
                    reader.SetFeature(Constants.NamespacesFeature, true);
                }
                catch (Exception e)
                {
#if DEBUG
                    Debugger.Break();
#endif
                }

                // http://xml.org/sax/features/external-general-entities
                try
                {
                    reader.SetFeature(Constants.ExternalGeneralFeature, true);
                }
                catch (Exception e)
                {
#if DEBUG
                    Debugger.Break();
#endif
                }

                // http://xml.org/sax/features/external-parameter-entities
                try
                {
                    reader.SetFeature(Constants.ExternalParameterFeature, true);
                }
                catch (Exception e)
                {
#if DEBUG
                    Debugger.Break();
#endif
                }

                // http://xml.org/sax/features/resolve-dtd-uris
                try
                {
                    reader.SetFeature(Constants.ResolveDtdUrisFeature, true);
                }
                catch (Exception e)
                {
#if DEBUG
                    Debugger.Break();
#endif
                }

                // http://xml.org/sax/features/lexical-handler/parameter-entities
                try
                {
                    reader.SetFeature(Constants.LexicalParameterFeature, true);
                }
                catch (Exception e)
                {
#if DEBUG
                    Debugger.Break();
#endif
                }

                if (false)
                {
                    try
                    {
                        reader.SetFeature("http://kd-soft.net/sax/features/skip-internal-entities",
                                          false);
                    }
                    catch (Exception e)
                    {
#if DEBUG
                        Debugger.Break();
#endif
                    }

                    try
                    {
                        reader.SetFeature(
                            "http://kd-soft.net/sax/features/parse-unless-standalone", true);
                    }
                    catch (Exception e)
                    {
#if DEBUG
                        Debugger.Break();
#endif
                    }

                    try
                    {
                        reader.SetFeature("http://kd-soft.net/sax/features/parameter-entities", true);
                    }
                    catch (Exception e)
                    {
#if DEBUG
                        Debugger.Break();
#endif
                    }

                    try
                    {
                        reader.SetFeature("http://kd-soft.net/sax/features/standalone-error", true);
                    }
                    catch (Exception e)
                    {
#if DEBUG
                        Debugger.Break();
#endif
                    }
                }

                // SUPPORTED, but then NOT SUPPORTED (deeper inside Expat C# wrapper code)

                //                                    // http://xml.org/sax/features/namespace-prefixes
                //                                    try
                //                                    {
                //                                        reader.SetFeature(Constants.NamespacePrefixesFeature, true);
                //                                    }
                //                                    catch (Exception e)
                //                                    {
                //#if DEBUG
                //                                        Debugger.Break();
                //#endif
                //                                    }

                //                                    // http://xml.org/sax/features/xmlns-uris
                //                                    try
                //                                    {
                //                                        reader.SetFeature(Constants.XmlNsUrisFeature, true);
                //                                    }
                //                                    catch (Exception e)
                //                                    {
                //#if DEBUG
                //                                        Debugger.Break();
                //#endif
                //                                    }
                //                                    // http://xml.org/sax/features/validation
                //                                    try
                //                                    {
                //                                        reader.SetFeature(Constants.ValidationFeature, true);
                //                                    }
                //                                    catch (Exception e)
                //                                    {
                //#if DEBUG
                //                                        Debugger.Break();
                //#endif
                //                                    }

                //                                    // http://xml.org/sax/features/unicode-normalization-checking
                //                                    try
                //                                    {
                //                                        reader.SetFeature(Constants.UnicodeNormCheckFeature, true);
                //                                    }
                //                                    catch (Exception e)
                //                                    {
                //#if DEBUG
                //                                        Debugger.Break();
                //#endif
                //                                    }


                // NOT SUPPORTED:


                // http://xml.org/sax/features/xml-1.1
                //                                    try
                //                                    {
                //                                        reader.SetFeature(Constants.Xml11Feature, true);
                //                                    }
                //                                    catch (Exception e)
                //                                    {
                //#if DEBUG
                //                                        Debugger.Break();
                //#endif
                //                                    }

                // http://xml.org/sax/features/xml-declaration
                //                                    try
                //                                    {
                //                                        reader.SetFeature(Constants.XmlDeclFeature, true);
                //                                    }
                //                                    catch (Exception e)
                //                                    {
                //#if DEBUG
                //                                        Debugger.Break();
                //#endif
                //                                    }

                // http://xml.org/sax/features/use-external-subset
                //                                    try
                //                                    {
                //                                        reader.SetFeature(Constants.UseExternalSubsetFeature, true);
                //                                    }
                //                                    catch (Exception e)
                //                                    {
                //#if DEBUG
                //                                        Debugger.Break();
                //#endif
                //                                    }

                // http://xml.org/sax/features/reader-control
                //                                    try
                //                                    {
                //                                        reader.SetFeature(Constants.ReaderControlFeature, true);
                //                                    }
                //                                    catch (Exception e)
                //                                    {
                //#if DEBUG
                //                                        Debugger.Break();
                //#endif
                //                                    }
            }

            SaxContentHandler handler = new SaxContentHandler(list);

            try
            {
                reader.DtdHandler = handler;
            }
            catch (Exception e)
            {
#if DEBUG
                Debugger.Break();
#endif
                errorHandler.AddMessage("Cannot set dtd handler: " + e.Message);
            }

            try
            {
                reader.ContentHandler = handler;
            }
            catch (Exception e)
            {
#if DEBUG
                Debugger.Break();
#endif
                errorHandler.AddMessage("Cannot set content handler: " + e.Message);
            }

            try
            {
                reader.LexicalHandler = handler;
            }
            catch (Exception e)
            {
#if DEBUG
                Debugger.Break();
#endif
                errorHandler.AddMessage("Cannot set lexical handler: " + e.Message);
            }

            try
            {
                reader.DeclHandler = handler;
            }
            catch (Exception e)
            {
#if DEBUG
                Debugger.Break();
#endif
                errorHandler.AddMessage("Cannot set declaration handler: " + e.Message);
            }

            string rootElementName = isHTML ? @"html" : @"dtbook";
            string dtdWrapper      = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE " + rootElementName + " SYSTEM \"" + dtdID + "\"><" + rootElementName + "></" + rootElementName + ">";
            //StringReader strReader = new StringReader(dtdWrapper);
            Stream      stream    = new MemoryStream(Encoding.UTF8.GetBytes(dtdWrapper));
            TextReader  txtReader = new StreamReader(stream, Encoding.UTF8);
            InputSource input     = new InputSource <TextReader>(txtReader, dtdID + "/////SYSID");
            input.Encoding = "UTF-8";
            input.PublicId = "??";

            reader.Parse(input);
#endif //ENABLE_DTDSHARP


            return(docMarkupType);
        }