Polyglossia + EPUB meta data
git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@80 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
parent
ea5d8679c9
commit
e02f738e85
20 changed files with 727 additions and 520 deletions
|
@ -20,7 +20,7 @@
|
|||
*
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* version 1.2 (2010-03-28)
|
||||
* version 1.2 (2010-12-15)
|
||||
*
|
||||
*/
|
||||
|
||||
|
@ -48,7 +48,7 @@ public final class EPUBConverter extends Xhtml11Converter {
|
|||
ConverterResult xhtmlResult = super.convert(is, sTargetFileName);
|
||||
|
||||
ConverterResultImpl epubResult = new ConverterResultImpl();
|
||||
epubResult.addDocument(new EPUBWriter(xhtmlResult,sTargetFileName));
|
||||
epubResult.addDocument(new EPUBWriter(xhtmlResult,sTargetFileName,getXhtmlConfig()));
|
||||
epubResult.setMetaData(xhtmlResult.getMetaData());
|
||||
return epubResult;
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
*
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* version 1.2 (2010-03-31)
|
||||
* version 1.2 (2010-12-15)
|
||||
*
|
||||
*/
|
||||
|
||||
|
@ -37,6 +37,7 @@ import java.util.zip.ZipOutputStream;
|
|||
import writer2latex.api.ConverterResult;
|
||||
import writer2latex.api.OutputFile;
|
||||
import writer2latex.util.Misc;
|
||||
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
|
||||
|
@ -49,10 +50,12 @@ public class EPUBWriter implements OutputFile {
|
|||
|
||||
private ConverterResult xhtmlResult;
|
||||
private String sFileName;
|
||||
private XhtmlConfig config;
|
||||
|
||||
public EPUBWriter(ConverterResult xhtmlResult, String sFileName) {
|
||||
public EPUBWriter(ConverterResult xhtmlResult, String sFileName, XhtmlConfig config) {
|
||||
this.xhtmlResult = xhtmlResult;
|
||||
this.sFileName = Misc.removeExtension(sFileName);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public String getFileName() {
|
||||
|
@ -90,7 +93,7 @@ public class EPUBWriter implements OutputFile {
|
|||
zos.closeEntry();
|
||||
|
||||
// Then manifest
|
||||
OutputFile manifest = new OPFWriter(xhtmlResult, sUUID);
|
||||
OutputFile manifest = new OPFWriter(xhtmlResult, sUUID, config.xhtmlUseDublinCore());
|
||||
ZipEntry manifestEntry = new ZipEntry("OEBPS/book.opf");
|
||||
zos.putNextEntry(manifestEntry);
|
||||
writeZipEntry(manifest,zos);
|
||||
|
|
|
@ -20,13 +20,14 @@
|
|||
*
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* version 1.2 (2010-07-02)
|
||||
* version 1.2 (2010-12-16)
|
||||
*
|
||||
*/
|
||||
|
||||
package writer2latex.epub;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
@ -47,7 +48,7 @@ import writer2latex.xmerge.NewDOMDocument;
|
|||
*/
|
||||
public class OPFWriter extends NewDOMDocument {
|
||||
|
||||
public OPFWriter(ConverterResult cr, String sUUID) {
|
||||
public OPFWriter(ConverterResult cr, String sUUID, boolean bUseDublinCore) {
|
||||
super("book", "opf");
|
||||
|
||||
// create DOM
|
||||
|
@ -83,11 +84,139 @@ public class OPFWriter extends NewDOMDocument {
|
|||
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));
|
||||
// Additional meta data
|
||||
if (bUseDublinCore) {
|
||||
// Subject and keywords in ODF both map to Dublin core subjects
|
||||
if (cr.getMetaData().getSubject().length()>0) {
|
||||
Element subject = contentDOM.createElement("dc:subject");
|
||||
metadata.appendChild(subject);
|
||||
subject.appendChild(contentDOM.createTextNode(cr.getMetaData().getSubject()));
|
||||
}
|
||||
if (cr.getMetaData().getKeywords().length()>0) {
|
||||
String[] sKeywords = cr.getMetaData().getKeywords().split(",");
|
||||
for (String sKeyword : sKeywords) {
|
||||
Element subject = contentDOM.createElement("dc:subject");
|
||||
metadata.appendChild(subject);
|
||||
subject.appendChild(contentDOM.createTextNode(sKeyword.trim()));
|
||||
}
|
||||
}
|
||||
if (cr.getMetaData().getDescription().length()>0) {
|
||||
Element description = contentDOM.createElement("dc:description");
|
||||
metadata.appendChild(description);
|
||||
description.appendChild(contentDOM.createTextNode(cr.getMetaData().getDescription()));
|
||||
}
|
||||
}
|
||||
|
||||
// User defined meta data
|
||||
// The identifier, creator, contributor and date has an optional attribute and there may be multiple instances of
|
||||
// the first three. The key can be in any of the forms name, name.attribute, name.attribute.id, name..id
|
||||
// where the id is some unique id amongst the instances with the same name
|
||||
// Thus you can have e.g. creator.aut.1="John Doe" and creator.aut.2="Jane Doe"
|
||||
boolean bHasIdentifier = false;
|
||||
boolean bHasCreator = false;
|
||||
boolean bHasDate = false;
|
||||
Map<String,String> userDefined = cr.getMetaData().getUserDefinedMetaData();
|
||||
for (String sKey : userDefined.keySet()) {
|
||||
if (sKey.length()>0) {
|
||||
String[] sKeyElements = sKey.toLowerCase().split("\\.");
|
||||
String sValue = userDefined.get(sKey);
|
||||
if ("identifier".equals(sKeyElements[0])) {
|
||||
Element identifier = contentDOM.createElement("dc:identifier");
|
||||
identifier.setAttribute("id", "BookId");
|
||||
if (sKeyElements.length>1 && sKeyElements[1].length()>0) {
|
||||
identifier.setAttribute("opf:scheme", sKeyElements[1]);
|
||||
}
|
||||
metadata.appendChild(identifier);
|
||||
identifier.appendChild(contentDOM.createTextNode(sValue));
|
||||
bHasIdentifier = true;
|
||||
}
|
||||
else if ("creator".equals(sKeyElements[0])) {
|
||||
Element creator = contentDOM.createElement("dc:creator");
|
||||
if (sKeyElements.length>1 && sKeyElements[1].length()>0) {
|
||||
creator.setAttribute("opf:role", sKeyElements[1]);
|
||||
}
|
||||
metadata.appendChild(creator);
|
||||
creator.appendChild(contentDOM.createTextNode(sValue));
|
||||
bHasCreator = true;
|
||||
}
|
||||
else if ("contributor".equals(sKeyElements[0])) {
|
||||
Element contributor = contentDOM.createElement("dc:contributor");
|
||||
if (sKeyElements.length>1 && sKeyElements[1].length()>0) {
|
||||
contributor.setAttribute("opf:role", sKeyElements[1]);
|
||||
}
|
||||
metadata.appendChild(contributor);
|
||||
contributor.appendChild(contentDOM.createTextNode(sValue));
|
||||
}
|
||||
else if ("date".equals(sKeyElements[0])) {
|
||||
Element date = contentDOM.createElement("dc:date");
|
||||
if (sKeyElements.length>1 && sKeyElements[1].length()>0) {
|
||||
date.setAttribute("opf:event", sKeyElements[1]);
|
||||
}
|
||||
metadata.appendChild(date);
|
||||
date.appendChild(contentDOM.createTextNode(sValue));
|
||||
bHasDate = true;
|
||||
}
|
||||
else if (sKeyElements.length==1) {
|
||||
// Remaining meta data elements must be unique
|
||||
if ("publisher".equals(sKeyElements[0])) {
|
||||
Element publisher = contentDOM.createElement("dc:publisher");
|
||||
metadata.appendChild(publisher);
|
||||
publisher.appendChild(contentDOM.createTextNode(sValue));
|
||||
}
|
||||
else if ("type".equals(sKeyElements[0])) {
|
||||
Element type = contentDOM.createElement("dc:type");
|
||||
metadata.appendChild(type);
|
||||
type.appendChild(contentDOM.createTextNode(sValue));
|
||||
}
|
||||
else if ("format".equals(sKeyElements[0])) {
|
||||
Element format = contentDOM.createElement("dc:format");
|
||||
metadata.appendChild(format);
|
||||
format.appendChild(contentDOM.createTextNode(sValue));
|
||||
}
|
||||
else if ("source".equals(sKeyElements[0])) {
|
||||
Element source = contentDOM.createElement("dc:source");
|
||||
metadata.appendChild(source);
|
||||
source.appendChild(contentDOM.createTextNode(sValue));
|
||||
}
|
||||
else if ("relation".equals(sKeyElements[0])) {
|
||||
Element relation = contentDOM.createElement("dc:relation");
|
||||
metadata.appendChild(relation);
|
||||
relation.appendChild(contentDOM.createTextNode(sValue));
|
||||
}
|
||||
else if ("coverage".equals(sKeyElements[0])) {
|
||||
Element coverage = contentDOM.createElement("dc:coverage");
|
||||
metadata.appendChild(coverage);
|
||||
coverage.appendChild(contentDOM.createTextNode(sValue));
|
||||
}
|
||||
else if ("rights".equals(sKeyElements[0])) {
|
||||
Element rights = contentDOM.createElement("dc:rights");
|
||||
metadata.appendChild(rights);
|
||||
rights.appendChild(contentDOM.createTextNode(sValue));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back values for creator and date
|
||||
if (bUseDublinCore) {
|
||||
if (!bHasIdentifier) {
|
||||
Element identifier = contentDOM.createElement("dc:identifier");
|
||||
identifier.setAttribute("id", "BookId");
|
||||
identifier.setAttribute("opf:scheme", "UUID");
|
||||
metadata.appendChild(identifier);
|
||||
identifier.appendChild(contentDOM.createTextNode(sUUID));
|
||||
}
|
||||
if (!bHasCreator && cr.getMetaData().getCreator().length()>0) {
|
||||
Element creator = contentDOM.createElement("dc:creator");
|
||||
metadata.appendChild(creator);
|
||||
creator.appendChild(contentDOM.createTextNode(cr.getMetaData().getCreator()));
|
||||
}
|
||||
if (!bHasDate && cr.getMetaData().getDate().length()>0) {
|
||||
Element date = contentDOM.createElement("dc:date");
|
||||
metadata.appendChild(date);
|
||||
date.appendChild(contentDOM.createTextNode(cr.getMetaData().getDate()));
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue