Ejemplo n.º 1
0
        private void AssertNcx(NcxDocument expected, NcxDocument actual)
        {
            Assert.Equal(expected == null, actual == null);
            if (expected != null && actual != null)
            {
                Assert.Equal(expected.DocAuthor, actual.DocAuthor);
                Assert.Equal(expected.DocTitle, actual.DocTitle);

                AssertCollection(expected.Meta, actual.Meta, (a, b) =>
                {
                    Assert.Equal(a.Name, b.Name);
                    Assert.Equal(a.Content, b.Content);
                    Assert.Equal(a.Scheme, b.Scheme);
                });

                Assert.Equal(expected.NavList == null, actual.NavList == null);
                if (expected.NavList != null && actual.NavList != null)
                {
                    Assert.Equal(expected.NavList.Id, actual.NavList.Id);
                    Assert.Equal(expected.NavList.Class, actual.NavList.Class);
                    Assert.Equal(expected.NavList.Label, actual.NavList.Label);

                    AssertCollection(expected.NavList.NavTargets, actual.NavList.NavTargets, (a, b) =>
                    {
                        Assert.Equal(a.Id, b.Id);
                        Assert.Equal(a.Class, b.Class);
                        Assert.Equal(a.Label, b.Label);
                        Assert.Equal(a.PlayOrder, b.PlayOrder);
                        Assert.Equal(a.ContentSource, b.ContentSource);
                    });
                }

                AssertCollection(expected.NavMap.NavPoints, actual.NavMap.NavPoints, (a, b) =>
                {
                    Assert.Equal(a.Id, b.Id);
                    Assert.Equal(a.PlayOrder, b.PlayOrder);
                    Assert.Equal(a.NavLabelText, b.NavLabelText);
                    Assert.Equal(a.Class, b.Class);
                    Assert.Equal(a.ContentSrc, b.ContentSrc);
                    AssertNavigationPoints(a.NavPoints, b.NavPoints);
                });

                Assert.Equal(expected.PageList == null, actual.PageList == null);
                if (expected.PageList != null && actual.PageList != null)
                {
                    AssertCollection(expected.PageList.PageTargets, actual.PageList.PageTargets, (a, b) =>
                    {
                        Assert.Equal(a.Id, b.Id);
                        Assert.Equal(a.Class, b.Class);
                        Assert.Equal(a.ContentSrc, b.ContentSrc);
                        Assert.Equal(a.NavLabelText, b.NavLabelText);
                        Assert.Equal(a.Type, b.Type);
                        Assert.Equal(a.Value, b.Value);
                    });
                }
            }
        }
Ejemplo n.º 2
0
        private void AssertNcx(NcxDocument expected, NcxDocument actual)
        {
            Assert.AreEqual(expected == null, actual == null, nameof(actual));
            if (expected != null && actual != null)
            {
                Assert.AreEqual(expected.DocAuthor, actual.DocAuthor, nameof(actual.DocAuthor));
                Assert.AreEqual(expected.DocTitle, actual.DocTitle, nameof(actual.DocTitle));

                AssertCollection(expected.Meta, actual.Meta, nameof(actual.Meta), (a, b) =>
                {
                    Assert.AreEqual(a.Name, b.Name, "Metadata.Name");
                    Assert.AreEqual(a.Content, b.Content, "Metadata.Content");
                    Assert.AreEqual(a.Scheme, b.Scheme, "Metadata.Scheme");
                });

                Assert.AreEqual(expected.NavList == null, actual.NavList == null, "NavigationList");
                if (expected.NavList != null && actual.NavList != null)
                {
                    Assert.AreEqual(expected.NavList.Id, actual.NavList.Id, "NavigationList.Id");
                    Assert.AreEqual(expected.NavList.Class, actual.NavList.Class, "NavigationList.Class");
                    Assert.AreEqual(expected.NavList.Label, actual.NavList.Label, "NavigationList.Label");

                    AssertCollection(expected.NavList.NavTargets, actual.NavList.NavTargets, nameof(actual.NavList.NavTargets), (a, b) =>
                    {
                        Assert.AreEqual(a.Id, b.Id, "NavigationTarget.Id");
                        Assert.AreEqual(a.Class, b.Class, "NavigationTarget.Class");
                        Assert.AreEqual(a.Label, b.Label, "NavigationTarget.Label");
                        Assert.AreEqual(a.PlayOrder, b.PlayOrder, "NavigationTarget.PlayOrder");
                        Assert.AreEqual(a.ContentSource, b.ContentSource, "NavigationTarget.ContentSrc");
                    });
                }

                AssertCollection(expected.NavMap.NavPoints, actual.NavMap.NavPoints, nameof(actual.NavMap), (a, b) =>
                {
                    Assert.AreEqual(a.Id, b.Id, "NavigationMap.Id");
                    Assert.AreEqual(a.PlayOrder, b.PlayOrder, "NavigationMap.PlayOrder");
                    Assert.AreEqual(a.NavLabelText, b.NavLabelText, "NavigationMap.NavLabelText");
                    Assert.AreEqual(a.Class, b.Class, "NavigationMap.Class");
                    Assert.AreEqual(a.ContentSrc, b.ContentSrc, "NavigationMap.ContentSorce");
                    AssertNavigationPoints(a.NavPoints, b.NavPoints);
                });

                Assert.AreEqual(expected.PageList == null, actual.PageList == null, nameof(actual.PageList));
                if (expected.PageList != null && actual.PageList != null)
                {
                    AssertCollection(expected.PageList.PageTargets, actual.PageList.PageTargets, nameof(actual.PageList.PageTargets), (a, b) =>
                    {
                        Assert.AreEqual(a.Id, b.Id, "PageList.Id");
                        Assert.AreEqual(a.Class, b.Class, "PageList.Class");
                        Assert.AreEqual(a.ContentSrc, b.ContentSrc, "PageList.ContentSrc");
                        Assert.AreEqual(a.NavLabelText, b.NavLabelText, "PageList.Label");
                        Assert.AreEqual(a.Type, b.Type, "PageList.Type");
                        Assert.AreEqual(a.Value, b.Value, "PageList.Value");
                    });
                }
            }
        }
Ejemplo n.º 3
0
        public static NcxDocument Read(XDocument xml)
        {
            if (xml == null)
            {
                throw new ArgumentNullException(nameof(xml));
            }
            if (xml.Root == null)
            {
                throw new ArgumentException("XML document has no root element.", nameof(xml));
            }

            var navMap   = xml.Root.Element(NcxElements.NavMap);
            var pageList = xml.Root.Element(NcxElements.PageList);
            var navInfo  = pageList?.Element(NcxElements.NavInfo);
            var navList  = xml.Root.Element(NcxElements.NavList);

            var ncx = new NcxDocument
            {
                Meta = xml.Root.Element(NcxElements.Head)?.Elements(NcxElements.Meta).AsObjectList(elem => new NcxMeta
                {
                    Name    = (string)elem.Attribute(NcxMeta.Attributes.Name),
                    Content = (string)elem.Attribute(NcxMeta.Attributes.Content),
                    Scheme  = (string)elem.Attribute(NcxMeta.Attributes.Scheme)
                }),
                DocTitle  = xml.Root.Element(NcxElements.DocTitle)?.Element(NcxElements.Text)?.Value,
                DocAuthor = xml.Root.Element(NcxElements.DocAuthor)?.Element(NcxElements.Text)?.Value,
                NavMap    = new NcxNapMap
                {
                    Dom       = navMap,
                    NavPoints = navMap == null ? new List <NcxNavPoint>() : navMap.Elements(NcxElements.NavPoint).AsObjectList(ReadNavPoint)
                },
                PageList = pageList == null ? null : new NcxPageList
                {
                    NavInfo = navInfo == null ? null : new NcxNavInfo {
                        Text = navInfo.Element(NcxElements.Text)?.Value
                    },
                    PageTargets = pageList.Elements(NcxElements.PageTarget).AsObjectList(elem => new NcxPageTarget
                    {
                        Id           = (string)elem.Attribute(NcxPageTarget.Attributes.Id),
                        Class        = (string)elem.Attribute(NcxPageTarget.Attributes.Class),
                        Value        = (int?)elem.Attribute(NcxPageTarget.Attributes.Value),
                        Type         = (NcxPageTargetType?)(elem.Attribute(NcxPageTarget.Attributes.Type) == null ? null : Enum.Parse(typeof(NcxPageTargetType), (string)elem.Attribute("type"), true)),
                        NavLabelText = elem.Element(NcxElements.NavLabel)?.Element(NcxElements.Text)?.Value,
                        ContentSrc   = (string)elem.Element(NcxElements.Content)?.Attribute(NcxPageTarget.Attributes.ContentSrc)
                    })
                },
                NavList = navList == null ? null : new NcxNavList
                {
                    Id         = (string)navList.Attribute("id"),
                    Class      = (string)navList.Attribute("class"),
                    Label      = navList.Element(NcxElements.NavLabel)?.Element(NcxElements.Text)?.Value,
                    NavTargets = navList.Elements(NcxElements.NavTarget).AsObjectList(elem => new NcxNavTarget
                    {
                        Id            = (string)elem.Attribute("id"),
                        Class         = (string)elem.Attribute("class"),
                        Label         = navList.Element(NcxElements.NavLabel)?.Element(NcxElements.Text)?.Value,
                        PlayOrder     = (int?)elem.Attribute("playOrder"),
                        ContentSource = (string)elem.Element(NcxElements.Content)?.Attribute("src")
                    })
                }
            };

            return(ncx);
        }
Ejemplo n.º 4
0
        public static string Format(NcxDocument ncx)
        {
            if (ncx == null)
            {
                throw new ArgumentNullException(nameof(ncx));
            }

            var root = new XElement(NcxElements.Ncx);

            root.Add(new XAttribute("xmlns", Constants.NcxNamespace));

            var head = new XElement(NcxElements.Head);

            foreach (var meta in ncx.Meta)
            {
                var element = new XElement(NcxElements.Meta);
                if (meta.Content != null)
                {
                    element.Add(new XAttribute(NcxMeta.Attributes.Content, meta.Content));
                }
                if (!string.IsNullOrWhiteSpace(meta.Name))
                {
                    element.Add(new XAttribute(NcxMeta.Attributes.Name, meta.Name));
                }
                if (!string.IsNullOrWhiteSpace(meta.Scheme))
                {
                    element.Add(new XAttribute(NcxMeta.Attributes.Scheme, meta.Scheme));
                }
                head.Add(element);
            }
            root.Add(head);

            if (!string.IsNullOrWhiteSpace(ncx.DocTitle))
            {
                root.Add(new XElement(NcxElements.DocTitle, new XElement(NcxElements.Text, ncx.DocTitle)));
            }

            // Null check instead of string.IsNullOrWhiteSpace(), because I've seen epubs having <docAuthor><text/></docAuthor>
            if (ncx.DocAuthor != null)
            {
                root.Add(new XElement(NcxElements.DocAuthor, new XElement(NcxElements.Text, ncx.DocAuthor)));
            }

            var navMap = new XElement(NcxElements.NavMap);

            WriteNavPoints(navMap, ncx.NavMap.NavPoints);
            root.Add(navMap);

            if (ncx.PageList != null)
            {
                var pageListElement = new XElement(NcxElements.PageList);

                if (ncx.PageList.NavInfo != null)
                {
                    pageListElement.Add(new XElement(NcxElements.NavInfo, new XElement(NcxElements.Text, ncx.PageList.NavInfo.Text)));
                }

                foreach (var pageTarget in ncx.PageList.PageTargets)
                {
                    var pageTargetElement = new XElement(NcxElements.PageTarget);
                    if (!string.IsNullOrWhiteSpace(pageTarget.Class))
                    {
                        pageTargetElement.Add(new XAttribute(NcxPageTarget.Attributes.Class, pageTarget.Class));
                    }
                    if (!string.IsNullOrWhiteSpace(pageTarget.Id))
                    {
                        pageTargetElement.Add(new XAttribute(NcxPageTarget.Attributes.Id, pageTarget.Id));
                    }
                    if (pageTarget.Type.HasValue)
                    {
                        pageTargetElement.Add(new XAttribute(NcxPageTarget.Attributes.Type, pageTarget.Type.Value));
                    }
                    if (pageTarget.Value.HasValue)
                    {
                        pageTargetElement.Add(new XAttribute(NcxPageTarget.Attributes.Value, pageTarget.Value));
                    }
                    if (!string.IsNullOrWhiteSpace(pageTarget.NavLabelText))
                    {
                        pageTargetElement.Add(new XElement(NcxElements.NavLabel, new XElement(NcxElements.Text, pageTarget.NavLabelText)));
                    }
                    if (!string.IsNullOrWhiteSpace(pageTarget.ContentSrc))
                    {
                        pageTargetElement.Add(new XElement(NcxElements.Content, new XAttribute(NcxPageTarget.Attributes.ContentSrc, pageTarget.ContentSrc)));
                    }
                    pageListElement.Add(pageTargetElement);
                }

                root.Add(pageListElement);
            }

            var xml = Constants.XmlDeclaration + "\n" + root;

            return(xml);
        }
Ejemplo n.º 5
0
        /*
         * @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
         * @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
         * @@@@                              Method                                                                @@@@
         * @@@@****************************Read Epub      ******************************************************@@@@
         * @@@@                                                                                                    @@@@
         * @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
         * @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
         */
        //------------method geeting smil fil -----------
        public void ProcessEpub(string currentFile)
        {
            try
            {
                string EpubNummerOnly = Path.GetFileNameWithoutExtension(currentFile);

                // Read an epub file
                EpubBook book = EpubReader.Read(currentFile);

                // Read metadata
                string title = book.Title;
                FinalReportlist.Add("Title of book = " + title);

                //******Author******
                var    authors          = book.Authors;
                int    Authors_count    = 0;
                string Authors_Metadata = "";
                foreach (var eachData in authors)
                {
                    Authors_count++;
                    Authors_Metadata = Authors_Metadata + " (" + eachData + "), ";
                }
                FinalReportlist.Add("Author of book= (" + Authors_count + ")" + Authors_Metadata);

                //******images***********
                ICollection <EpubByteFile> images = book.Resources.Images;
                FinalReportlist.Add("Images in  book= (" + images.Count + ")");

                //******other metadata***********
                // Access internal EPUB format specific data structures.
                EpubFormat  format      = book.Format;
                OcfDocument ocf         = format.Ocf;
                OpfDocument opf         = format.Opf;
                NcxDocument ncx         = format.Ncx;
                NavDocument nav         = format.Nav;
                var         bookVersion = format.Opf.EpubVersion;
                var         bookIsbnID  = format.Opf.Metadata.Identifiers;

                var    allMetadata       = opf.Metadata.Languages;
                string language_Metadata = "";
                foreach (var eachData in allMetadata)
                {
                    language_Metadata = language_Metadata + eachData + ", ";
                }
                FinalReportlist.Add("Language of of book= (" + allMetadata.Count + ")" + language_Metadata);
                FinalReportlist.Add("version of book= " + bookVersion);


                // Get table of contents
                FinalReportlist.Add("\n*****Table of Content*****");
                ICollection <EpubChapter> chapters = book.TableOfContents;
                foreach (var eachH1chapter in chapters)
                {
                    FinalReportlist.Add(eachH1chapter.Title);
                    //*******writing h2 Chapter****
                    if (eachH1chapter.SubChapters.Count >= 1)
                    {
                        var h2chapter = eachH1chapter.SubChapters;
                        foreach (var eachH2chapter in h2chapter)
                        {
                            FinalReportlist.Add("\t" + eachH2chapter.Title);
                            //*******writing h3 Chapter****
                            if (eachH2chapter.SubChapters.Count >= 1)
                            {
                                var h3chapter = eachH2chapter.SubChapters;
                                foreach (var eachH3chapter in h3chapter)
                                {
                                    FinalReportlist.Add("\t\t" + eachH3chapter.Title);
                                }
                            }
                        }
                    }
                }
            }// try end
            catch (Exception e)
            {
                string error = "\n Error in syncStart " + Program.Current_bookPath;
                Console.WriteLine(error + e);
                Program.Global_error = Program.Global_error + error;
            } // catch end
        }     //syncStart end