/** * 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 void setContextResource(Resource resource) { if (resource == null) { return; } if (StringUtils.isNotBlank(resource.getHref())) { int lastSlashPos = resource.getHref().lastIndexOf('/'); if (lastSlashPos >= 0) { this.currentFolder = resource.getHref().substring(0, lastSlashPos + 1); } } }
/** * 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); }