예제 #1
0
파일: Epub.cs 프로젝트: adarmus/ebook
        void LoadTableOfContents()
        {
            ExtendedData extendedData = ExtendedData[_TocFileName] as ExtendedData;

            if (extendedData == null)
            {
                return;
            }

            XElement   xElement   = XElement.Parse(extendedData.Content);
            XNamespace xNamespace = xElement.Attribute("xmlns") != null?xElement.Attribute("xmlns").Value : XNamespace.None;

            //Developer: Brian Kenney
            //Date: 7/29/2012
            //
            //some files have the namespace prefix of ncx
            //if it does then then xNamespace will evaluate to None
            if (xNamespace != XNamespace.None)
            {
                TOC = GetNavigationChildren(xElement.Element(xNamespace + "navMap").Elements(xNamespace + "navPoint"), xNamespace);
            }
            else
            {
                //Change: Brian Kenney
                //Date: 7/29/2012
                //Change: duplicate dictionary key
                //Details: the file may have an ncx namespace prefix
                //romeve the ncx prefix itself
                XDocument xDocument = XDocument.Parse(@extendedData.Content);
                xDocument.Root.Add(new XAttribute("xmlns", "http://www.daisy.org/z3986/2005/ncx/"));
                xDocument.Root.Attributes(XNamespace.Xmlns + "ncx").Remove();
                xElement   = XElement.Parse(xDocument.ToString());
                xNamespace = xElement.Attribute("xmlns") != null?xElement.Attribute("xmlns").Value : XNamespace.None;

                if (xNamespace != XNamespace.None)
                {
                    TOC = GetNavigationChildren(xElement.Element(xNamespace + "navMap").Elements(xNamespace + "navPoint"), xNamespace);
                }
            }
        }
예제 #2
0
파일: Epub.cs 프로젝트: adarmus/ebook
        void LoadManifestSectionFromOpfFile(XElement contentOpf, XNamespace xNamespace)
        {
            //NOTE: with the content.opf file
            //NOTE: grab the idref from the spine element and
            //NOTE: find a match corresponding to the elements listed under the manifest section
            HashSet <string> alreadyProcessedFiles = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase);

            foreach (var spinElement in contentOpf.Elements(xNamespace + "spine").Elements())
            {
                var itemElement = contentOpf.Elements(xNamespace + "manifest").Elements().FirstOrDefault(
                    e =>
                    e.Attribute("id").Value == spinElement.Attribute("idref").Value);

                if (itemElement == null && FailIfEpubNotValid)
                {
                    throw new Exception("Invalid epub file.");
                }
                else if (itemElement == null)
                {
                    continue;
                }

                string   fileName        = HttpUtility.UrlDecode(itemElement.Attribute("href").Value);
                ZipEntry contentZipEntry = _EpubFile.Entries.FirstOrDefault(e => e.FileName.Equals(_ContentOpfPath + fileName, StringComparison.InvariantCultureIgnoreCase));

                if (contentZipEntry == null && FailIfEpubNotValid)
                {
                    throw new Exception("Invalid epub file.");
                }
                else if (contentZipEntry == null)
                {
                    continue;
                }

                //
                //Developer: Brian Kenney
                //Date: 7/29/2012
                //Change: duplicate dictionary key
                //Details: ran into a mis-packaged epub file added key check
                //to ensure that we don't crash should someone mispackage
                //
                //check to see if fileName has already been added to Content dictionary
                if (!Content.Contains(fileName))
                {
                    Content.Add(fileName, new ContentData(fileName, contentZipEntry));
                }

                if (!alreadyProcessedFiles.Contains(spinElement.Attribute("idref").Value))
                {
                    alreadyProcessedFiles.Add(spinElement.Attribute("idref").Value);
                }
            }

            //grab the rest of the elements not already processed in the manifest
            IEnumerable <XElement> manifestElements = contentOpf.Elements(xNamespace + "manifest").Elements().Where(e => !alreadyProcessedFiles.Contains(e.Attribute("id").Value));

            foreach (var manifestElement in manifestElements)
            {
                string   fileName         = manifestElement.Attribute("href").Value;
                ZipEntry extendedZipEntry = _EpubFile.Entries.FirstOrDefault(e => e.FileName.Equals(_ContentOpfPath + fileName, StringComparison.InvariantCultureIgnoreCase));
                if (extendedZipEntry == null)
                {
                    continue;
                }

                //check to see if fileName has already been added to Extended dictionary
                string trimmedFileName = GetTrimmedFileName(fileName, true);

                if (!ExtendedData.Contains(trimmedFileName))
                {
                    ExtendedData.Add(trimmedFileName, new ExtendedData(fileName, manifestElement.Attribute("media-type").Value, extendedZipEntry));
                }

                if (string.Equals(manifestElement.Attribute("media-type").Value, "application/x-dtbncx+xml", StringComparison.InvariantCultureIgnoreCase))
                {
                    _TocFileName = manifestElement.Attribute("href").Value;
                }
            }
        }