private static async System.Threading.Tasks.Task<EpubMetadata> ReadMetadataAsync(XmlReader reader, EpubVersion epubVersion)
        {
            EpubMetadata result = new EpubMetadata();
            result.Titles = new List<string>();
            result.Creators = new List<EpubMetadataCreator>();
            result.Subjects = new List<string>();
            result.Publishers = new List<string>();
            result.Contributors = new List<EpubMetadataContributor>();
            result.Dates = new List<EpubMetadataDate>();
            result.Types = new List<string>();
            result.Formats = new List<string>();
            result.Identifiers = new List<EpubMetadataIdentifier>();
            result.Sources = new List<string>();
            result.Languages = new List<string>();
            result.Relations = new List<string>();
            result.Coverages = new List<string>();
            result.Rights = new List<string>();
            result.MetaItems = new List<EpubMetadataMeta>();

            //Parsing all metadata insides and saving it in EpubMetadata instance
            //

            //Мне нужно пройтись по всем нодам внутри метадаты последовательно, извлечь ноды указанные в массиве metadataNodesNames...
            //... и сохранить их в структуре EpubMetadata
            //В каждой итерации нам нужно извлечь имя нода, сделать маленькими буквами и,
            // в зависимости от того есть ли он в массиве - выполнить запись в структуру
            //ИЛИ мы можем тупо искать по заданным в массиве именам, с опасностью, что какая-то сука написала капсами и это ебнет весь ридер
            //
            bool isMetadataAvailable = await reader.ReadToFollowingAsync("metadata", "http://www.idpf.org/2007/opf");
            if (!isMetadataAvailable)
                throw new Exception("EPUB parsing error: metadata not found in the package.");

            while (await reader.ReadAsync() && !(reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "metadata"))
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        switch (reader.LocalName.ToLowerInvariant())
                        {
                            case "title":
                                result.Titles.Add(reader.ReadElementContentAsString());
                                break;
                            case "creator":
                                EpubMetadataCreator creator = new EpubMetadataCreator();
                                creator.Role = reader.GetAttribute("opf:role");
                                creator.FileAs = reader.GetAttribute("opf:file-as");
                                creator.Creator = reader.ReadElementContentAsString();
                                result.Creators.Add(creator);
                                break;
                            case "subject":
                                result.Subjects.Add(reader.ReadElementContentAsString());
                                break;
                            case "description":
                                result.Description = reader.ReadElementContentAsString();
                                break;
                            case "publisher":
                                result.Publishers.Add(reader.ReadElementContentAsString());
                                break;
                            case "contributor":
                                EpubMetadataContributor contributor = new EpubMetadataContributor();
                                contributor.Role = reader.GetAttribute("opf:role");
                                contributor.FileAs = reader.GetAttribute("opf:file-as");
                                contributor.Contributor = reader.ReadElementContentAsString();
                                result.Contributors.Add(contributor);
                                break;
                            case "date":
                                EpubMetadataDate date = new EpubMetadataDate();
                                date.Event = reader.GetAttribute("opf:event");
                                date.Date = reader.ReadElementContentAsString();
                                result.Dates.Add(date);
                                break;
                            case "type":
                                result.Types.Add(reader.ReadElementContentAsString());
                                break;
                            case "format":
                                result.Formats.Add(reader.ReadElementContentAsString());
                                break;
                            case "identifier":
                                EpubMetadataIdentifier identifier = new EpubMetadataIdentifier();
                                identifier.Id = reader.GetAttribute("id");
                                identifier.Scheme = reader.GetAttribute("opf:scheme");
                                identifier.Identifier = reader.ReadElementContentAsString();
                                result.Identifiers.Add(identifier);
                                break;
                            case "source":
                                result.Sources.Add(reader.ReadElementContentAsString());
                                break;
                            case "language":
                                result.Languages.Add(reader.ReadElementContentAsString());
                                break;
                            case "relation":
                                result.Relations.Add(reader.ReadElementContentAsString());
                                break;
                            case "coverage":
                                result.Coverages.Add(reader.ReadElementContentAsString());
                                break;
                            case "rights":
                                result.Rights.Add(reader.ReadElementContentAsString());
                                break;
                            //looks like there is an optional refining node "meta" and it is present in EPUB3
                            case "meta":
                                if (epubVersion == EpubVersion.EPUB_2)
                                {
                                    EpubMetadataMeta meta = new EpubMetadataMeta();
                                    meta.Name = reader.GetAttribute("name");
                                    meta.Content = reader.GetAttribute("content");
                                    result.MetaItems.Add(meta);
                                }
                                else
                                    if (epubVersion == EpubVersion.EPUB_3)
                                {
                                    EpubMetadataMeta meta = new EpubMetadataMeta();
                                    meta.Id = reader.GetAttribute("id");
                                    meta.Refines = reader.GetAttribute("refines");
                                    meta.Property = reader.GetAttribute("property");
                                    meta.Scheme = reader.GetAttribute("scheme");
                                    meta.Content = reader.ReadElementContentAsString();
                                    result.MetaItems.Add(meta);
                                }
                                break;
                        }
                        break;
                }
            }

            return result;
        }
Exemple #2
0
 private static EpubMetadata ReadMetadata(XmlNode metadataNode, EpubVersion epubVersion)
 {
     EpubMetadata result = new EpubMetadata();
     result.Titles = new List<string>();
     result.Creators = new List<EpubMetadataCreator>();
     result.Subjects = new List<string>();
     result.Publishers = new List<string>();
     result.Contributors = new List<EpubMetadataContributor>();
     result.Dates = new List<EpubMetadataDate>();
     result.Types = new List<string>();
     result.Formats = new List<string>();
     result.Identifiers = new List<EpubMetadataIdentifier>();
     result.Sources = new List<string>();
     result.Languages = new List<string>();
     result.Relations = new List<string>();
     result.Coverages = new List<string>();
     result.Rights = new List<string>();
     result.MetaItems = new List<EpubMetadataMeta>();
     foreach (XmlNode metadataItemNode in metadataNode.ChildNodes)
     {
         string innerText = metadataItemNode.InnerText;
         switch (metadataItemNode.LocalName.ToLowerInvariant())
         {
             case "title":
                 result.Titles.Add(innerText);
                 break;
             case "creator":
                 EpubMetadataCreator creator = ReadMetadataCreator(metadataItemNode);
                 result.Creators.Add(creator);
                 break;
             case "subject":
                 result.Subjects.Add(innerText);
                 break;
             case "description":
                 result.Description = innerText;
                 break;
             case "publisher":
                 result.Publishers.Add(innerText);
                 break;
             case "contributor":
                 EpubMetadataContributor contributor = ReadMetadataContributor(metadataItemNode);
                 result.Contributors.Add(contributor);
                 break;
             case "date":
                 EpubMetadataDate date = ReadMetadataDate(metadataItemNode);
                 result.Dates.Add(date);
                 break;
             case "type":
                 result.Types.Add(innerText);
                 break;
             case "format":
                 result.Formats.Add(innerText);
                 break;
             case "identifier":
                 EpubMetadataIdentifier identifier = ReadMetadataIdentifier(metadataItemNode);
                 result.Identifiers.Add(identifier);
                 break;
             case "source":
                 result.Sources.Add(innerText);
                 break;
             case "language":
                 result.Languages.Add(innerText);
                 break;
             case "relation":
                 result.Relations.Add(innerText);
                 break;
             case "coverage":
                 result.Coverages.Add(innerText);
                 break;
             case "rights":
                 result.Rights.Add(innerText);
                 break;
             case "meta":
                 if (epubVersion == EpubVersion.EPUB_2)
                 {
                     EpubMetadataMeta meta = ReadMetadataMetaVersion2(metadataItemNode);
                     result.MetaItems.Add(meta);
                 }
                 else
                     if (epubVersion == EpubVersion.EPUB_3)
                     {
                         EpubMetadataMeta meta = ReadMetadataMetaVersion3(metadataItemNode);
                         result.MetaItems.Add(meta);
                     }
                 break;
         }
     }
     return result;
 }