Compare commits
No commits in common. "21f64b71479766b2f5661d9a4c74e3df72149a39" and "95d6df60ea5e38a6c877b3b96ae242f1e7c41b12" have entirely different histories.
21f64b7147
...
95d6df60ea
3 changed files with 291 additions and 352 deletions
28
pom.xml
28
pom.xml
|
@ -9,8 +9,9 @@
|
||||||
<relativePath/>
|
<relativePath/>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
|
<groupId>net.kemitix</groupId>
|
||||||
<artifactId>epub-creator</artifactId>
|
<artifactId>epub-creator</artifactId>
|
||||||
<version>1.2.0</version>
|
<version>1.1.0</version>
|
||||||
|
|
||||||
<scm>
|
<scm>
|
||||||
<connection>scm:git:git@github.com:kemitix/epub-creator.git</connection>
|
<connection>scm:git:git@github.com:kemitix/epub-creator.git</connection>
|
||||||
|
@ -24,51 +25,40 @@
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
|
||||||
<tiles-maven-plugin.version>2.27</tiles-maven-plugin.version>
|
<tiles-maven-plugin.version>2.20</tiles-maven-plugin.version>
|
||||||
<kemitix-tiles.version>2.8.0</kemitix-tiles.version>
|
<kemitix-tiles.version>2.8.0</kemitix-tiles.version>
|
||||||
<assertj.version>3.22.0</assertj.version>
|
|
||||||
<lombok.version>1.18.22</lombok.version>
|
|
||||||
<commons-collections.version>3.2.2</commons-collections.version>
|
|
||||||
<commons-io.version>2.11.0</commons-io.version>
|
|
||||||
<htmlcleaner.version>2.26</htmlcleaner.version>
|
|
||||||
<junit.version>4.13.2</junit.version>
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<version>${junit.version}</version>
|
<version>4.13.2</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.sourceforge.htmlcleaner</groupId>
|
<groupId>net.sourceforge.htmlcleaner</groupId>
|
||||||
<artifactId>htmlcleaner</artifactId>
|
<artifactId>htmlcleaner</artifactId>
|
||||||
<version>${htmlcleaner.version}</version>
|
<version>2.24</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>commons-io</groupId>
|
<groupId>commons-io</groupId>
|
||||||
<artifactId>commons-io</artifactId>
|
<artifactId>commons-io</artifactId>
|
||||||
<version>${commons-io.version}</version>
|
<version>2.8.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>commons-collections</groupId>
|
<groupId>commons-collections</groupId>
|
||||||
<artifactId>commons-collections</artifactId>
|
<artifactId>commons-collections</artifactId>
|
||||||
<version>${commons-collections.version}</version>
|
<version>3.2.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
<version>${lombok.version}</version>
|
<version>1.18.18</version>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.assertj</groupId>
|
|
||||||
<artifactId>assertj-core</artifactId>
|
|
||||||
<version>${assertj.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -1,246 +1,234 @@
|
||||||
/* Copyright 2014 OpenCollab.
|
/* Copyright 2014 OpenCollab.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
*
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
*
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
package coza.opencollab.epub.creator.impl;
|
package coza.opencollab.epub.creator.impl;
|
||||||
|
|
||||||
import coza.opencollab.epub.creator.EpubConstants;
|
import coza.opencollab.epub.creator.EpubConstants;
|
||||||
import coza.opencollab.epub.creator.api.MetadataItem;
|
import coza.opencollab.epub.creator.api.MetadataItem;
|
||||||
import coza.opencollab.epub.creator.api.OpfCreator;
|
import coza.opencollab.epub.creator.api.OpfCreator;
|
||||||
import coza.opencollab.epub.creator.model.Content;
|
import coza.opencollab.epub.creator.model.Content;
|
||||||
import coza.opencollab.epub.creator.model.EpubBook;
|
import coza.opencollab.epub.creator.model.EpubBook;
|
||||||
import org.htmlcleaner.CleanerProperties;
|
import org.htmlcleaner.CleanerProperties;
|
||||||
import org.htmlcleaner.ContentNode;
|
import org.htmlcleaner.ContentNode;
|
||||||
import org.htmlcleaner.HtmlCleaner;
|
import org.htmlcleaner.HtmlCleaner;
|
||||||
import org.htmlcleaner.PrettyXmlSerializer;
|
import org.htmlcleaner.PrettyXmlSerializer;
|
||||||
import org.htmlcleaner.Serializer;
|
import org.htmlcleaner.Serializer;
|
||||||
import org.htmlcleaner.TagNode;
|
import org.htmlcleaner.TagNode;
|
||||||
|
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
|
||||||
|
/**
|
||||||
/**
|
* Default implementation of the OpfCreator. This follows EPUB3 standards to
|
||||||
* Default implementation of the OpfCreator. This follows EPUB3 standards to
|
* create the OPF file content.
|
||||||
* create the OPF file content.
|
*
|
||||||
*
|
* @author OpenCollab
|
||||||
* @author OpenCollab
|
*/
|
||||||
*/
|
public class OpfCreatorDefault implements OpfCreator {
|
||||||
public class OpfCreatorDefault implements OpfCreator {
|
|
||||||
|
/**
|
||||||
/**
|
* The template XML used to create the OPF file. This is settable if a
|
||||||
* The template XML used to create the OPF file. This is settable if a
|
* different template needs to be used.
|
||||||
* different template needs to be used.
|
*/
|
||||||
*/
|
private String opfXML = EpubConstants.OPF_XML;
|
||||||
private String opfXML = EpubConstants.OPF_XML;
|
|
||||||
|
/**
|
||||||
/**
|
* HtmlCleaner used to clean the XHTML document
|
||||||
* HtmlCleaner used to clean the XHTML document
|
*/
|
||||||
*/
|
private final HtmlCleaner cleaner;
|
||||||
private final HtmlCleaner cleaner;
|
|
||||||
|
/**
|
||||||
/**
|
* XmlSerializer used to format to XML String output
|
||||||
* XmlSerializer used to format to XML String output
|
*/
|
||||||
*/
|
private final Serializer htmlSetdown;
|
||||||
private final Serializer htmlSetdown;
|
|
||||||
|
private final List<MetadataItem> metadataItems = new ArrayList<>();
|
||||||
private final List<MetadataItem> metadataItems = new ArrayList<>();
|
|
||||||
|
public OpfCreatorDefault() {
|
||||||
public OpfCreatorDefault() {
|
cleaner = new HtmlCleaner();
|
||||||
cleaner = new HtmlCleaner();
|
CleanerProperties htmlProperties = cleaner.getProperties();
|
||||||
CleanerProperties htmlProperties = cleaner.getProperties();
|
htmlProperties.setOmitHtmlEnvelope(true);
|
||||||
htmlProperties.setOmitHtmlEnvelope(true);
|
htmlProperties.setAdvancedXmlEscape(false);
|
||||||
htmlProperties.setAdvancedXmlEscape(false);
|
htmlProperties.setUseEmptyElementTags(true);
|
||||||
htmlProperties.setUseEmptyElementTags(true);
|
htmlSetdown = new PrettyXmlSerializer(htmlProperties);
|
||||||
htmlSetdown = new PrettyXmlSerializer(htmlProperties);
|
}
|
||||||
}
|
|
||||||
|
@Override
|
||||||
@Override
|
public void addMetadata(MetadataItem metadataItem) {
|
||||||
public void addMetadata(MetadataItem metadataItem) {
|
this.metadataItems.add(metadataItem);
|
||||||
this.metadataItems.add(metadataItem);
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* {@inheritDoc}
|
||||||
* {@inheritDoc}
|
*/
|
||||||
*/
|
@Override
|
||||||
@Override
|
public String createOpfString(EpubBook book) {
|
||||||
public String createOpfString(EpubBook book) {
|
TagNode tagNode = cleaner.clean(opfXML);
|
||||||
TagNode tagNode = cleaner.clean(opfXML);
|
addMetaDataTags(tagNode, book);
|
||||||
addMetaDataTags(tagNode, book);
|
addManifestTags(tagNode, book);
|
||||||
addManifestTags(tagNode, book);
|
addSpineTags(tagNode, book);
|
||||||
addSpineTags(tagNode, book);
|
addCustomMetadata(tagNode, book);
|
||||||
addCustomMetadata(tagNode, book);
|
return htmlSetdown.getAsString(tagNode);
|
||||||
return htmlSetdown.getAsString(tagNode);
|
}
|
||||||
}
|
|
||||||
|
private void addCustomMetadata(TagNode tagNode, EpubBook book) {
|
||||||
private void addCustomMetadata(TagNode tagNode, EpubBook book) {
|
TagNode metaNode = tagNode.findElementByName("metadata", true);
|
||||||
TagNode metaNode = tagNode.findElementByName("metadata", true);
|
metadataItems.forEach(item -> {
|
||||||
metadataItems.forEach(item -> {
|
TagNode node = new TagNode(item.getName());
|
||||||
TagNode node = new TagNode(item.getName());
|
if (item.hasId()) {
|
||||||
if (item.hasId()) {
|
node.addAttribute("id", item.getId());
|
||||||
node.addAttribute("id", item.getId());
|
}
|
||||||
}
|
if (item.hasProperty()) {
|
||||||
if (item.hasProperty()) {
|
node.addAttribute("property", item.getProperty());
|
||||||
node.addAttribute("property", item.getProperty());
|
}
|
||||||
}
|
if (item.hasRefines()) {
|
||||||
if (item.hasRefines()) {
|
node.addAttribute("refines", item.getRefines());
|
||||||
node.addAttribute("refines", item.getRefines());
|
}
|
||||||
}
|
if (item.hasValue()) {
|
||||||
if (item.hasValue()) {
|
node.addChild(new ContentNode(item.getValue()));
|
||||||
node.addChild(new ContentNode(item.getValue()));
|
}
|
||||||
}
|
metaNode.addChild(node);
|
||||||
metaNode.addChild(node);
|
});
|
||||||
});
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* Add the required meta data
|
||||||
* Add the required meta data
|
*
|
||||||
*
|
* @param tagNode the HTML tagNode of the OPF template
|
||||||
* @param tagNode the HTML tagNode of the OPF template
|
* @param book the EpubBook
|
||||||
* @param book the EpubBook
|
*/
|
||||||
*/
|
private void addMetaDataTags(TagNode tagNode, EpubBook book) {
|
||||||
private void addMetaDataTags(TagNode tagNode, EpubBook book) {
|
TagNode metaNode = tagNode.findElementByName("metadata", true);
|
||||||
TagNode metaNode = tagNode.findElementByName("metadata", true);
|
addNodeData(metaNode, "dc:identifier", book.getId());
|
||||||
addNodeData(metaNode, "dc:identifier", book.getId());
|
addNodeData(metaNode, "dc:title", book.getTitle());
|
||||||
addNodeData(metaNode, "dc:title", book.getTitle());
|
addNodeData(metaNode, "dc:language", book.getLanguage());
|
||||||
addNodeData(metaNode, "dc:language", book.getLanguage());
|
addNodeData(metaNode, "meta", new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'").format(new Date()));
|
||||||
Optional<MetadataItem> customModifiedValue = metadataItems.stream()
|
if (book.getAuthor() != null) {
|
||||||
.filter(MetadataItem::hasValue)
|
TagNode creatorNode = new TagNode("dc:creator");
|
||||||
.filter(MetadataItem::hasProperty)
|
creatorNode.addChild(new ContentNode(book.getAuthor()));
|
||||||
.filter(item -> item.getProperty().equals("dcterms:modified"))
|
metaNode.addChild(creatorNode);
|
||||||
.findFirst();
|
}
|
||||||
if (customModifiedValue.isPresent()) {
|
}
|
||||||
MetadataItem item = customModifiedValue.get();
|
|
||||||
addNodeData(metaNode, "meta", item.getValue());
|
/**
|
||||||
metadataItems.remove(item);
|
* Adds a item tag to the manifest for each Content object.
|
||||||
} else {
|
*
|
||||||
addNodeData(metaNode, "meta", new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'").format(new Date()));
|
* The manifest contains all Content that will be added to the EPUB as files
|
||||||
}
|
*
|
||||||
if (book.getAuthor() != null) {
|
* @param tagNode the HTML tagNode of the OPF template
|
||||||
TagNode creatorNode = new TagNode("dc:creator");
|
* @param book the EpubBook
|
||||||
creatorNode.addChild(new ContentNode(book.getAuthor()));
|
*/
|
||||||
metaNode.addChild(creatorNode);
|
private void addManifestTags(TagNode tagNode, EpubBook book) {
|
||||||
}
|
TagNode manifestNode = tagNode.findElementByName("manifest", true);
|
||||||
}
|
for (Content content : book.getContents()) {
|
||||||
|
manifestNode.addChild(buildItemNode(content));
|
||||||
/**
|
}
|
||||||
* Adds a item tag to the manifest for each Content object.
|
}
|
||||||
*
|
|
||||||
* The manifest contains all Content that will be added to the EPUB as files
|
/**
|
||||||
*
|
* Builds an item tag from the Content object
|
||||||
* @param tagNode the HTML tagNode of the OPF template
|
*
|
||||||
* @param book the EpubBook
|
* @param content
|
||||||
*/
|
* @return
|
||||||
private void addManifestTags(TagNode tagNode, EpubBook book) {
|
*/
|
||||||
TagNode manifestNode = tagNode.findElementByName("manifest", true);
|
private TagNode buildItemNode(Content content) {
|
||||||
for (Content content : book.getContents()) {
|
TagNode itemNode = new TagNode("item");
|
||||||
manifestNode.addChild(buildItemNode(content));
|
itemNode.addAttribute("href", content.getHref());
|
||||||
}
|
itemNode.addAttribute("id", content.getId());
|
||||||
}
|
itemNode.addAttribute("media-type", content.getMediaType());
|
||||||
|
if (content.getProperties() != null) {
|
||||||
/**
|
itemNode.addAttribute("properties", content.getProperties());
|
||||||
* Builds an item tag from the Content object
|
}
|
||||||
*
|
if (content.hasFallBack()) {
|
||||||
* @param content
|
itemNode.addAttribute("fallback", content.getFallBack().getId());
|
||||||
* @return
|
}
|
||||||
*/
|
return itemNode;
|
||||||
private TagNode buildItemNode(Content content) {
|
}
|
||||||
TagNode itemNode = new TagNode("item");
|
|
||||||
itemNode.addAttribute("href", content.getHref());
|
/**
|
||||||
itemNode.addAttribute("id", content.getId());
|
* Adds item ref tags for all Content objects that must be added to the
|
||||||
itemNode.addAttribute("media-type", content.getMediaType());
|
* spine.
|
||||||
if (content.getProperties() != null) {
|
*
|
||||||
itemNode.addAttribute("properties", content.getProperties());
|
* The spine contains all the resources that will be shown when reading the
|
||||||
}
|
* book from start to end
|
||||||
if (content.hasFallBack()) {
|
*
|
||||||
itemNode.addAttribute("fallback", content.getFallBack().getId());
|
* @param tagNode the HTML tagNode of the OPF template
|
||||||
}
|
* @param book the EpubBook
|
||||||
return itemNode;
|
*/
|
||||||
}
|
private void addSpineTags(TagNode tagNode, EpubBook book) {
|
||||||
|
TagNode spineNode = tagNode.findElementByName("spine", true);
|
||||||
/**
|
for (Content content : book.getContents()) {
|
||||||
* Adds item ref tags for all Content objects that must be added to the
|
if (content.isSpine()) {
|
||||||
* spine.
|
spineNode.addChild(buildItemrefNode(content));
|
||||||
*
|
}
|
||||||
* The spine contains all the resources that will be shown when reading the
|
}
|
||||||
* book from start to end
|
}
|
||||||
*
|
|
||||||
* @param tagNode the HTML tagNode of the OPF template
|
/**
|
||||||
* @param book the EpubBook
|
* Builds an item ref tag from the Content object
|
||||||
*/
|
*
|
||||||
private void addSpineTags(TagNode tagNode, EpubBook book) {
|
* @param content
|
||||||
TagNode spineNode = tagNode.findElementByName("spine", true);
|
* @return
|
||||||
for (Content content : book.getContents()) {
|
*/
|
||||||
if (content.isSpine()) {
|
private TagNode buildItemrefNode(Content content) {
|
||||||
spineNode.addChild(buildItemrefNode(content));
|
TagNode itemNode = new TagNode("itemref");
|
||||||
}
|
itemNode.addAttribute("idref", content.getId());
|
||||||
}
|
if (!content.isLinear()) {
|
||||||
}
|
itemNode.addAttribute("linear", "no");
|
||||||
|
}
|
||||||
/**
|
return itemNode;
|
||||||
* Builds an item ref tag from the Content object
|
}
|
||||||
*
|
|
||||||
* @param content
|
/**
|
||||||
* @return
|
* Adds a ContentNode (value) with to a child element of the TagNode
|
||||||
*/
|
*
|
||||||
private TagNode buildItemrefNode(Content content) {
|
* <elementName>{value}<elementName>
|
||||||
TagNode itemNode = new TagNode("itemref");
|
*
|
||||||
itemNode.addAttribute("idref", content.getId());
|
* @param tagNode
|
||||||
if (!content.isLinear()) {
|
* @param elementName
|
||||||
itemNode.addAttribute("linear", "no");
|
* @param value
|
||||||
}
|
*/
|
||||||
return itemNode;
|
private void addNodeData(TagNode tagNode, String elementName, String value) {
|
||||||
}
|
TagNode editNode = tagNode.findElementByName(elementName, true);
|
||||||
|
editNode.addChild(new ContentNode(value));
|
||||||
/**
|
}
|
||||||
* Adds a ContentNode (value) with to a child element of the TagNode
|
|
||||||
*
|
/**
|
||||||
* <elementName>{value}<elementName>
|
* The base XML used for the OPF file.
|
||||||
*
|
*
|
||||||
* @param tagNode
|
* @return the OPF XML text
|
||||||
* @param elementName
|
*/
|
||||||
* @param value
|
public String getOpfXML() {
|
||||||
*/
|
return opfXML;
|
||||||
private void addNodeData(TagNode tagNode, String elementName, String value) {
|
}
|
||||||
TagNode editNode = tagNode.findElementByName(elementName, true);
|
|
||||||
editNode.addChild(new ContentNode(value));
|
/**
|
||||||
}
|
* The base XML used for the OPF file. This is optional as there is a EPUB3
|
||||||
|
* standard default but it can be overridden.
|
||||||
/**
|
*
|
||||||
* The base XML used for the OPF file.
|
* @param opfXML the OPF XML to set
|
||||||
*
|
*/
|
||||||
* @return the OPF XML text
|
public void setOpfXML(String opfXML) {
|
||||||
*/
|
this.opfXML = opfXML;
|
||||||
public String getOpfXML() {
|
}
|
||||||
return opfXML;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* The base XML used for the OPF file. This is optional as there is a EPUB3
|
|
||||||
* standard default but it can be overridden.
|
|
||||||
*
|
|
||||||
* @param opfXML the OPF XML to set
|
|
||||||
*/
|
|
||||||
public void setOpfXML(String opfXML) {
|
|
||||||
this.opfXML = opfXML;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,87 +1,48 @@
|
||||||
package coza.opencollab.epub.creator;
|
package coza.opencollab.epub.creator;
|
||||||
|
|
||||||
import coza.opencollab.epub.creator.api.MetadataItem;
|
import coza.opencollab.epub.creator.api.MetadataItem;
|
||||||
import coza.opencollab.epub.creator.model.EpubBook;
|
import coza.opencollab.epub.creator.model.EpubBook;
|
||||||
import lombok.SneakyThrows;
|
import java.io.File;
|
||||||
import lombok.val;
|
import java.io.FileOutputStream;
|
||||||
import org.apache.commons.io.IOUtils;
|
import junit.framework.Assert;
|
||||||
import org.assertj.core.api.WithAssertions;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.File;
|
/**
|
||||||
import java.io.FileOutputStream;
|
*
|
||||||
import java.io.OutputStream;
|
* @author OpenCollab
|
||||||
import java.util.Scanner;
|
*/
|
||||||
import java.util.zip.ZipFile;
|
public class EpubCreatorTest {
|
||||||
|
|
||||||
/**
|
@Test
|
||||||
* @author OpenCollab
|
public void testEpubCreate() {
|
||||||
*/
|
try (FileOutputStream file = new FileOutputStream(new File("test.epub"))) {
|
||||||
public class EpubCreatorTest implements WithAssertions {
|
EpubBook book = new EpubBook("en", "Samuel .-__Id1", "Samuel Test Book", "Samuel Holtzkampf");
|
||||||
|
|
||||||
String author = "Samuel Holtzkampf";
|
MetadataItem.Builder builder = MetadataItem.builder();
|
||||||
String modified = "modified-date-and-time";
|
book.addMetadata(builder.name("dc:creator").value("Bob Smith"));
|
||||||
|
book.addMetadata(builder.name("meta")
|
||||||
@Test
|
.property("role").refines("#editor-id")
|
||||||
public void bookHasAuthor() {
|
.value("Editor"));
|
||||||
//when
|
|
||||||
val book = createEpubBook();
|
book.addContent(this.getClass().getResourceAsStream("/epub30-overview.xhtml"),
|
||||||
//then
|
"application/xhtml+xml", "xhtml/epub30-overview.xhtml", true, true).setId("Overview");
|
||||||
assertThat(book.getAuthor()).isEqualTo(author);
|
book.addContent(this.getClass().getResourceAsStream("/idpflogo_web_125.jpg"),
|
||||||
|
"image/jpeg", "img/idpflogo_web_125.jpg", false, false);
|
||||||
}
|
book.addContent(this.getClass().getResourceAsStream("/epub-spec.css"),
|
||||||
|
"text/css", "css/epub-spec.css", false, false);
|
||||||
@Test
|
book.addTextContent("TestHtml", "xhtml/samuelTest2.xhtml", "Samuel test one two four!!!!!\nTesting two").setToc(true);
|
||||||
public void hasSetModifiedValue() {
|
book.addTextContent("TestHtml", "xhtml/samuelTest.xhtml", "Samuel test one two three\nTesting two").setToc(true);
|
||||||
//given
|
book.addCoverImage(IOUtils.toByteArray(this.getClass().getResourceAsStream("/P1010832.jpg")),
|
||||||
//TODO use a proper temp file
|
"image/jpeg", "images/P1010832.jpg");
|
||||||
val file = new File("test.epub");
|
|
||||||
writeBookToFile(createEpubBook(), file);
|
|
||||||
//when
|
book.writeToStream(file);
|
||||||
String bookOpf = unzipFileEntry(file, "content/book.opf");
|
// TODO : real tests to see if document correct, this is just to test that creation is succesfull
|
||||||
//then
|
Assert.assertEquals("test", "test");
|
||||||
assertThat(bookOpf).containsOnlyOnce("<meta property=\"dcterms:modified\">");
|
} catch (Exception ex) {
|
||||||
assertThat(bookOpf).contains(String.format("<meta property=\"dcterms:modified\">%s</meta>", modified));
|
System.out.println(ex);
|
||||||
}
|
Assert.assertEquals("test", "test1");
|
||||||
|
}
|
||||||
@SneakyThrows
|
}
|
||||||
private String unzipFileEntry(File file, String name) {
|
}
|
||||||
val zipFile = new ZipFile(file);
|
|
||||||
val entry = zipFile.getEntry(name);
|
|
||||||
val inputStream = zipFile.getInputStream(entry);
|
|
||||||
try (Scanner scanner = new Scanner(inputStream)) {
|
|
||||||
return scanner.useDelimiter("\\A").next();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@SneakyThrows
|
|
||||||
private void writeBookToFile(EpubBook book, File file) {
|
|
||||||
try (OutputStream outputStream = new FileOutputStream(file)) {
|
|
||||||
book.writeToStream(outputStream);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@SneakyThrows
|
|
||||||
private EpubBook createEpubBook() {
|
|
||||||
EpubBook book = new EpubBook("en", "Samuel .-__Id1", "Samuel Test Book", author);
|
|
||||||
|
|
||||||
MetadataItem.Builder builder = MetadataItem.builder();
|
|
||||||
book.addMetadata(builder.name("dc:creator").value("Bob Smith"));
|
|
||||||
book.addMetadata(builder.name("meta")
|
|
||||||
.property("role").refines("#editor-id")
|
|
||||||
.value("Editor"));
|
|
||||||
book.addMetadata((builder.name("meta").property("dcterms:modified").value(modified)));
|
|
||||||
|
|
||||||
book.addContent(this.getClass().getResourceAsStream("/epub30-overview.xhtml"),
|
|
||||||
"application/xhtml+xml", "xhtml/epub30-overview.xhtml", true, true).setId("Overview");
|
|
||||||
book.addContent(this.getClass().getResourceAsStream("/idpflogo_web_125.jpg"),
|
|
||||||
"image/jpeg", "img/idpflogo_web_125.jpg", false, false);
|
|
||||||
book.addContent(this.getClass().getResourceAsStream("/epub-spec.css"),
|
|
||||||
"text/css", "css/epub-spec.css", false, false);
|
|
||||||
book.addTextContent("TestHtml", "xhtml/samuelTest2.xhtml", "Samuel test one two four!!!!!\nTesting two").setToc(true);
|
|
||||||
book.addTextContent("TestHtml", "xhtml/samuelTest.xhtml", "Samuel test one two three\nTesting two").setToc(true);
|
|
||||||
book.addCoverImage(IOUtils.toByteArray(this.getClass().getResourceAsStream("/P1010832.jpg")),
|
|
||||||
"image/jpeg", "images/P1010832.jpg");
|
|
||||||
return book;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue