public void testGetContents3() { Book book = new Book(); Resource resource1 = new Resource("id1", System.Text.Encoding.UTF8.GetBytes("Hello, world !"), "chapter1.html", MediatypeService.XHTML); book.getSpine().addResource(resource1); Resource resource2 = new Resource("id1", System.Text.Encoding.UTF8.GetBytes("Hello, world !"), "chapter2.html", MediatypeService.XHTML); book.getTableOfContents().addSection(resource2, "My first chapter"); book.getGuide().addReference(new GuideReference(resource2, GuideReference.FOREWORD, "The Foreword")); Assert.AreEqual(2, book.getContents().Count); }
/// <summary> /// Reads the book's guide. Here some more attempts are made at finding the cover /// page. /// </summary> /// <param name="packageDocument"></param> /// <param name="epubReader"></param> /// <param name="book"></param> /// <param name="resources">resources</param> private static void readGuide(XElement packageDocument, EpubReader epubReader, Book book, Resources resources) { XElement guideElement = DOMUtil.getFirstElementByTagNameNS(packageDocument, NAMESPACE_OPF, OPFTags.guide); if (guideElement == null) { return; } Guide guide = book.getGuide(); var guideReferences = packageDocument.Elements(NAMESPACE_OPF + OPFTags.reference).Elements<XElement>(); foreach (XElement referenceElement in (from e in guideReferences where e.Value.Trim() != string.Empty select e)) { String resourceHref = DOMUtil.getAttribute(referenceElement, OPFAttributes.href); if (StringUtil.isBlank(resourceHref)) { continue; } Resource resource = resources.getByHref(StringUtil.substringBefore(resourceHref, Constants.FRAGMENT_SEPARATOR_CHAR)); if (resource == null) { //log.error("Guide is referencing resource with href " + resourceHref + " which could not be found"); continue; } String type = DOMUtil.getAttribute(referenceElement, OPFAttributes.type); if (StringUtil.isBlank(type)) { //log.error("Guide is referencing resource with href " + resourceHref + " which is missing the 'type' attribute"); continue; } String title = DOMUtil.getAttribute(referenceElement, OPFAttributes.title); if (GuideReference.COVER.Equals(type)) { continue; // cover is handled elsewhere } GuideReference reference = new GuideReference(resource, type, title, StringUtil.substringAfter(resourceHref, Constants.FRAGMENT_SEPARATOR_CHAR)); guide.addReference(reference); } }