Beispiel #1
0
        public DTD UseDtd(string dtdIdentifier)
        {
            string dtdCache = dtdIdentifier + ".cache";

            //check to see if we have a cached version of this file
#if USE_ISOLATED_STORAGE
            Stream stream = null;
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                string[] filenames = store.GetFileNames(dtdCache);
                if (filenames.Length > 0)
                {
                    stream = new IsolatedStorageFileStream(dtdCache, FileMode.Open, FileAccess.Read, FileShare.None, store);
                }
            }
            if (stream != null)
            {
                try
                {
                    m_DtdRegex.ReadFromCache(new StreamReader(stream, Encoding.UTF8));
                }
                finally
                {
                    stream.Close();
                }
            }

            // NOTE: we could actually use the same code as below, which gives more control over the subdirectory and doesn't have any size limits:
#else
            string dirpath = Path.Combine(ExternalFilesDataManager.STORAGE_FOLDER_PATH, m_DtdStoreDirName);
            //if (!Directory.Exists(dirpath))
            //{
            //    Directory.CreateDirectory(dirpath);
            //}

            string path = Path.Combine(dirpath, dtdCache);
            if (File.Exists(path))
            {
                Stream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
                try
                {
                    m_DtdRegex.ReadFromCache(new StreamReader(stream, Encoding.UTF8));
                }
                finally
                {
                    stream.Close();
                }

                return(null);
            }
#endif //USE_ISOLATED_STORAGE
            else
            {
                //else read the .dtd file
                Stream dtdStream = DTDs.DTDs.Fetch(dtdIdentifier);
                if (dtdStream == null)
                {
                    //DebugFix.Assert(false);
#if DEBUG
                    Debugger.Break();
#endif // DEBUG
                    MissingDtdValidationError error = new MissingDtdValidationError()
                    {
                        DtdIdentifier = dtdIdentifier
                    };
                    addValidationItem(error);
                    return(null);
                }

                // NOTE: the Stream is automatically closed by the parser, see Scanner.ReadNextChar()
                DTDParser parser = new DTDParser(new StreamReader(dtdStream, Encoding.UTF8));
                DTD       dtd    = parser.Parse(true);

                m_DtdRegex.ParseDtdIntoHashtable(dtd);

                return(dtd);
            }
        }
Beispiel #2
0
        public override bool Validate()
        {
            if (m_Session.IsXukSpine)
            {
                return(true);
            }

            //#if DEBUG
            //            m_DtdRegex.Reset();
            //#endif // DEBUG

            if (!Settings.Default.EnableMarkupValidation)
            {
                resetToValid();
                return(IsValid);
            }

            String dtdIdentifier = DTDs.DTDs.DTBOOK_2005_3_MATHML;

            if (m_Session.DocumentProject != null && "body".Equals(m_Session.DocumentProject.Presentations.Get(0).RootNode.GetXmlElementLocalName(),
                                                                   StringComparison.OrdinalIgnoreCase))
            {
                dtdIdentifier = DTDs.DTDs.HTML5;

                //// TODO: a DTD does not exist per-say for HTML5 (as this is not an SGML-based markup language),
                //// and Tobi's own HTML5 DTD hack does not cut it...just way too many false positives,
                //// as well as incorrect handling of "transparent" elements that can have dual block inline content models.
                //// So, the document is always considered valid.
                resetToValid();
                return(IsValid);
            }

            bool dtdHasChanged = String.IsNullOrEmpty(m_DtdIdentifier) || m_DtdIdentifier != dtdIdentifier;

            m_DtdIdentifier = dtdIdentifier;

            if (dtdHasChanged || m_DtdRegex.DtdRegexTable == null || m_DtdRegex.DtdRegexTable.Count == 0)
            {
                loadDTD(m_DtdIdentifier);

                string dtdCache = m_DtdIdentifier + ".cache";
                string dirpath  = Path.Combine(ExternalFilesDataManager.STORAGE_FOLDER_PATH, m_DtdStoreDirName);
                string path     = Path.Combine(dirpath, dtdCache);

                //cache the dtd and save it as dtdIdenfier + ".cache"
#if USE_ISOLATED_STORAGE
                using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    stream = new IsolatedStorageFileStream(dtdCache, FileMode.Create, FileAccess.Write, FileShare.None, store);
                }

                // NOTE: we could actually use the same code as below, which gives more control over the subdirectory and doesn't have any size limits:
#else
                if (!Directory.Exists(dirpath))
                {
                    FileDataProvider.CreateDirectory(dirpath);
                }

                Stream stream = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None);
#endif //USE_ISOLATED_STORAGE
                var writer = new StreamWriter(stream);
                try
                {
                    m_DtdRegex.WriteToCache(writer);
                }
                finally
                {
                    writer.Flush();
                    writer.Close();
                }
            }

            if (m_Session.DocumentProject != null)
            {
                resetToValid();

                if (m_DtdRegex == null || m_DtdRegex.DtdRegexTable == null || m_DtdRegex.DtdRegexTable.Count == 0)
                {
                    MissingDtdValidationError error = new MissingDtdValidationError()
                    {
                        DtdIdentifier = m_DtdIdentifier
                    };
                    addValidationItem(error);
                }
                else
                {
                    var strBuilder = new StringBuilder();
                    ValidateNode(strBuilder, m_Session.DocumentProject.Presentations.Get(0).RootNode);
                }
            }
            return(IsValid);
        }