Permalink
Browse files

fix a very old bug where setting a coverImage without a resource id w…

…ill throw a NullPointerException when writing the epub
  • Loading branch information...
1 parent 25f579a commit c7a5e96275d25d9b0c6effc899b8777b127833c4 @psiegman committed Feb 29, 2012
@@ -304,7 +304,7 @@
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
@@ -451,7 +451,7 @@ public String getTitle() {
* @return
*/
public Resource getCoverImage() {
- return metadata.getCoverImage();
+ return coverImage;
}
public void setCoverImage(Resource coverImage) {
@@ -461,7 +461,7 @@ public void setCoverImage(Resource coverImage) {
if (! resources.containsByHref(coverImage.getHref())) {
resources.add(coverImage);
}
- metadata.setCoverImage(coverImage);
+ this.coverImage = coverImage;
}
/**
@@ -207,16 +207,4 @@ public String addType(String type) {
public void setTypes(List<String> types) {
this.types = types;
}
-
- /**
- * The main image used by the cover page.
- *
- * @return
- */
- public Resource getCoverImage() {
- return coverImage;
- }
- public void setCoverImage(Resource coverImage) {
- this.coverImage = coverImage;
- }
}
@@ -7,7 +7,6 @@
import nl.siegmann.epublib.domain.Date;
import nl.siegmann.epublib.domain.Identifier;
import nl.siegmann.epublib.domain.Metadata;
-import nl.siegmann.epublib.domain.Resource;
import nl.siegmann.epublib.domain.Resources;
import nl.siegmann.epublib.util.StringUtil;
@@ -47,9 +46,6 @@ public static Metadata readMetadata(Document packageDocument, Resources resource
result.setAuthors(readCreators(metadataElement));
result.setContributors(readContributors(metadataElement));
result.setDates(readDates(metadataElement));
- if (result.getCoverImage() == null) {
- result.setCoverImage(readCoverImage(metadataElement, resources));
- }
return result;
}
@@ -62,17 +58,7 @@ private static String getBookIdId(Document document) {
String result = packageElement.getAttributeNS(NAMESPACE_OPF, OPFAttributes.uniqueIdentifier);
return result;
}
-
- private static Resource readCoverImage(Element metadataElement, Resources resources) {
- String coverResourceId = DOMUtil.getFindAttributeValue(metadataElement.getOwnerDocument(), NAMESPACE_OPF, OPFTags.meta, OPFAttributes.name, OPFValues.meta_cover, OPFAttributes.content);
- if (StringUtil.isBlank(coverResourceId)) {
- return null;
- }
- Resource coverResource = resources.getByIdOrHref(coverResourceId);
- return coverResource;
- }
-
-
+
private static List<Author> readCreators(Element metadataElement) {
return readAuthors(DCTags.creator, metadataElement);
}
@@ -87,10 +87,10 @@ public static void writeMetaData(Book book, XmlSerializer serializer) throws Ill
}
// write coverimage
- if(book.getMetadata().getCoverImage() != null) { // write the cover image
+ if(book.getCoverImage() != null) { // write the cover image
serializer.startTag(NAMESPACE_OPF, OPFTags.meta);
serializer.attribute(EpubWriter.EMPTY_NAMESPACE_PREFIX, OPFAttributes.name, OPFValues.meta_cover);
- serializer.attribute(EpubWriter.EMPTY_NAMESPACE_PREFIX, OPFAttributes.content, book.getMetadata().getCoverImage().getId());
+ serializer.attribute(EpubWriter.EMPTY_NAMESPACE_PREFIX, OPFAttributes.content, book.getCoverImage().getId());
serializer.endTag(NAMESPACE_OPF, OPFTags.meta);
}
@@ -67,6 +67,17 @@ public static void read(Resource packageResource, EpubReader epubReader, Book bo
}
}
+// private static Resource readCoverImage(Element metadataElement, Resources resources) {
+// String coverResourceId = DOMUtil.getFindAttributeValue(metadataElement.getOwnerDocument(), NAMESPACE_OPF, OPFTags.meta, OPFAttributes.name, OPFValues.meta_cover, OPFAttributes.content);
+// if (StringUtil.isBlank(coverResourceId)) {
+// return null;
+// }
+// Resource coverResource = resources.getByIdOrHref(coverResourceId);
+// return coverResource;
+// }
+
+
+
/**
* Reads the manifest containing the resource ids, hrefs and mediatypes.
*
@@ -2,7 +2,9 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
+import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
import junit.framework.TestCase;
import nl.siegmann.epublib.domain.Author;
@@ -49,6 +51,28 @@ public void testBook1() {
}
}
+ /**
+ * Test for a very old bug where epublib would throw a NullPointerException when writing a book with a cover that has no id.
+ *
+ */
+ public void testWritingBookWithCoverWithNullId() {
+ try {
+ Book book = new Book();
+ book.getMetadata().addTitle("Epub test book 1");
+ book.getMetadata().addAuthor(new Author("Joe", "Tester"));
+ InputStream is = this.getClass().getResourceAsStream("/book1/cover.png");
+ book.setCoverImage(new Resource(is, "cover.png"));
+ // Add Chapter 1
+ InputStream is1 = this.getClass().getResourceAsStream("/book1/chapter1.html");
+ book.addSection("Introduction", new Resource(is1, "chapter1.html"));
+
+ EpubWriter epubWriter = new EpubWriter();
+ epubWriter.write(book, new FileOutputStream("test1_book1.epub"));
+ } catch (IOException e) {
+ fail(e.getMessage());
+ }
+ }
+
private Book createTestBook() throws IOException {
Book book = new Book();
@@ -0,0 +1,52 @@
+package nl.siegmann.epublib.epub;
+
+import java.io.FileOutputStream;
+
+import nl.siegmann.epublib.domain.Author;
+import nl.siegmann.epublib.domain.Book;
+import nl.siegmann.epublib.domain.Resource;
+import nl.siegmann.epublib.domain.TOCReference;
+
+public class Simple1 {
+ public static void main(String[] args) {
+ try {
+ // Create new Book
+ Book book = new Book();
+
+ // Set the title
+ book.getMetadata().addTitle("Epublib test book 1");
+
+ // Add an Author
+ book.getMetadata().addAuthor(new Author("Joe", "Tester"));
+
+ // Set cover image
+ book.setCoverImage(new Resource(Simple1.class.getResourceAsStream("/book1/test_cover.png"), "cover.png"));
+
+ // Add Chapter 1
+ book.addSection("Introduction", new Resource(Simple1.class.getResourceAsStream("/book1/chapter1.html"), "chapter1.html"));
+
+ // Add css file
+ book.getResources().add(new Resource(Simple1.class.getResourceAsStream("/book1/book1.css"), "book1.css"));
+
+ // Add Chapter 2
+ TOCReference chapter2 = book.addSection("Second Chapter", new Resource(Simple1.class.getResourceAsStream("/book1/chapter2.html"), "chapter2.html"));
+
+ // Add image used by Chapter 2
+ book.getResources().add(new Resource(Simple1.class.getResourceAsStream("/book1/flowers_320x240.jpg"), "flowers.jpg"));
+
+ // Add Chapter2, Section 1
+ book.addSection(chapter2, "Chapter 2, section 1", new Resource(Simple1.class.getResourceAsStream("/book1/chapter2_1.html"), "chapter2_1.html"));
+
+ // Add Chapter 3
+ book.addSection("Conclusion", new Resource(Simple1.class.getResourceAsStream("/book1/chapter3.html"), "chapter3.html"));
+
+ // Create EpubWriter
+ EpubWriter epubWriter = new EpubWriter();
+
+ // Write the Book as Epub
+ epubWriter.write(book, new FileOutputStream("test1_book1.epub"));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+}
@@ -86,7 +86,7 @@ public static void main(String[] args) throws Exception {
if(StringUtils.isNotBlank(coverImage)) {
// book.getResourceByHref(book.getCoverImage());
- book.getMetadata().setCoverImage(new Resource(VFSUtil.resolveInputStream(coverImage), coverImage));
+ book.setCoverImage(new Resource(VFSUtil.resolveInputStream(coverImage), coverImage));
epubCleaner.getBookProcessors().add(new CoverpageBookProcessor());
}

1 comment on commit c7a5e96

Contributor

NightWhistler commented on c7a5e96 Mar 3, 2012

Hi Paul.. great to see you updating epublib :)

This borders on an issue I ran into for PageTurner: some books do have a cover-image inside the ePub, but don't actually declare it as such. It's often just the first image of the book. In my fork I had included some educated guessing which, if no declared cover could be found, would guess that the biggest image in the book was the cover. It worked fairly well, though it also got it wrong on a number of occasions.

I pulled this update into my fork, which breaks that logic and I figured before I invest time in making that work again I'd send you a note to see if you had a better idea of how to handle these books. I admit: they're crappy ePub files, but crappy ePub files are what we find out there :P

Alex

Please sign in to comment.