Exemple #1
0
        public static async Task <Epub3NavDocument> ReadEpub3NavDocumentAsync(ZipArchive epubArchive, string contentDirectoryPath, EpubPackage package)
        {
            Epub3NavDocument result          = new Epub3NavDocument();
            EpubManifestItem navManifestItem =
                package.Manifest.FirstOrDefault(item => item.Properties != null && item.Properties.Contains(ManifestProperty.NAV));

            if (navManifestItem == null)
            {
                if (package.EpubVersion == EpubVersion.EPUB_2)
                {
                    return(null);
                }
                else
                {
                    throw new Exception("EPUB parsing error: NAV item not found in EPUB manifest.");
                }
            }
            string          navFileEntryPath = ZipPathUtils.Combine(contentDirectoryPath, navManifestItem.Href);
            ZipArchiveEntry navFileEntry     = epubArchive.GetEntry(navFileEntryPath);

            if (navFileEntry == null)
            {
                throw new Exception($"EPUB parsing error: navigation file {navFileEntryPath} not found in archive.");
            }
            if (navFileEntry.Length > Int32.MaxValue)
            {
                throw new Exception($"EPUB parsing error: navigation file {navFileEntryPath} is larger than 2 Gb.");
            }
            XDocument navDocument;

            using (Stream containerStream = navFileEntry.Open())
            {
                navDocument = await XmlUtils.LoadDocumentAsync(containerStream).ConfigureAwait(false);
            }
            XNamespace xhtmlNamespace = navDocument.Root.Name.Namespace;
            XElement   htmlNode       = navDocument.Element(xhtmlNamespace + "html");

            if (htmlNode == null)
            {
                throw new Exception("EPUB parsing error: navigation file does not contain html element.");
            }
            XElement bodyNode = htmlNode.Element(xhtmlNamespace + "body");

            if (bodyNode == null)
            {
                throw new Exception("EPUB parsing error: navigation file does not contain body element.");
            }

            result.Navs = new List <Epub3Nav>();
            string folder = ZipPathUtils.GetDirectoryPath(navManifestItem.Href);

            foreach (XElement navNode in bodyNode.Elements(xhtmlNamespace + "nav"))
            {
                Epub3Nav epub3Nav = ReadEpub3Nav(navNode);
                AdjustRelativePath(epub3Nav.Ol, folder);
                result.Navs.Add(epub3Nav);
            }
            return(result);
        }
Exemple #2
0
        public static async Task <EpubSchema> ReadSchemaAsync(ZipArchive epubArchive)
        {
            var result       = new EpubSchema();
            var rootFilePath = await RootFilePathReader.GetRootFilePathAsync(epubArchive).ConfigureAwait(false);

            var contentDirectoryPath = ZipPathUtils.GetDirectoryPath(rootFilePath);

            result.ContentDirectoryPath = contentDirectoryPath;
            EpubPackage package = await PackageReader.ReadPackageAsync(epubArchive, rootFilePath).ConfigureAwait(false);

            result.Package = package;
            return(result);
        }
Exemple #3
0
        public static async Task <EpubSchema> ReadSchemaAsync(ZipArchive epubArchive)
        {
            EpubSchema result       = new EpubSchema();
            string     rootFilePath = await RootFilePathReader.GetRootFilePathAsync(epubArchive).ConfigureAwait(false);

            string contentDirectoryPath = ZipPathUtils.GetDirectoryPath(rootFilePath);

            result.ContentDirectoryPath = contentDirectoryPath;
            EpubPackage package = await PackageReader.ReadPackageAsync(epubArchive, rootFilePath).ConfigureAwait(false);

            result.Package = package;
            EpubNavigation navigation = await NavigationReader.ReadNavigationAsync(epubArchive, contentDirectoryPath, package).ConfigureAwait(false);

            result.Navigation = navigation;
            return(result);
        }
        public static async Task <EpubSchema> ReadSchemaAsync(ZipArchive epubArchive)
        {
            EpubSchema result       = new EpubSchema();
            string     rootFilePath = await RootFilePathReader.GetRootFilePathAsync(epubArchive).ConfigureAwait(false);

            string contentDirectoryPath = ZipPathUtils.GetDirectoryPath(rootFilePath);

            result.ContentDirectoryPath = contentDirectoryPath;
            EpubPackage package = await PackageReader.ReadPackageAsync(epubArchive, rootFilePath).ConfigureAwait(false);

            result.Package  = package;
            result.Epub2Ncx = await Epub2NcxReader.ReadEpub2NcxAsync(epubArchive, contentDirectoryPath, package).ConfigureAwait(false);

            result.Epub3NavDocument = await Epub3NavDocumentReader.ReadEpub3NavDocumentAsync(epubArchive, contentDirectoryPath, package).ConfigureAwait(false);

            return(result);
        }
        public static async Task <EpubNavigation> ReadNavigationAsync(ZipArchive epubArchive, string contentDirectoryPath, EpubPackage package)
        {
            EpubNavigation result = new EpubNavigation();
            string         tocId  = package.Spine.Toc;

            if (String.IsNullOrEmpty(tocId))
            {
                throw new Exception("EPUB parsing error: TOC ID is empty.");
            }
            EpubManifestItem tocManifestItem = package.Manifest.FirstOrDefault(item => String.Compare(item.Id, tocId, StringComparison.OrdinalIgnoreCase) == 0);

            if (tocManifestItem == null)
            {
                throw new Exception(String.Format("EPUB parsing error: TOC item {0} not found in EPUB manifest.", tocId));
            }
            string          tocFileEntryPath = ZipPathUtils.Combine(contentDirectoryPath, tocManifestItem.Href);
            ZipArchiveEntry tocFileEntry     = epubArchive.GetEntry(tocFileEntryPath);

            if (tocFileEntry == null)
            {
                throw new Exception(String.Format("EPUB parsing error: TOC file {0} not found in archive.", tocFileEntryPath));
            }
            if (tocFileEntry.Length > Int32.MaxValue)
            {
                throw new Exception(String.Format("EPUB parsing error: TOC file {0} is larger than 2 Gb.", tocFileEntryPath));
            }
            XDocument containerDocument;

            using (Stream containerStream = tocFileEntry.Open())
            {
                containerDocument = await XmlUtils.LoadDocumentAsync(containerStream).ConfigureAwait(false);
            }
            XNamespace ncxNamespace = "http://www.daisy.org/z3986/2005/ncx/";
            XElement   ncxNode      = containerDocument.Element(ncxNamespace + "ncx");

            if (ncxNode == null)
            {
                throw new Exception("EPUB parsing error: TOC file does not contain ncx element.");
            }
            XElement headNode = ncxNode.Element(ncxNamespace + "head");

            if (headNode == null)
            {
                throw new Exception("EPUB parsing error: TOC file does not contain head element.");
            }
            EpubNavigationHead navigationHead = ReadNavigationHead(headNode);

            result.Head = navigationHead;
            XElement docTitleNode = ncxNode.Element(ncxNamespace + "docTitle");

            if (docTitleNode == null)
            {
                throw new Exception("EPUB parsing error: TOC file does not contain docTitle element.");
            }
            EpubNavigationDocTitle navigationDocTitle = ReadNavigationDocTitle(docTitleNode);

            result.DocTitle   = navigationDocTitle;
            result.DocAuthors = new List <EpubNavigationDocAuthor>();
            foreach (XElement docAuthorNode in ncxNode.Elements(ncxNamespace + "docAuthor"))
            {
                EpubNavigationDocAuthor navigationDocAuthor = ReadNavigationDocAuthor(docAuthorNode);
                result.DocAuthors.Add(navigationDocAuthor);
            }
            XElement navMapNode = ncxNode.Element(ncxNamespace + "navMap");

            if (navMapNode == null)
            {
                throw new Exception("EPUB parsing error: TOC file does not contain navMap element.");
            }
            EpubNavigationMap navMap = ReadNavigationMap(navMapNode);

            result.NavMap = navMap;
            XElement pageListNode = ncxNode.Element(ncxNamespace + "pageList");

            if (pageListNode != null)
            {
                EpubNavigationPageList pageList = ReadNavigationPageList(pageListNode);
                result.PageList = pageList;
            }
            result.NavLists = new List <EpubNavigationList>();
            foreach (XElement navigationListNode in ncxNode.Elements(ncxNamespace + "navList"))
            {
                EpubNavigationList navigationList = ReadNavigationList(navigationListNode);
                result.NavLists.Add(navigationList);
            }
            return(result);
        }