Writer2xhtml custom config ui + EPUB export

git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@55 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
henrikjust 2010-03-29 11:07:24 +00:00
parent a58ea7fa19
commit ce61f7bc3b
41 changed files with 1118 additions and 212 deletions

View file

@ -0,0 +1,77 @@
/************************************************************************
*
* ContainerWriter.java
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Copyright: 2001-2010 by Henrik Just
*
* All Rights Reserved.
*
* version 1.2 (2010-03-29)
*
*/
package writer2latex.epub;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import writer2latex.xmerge.NewDOMDocument;
/** This class creates the required META-INF/container.xml file for an EPUB package
* (see http://www.idpf.org/ocf/ocf1.0/download/ocf10.htm)
*/
public class ContainerWriter extends NewDOMDocument {
public ContainerWriter(String sRootFile) {
super("container", "xml");
// create DOM
Document contentDOM = null;
try {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
DOMImplementation domImpl = builder.getDOMImplementation();
DocumentType doctype = domImpl.createDocumentType("container","","");
contentDOM = domImpl.createDocument("urn:oasis:names:tc:opendocument:xmlns:container","container",doctype);
}
catch (ParserConfigurationException t) { // this should never happen
throw new RuntimeException(t);
}
// Populate the DOM tree
Element container = contentDOM.getDocumentElement();
container.setAttribute("version", "1.0");
container.setAttribute("xmlns","urn:oasis:names:tc:opendocument:xmlns:container");
Element rootfiles = contentDOM.createElement("rootfiles");
container.appendChild(rootfiles);
Element rootfile = contentDOM.createElement("rootfile");
rootfile.setAttribute("full-path", sRootFile);
rootfile.setAttribute("media-type", "application/oebps-package+xml");
rootfiles.appendChild(rootfile);
setContentDOM(contentDOM);
}
}

View file

@ -0,0 +1,55 @@
/************************************************************************
*
* EPUBConverter.java
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Copyright: 2001-2010 by Henrik Just
*
* All Rights Reserved.
*
* version 1.2 (2010-03-28)
*
*/
package writer2latex.epub;
import java.io.IOException;
import java.io.InputStream;
import writer2latex.api.ConverterResult;
import writer2latex.base.ConverterResultImpl;
import writer2latex.xhtml.Xhtml11Converter;
/** This class converts an OpenDocument file to an EPUB document.
*/
public final class EPUBConverter extends Xhtml11Converter {
// Constructor
public EPUBConverter() {
super();
}
@Override public ConverterResult convert(InputStream is, String sTargetFileName) throws IOException {
ConverterResult xhtmlResult = super.convert(is, sTargetFileName);
ConverterResultImpl epubResult = new ConverterResultImpl();
epubResult.addDocument(new EPUBWriter(xhtmlResult,sTargetFileName));
epubResult.setMetaData(xhtmlResult.getMetaData());
return epubResult;
}
}

View file

@ -0,0 +1,122 @@
/************************************************************************
*
* EPUBWriter.java
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Copyright: 2001-2010 by Henrik Just
*
* All Rights Reserved.
*
* version 1.2 (2010-03-29)
*
*/
package writer2latex.epub;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import writer2latex.api.ConverterResult;
import writer2latex.api.OutputFile;
import writer2latex.util.Misc;
/** This class repackages an XHTML document into EPUB format
*
*/
public class EPUBWriter implements OutputFile {
private static final byte[] mimeBytes = { 'a', 'p', 'p', 'l', 'i', 'c', 'a', 't', 'i', 'o', 'n', '/',
'e', 'p', 'u', 'b', '+', 'z', 'i', 'p'};
private ConverterResult xhtmlResult;
private String sFileName;
public EPUBWriter(ConverterResult xhtmlResult, String sFileName) {
this.xhtmlResult = xhtmlResult;
this.sFileName = Misc.removeExtension(sFileName);
}
public String getFileName() {
return sFileName+".epub";
}
public String getMIMEType() {
return "application/epub+zip";
}
public boolean isMasterDocument() {
return true;
}
public void write(OutputStream os) throws IOException {
ZipOutputStream zos = new ZipOutputStream(os);
// Write MIME type as first entry
ZipEntry mimeEntry = new ZipEntry("mimetype");
mimeEntry.setMethod(ZipEntry.STORED);
mimeEntry.setCrc(0x2CAB616F);
mimeEntry.setSize(mimeBytes.length);
zos.putNextEntry(mimeEntry);
zos.write(mimeBytes, 0, mimeBytes.length);
zos.closeEntry();
// Write container entry next
OutputFile containerWriter = new ContainerWriter("OEBPS/book.opf");
ZipEntry containerEntry = new ZipEntry("META-INF/container.xml");
zos.putNextEntry(containerEntry);
writeZipEntry(containerWriter,zos);
zos.closeEntry();
// Then manifest
OutputFile manifest = new OPFWriter(xhtmlResult, "xxx", "book.ncx", "book.opf");
ZipEntry manifestEntry = new ZipEntry("OEBPS/book.opf");
zos.putNextEntry(manifestEntry);
writeZipEntry(manifest,zos);
zos.closeEntry();
// And content table
OutputFile ncx = new NCXWriter(xhtmlResult, "xxx", "book.ncx");
ZipEntry ncxEntry = new ZipEntry("OEBPS/book.ncx");
zos.putNextEntry(ncxEntry);
writeZipEntry(ncx,zos);
zos.closeEntry();
// Finally XHTML content in the OEBPS sub directory
Iterator<OutputFile> iter = xhtmlResult.iterator();
while (iter.hasNext()) {
OutputFile file = iter.next();
ZipEntry entry = new ZipEntry("OEBPS/"+file.getFileName());
zos.putNextEntry(entry);
writeZipEntry(file, zos);
zos.closeEntry();
}
zos.close();
}
private void writeZipEntry(OutputFile file, ZipOutputStream zos) throws IOException {
// TODO: Fix this waste of memory :-)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
file.write(baos);
byte[] content = baos.toByteArray();
zos.write(content, 0, content.length);
}
}

View file

@ -0,0 +1,141 @@
/************************************************************************
*
* NCXWriter.java
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Copyright: 2001-2010 by Henrik Just
*
* All Rights Reserved.
*
* version 1.2 (2010-03-29)
*
*/
package writer2latex.epub;
import java.util.Iterator;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import writer2latex.api.ContentEntry;
import writer2latex.api.ConverterResult;
import writer2latex.util.Misc;
import writer2latex.xmerge.NewDOMDocument;
/** This class creates the required NXC file for an EPUB document
* (see http://www.idpf.org/2007/opf/OPF_2.0_final_spec.html#Section2.4)
*
*/
public class NCXWriter extends NewDOMDocument {
public NCXWriter(ConverterResult cr, String sUUID, String sFileName) {
super(Misc.removeExtension(sFileName), "ncx");
// create DOM
Document contentDOM = null;
try {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
DOMImplementation domImpl = builder.getDOMImplementation();
DocumentType doctype = domImpl.createDocumentType("ncx","","");
contentDOM = domImpl.createDocument("http://www.daisy.org/z3986/2005/ncx/","ncx",doctype);
}
catch (ParserConfigurationException t) { // this should never happen
throw new RuntimeException(t);
}
System.out.println("populating the ncx");
// Populate the DOM tree
Element ncx = contentDOM.getDocumentElement();
ncx.setAttribute("version", "2005-1");
ncx.setAttribute("xml:lang", cr.getMetaData().getLanguage());
ncx.setAttribute("xmlns","http://www.daisy.org/z3986/2005/ncx/");
// The head has four required meta data items
Element head = contentDOM.createElement("head");
ncx.appendChild(head);
Element uid = contentDOM.createElement("meta");
uid.setAttribute("name","dtb:uid");
uid.setAttribute("content", sUUID);
head.appendChild(uid);
Element depth = contentDOM.createElement("meta");
depth.setAttribute("name","dtb:depth");
depth.setAttribute("content", "1");
head.appendChild(depth);
Element totalPageCount = contentDOM.createElement("meta");
totalPageCount.setAttribute("name","dtb:totalPageCount");
totalPageCount.setAttribute("content", "0");
head.appendChild(totalPageCount);
Element maxPageNumber = contentDOM.createElement("meta");
maxPageNumber.setAttribute("name","dtb:maxPageNumber");
maxPageNumber.setAttribute("content", "0");
head.appendChild(maxPageNumber);
// The ncx must contain a docTitle element
Element docTitle = contentDOM.createElement("docTitle");
ncx.appendChild(docTitle);
Element docTitleText = contentDOM.createElement("text");
docTitle.appendChild(docTitleText);
docTitleText.appendChild(contentDOM.createTextNode(cr.getMetaData().getTitle()));
// Build the navMap from the content table in the converter result
Element navMap = contentDOM.createElement("navMap");
ncx.appendChild(navMap);
int nPlayOrder = 0;
Iterator<ContentEntry> content = cr.getContent().iterator();
while (content.hasNext()) {
ContentEntry entry = content.next();
//if (entry.getLevel()==1) {
System.out.println("Found content entry "+entry.getTitle());
Element navPoint = contentDOM.createElement("navPoint");
navPoint.setAttribute("playOrder", Integer.toString(++nPlayOrder));
navPoint.setAttribute("id", "text"+nPlayOrder);
navMap.appendChild(navPoint);
Element navLabel = contentDOM.createElement("navLabel");
navPoint.appendChild(navLabel);
Element navLabelText = contentDOM.createElement("text");
navLabel.appendChild(navLabelText);
navLabelText.appendChild(contentDOM.createTextNode(entry.getTitle()));
Element navPointContent = contentDOM.createElement("content");
String sHref = entry.getFile().getFileName();
if (entry.getTarget()!=null) { sHref+="#"+entry.getTarget(); }
navPointContent.setAttribute("src", sHref);
navPoint.appendChild(navPointContent);
//}
}
setContentDOM(contentDOM);
System.out.println("finished populating the ncx");
}
}

View file

@ -0,0 +1,159 @@
/************************************************************************
*
* OPFWriter.java
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Copyright: 2001-2010 by Henrik Just
*
* All Rights Reserved.
*
* version 1.2 (2010-03-29)
*
*/
package writer2latex.epub;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import writer2latex.api.ContentEntry;
import writer2latex.api.ConverterResult;
import writer2latex.api.OutputFile;
import writer2latex.util.Misc;
import writer2latex.xmerge.NewDOMDocument;
/** 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 NewDOMDocument {
public OPFWriter(ConverterResult cr, String sUUID, String sNcxFileName, String sFileName) {
super(Misc.removeExtension(sFileName), "opf");
// create DOM
Document contentDOM = null;
try {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
DOMImplementation domImpl = builder.getDOMImplementation();
DocumentType doctype = domImpl.createDocumentType("package","","");
contentDOM = domImpl.createDocument("http://www.idpf.org/2007/opf","package",doctype);
}
catch (ParserConfigurationException t) { // this should never happen
throw new RuntimeException(t);
}
// Populate the DOM tree
Element pack = contentDOM.getDocumentElement();
pack.setAttribute("version", "2.0");
pack.setAttribute("xmlns","http://www.idpf.org/2007/opf");
pack.setAttribute("unique-identifier", "BookId");
// Meta data, at least dc:title, dc:language and dc:identifier are required by the specification
Element metadata = contentDOM.createElement("metadata");
metadata.setAttribute("xmlns:dc", "http://purl.org/dc/elements/1.1/");
metadata.setAttribute("xmlns:opf", "http://www.idpf.org/2007/opf");
pack.appendChild(metadata);
Element title = contentDOM.createElement("dc:title");
metadata.appendChild(title);
title.appendChild(contentDOM.createTextNode(cr.getMetaData().getTitle()));
Element language = contentDOM.createElement("dc:language");
metadata.appendChild(language);
language.appendChild(contentDOM.createTextNode(cr.getMetaData().getLanguage()));
Element identifier = contentDOM.createElement("dc:identifier");
identifier.setAttribute("id", "BookId");
identifier.setAttribute("opf:scheme", "UUID");
metadata.appendChild(identifier);
identifier.appendChild(contentDOM.createTextNode(sUUID));
// Manifest must contain references to all the files in the XHTML converter result
// Spine should contain references to all the master documents within the converter result
Element manifest = contentDOM.createElement("manifest");
pack.appendChild(manifest);
Element spine = contentDOM.createElement("spine");
spine.setAttribute("toc", "ncx");
pack.appendChild(spine);
int nMasterCount = 0;
int nResourceCount = 0;
Iterator<OutputFile> iterator = cr.iterator();
while (iterator.hasNext()) {
OutputFile file = iterator.next();
Element item = contentDOM.createElement("item");
manifest.appendChild(item);
item.setAttribute("href",file.getFileName());
item.setAttribute("media-type", file.getMIMEType());
if (file.isMasterDocument()) {
String sId = "text"+(++nMasterCount);
item.setAttribute("id", sId);
Element itemref = contentDOM.createElement("itemref");
itemref.setAttribute("idref", sId);
spine.appendChild(itemref);
}
else {
item.setAttribute("id", "resource"+(++nResourceCount));
}
}
Element item = contentDOM.createElement("item");
item.setAttribute("href", sNcxFileName);
item.setAttribute("media-type", "application/x-dtbncx+xml");
item.setAttribute("id", "ncx");
manifest.appendChild(item);
// The guide may contain references to some fundamental structural components
Element guide = contentDOM.createElement("guide");
pack.appendChild(guide);
addGuideReference(contentDOM,guide,"toc",cr.getTocFile());
addGuideReference(contentDOM,guide,"index",cr.getIndexFile());
addGuideReference(contentDOM,guide,"loi",cr.getLofFile());
addGuideReference(contentDOM,guide,"lot",cr.getLotFile());
// TODO addGuideReference(contentDOM,guide,"bibliography",cr.getBibliographyile());
List<ContentEntry> contents = cr.getContent();
if (contents.size()>0) {
addGuideReference(contentDOM,guide,"text",contents.get(0));
}
setContentDOM(contentDOM);
}
private void addGuideReference(Document contentDOM, Element guide, String sType, ContentEntry entry) {
if (entry!=null) {
Element reference = contentDOM.createElement("reference");
reference.setAttribute("type", sType);
reference.setAttribute("title", entry.getTitle());
String sHref = entry.getFile().getFileName();
if (entry.getTarget()!=null) { sHref+="#"+entry.getTarget(); }
reference.setAttribute("href", sHref);
guide.appendChild(reference);
}
}
}