Esempio n. 1
0
        private static EpubMetadataContributor ReadMetadataContributor(XmlNode metadataContributorNode)
        {
            EpubMetadataContributor result = new EpubMetadataContributor();

            foreach (XmlAttribute metadataContributorNodeAttribute in metadataContributorNode.Attributes)
            {
                string attributeValue = metadataContributorNodeAttribute.Value;
                switch (metadataContributorNodeAttribute.Name.ToLowerInvariant())
                {
                case "opf:role":
                    result.Role = attributeValue;
                    break;

                case "opf:file-as":
                    result.FileAs = attributeValue;
                    break;
                }
            }
            result.Contributor = metadataContributorNode.InnerText;
            return(result);
        }
Esempio n. 2
0
        private static EpubMetadataContributor ReadMetadataContributor(XElement metadataContributorNode)
        {
            EpubMetadataContributor result = new EpubMetadataContributor();

            foreach (XAttribute metadataContributorNodeAttribute in metadataContributorNode.Attributes())
            {
                string attributeValue = metadataContributorNodeAttribute.Value;
                switch (metadataContributorNodeAttribute.Name.LocalName.ToLowerInvariant())
                {
                case "role":
                    result.Role = attributeValue;
                    break;

                case "file-as":
                    result.FileAs = attributeValue;
                    break;
                }
            }
            result.Contributor = metadataContributorNode.Value;
            return(result);
        }
Esempio n. 3
0
        private static EpubMetadataContributor ReadMetadataContributor(XElement metadataContributorNode)
        {
            var result = new EpubMetadataContributor();

            foreach (var metadataContributorNodeAttribute in metadataContributorNode.Attributes())
            {
                var attributeValue = metadataContributorNodeAttribute.Value;
                switch (metadataContributorNodeAttribute.GetLowerCaseLocalName())
                {
                case "role":
                    result.Role = attributeValue;
                    break;

                case "file-as":
                    result.FileAs = attributeValue;
                    break;
                }
            }

            result.Contributor = metadataContributorNode.Value;
            return(result);
        }
Esempio n. 4
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);
        }
        private static EpubMetadata ReadMetadata(XElement metadataNode, EpubVersion epubVersion)
        {
            EpubMetadata result = new EpubMetadata
            {
                Titles       = new List <string>(),
                Creators     = new List <EpubMetadataCreator>(),
                Subjects     = new List <string>(),
                Publishers   = new List <string>(),
                Contributors = new List <EpubMetadataContributor>(),
                Dates        = new List <EpubMetadataDate>(),
                Types        = new List <string>(),
                Formats      = new List <string>(),
                Identifiers  = new List <EpubMetadataIdentifier>(),
                Sources      = new List <string>(),
                Languages    = new List <string>(),
                Relations    = new List <string>(),
                Coverages    = new List <string>(),
                Rights       = new List <string>(),
                MetaItems    = new List <EpubMetadataMeta>()
            };

            foreach (XElement metadataItemNode in metadataNode.Elements())
            {
                string innerText = metadataItemNode.Value;
                switch (metadataItemNode.GetLowerCaseLocalName())
                {
                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_0 || epubVersion == EpubVersion.EPUB_3_1)
                    {
                        EpubMetadataMeta meta = ReadMetadataMetaVersion3(metadataItemNode);
                        result.MetaItems.Add(meta);
                    }
                    break;
                }
            }
            return(result);
        }
Esempio n. 6
0
        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);
        }