Compare commits

..

10 commits

Author SHA1 Message Date
dependabot[bot]
21f64b7147
Bump commons-io from 2.10.0 to 2.11.0 (#19)
Bumps commons-io from 2.10.0 to 2.11.0.

---
updated-dependencies:
- dependency-name: commons-io:commons-io
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-03-26 19:02:10 +00:00
dependabot[bot]
3a41276f3e
Bump lombok from 1.18.20 to 1.18.22 (#22)
Bumps [lombok](https://github.com/projectlombok/lombok) from 1.18.20 to 1.18.22.
- [Release notes](https://github.com/projectlombok/lombok/releases)
- [Changelog](https://github.com/projectlombok/lombok/blob/master/doc/changelog.markdown)
- [Commits](https://github.com/projectlombok/lombok/compare/v1.18.20...v1.18.22)

---
updated-dependencies:
- dependency-name: org.projectlombok:lombok
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-03-26 18:55:49 +00:00
dependabot[bot]
3e05384d05
Bump htmlcleaner from 2.24 to 2.26 (#24)
Bumps htmlcleaner from 2.24 to 2.26.

---
updated-dependencies:
- dependency-name: net.sourceforge.htmlcleaner:htmlcleaner
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-03-26 18:51:18 +00:00
dependabot[bot]
4504196223
Bump tiles-maven-plugin from 2.22 to 2.27 (#26)
Bumps [tiles-maven-plugin](https://github.com/repaint-io/maven-tiles) from 2.22 to 2.27.
- [Release notes](https://github.com/repaint-io/maven-tiles/releases)
- [Changelog](https://github.com/repaint-io/maven-tiles/blob/master/CHANGELOG.adoc)
- [Commits](https://github.com/repaint-io/maven-tiles/compare/tiles-maven-plugin-2.22...tiles-maven-plugin-2.27)

---
updated-dependencies:
- dependency-name: io.repaint.maven:tiles-maven-plugin
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-03-26 18:38:56 +00:00
6b450a36df
Enable specifying the value for dcterms:modified value (#27)
* pom: set version to 1.2.0

* pom: specify dependency versions as properties

* pom: add assertj-core as test dependency

* Add test for setting modified metadata value

* Revert "pom: set version to 1.2.0"

No change was needed waranting a ‘minor’ version change.

This reverts commit 5051fcf6bac670fffbf5fdfbb769818fee8cf637.

* Override default dcterms:modified value if provided

* pom: version set to 1.2.0
2022-03-26 18:29:39 +00:00
dependabot[bot]
5cd01918c4
Bump commons-io from 2.9.0 to 2.10.0 (#17)
Bumps commons-io from 2.9.0 to 2.10.0.

---
updated-dependencies:
- dependency-name: commons-io:commons-io
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-06-14 08:51:18 +01:00
dependabot[bot]
3151eb1257
Bump tiles-maven-plugin from 2.21 to 2.22 (#15)
Bumps [tiles-maven-plugin](https://github.com/repaint-io/maven-tiles) from 2.21 to 2.22.
- [Release notes](https://github.com/repaint-io/maven-tiles/releases)
- [Changelog](https://github.com/repaint-io/maven-tiles/blob/master/CHANGELOG.adoc)
- [Commits](https://github.com/repaint-io/maven-tiles/compare/tiles-maven-plugin-2.21...tiles-maven-plugin-2.22)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-05-26 15:57:53 +01:00
dependabot[bot]
c7e61f9594
Bump commons-io from 2.8.0 to 2.9.0 (#16)
Bumps commons-io from 2.8.0 to 2.9.0.

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-05-26 15:02:25 +01:00
dependabot[bot]
3160977288
Bump tiles-maven-plugin from 2.20 to 2.21 (#14)
Bumps [tiles-maven-plugin](https://github.com/repaint-io/maven-tiles) from 2.20 to 2.21.
- [Release notes](https://github.com/repaint-io/maven-tiles/releases)
- [Changelog](https://github.com/repaint-io/maven-tiles/blob/master/CHANGELOG.adoc)
- [Commits](https://github.com/repaint-io/maven-tiles/compare/tiles-maven-plugin-2.20...tiles-maven-plugin-2.21)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-05-06 06:51:08 +01:00
dependabot[bot]
6340d4e43d
Bump lombok from 1.18.18 to 1.18.20 (#13)
Bumps [lombok](https://github.com/rzwitserloot/lombok) from 1.18.18 to 1.18.20.
- [Release notes](https://github.com/rzwitserloot/lombok/releases)
- [Changelog](https://github.com/rzwitserloot/lombok/blob/master/doc/changelog.markdown)
- [Commits](https://github.com/rzwitserloot/lombok/compare/v1.18.18...v1.18.20)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-04-02 07:05:22 +01:00
3 changed files with 352 additions and 291 deletions

28
pom.xml
View file

@ -9,9 +9,8 @@
<relativePath/>
</parent>
<groupId>net.kemitix</groupId>
<artifactId>epub-creator</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>
<scm>
<connection>scm:git:git@github.com:kemitix/epub-creator.git</connection>
@ -25,40 +24,51 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<tiles-maven-plugin.version>2.20</tiles-maven-plugin.version>
<tiles-maven-plugin.version>2.27</tiles-maven-plugin.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>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.htmlcleaner</groupId>
<artifactId>htmlcleaner</artifactId>
<version>2.24</version>
<version>${htmlcleaner.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
<version>${commons-collections.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
<version>${lombok.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View file

@ -1,234 +1,246 @@
/* Copyright 2014 OpenCollab.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* 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
* THE SOFTWARE.
*/
package coza.opencollab.epub.creator.impl;
import coza.opencollab.epub.creator.EpubConstants;
import coza.opencollab.epub.creator.api.MetadataItem;
import coza.opencollab.epub.creator.api.OpfCreator;
import coza.opencollab.epub.creator.model.Content;
import coza.opencollab.epub.creator.model.EpubBook;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.ContentNode;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.PrettyXmlSerializer;
import org.htmlcleaner.Serializer;
import org.htmlcleaner.TagNode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Default implementation of the OpfCreator. This follows EPUB3 standards to
* create the OPF file content.
*
* @author OpenCollab
*/
public class OpfCreatorDefault implements OpfCreator {
/**
* The template XML used to create the OPF file. This is settable if a
* different template needs to be used.
*/
private String opfXML = EpubConstants.OPF_XML;
/**
* HtmlCleaner used to clean the XHTML document
*/
private final HtmlCleaner cleaner;
/**
* XmlSerializer used to format to XML String output
*/
private final Serializer htmlSetdown;
private final List<MetadataItem> metadataItems = new ArrayList<>();
public OpfCreatorDefault() {
cleaner = new HtmlCleaner();
CleanerProperties htmlProperties = cleaner.getProperties();
htmlProperties.setOmitHtmlEnvelope(true);
htmlProperties.setAdvancedXmlEscape(false);
htmlProperties.setUseEmptyElementTags(true);
htmlSetdown = new PrettyXmlSerializer(htmlProperties);
}
@Override
public void addMetadata(MetadataItem metadataItem) {
this.metadataItems.add(metadataItem);
}
/**
* {@inheritDoc}
*/
@Override
public String createOpfString(EpubBook book) {
TagNode tagNode = cleaner.clean(opfXML);
addMetaDataTags(tagNode, book);
addManifestTags(tagNode, book);
addSpineTags(tagNode, book);
addCustomMetadata(tagNode, book);
return htmlSetdown.getAsString(tagNode);
}
private void addCustomMetadata(TagNode tagNode, EpubBook book) {
TagNode metaNode = tagNode.findElementByName("metadata", true);
metadataItems.forEach(item -> {
TagNode node = new TagNode(item.getName());
if (item.hasId()) {
node.addAttribute("id", item.getId());
}
if (item.hasProperty()) {
node.addAttribute("property", item.getProperty());
}
if (item.hasRefines()) {
node.addAttribute("refines", item.getRefines());
}
if (item.hasValue()) {
node.addChild(new ContentNode(item.getValue()));
}
metaNode.addChild(node);
});
}
/**
* Add the required meta data
*
* @param tagNode the HTML tagNode of the OPF template
* @param book the EpubBook
*/
private void addMetaDataTags(TagNode tagNode, EpubBook book) {
TagNode metaNode = tagNode.findElementByName("metadata", true);
addNodeData(metaNode, "dc:identifier", book.getId());
addNodeData(metaNode, "dc:title", book.getTitle());
addNodeData(metaNode, "dc:language", book.getLanguage());
addNodeData(metaNode, "meta", new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'").format(new Date()));
if (book.getAuthor() != null) {
TagNode creatorNode = new TagNode("dc:creator");
creatorNode.addChild(new ContentNode(book.getAuthor()));
metaNode.addChild(creatorNode);
}
}
/**
* 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
*
* @param tagNode the HTML tagNode of the OPF template
* @param book the EpubBook
*/
private void addManifestTags(TagNode tagNode, EpubBook book) {
TagNode manifestNode = tagNode.findElementByName("manifest", true);
for (Content content : book.getContents()) {
manifestNode.addChild(buildItemNode(content));
}
}
/**
* Builds an item tag from the Content object
*
* @param content
* @return
*/
private TagNode buildItemNode(Content content) {
TagNode itemNode = new TagNode("item");
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());
}
if (content.hasFallBack()) {
itemNode.addAttribute("fallback", content.getFallBack().getId());
}
return itemNode;
}
/**
* Adds item ref tags for all Content objects that must be added to the
* spine.
*
* 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
*/
private void addSpineTags(TagNode tagNode, EpubBook book) {
TagNode spineNode = tagNode.findElementByName("spine", true);
for (Content content : book.getContents()) {
if (content.isSpine()) {
spineNode.addChild(buildItemrefNode(content));
}
}
}
/**
* Builds an item ref tag from the Content object
*
* @param content
* @return
*/
private TagNode buildItemrefNode(Content content) {
TagNode itemNode = new TagNode("itemref");
itemNode.addAttribute("idref", content.getId());
if (!content.isLinear()) {
itemNode.addAttribute("linear", "no");
}
return itemNode;
}
/**
* Adds a ContentNode (value) with to a child element of the TagNode
*
* <elementName>{value}<elementName>
*
* @param tagNode
* @param elementName
* @param value
*/
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.
*
* @return the OPF XML text
*/
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;
}
}
/* Copyright 2014 OpenCollab.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* 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
* THE SOFTWARE.
*/
package coza.opencollab.epub.creator.impl;
import coza.opencollab.epub.creator.EpubConstants;
import coza.opencollab.epub.creator.api.MetadataItem;
import coza.opencollab.epub.creator.api.OpfCreator;
import coza.opencollab.epub.creator.model.Content;
import coza.opencollab.epub.creator.model.EpubBook;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.ContentNode;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.PrettyXmlSerializer;
import org.htmlcleaner.Serializer;
import org.htmlcleaner.TagNode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
/**
* Default implementation of the OpfCreator. This follows EPUB3 standards to
* create the OPF file content.
*
* @author OpenCollab
*/
public class OpfCreatorDefault implements OpfCreator {
/**
* The template XML used to create the OPF file. This is settable if a
* different template needs to be used.
*/
private String opfXML = EpubConstants.OPF_XML;
/**
* HtmlCleaner used to clean the XHTML document
*/
private final HtmlCleaner cleaner;
/**
* XmlSerializer used to format to XML String output
*/
private final Serializer htmlSetdown;
private final List<MetadataItem> metadataItems = new ArrayList<>();
public OpfCreatorDefault() {
cleaner = new HtmlCleaner();
CleanerProperties htmlProperties = cleaner.getProperties();
htmlProperties.setOmitHtmlEnvelope(true);
htmlProperties.setAdvancedXmlEscape(false);
htmlProperties.setUseEmptyElementTags(true);
htmlSetdown = new PrettyXmlSerializer(htmlProperties);
}
@Override
public void addMetadata(MetadataItem metadataItem) {
this.metadataItems.add(metadataItem);
}
/**
* {@inheritDoc}
*/
@Override
public String createOpfString(EpubBook book) {
TagNode tagNode = cleaner.clean(opfXML);
addMetaDataTags(tagNode, book);
addManifestTags(tagNode, book);
addSpineTags(tagNode, book);
addCustomMetadata(tagNode, book);
return htmlSetdown.getAsString(tagNode);
}
private void addCustomMetadata(TagNode tagNode, EpubBook book) {
TagNode metaNode = tagNode.findElementByName("metadata", true);
metadataItems.forEach(item -> {
TagNode node = new TagNode(item.getName());
if (item.hasId()) {
node.addAttribute("id", item.getId());
}
if (item.hasProperty()) {
node.addAttribute("property", item.getProperty());
}
if (item.hasRefines()) {
node.addAttribute("refines", item.getRefines());
}
if (item.hasValue()) {
node.addChild(new ContentNode(item.getValue()));
}
metaNode.addChild(node);
});
}
/**
* Add the required meta data
*
* @param tagNode the HTML tagNode of the OPF template
* @param book the EpubBook
*/
private void addMetaDataTags(TagNode tagNode, EpubBook book) {
TagNode metaNode = tagNode.findElementByName("metadata", true);
addNodeData(metaNode, "dc:identifier", book.getId());
addNodeData(metaNode, "dc:title", book.getTitle());
addNodeData(metaNode, "dc:language", book.getLanguage());
Optional<MetadataItem> customModifiedValue = metadataItems.stream()
.filter(MetadataItem::hasValue)
.filter(MetadataItem::hasProperty)
.filter(item -> item.getProperty().equals("dcterms:modified"))
.findFirst();
if (customModifiedValue.isPresent()) {
MetadataItem item = customModifiedValue.get();
addNodeData(metaNode, "meta", item.getValue());
metadataItems.remove(item);
} else {
addNodeData(metaNode, "meta", new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'").format(new Date()));
}
if (book.getAuthor() != null) {
TagNode creatorNode = new TagNode("dc:creator");
creatorNode.addChild(new ContentNode(book.getAuthor()));
metaNode.addChild(creatorNode);
}
}
/**
* 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
*
* @param tagNode the HTML tagNode of the OPF template
* @param book the EpubBook
*/
private void addManifestTags(TagNode tagNode, EpubBook book) {
TagNode manifestNode = tagNode.findElementByName("manifest", true);
for (Content content : book.getContents()) {
manifestNode.addChild(buildItemNode(content));
}
}
/**
* Builds an item tag from the Content object
*
* @param content
* @return
*/
private TagNode buildItemNode(Content content) {
TagNode itemNode = new TagNode("item");
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());
}
if (content.hasFallBack()) {
itemNode.addAttribute("fallback", content.getFallBack().getId());
}
return itemNode;
}
/**
* Adds item ref tags for all Content objects that must be added to the
* spine.
*
* 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
*/
private void addSpineTags(TagNode tagNode, EpubBook book) {
TagNode spineNode = tagNode.findElementByName("spine", true);
for (Content content : book.getContents()) {
if (content.isSpine()) {
spineNode.addChild(buildItemrefNode(content));
}
}
}
/**
* Builds an item ref tag from the Content object
*
* @param content
* @return
*/
private TagNode buildItemrefNode(Content content) {
TagNode itemNode = new TagNode("itemref");
itemNode.addAttribute("idref", content.getId());
if (!content.isLinear()) {
itemNode.addAttribute("linear", "no");
}
return itemNode;
}
/**
* Adds a ContentNode (value) with to a child element of the TagNode
*
* <elementName>{value}<elementName>
*
* @param tagNode
* @param elementName
* @param value
*/
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.
*
* @return the OPF XML text
*/
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;
}
}

View file

@ -1,48 +1,87 @@
package coza.opencollab.epub.creator;
import coza.opencollab.epub.creator.api.MetadataItem;
import coza.opencollab.epub.creator.model.EpubBook;
import java.io.File;
import java.io.FileOutputStream;
import junit.framework.Assert;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
/**
*
* @author OpenCollab
*/
public class EpubCreatorTest {
@Test
public void testEpubCreate() {
try (FileOutputStream file = new FileOutputStream(new File("test.epub"))) {
EpubBook book = new EpubBook("en", "Samuel .-__Id1", "Samuel Test Book", "Samuel Holtzkampf");
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.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");
book.writeToStream(file);
// TODO : real tests to see if document correct, this is just to test that creation is succesfull
Assert.assertEquals("test", "test");
} catch (Exception ex) {
System.out.println(ex);
Assert.assertEquals("test", "test1");
}
}
}
package coza.opencollab.epub.creator;
import coza.opencollab.epub.creator.api.MetadataItem;
import coza.opencollab.epub.creator.model.EpubBook;
import lombok.SneakyThrows;
import lombok.val;
import org.apache.commons.io.IOUtils;
import org.assertj.core.api.WithAssertions;
import org.junit.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Scanner;
import java.util.zip.ZipFile;
/**
* @author OpenCollab
*/
public class EpubCreatorTest implements WithAssertions {
String author = "Samuel Holtzkampf";
String modified = "modified-date-and-time";
@Test
public void bookHasAuthor() {
//when
val book = createEpubBook();
//then
assertThat(book.getAuthor()).isEqualTo(author);
}
@Test
public void hasSetModifiedValue() {
//given
//TODO use a proper temp file
val file = new File("test.epub");
writeBookToFile(createEpubBook(), file);
//when
String bookOpf = unzipFileEntry(file, "content/book.opf");
//then
assertThat(bookOpf).containsOnlyOnce("<meta property=\"dcterms:modified\">");
assertThat(bookOpf).contains(String.format("<meta property=\"dcterms:modified\">%s</meta>", modified));
}
@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;
}
}