All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.siegmann.epublib.domain.Book Maven / Gradle / Ivy

The newest version!
package nl.siegmann.epublib.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;




/**
 * Representation of a Book.
 * 
 * All resources of a Book (html, css, xml, fonts, images) are represented as Resources. See getResources() for access to these.
* A Book as 3 indexes into these Resources, as per the epub specification.
*
*
Spine
*
these are the Resources to be shown when a user reads the book from start to finish.
*
Table of Contents
*
The table of contents. Table of Contents references may be in a different order and contain different Resources than the spine, and often do. *
Guide
*
The Guide has references to a set of special Resources like the cover page, the Glossary, the copyright page, etc. *
*

* The complication is that these 3 indexes may and usually do point to different pages. * A chapter may be split up in 2 pieces to fit it in to memory. Then the spine will contain both pieces, but the Table of Contents only the first. * The Content page may be in the Table of Contents, the Guide, but not in the Spine. * Etc. *

image/svg+xml Spine Table of Contents Guide Chapter 1 Chapter 1 Part 2 Chapter 2 Chapter 1 Chapter 2 Cover Resources Preface * @author paul * */ public class Book implements Serializable { private static final long serialVersionUID = 2068355170895770100L; private Resources resources = new Resources(); private Metadata metadata = new Metadata(); private Spine spine = new Spine(); private TableOfContents tableOfContents = new TableOfContents(); private Guide guide = new Guide(); private Resource opfResource; private Resource ncxResource; private Resource coverImage; /** * Adds the resource to the table of contents of the book as a child section of the given parentSection * * @param parentSection * @param sectionTitle * @param resource * @return The table of contents */ public TOCReference addSection(TOCReference parentSection, String sectionTitle, Resource resource) { getResources().add(resource); if (spine.findFirstResourceById(resource.getId()) < 0) { spine.addSpineReference(new SpineReference(resource)); } return parentSection.addChildSection(new TOCReference(sectionTitle, resource)); } public void generateSpineFromTableOfContents() { Spine spine = new Spine(tableOfContents); // in case the tocResource was already found and assigned spine.setTocResource(this.spine.getTocResource()); this.spine = spine; } /** * Adds a resource to the book's set of resources, table of contents and if there is no resource with the id in the spine also adds it to the spine. * * @param title * @param resource * @return The table of contents */ public TOCReference addSection(String title, Resource resource) { getResources().add(resource); TOCReference tocReference = tableOfContents.addTOCReference(new TOCReference(title, resource)); if (spine.findFirstResourceById(resource.getId()) < 0) { spine.addSpineReference(new SpineReference(resource)); } return tocReference; } /** * The Book's metadata (titles, authors, etc) * * @return The Book's metadata (titles, authors, etc) */ public Metadata getMetadata() { return metadata; } public void setMetadata(Metadata metadata) { this.metadata = metadata; } public void setResources(Resources resources) { this.resources = resources; } public Resource addResource(Resource resource) { return resources.add(resource); } /** * The collection of all images, chapters, sections, xhtml files, stylesheets, etc that make up the book. * * @return The collection of all images, chapters, sections, xhtml files, stylesheets, etc that make up the book. */ public Resources getResources() { return resources; } /** * The sections of the book that should be shown if a user reads the book from start to finish. * * @return The Spine */ public Spine getSpine() { return spine; } public void setSpine(Spine spine) { this.spine = spine; } /** * The Table of Contents of the book. * * @return The Table of Contents of the book. */ public TableOfContents getTableOfContents() { return tableOfContents; } public void setTableOfContents(TableOfContents tableOfContents) { this.tableOfContents = tableOfContents; } /** * The book's cover page as a Resource. * An XHTML document containing a link to the cover image. * * @return The book's cover page as a Resource */ public Resource getCoverPage() { Resource coverPage = guide.getCoverPage(); if (coverPage == null) { coverPage = spine.getResource(0); } return coverPage; } public void setCoverPage(Resource coverPage) { if (coverPage == null) { return; } if (! resources.containsByHref(coverPage.getHref())) { resources.add(coverPage); } guide.setCoverPage(coverPage); } /** * Gets the first non-blank title from the book's metadata. * * @return the first non-blank title from the book's metadata. */ public String getTitle() { return getMetadata().getFirstTitle(); } /** * The book's cover image. * * @return The book's cover image. */ public Resource getCoverImage() { return coverImage; } public void setCoverImage(Resource coverImage) { if (coverImage == null) { return; } if (! resources.containsByHref(coverImage.getHref())) { resources.add(coverImage); } this.coverImage = coverImage; } /** * The guide; contains references to special sections of the book like colophon, glossary, etc. * * @return The guide; contains references to special sections of the book like colophon, glossary, etc. */ public Guide getGuide() { return guide; } /** * All Resources of the Book that can be reached via the Spine, the TableOfContents or the Guide. *

* Consists of a list of "reachable" resources: *

    *
  • The coverpage
  • *
  • The resources of the Spine that are not already in the result
  • *
  • The resources of the Table of Contents that are not already in the result
  • *
  • The resources of the Guide that are not already in the result
  • *
* To get all html files that make up the epub file use {@link #getResources()} * @return All Resources of the Book that can be reached via the Spine, the TableOfContents or the Guide. */ public List getContents() { Map result = new LinkedHashMap(); addToContentsResult(getCoverPage(), result); for (SpineReference spineReference: getSpine().getSpineReferences()) { addToContentsResult(spineReference.getResource(), result); } for (Resource resource: getTableOfContents().getAllUniqueResources()) { addToContentsResult(resource, result); } for (GuideReference guideReference: getGuide().getReferences()) { addToContentsResult(guideReference.getResource(), result); } return new ArrayList(result.values()); } private static void addToContentsResult(Resource resource, Map allReachableResources){ if (resource != null && (! allReachableResources.containsKey(resource.getHref()))) { allReachableResources.put(resource.getHref(), resource); } } public Resource getOpfResource() { return opfResource; } public void setOpfResource(Resource opfResource) { this.opfResource = opfResource; } public void setNcxResource(Resource ncxResource) { this.ncxResource = ncxResource; } public Resource getNcxResource() { return ncxResource; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy