use of org.eclipse.mylyn.docs.epub.opf.Itemref in project mylyn.docs by eclipse.
the class OPSPublication method generateTableOfContents.
/**
* This mechanism will traverse the spine of the publication (which is representing the reading order) and parse
* each file for information that can be used to assemble a table of contents. Only XHTML type of files will be
* taken into consideration.
*
* @throws SAXException
* @throws IOException
* @throws ParserConfigurationException
*/
@Override
protected void generateTableOfContents() throws ParserConfigurationException, SAXException, IOException {
// $NON-NLS-1$
log(Messages.getString("OPS2Publication.0"), Severity.INFO, indent++);
Meta meta = NCXFactory.eINSTANCE.createMeta();
// $NON-NLS-1$
meta.setName("dtb:uid");
meta.setContent(getIdentifier().getMixed().getValue(0).toString());
ncxTOC.getHead().getMetas().add(meta);
int playOrder = 0;
// Iterate over the spine
EList<Itemref> spineItems = getSpine().getSpineItems();
EList<Item> manifestItems = opfPackage.getManifest().getItems();
for (Itemref itemref : spineItems) {
Item referencedItem = null;
String id = itemref.getIdref();
// Find the manifest item that is referenced
for (Item item : manifestItems) {
if (item.getId().equals(id)) {
referencedItem = item;
break;
}
}
if (referencedItem != null && !referencedItem.isNoToc() && referencedItem.getMedia_type().equals(MIMETYPE_XHTML)) {
File file = new File(referencedItem.getFile());
FileInputStream fis = new FileInputStream(file);
log(// $NON-NLS-1$
MessageFormat.format(Messages.getString("OPS2Publication.1"), referencedItem.getHref()), Severity.VERBOSE, indent);
playOrder = TOCGenerator.parse(new InputSource(fis), referencedItem.getHref(), ncxTOC, playOrder);
}
}
indent--;
}
use of org.eclipse.mylyn.docs.epub.opf.Itemref in project mylyn.docs by eclipse.
the class Publication method writeCoverHTML.
/**
* Writes a XHTML-file for the cover image. This is added to the publication and all required references set.
*
* @param rootFolder
* the publication root folder
* @throws IOException
*/
private void writeCoverHTML(File rootFolder) throws IOException {
Item coverImage = getItemById(COVER_IMAGE_ID);
// $NON-NLS-1$
File coverFile = new File(rootFolder.getAbsolutePath() + File.separator + "cover-page.xhtml");
if (!coverFile.exists()) {
try {
log(// $NON-NLS-1$
MessageFormat.format(Messages.getString("OPSPublication.28"), coverImage.getHref()), // $NON-NLS-1$
Severity.INFO, indent);
FileWriter fw = new FileWriter(coverFile);
// $NON-NLS-1$
fw.append("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n");
fw.append(// $NON-NLS-1$
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n");
// $NON-NLS-1$
fw.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">\n");
// $NON-NLS-1$
fw.append(" <head>\n");
// $NON-NLS-1$ //$NON-NLS-2$
fw.append(" <title>" + coverImage.getTitle() + "</title>\n");
// $NON-NLS-1$
fw.append(" <style type=\"text/css\">");
// $NON-NLS-1$
fw.append(" #cover-body {\n");
// $NON-NLS-1$
fw.append(" margin: 0px;\n");
// $NON-NLS-1$
fw.append(" text-align: center;\n");
// $NON-NLS-1$
fw.append(" background-color: #222222;\n");
// $NON-NLS-1$
fw.append(" }\n");
// $NON-NLS-1$
fw.append(" #cover-block {\n");
// $NON-NLS-1$
fw.append(" height: 100%;\n");
// $NON-NLS-1$
fw.append(" margin-top: 0;\n");
// $NON-NLS-1$
fw.append(" }\n");
// $NON-NLS-1$
fw.append(" #cover-image {\n");
// $NON-NLS-1$
fw.append(" height: 100%;\n");
// $NON-NLS-1$
fw.append(" text-align: center;\n");
// $NON-NLS-1$
fw.append(" max-width: 100%;\n");
// $NON-NLS-1$
fw.append(" }\n");
// $NON-NLS-1$
fw.append(" </style>\n");
// $NON-NLS-1$
fw.append(" </head>\n");
// $NON-NLS-1$
fw.append(" <body id=\"cover-body\">\n");
// $NON-NLS-1$
fw.append(" <div id=\"cover-block\">\n");
fw.append(// $NON-NLS-1$ //$NON-NLS-2$
" <img id=\"cover-image\" src=\"" + coverImage.getHref() + "\" alt=\"" + coverImage.getTitle() + // $NON-NLS-1$
"\"/>\n");
// $NON-NLS-1$
fw.append(" </div>\n");
// $NON-NLS-1$
fw.append(" </body>\n");
// $NON-NLS-1$
fw.append("</html>\n");
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// Add the cover page item
Item coverPage = addItem(COVER_ID, null, coverFile, null, MIMETYPE_XHTML, true, false, false);
coverPage.setGenerated(true);
addReference(coverPage.getHref(), coverImage.getTitle(), Type.COVER.getLiteral());
// Move the cover page first in the spine.
EList<Itemref> spine = opfPackage.getSpine().getSpineItems();
Itemref cover = null;
for (Itemref itemref : spine) {
if (itemref.getIdref().equals(COVER_ID)) {
cover = itemref;
}
}
if (cover != null) {
spine.move(0, cover);
}
}
use of org.eclipse.mylyn.docs.epub.opf.Itemref in project mylyn.docs by eclipse.
the class Publication method addItem.
/**
* Adds a new item to the manifest. If an identifier is not specified it will automatically be assigned.
* <p>
* The <i>spine</i> defines the reading order, so the order items are added and whether or not <i>spine</i> is
* <code>true</code> does matter. Unless a table of contents file has been specified it will be generated. All files
* that have been added to the spine will be examined unless the <i>noToc</i> attribute has been set to
* <code>true</code>.
* </p>
*
* @param file
* the file to add
* @param dest
* the destination sub-folder or <code>null</code>
* @param id
* identifier or <code>null</code>
* @param type
* MIME file type
* @param spine
* whether or not to add the item to the spine
* @param linear
* whether or not the item is part of the reading order
* @param noToc
* whether or not to include in TOC when automatically generated
* @return the new item
*/
public Item addItem(String id, Locale lang, File file, String dest, String type, boolean spine, boolean linear, boolean noToc) {
if (file == null) {
// $NON-NLS-1$
throw new IllegalArgumentException("\"file\" must be specified");
}
if (!file.exists()) {
// $NON-NLS-1$ //$NON-NLS-2$
throw new IllegalArgumentException("\"file\" " + file.getAbsolutePath() + " must exist.");
}
if (file.isDirectory()) {
// $NON-NLS-1$ //$NON-NLS-2$
throw new IllegalArgumentException("\"file\" " + file.getAbsolutePath() + " must not be a directory.");
}
Item item = OPFFactory.eINSTANCE.createItem();
// Attempt to determine the type of file if none has been specified
if (type == null) {
type = EPUBFileUtil.getMimeType(file);
if (type == null) {
throw new IllegalArgumentException(// $NON-NLS-1$
"Could not automatically determine MIME type for file " + file + // $NON-NLS-1$
". Please specify the correct value");
}
}
// Assign the required identifier if none has been specified
if (id == null) {
String prefix = EMPTY_STRING;
if (!type.equals(MIMETYPE_XHTML)) {
prefix = (type.indexOf('/')) == -1 ? type : type.substring(0, type.indexOf('/')) + '-';
}
id = prefix + file.getName().substring(0, file.getName().lastIndexOf('.'));
}
item.setId(id);
if (dest == null) {
item.setHref(file.getName());
} else {
item.setHref(dest + '/' + file.getName());
}
item.setNoToc(noToc);
item.setMedia_type(type);
item.setFile(file.getAbsolutePath());
log(// $NON-NLS-1$
MessageFormat.format(Messages.getString("OPSPublication.8"), item.getHref(), item.getMedia_type()), Severity.VERBOSE, indent);
opfPackage.getManifest().getItems().add(item);
if (spine) {
Itemref ref = OPFFactory.eINSTANCE.createItemref();
if (!linear) {
// $NON-NLS-1$
ref.setLinear("no");
}
ref.setIdref(id);
getSpine().getSpineItems().add(ref);
}
return item;
}
Aggregations