EPUB 3 support

git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@240 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
henrikjust 2015-05-05 08:55:48 +00:00
parent d141619053
commit 9c99999ad1
16 changed files with 154 additions and 64 deletions

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* version 1.6 (2015-04-21)
* version 1.6 (2015-05-05)
*
*/
@ -40,7 +40,8 @@ import writer2latex.xhtml.XhtmlConfig;
/** This class repackages an XHTML document into EPUB format.
* Some filenames are hard wired in this implementation: The main directory is OEBPS and
* the OPF and NCX files are book.opf and book.ncx respectively
* the OPF and NCX files are book.opf and book.ncx respectively; finally the EPUB 3 navigation
* document is nav.xhtml
*/
public class EPUBWriter implements OutputFile {
@ -50,7 +51,7 @@ public class EPUBWriter implements OutputFile {
private ConverterResult xhtmlResult;
private String sFileName;
private int nVersion;
//private XhtmlConfig config;
private XhtmlConfig config;
/** Create a new <code>EPUBWriter</code> based on a <code>ConverterResult</code>.
*
@ -63,7 +64,7 @@ public class EPUBWriter implements OutputFile {
this.xhtmlResult = xhtmlResult;
this.sFileName = Misc.removeExtension(sFileName);
this.nVersion = nVersion;
//this.config = config;
this.config = config;
}
// Implement OutputFile
@ -79,6 +80,11 @@ public class EPUBWriter implements OutputFile {
@Override public boolean isMasterDocument() {
return true;
}
@Override public boolean containsMath() {
// We don't really care about this
return nVersion==3;
}
@Override public void write(OutputStream os) throws IOException {
ZipOutputStream zos = new ZipOutputStream(os);
@ -100,7 +106,7 @@ public class EPUBWriter implements OutputFile {
zos.closeEntry();
// Then manifest
OPFWriter manifest = new OPFWriter(xhtmlResult,nVersion);
OPFWriter manifest = new OPFWriter(xhtmlResult,nVersion,config);
ZipEntry manifestEntry = new ZipEntry("OEBPS/book.opf");
zos.putNextEntry(manifestEntry);
writeZipEntry(manifest,zos);
@ -114,7 +120,7 @@ public class EPUBWriter implements OutputFile {
writeZipEntry(navigation,zos);
zos.closeEntry();
}
else {
if (nVersion!=3 || config.includeNCX()) {
OutputFile ncx = new NCXWriter(xhtmlResult, manifest.getUid());
ZipEntry ncxEntry = new ZipEntry("OEBPS/book.ncx");
zos.putNextEntry(ncxEntry);

View file

@ -20,15 +20,20 @@
*
* All Rights Reserved.
*
* version 1.6 (2015-04-21)
* version 1.6 (2015-05-05)
*
*/
package writer2latex.epub;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TimeZone;
import java.util.UUID;
import javax.xml.parsers.DocumentBuilder;
@ -45,13 +50,14 @@ import writer2latex.api.ConverterResult;
import writer2latex.api.OutputFile;
import writer2latex.base.DOMDocument;
import writer2latex.util.Misc;
import writer2latex.xhtml.XhtmlConfig;
/** This class writes an OPF-file for an EPUB document (see http://www.idpf.org/2007/opf/OPF_2.0_final_spec.html).
*/
public class OPFWriter extends DOMDocument {
private String sUID=null;
public OPFWriter(ConverterResult cr, int nVersion) {
public OPFWriter(ConverterResult cr, int nVersion, XhtmlConfig config) {
super("book", "opf");
// create DOM
@ -78,8 +84,6 @@ public class OPFWriter extends DOMDocument {
pack.setAttribute("xmlns","http://www.idpf.org/2007/opf");
pack.setAttribute("unique-identifier", "BookId");
// TODO: http://sketchytech.blogspot.dk/2014/03/epub2-to-epub3-lessons-learnt-in.html
// Meta data, at least dc:title, dc:language and dc:identifier are required by the specification
// For EPUB 3, also dcterms:modified is required
Element metadata = contentDOM.createElement("metadata");
@ -94,9 +98,10 @@ public class OPFWriter extends DOMDocument {
appendElement(contentDOM, metadata, "dc:language", cr.getMetaData().getLanguage());
// Modification (required in EPUB 3)
appendElement(contentDOM, metadata, "meta", cr.getMetaData().getDate())
.setAttribute("property", "dcterms:modified");
if (nVersion==3) {
appendElement(contentDOM, metadata, "meta", getCurrentDateTime())
.setAttribute("property", "dcterms:modified");
}
// Subject and keywords in ODF both map to Dublin core subjects
if (cr.getMetaData().getSubject().length()>0) {
@ -286,7 +291,7 @@ public class OPFWriter extends DOMDocument {
pack.appendChild(manifest);
Element spine = contentDOM.createElement("spine");
if (nVersion!=3) { // Use old NCX file for navigation
if (nVersion!=3 || config.includeNCX()) { // Use old NCX file for navigation
spine.setAttribute("toc", "ncx");
}
pack.appendChild(spine);
@ -321,6 +326,10 @@ public class OPFWriter extends DOMDocument {
String sId = "text"+(++nMasterCount);
item.setAttribute("id", sId);
if (nVersion==3 && file.containsMath()) {
item.setAttribute("properties","mathml");
}
Element itemref = contentDOM.createElement("itemref");
itemref.setAttribute("idref", sId);
spine.appendChild(itemref);
@ -338,7 +347,7 @@ public class OPFWriter extends DOMDocument {
item.setAttribute("properties", "nav");
manifest.appendChild(item);
}
else { // Include old NCX file
if (nVersion!=3 || config.includeNCX()) { // Include old NCX file
Element item = contentDOM.createElement("item");
item.setAttribute("href", "book.ncx");
item.setAttribute("media-type", "application/x-dtbncx+xml");
@ -398,5 +407,13 @@ public class OPFWriter extends DOMDocument {
guide.appendChild(reference);
}
}
// Get the current date and time in the required format
private String getCurrentDateTime() {
Date date = Calendar.getInstance().getTime();
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
return formatter.format(date);
}
}