Ejemplo n.º 1
0
 public void testGetContents1()
 {
     Book book = new Book();
     Resource resource1 = new Resource("id1", System.Text.Encoding.UTF8.GetBytes("Hello, world !"), "chapter1.html", MediatypeService.XHTML);
     book.getSpine().addResource(resource1);
     book.getTableOfContents().addSection(resource1, "My first chapter");
     Assert.AreEqual(1, book.getContents().Count);
 }
Ejemplo n.º 2
0
 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);
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Read epub from inputstream
 /// </summary> 
 /// <param name="zipInputStream">the inputstream from which to read the epub</param>
 /// <param name="encoding"></param>
 public Book readEpub(ZipInputStream zipInputStream, String encoding)
 {
     Book result = new Book();
     Resources resources = readResources(zipInputStream, encoding);
     handleMimeType(result, resources);
     String packageResourceHref = getPackageResourceHref(resources);
     Resource packageResource = processPackageResource(packageResourceHref, result, resources);
     result.setOpfResource(packageResource);
     Resource ncxResource = processNcxResource(packageResource, result);
     result.setNcxResource(ncxResource);
     result = postProcessBook(result);
     return result;
 }
        /// 
        /// <param name="book"></param>
        public Book processBook(Book book)
        {
            if (bookProcessors == null)
            {
                return book;
            }
            foreach (BookProcessor bookProcessor in bookProcessors)
            {
                try
                {
                    book = bookProcessor.processBook(book);
                }
                catch (Exception e)
                {

                }
            }
            return book;
        }
        /// 
        /// <param name="packageResource"></param>
        /// <param name="epubReader"></param>
        /// <param name="book"></param>
        /// <param name="resources"></param>
        public static void read(Resource packageResource, EpubReader epubReader, Book book, Resources resources)
        {
            XElement packageDocument = XElement.Load(packageResource.getInputStream());
            String packageHref = packageResource.getHref();
            resources = fixHrefs(packageHref, resources);
            readGuide(packageDocument, epubReader, book, resources);
            System.Collections.Generic.Dictionary<string, string> idMapping = new Dictionary<string, string>();
            resources = readManifest(packageDocument, packageHref, epubReader, resources, idMapping);

            book.setResources(resources);
            readCover(packageDocument, book);
            //  book.setMetadata(PackageDocumentMetadataReader.readMetadata(packageDocument, book.getResources()));
            book.setSpine(readSpine(packageDocument, epubReader, book.getResources(), idMapping));

            // if we did not find a cover page then we make the first page of the book the cover page
            if (book.getCoverPage() == null && book.getSpine().size() > 0)
            {
                book.setCoverPage(book.getSpine().getResource(0));
            }
        }
Ejemplo n.º 6
0
 public virtual Book processBook(Book book)
 {
     return book;
 }
 /// <summary>
 /// Finds the cover resource in the packageDocument and adds it to the book if
 /// found. Keeps the cover resource in the resources map
 /// </summary>
 /// <param>resources</param>
 /// <param name="packageDocument"></param>
 /// <param name="book"></param>
 private static void readCover(XElement packageDocument, Book book)
 {
     List<String> coverHrefs = findCoverHrefs(packageDocument);
     foreach (String coverHref in coverHrefs)
     {
         Resource resource = book.getResources().getByHref(coverHref);
         if (resource == null)
         {
             //log.error("Cover resource " + coverHref + " not found");
             continue;
         }
         if (resource.getMediaType() == MediatypeService.XHTML)
         {
             book.setCoverPage(resource);
         }
         else if (MediatypeService.isBitmapImage(resource.getMediaType()))
         {
             book.setCoverImage(resource);
         }
     }
 }
 /// <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);
     }
 }
Ejemplo n.º 9
0
 /// 
 /// <param name="packageResourceHref"></param>
 /// <param name="book"></param>
 /// <param name="resources"></param>
 private Resource processPackageResource(String packageResourceHref, Book book, Resources resources)
 {
     Resource packageResource = resources.remove(packageResourceHref);
     try
     {
         PackageDocumentReader.read(packageResource, this, book, resources);
     }
     catch (Exception e)
     {
         //log.error(e.getMessage(), e);
     }
     return packageResource;
 }
Ejemplo n.º 10
0
 /// 
 /// <param name="book"></param>
 /// <param name="resultStream"></param>
 private void writePackageDocument(Book book, ZipOutputStream resultStream)
 {
 }
Ejemplo n.º 11
0
 /// 
 /// <param name="book"></param>
 private Book postProcessBook(Book book)
 {
     return null;
 }
Ejemplo n.º 12
0
 /// 
 /// <param name="packageResource"></param>
 /// <param name="book"></param>
 private Resource processNcxResource(Resource packageResource, Book book)
 {
     return null;
 }
Ejemplo n.º 13
0
 /// 
 /// <param name="result"></param>
 /// <param name="resources"></param>
 private void handleMimeType(Book result, Resources resources)
 {
     resources.remove("mimetype");
 }
Ejemplo n.º 14
0
 /// 
 /// <param name="book"></param>
 private void initTOCResource(Book book)
 {
 }
Ejemplo n.º 15
0
 /// 
 /// <param name="book"></param>
 /// <param name="resultStream"></param>
 private void writeResources(Book book, ZipOutputStream resultStream)
 {
 }
Ejemplo n.º 16
0
 /// 
 /// <param name="book"></param>
 /// <param name="out"></param>
 public void write(Book book, Stream stream)
 {
 }
Ejemplo n.º 17
0
 public void BookConstructorTest()
 {
     Book target = new Book();
     //Assert.Inconclusive("TODO: Implement code to verify target");
 }