コード例 #1
0
        private static EpubManifest ReadManifest(XmlNode manifestNode)
        {
            EpubManifest result = new EpubManifest();

            foreach (XmlNode manifestItemNode in manifestNode.ChildNodes)
            {
                if (String.Compare(manifestItemNode.LocalName, "item", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    EpubManifestItem manifestItem = new EpubManifestItem();
                    foreach (XmlAttribute manifestItemNodeAttribute in manifestItemNode.Attributes)
                    {
                        string attributeValue = manifestItemNodeAttribute.Value;
                        switch (manifestItemNodeAttribute.Name.ToLowerInvariant())
                        {
                        case "id":
                            manifestItem.Id = attributeValue;
                            break;

                        case "href":
                            manifestItem.Href = attributeValue;
                            break;

                        case "media-type":
                            manifestItem.MediaType = attributeValue;
                            break;

                        case "required-namespace":
                            manifestItem.RequiredNamespace = attributeValue;
                            break;

                        case "required-modules":
                            manifestItem.RequiredModules = attributeValue;
                            break;

                        case "fallback":
                            manifestItem.Fallback = attributeValue;
                            break;

                        case "fallback-style":
                            manifestItem.FallbackStyle = attributeValue;
                            break;
                        }
                    }
                    if (String.IsNullOrWhiteSpace(manifestItem.Id))
                    {
                        throw new Exception("Incorrect EPUB manifest: item ID is missing");
                    }
                    if (String.IsNullOrWhiteSpace(manifestItem.Href))
                    {
                        throw new Exception("Incorrect EPUB manifest: item href is missing");
                    }
                    if (String.IsNullOrWhiteSpace(manifestItem.MediaType))
                    {
                        throw new Exception("Incorrect EPUB manifest: item media type is missing");
                    }
                    result.Add(manifestItem);
                }
            }
            return(result);
        }
コード例 #2
0
        private static async System.Threading.Tasks.Task<EpubManifest> ReadManifestAsync(XmlReader reader)
        {
            EpubManifest result = new EpubManifest();
            
            bool isManifestFound = await reader.ReadToFollowingAsync("manifest", "http://www.idpf.org/2007/opf");
            if (!isManifestFound)
                throw new Exception("EPUB parsing error: manifest declarations not found in the package.");

            while (await reader.ReadAsync() && !(reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "manifest"))
            {
                if (!String.IsNullOrWhiteSpace(reader.LocalName))
                {
                    EpubManifestItem manifestItem = new EpubManifestItem();
                    switch (reader.LocalName.ToLowerInvariant())
                    {
                        case "item":
                            while (reader.MoveToNextAttribute())
                            {
                                switch (reader.LocalName.ToLowerInvariant())
                                {
                                    case "id":
                                        manifestItem.Id = reader.Value;
                                        break;
                                    case "href":
                                        manifestItem.Href = reader.Value;
                                        break;
                                    case "media-type":
                                        manifestItem.MediaType = reader.Value;
                                        break;
                                    case "required-namespace":
                                        manifestItem.RequiredNamespace = reader.Value;
                                        break;
                                    case "required-modules":
                                        manifestItem.RequiredModules = reader.Value;
                                        break;
                                    case "fallback":
                                        manifestItem.Fallback = reader.Value;
                                        break;
                                    case "fallback-style":
                                        manifestItem.FallbackStyle = reader.Value;
                                        break;
                                }
                            }
                            break;
                    }
                    if (String.IsNullOrWhiteSpace(manifestItem.Id))
                        throw new Exception("Incorrect EPUB manifest: item ID is missing");
                    if (String.IsNullOrWhiteSpace(manifestItem.Href))
                        throw new Exception("Incorrect EPUB manifest: item href is missing");
                    if (String.IsNullOrWhiteSpace(manifestItem.MediaType))
                        throw new Exception("Incorrect EPUB manifest: item media type is missing");
                    result.Add(manifestItem);
                }
            }
            return result;
        }
コード例 #3
0
        public static EpubPackage ReadPackage(ZipArchive epubArchive, string rootFilePath)
        {
            ZipArchiveEntry rootFileEntry = epubArchive.GetEntry(rootFilePath);

            if (rootFileEntry == null)
            {
                throw new Exception("EPUB parsing error: root file not found in archive.");
            }
            XmlDocument containerDocument;

            using (Stream containerStream = rootFileEntry.Open())
                containerDocument = XmlUtils.LoadDocument(containerStream);
            XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(containerDocument.NameTable);

            xmlNamespaceManager.AddNamespace("opf", "http://www.idpf.org/2007/opf");
            XmlNode     packageNode      = containerDocument.DocumentElement.SelectSingleNode("/opf:package", xmlNamespaceManager);
            EpubPackage result           = new EpubPackage();
            string      epubVersionValue = packageNode.Attributes["version"].Value;

            if (epubVersionValue == "2.0")
            {
                result.EpubVersion = EpubVersion.EPUB_2;
            }
            else
            if (epubVersionValue == "3.0")
            {
                result.EpubVersion = EpubVersion.EPUB_3;
            }
            else
            {
                throw new Exception(String.Format("Unsupported EPUB version: {0}.", epubVersionValue));
            }
            XmlNode metadataNode = packageNode.SelectSingleNode("opf:metadata", xmlNamespaceManager);

            if (metadataNode == null)
            {
                throw new Exception("EPUB parsing error: metadata not found in the package.");
            }
            EpubMetadata metadata = ReadMetadata(metadataNode, result.EpubVersion);

            result.Metadata = metadata;
            XmlNode manifestNode = packageNode.SelectSingleNode("opf:manifest", xmlNamespaceManager);

            if (manifestNode == null)
            {
                throw new Exception("EPUB parsing error: manifest not found in the package.");
            }
            EpubManifest manifest = ReadManifest(manifestNode);

            result.Manifest = manifest;
            XmlNode spineNode = packageNode.SelectSingleNode("opf:spine", xmlNamespaceManager);

            if (spineNode == null)
            {
                throw new Exception("EPUB parsing error: spine not found in the package.");
            }
            EpubSpine spine = ReadSpine(spineNode);

            result.Spine = spine;
            XmlNode guideNode = packageNode.SelectSingleNode("opf:guide", xmlNamespaceManager);

            if (guideNode != null)
            {
                EpubGuide guide = ReadGuide(guideNode);
                result.Guide = guide;
            }
            return(result);
        }
コード例 #4
0
ファイル: PackageReader.cs プロジェクト: gareiz/EpubReader
 private static EpubManifest ReadManifest(XmlNode manifestNode)
 {
     EpubManifest result = new EpubManifest();
     foreach (XmlNode manifestItemNode in manifestNode.ChildNodes)
         if (String.Compare(manifestItemNode.LocalName, "item", StringComparison.OrdinalIgnoreCase) == 0)
         {
             EpubManifestItem manifestItem = new EpubManifestItem();
             foreach (XmlAttribute manifestItemNodeAttribute in manifestItemNode.Attributes)
             {
                 string attributeValue = manifestItemNodeAttribute.Value;
                 switch (manifestItemNodeAttribute.Name.ToLowerInvariant())
                 {
                     case "id":
                         manifestItem.Id = attributeValue;
                         break;
                     case "href":
                         manifestItem.Href = attributeValue;
                         break;
                     case "media-type":
                         manifestItem.MediaType = attributeValue;
                         break;
                     case "required-namespace":
                         manifestItem.RequiredNamespace = attributeValue;
                         break;
                     case "required-modules":
                         manifestItem.RequiredModules = attributeValue;
                         break;
                     case "fallback":
                         manifestItem.Fallback = attributeValue;
                         break;
                     case "fallback-style":
                         manifestItem.FallbackStyle = attributeValue;
                         break;
                 }
             }
             if (String.IsNullOrWhiteSpace(manifestItem.Id))
                 throw new Exception("Incorrect EPUB manifest: item ID is missing");
             if (String.IsNullOrWhiteSpace(manifestItem.Href))
                 throw new Exception("Incorrect EPUB manifest: item href is missing");
             if (String.IsNullOrWhiteSpace(manifestItem.MediaType))
                 throw new Exception("Incorrect EPUB manifest: item media type is missing");
             result.Add(manifestItem);
         }
     return result;
 }
コード例 #5
0
ファイル: PackageReader.cs プロジェクト: Didstopia/EpubReader
        public static async Task <EpubPackage> ReadPackageAsync(ZipArchive epubArchive, string rootFilePath)
        {
            ZipArchiveEntry rootFileEntry = epubArchive.GetEntry(rootFilePath);

            if (rootFileEntry == null)
            {
                throw new Exception("EPUB parsing error: root file not found in archive.");
            }
            XDocument containerDocument;

            using (Stream containerStream = rootFileEntry.Open())
                containerDocument = await XmlUtils.LoadDocumentAsync(containerStream).ConfigureAwait(false);
            XNamespace  opfNamespace     = "http://www.idpf.org/2007/opf";
            XElement    packageNode      = containerDocument.Element(opfNamespace + "package");
            EpubPackage result           = new EpubPackage();
            string      epubVersionValue = packageNode.Attribute("version").Value;

            if (epubVersionValue == "2.0")
            {
                result.EpubVersion = EpubVersion.EPUB_2;
            }
            else
            if (epubVersionValue == "3.0")
            {
                result.EpubVersion = EpubVersion.EPUB_3;
            }
            else
            {
                throw new Exception(String.Format("Unsupported EPUB version: {0}.", epubVersionValue));
            }
            XElement metadataNode = packageNode.Element(opfNamespace + "metadata");

            if (metadataNode == null)
            {
                throw new Exception("EPUB parsing error: metadata not found in the package.");
            }
            EpubMetadata metadata = ReadMetadata(metadataNode, result.EpubVersion);

            result.Metadata = metadata;
            XElement manifestNode = packageNode.Element(opfNamespace + "manifest");

            if (manifestNode == null)
            {
                throw new Exception("EPUB parsing error: manifest not found in the package.");
            }
            EpubManifest manifest = ReadManifest(manifestNode);

            result.Manifest = manifest;
            XElement spineNode = packageNode.Element(opfNamespace + "spine");

            if (spineNode == null)
            {
                throw new Exception("EPUB parsing error: spine not found in the package.");
            }
            EpubSpine spine = ReadSpine(spineNode);

            result.Spine = spine;
            XElement guideNode = packageNode.Element(opfNamespace + "guide");

            if (guideNode != null)
            {
                EpubGuide guide = ReadGuide(guideNode);
                result.Guide = guide;
            }
            return(result);
        }
コード例 #6
0
        private static EpubManifest ReadManifest(XElement manifestNode)
        {
            EpubManifest result = new EpubManifest();

            foreach (XElement manifestItemNode in manifestNode.Elements())
            {
                if (manifestItemNode.CompareNameTo("item"))
                {
                    EpubManifestItem manifestItem = new EpubManifestItem();
                    foreach (XAttribute manifestItemNodeAttribute in manifestItemNode.Attributes())
                    {
                        string attributeValue = manifestItemNodeAttribute.Value;
                        switch (manifestItemNodeAttribute.GetLowerCaseLocalName())
                        {
                        case "id":
                            manifestItem.Id = attributeValue;
                            break;

                        case "href":
                            manifestItem.Href = Uri.UnescapeDataString(attributeValue);
                            break;

                        case "media-type":
                            manifestItem.MediaType = attributeValue;
                            break;

                        case "required-namespace":
                            manifestItem.RequiredNamespace = attributeValue;
                            break;

                        case "required-modules":
                            manifestItem.RequiredModules = attributeValue;
                            break;

                        case "fallback":
                            manifestItem.Fallback = attributeValue;
                            break;

                        case "fallback-style":
                            manifestItem.FallbackStyle = attributeValue;
                            break;

                        case "properties":
                            manifestItem.Properties = ReadManifestProperties(attributeValue);
                            break;
                        }
                    }
                    if (String.IsNullOrWhiteSpace(manifestItem.Id))
                    {
                        throw new Exception("Incorrect EPUB manifest: item ID is missing");
                    }
                    if (String.IsNullOrWhiteSpace(manifestItem.Href))
                    {
                        throw new Exception("Incorrect EPUB manifest: item href is missing");
                    }
                    if (String.IsNullOrWhiteSpace(manifestItem.MediaType))
                    {
                        throw new Exception("Incorrect EPUB manifest: item media type is missing");
                    }
                    result.Add(manifestItem);
                }
            }
            return(result);
        }
コード例 #7
0
        private static async System.Threading.Tasks.Task <EpubManifest> ReadManifestAsync(XmlReader reader)
        {
            EpubManifest result = new EpubManifest();

            bool isManifestFound = await reader.ReadToFollowingAsync("manifest", "http://www.idpf.org/2007/opf");

            if (!isManifestFound)
            {
                throw new Exception("EPUB parsing error: manifest declarations not found in the package.");
            }

            while (await reader.ReadAsync() && !(reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "manifest"))
            {
                if (!String.IsNullOrWhiteSpace(reader.LocalName))
                {
                    EpubManifestItem manifestItem = new EpubManifestItem();
                    switch (reader.LocalName.ToLowerInvariant())
                    {
                    case "item":
                        while (reader.MoveToNextAttribute())
                        {
                            switch (reader.LocalName.ToLowerInvariant())
                            {
                            case "id":
                                manifestItem.Id = reader.Value;
                                break;

                            case "href":
                                manifestItem.Href = reader.Value;
                                break;

                            case "media-type":
                                manifestItem.MediaType = reader.Value;
                                break;

                            case "required-namespace":
                                manifestItem.RequiredNamespace = reader.Value;
                                break;

                            case "required-modules":
                                manifestItem.RequiredModules = reader.Value;
                                break;

                            case "fallback":
                                manifestItem.Fallback = reader.Value;
                                break;

                            case "fallback-style":
                                manifestItem.FallbackStyle = reader.Value;
                                break;
                            }
                        }
                        break;
                    }
                    if (String.IsNullOrWhiteSpace(manifestItem.Id))
                    {
                        throw new Exception("Incorrect EPUB manifest: item ID is missing");
                    }
                    if (String.IsNullOrWhiteSpace(manifestItem.Href))
                    {
                        throw new Exception("Incorrect EPUB manifest: item href is missing");
                    }
                    if (String.IsNullOrWhiteSpace(manifestItem.MediaType))
                    {
                        throw new Exception("Incorrect EPUB manifest: item media type is missing");
                    }
                    result.Add(manifestItem);
                }
            }
            return(result);
        }
コード例 #8
0
        //Parsing metadata, manifest, spine and guide
        public static async Task <EpubPackage> ReadPackageAsync(ZipArchive epubArchive, string rootFilePath)
        {
            EpubPackage result = new EpubPackage();

            XmlReaderSettings xmlReaderSettings = new XmlReaderSettings
            {
                // XmlResolver = null,
                Async         = true,
                DtdProcessing = DtdProcessing.Ignore
            };

            ZipArchiveEntry rootFileEntry = epubArchive.GetEntry(rootFilePath);

            if (rootFileEntry == null)
            {
                throw new Exception(string.Format("EPUB parsing error: {0} file not found in archive.", rootFilePath));
            }
            //Starting content.opf parsing...
            using (Stream containerStream = rootFileEntry.Open())
            {
                using (XmlReader xmlReader = XmlReader.Create(containerStream, xmlReaderSettings))
                {
                    await xmlReader.ReadToFollowingAsync("package", "http://www.idpf.org/2007/opf");

                    //Trying to get version attribute from <package version=...
                    //Looks like we only need EPUB version data and we don`t care about unique-identifier
                    //if EPUB version is FUBAR then throwing an exeption
                    xmlReader.MoveToAttribute("version");
                    string epubVersionValue = xmlReader.Value;
                    if (epubVersionValue == "2.0")
                    {
                        result.EpubVersion = EpubVersion.EPUB_2;
                    }
                    else
                    if (epubVersionValue == "3.0")
                    {
                        result.EpubVersion = EpubVersion.EPUB_3;
                    }
                    else
                    {
                        throw new Exception(String.Format("Unsupported EPUB version: {0}.", epubVersionValue));
                    }

                    //Reading metadata
                    EpubMetadata metadata = await ReadMetadataAsync(xmlReader, result.EpubVersion);

                    result.Metadata = metadata;
                    //Reading manifest
                    EpubManifest manifest = await ReadManifestAsync(xmlReader);

                    result.Manifest = manifest;
                    //Reading spine
                    EpubSpine spine = await ReadSpineAsync(xmlReader);

                    result.Spine = spine;
                    //Reading guide. And we actually don`t care if it is no present in our EPUB...
                    bool isGuidePresent = await xmlReader.ReadToFollowingAsync("guide", "http://www.idpf.org/2007/opf");

                    if (isGuidePresent)
                    {
                        EpubGuide guide = await ReadGuideAsync(xmlReader);

                        result.Guide = guide;
                    }
                }
            }

            return(result);
        }