/** * Check whether a resource is valid. A valid resource must have a non-blank ID, a non-blank link * and a media type. * * @param book the book the resource is for * @param resource the resource to check * @return whether the resource is valid */ private static boolean isValidResource(final Book book, final Resource resource) { if (resource == null || (resource.getMediaType() == MediatypeService.NCX && book.getSpine().getTocResource() != null)) { return false; } if (StringUtil.isBlank(resource.getId())) { LOGGER.error( "resource id must not be blank (href: " + resource.getHref() + ", mediatype:" + resource.getMediaType() + ")"); return false; } if (StringUtil.isBlank(resource.getHref())) { LOGGER.error( "resource href must not be blank (id: " + resource.getId() + ", mediatype:" + resource.getMediaType() + ")"); return false; } if (resource.getMediaType() == null) { LOGGER.error( "resource media type must be specified (id: " + resource.getId() + ", href:" + resource.getHref() + ")"); return false; } return true; }
public static void processEpub(String bookPath, String dest) throws FileNotFoundException, IOException { EpubReader reader = new EpubReader(); Book b = reader.readEpub(new FileInputStream(new File(bookPath))); String content = ""; int pagecount = 1; int tempCounter; Count cnt = new Count(0, 0); for (Resource res : b.getContents()) { content = new String(res.getData()); Document doc = Jsoup.parse(content, "UTF-8"); // http-equiv=\"content-type\" content=\"text/html; charset=utf-8\""); Element elem = new Element(Tag.valueOf("meta"), ""); elem.attr("http-equiv", "content-type"); elem.attr("content", "text/html; charset=utf-8"); doc.head().after(elem); System.out.println(doc.head().data()); Element ele = doc.body(); alterElement(ele); Count cTemp = modify(ele, cnt); cnt.setCount(cTemp.getCount()); cnt.setPgCount(cTemp.getPgCount()); doc.body().html(ele.html()); res.setData(doc.html().getBytes()); if (res.getMediaType() == null) res.setMediaType(new MediaType("html", "html")); } EpubWriter wr = new EpubWriter(); wr.write(b, new FileOutputStream(new File(dest))); }
/** * Write a resource as an item element. * * @param serializer the XML serialiser to write the item element to * @param book the book to write the resource for * @param resource the resource to write as an item element * @throws IOException if an I/O error occurs */ private static void writeItem( final XmlSerializer serializer, final Book book, final Resource resource) throws IOException { if (!isValidResource(book, resource)) { return; } serializer.startTag(NAMESPACE_OPF, OPFElements.ITEM); serializer.attribute(PREFIX_EMPTY, OPFAttributes.ID, resource.getId()); serializer.attribute(PREFIX_EMPTY, OPFAttributes.HREF, resource.getHref()); serializer.attribute(PREFIX_EMPTY, OPFAttributes.MEDIA_TYPE, resource.getMediaType().getName()); if (StringUtil.isNotBlank(resource.getProperties())) { serializer.attribute(PREFIX_EMPTY, OPFAttributes.PROPERTIES, resource.getProperties()); } serializer.endTag(NAMESPACE_OPF, OPFElements.ITEM); }