Exemple #1
0
 private static EpubGuide ReadGuide(XmlNode guideNode)
 {
     EpubGuide result = new EpubGuide();
     foreach (XmlNode guideReferenceNode in guideNode.ChildNodes)
         if (String.Compare(guideReferenceNode.LocalName, "reference", StringComparison.OrdinalIgnoreCase) == 0)
         {
             EpubGuideReference guideReference = new EpubGuideReference();
             foreach (XmlAttribute guideReferenceNodeAttribute in guideReferenceNode.Attributes)
             {
                 string attributeValue = guideReferenceNodeAttribute.Value;
                 switch (guideReferenceNodeAttribute.Name.ToLowerInvariant())
                 {
                     case "type":
                         guideReference.Type = attributeValue;
                         break;
                     case "title":
                         guideReference.Title = attributeValue;
                         break;
                     case "href":
                         guideReference.Href = attributeValue;
                         break;
                 }
             }
             if (String.IsNullOrWhiteSpace(guideReference.Type))
                 throw new Exception("Incorrect EPUB guide: item type is missing");
             if (String.IsNullOrWhiteSpace(guideReference.Href))
                 throw new Exception("Incorrect EPUB guide: item href is missing");
             result.Add(guideReference);
         }
     return result;
 }
        private static async Task<EpubGuide> ReadGuideAsync(XmlReader reader)
        {
            EpubGuide result = new EpubGuide();

            while (await reader.ReadAsync() && !(reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "guide"))
            {
                if (reader.LocalName.ToLowerInvariant() == "reference")
                {
                    EpubGuideReference guideReference = new EpubGuideReference();
                    while (reader.MoveToNextAttribute())
                    {
                        switch (reader.LocalName.ToLowerInvariant())
                        {
                            case "type":
                                guideReference.Type = reader.Value;
                                break;
                            case "title":
                                guideReference.Title = reader.Value;
                                break;
                            case "href":
                                guideReference.Href = reader.Value;
                                break;
                        }
                    }
                    if (String.IsNullOrWhiteSpace(guideReference.Type))
                        throw new Exception("Incorrect EPUB guide: item type is missing");
                    if (String.IsNullOrWhiteSpace(guideReference.Href))
                        throw new Exception("Incorrect EPUB guide: item href is missing");
                    result.Add(guideReference);
                }
            }
            return result;
        }
Exemple #3
0
        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);
        }