Search in sources :

Example 11 with Item

use of org.eclipse.mylyn.docs.epub.opf.Item in project mylyn.docs by eclipse.

the class OPSPublication method setTableOfContents.

@Override
public void setTableOfContents(File ncxFile) {
    // Add the file to the publication and make sure we use the table of
    // contents identifier.
    Item item = addItem(opfPackage.getSpine().getToc(), null, ncxFile, null, MIMETYPE_NCX, false, false, false);
    // The table of contents file must be first.
    opfPackage.getManifest().getItems().move(0, item);
    log(// $NON-NLS-1$
    MessageFormat.format("Using table of contents file {0} for OPS", new Object[] { ncxFile.getName() }), Severity.VERBOSE, indent);
}
Also used : Item(org.eclipse.mylyn.docs.epub.opf.Item)

Example 12 with Item

use of org.eclipse.mylyn.docs.epub.opf.Item in project mylyn.docs by eclipse.

the class OPSPublication method writeTableOfContents.

/**
 * Writes the table of contents file in the specified folder using the NCX format. If a table of contents file has
 * not been specified an empty one will be created (since it is required to have one). If in addition it has been
 * specified that the table of contents should be created, the content files will be parsed and a TOC will be
 * generated.
 *
 * @param oepbsFolder
 *            the folder to create the NCX file in
 * @throws IOException
 * @throws SAXException
 * @throws ParserConfigurationException
 * @see {@link #setTableOfContents(File)}
 */
@Override
protected void writeTableOfContents(File oepbsFolder) throws IOException, ParserConfigurationException, SAXException {
    // one. If it has been specified it will be copied.
    if (getItemById(opfPackage.getSpine().getToc()) == null) {
        configureNCX();
        File ncxFile = new File(oepbsFolder.getAbsolutePath() + File.separator + TOCFILE_NAME);
        ResourceSet resourceSet = new ResourceSetImpl();
        // Register the packages to make it available during loading.
        resourceSet.getPackageRegistry().put(NCXPackage.eNS_URI, NCXPackage.eINSTANCE);
        URI fileURI = URI.createFileURI(ncxFile.getAbsolutePath());
        Resource resource = resourceSet.createResource(fileURI);
        // contained in the spine.
        if (opfPackage.isGenerateTableOfContents()) {
            generateTableOfContents();
        }
        resource.getContents().add(ncxTOC);
        Map<String, Object> options = new HashMap<String, Object>();
        // NCX requires that we encode using UTF-8
        options.put(XMLResource.OPTION_ENCODING, XML_ENCODING);
        options.put(XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE);
        resource.save(options);
        // Make sure the table of contents file is in the manifest and
        // referenced in the spine. We also want it to be the first element
        // in the manifest.
        Item item = addItem(opfPackage.getSpine().getToc(), null, ncxFile, null, MIMETYPE_NCX, false, false, false);
        opfPackage.getManifest().getItems().move(0, item);
    }
}
Also used : Item(org.eclipse.mylyn.docs.epub.opf.Item) ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) HashMap(java.util.HashMap) XMLResource(org.eclipse.emf.ecore.xmi.XMLResource) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) File(java.io.File) URI(org.eclipse.emf.common.util.URI)

Example 13 with Item

use of org.eclipse.mylyn.docs.epub.opf.Item in project mylyn.docs by eclipse.

the class OPSPublication method setCover.

/**
 * Convenience method for adding a cover to the publication. This method will make sure the required actions are
 * taken to provide a cover page for all reading systems.
 *
 * @param image
 *            the cover image (jpeg, png, svg or gif)
 * @param title
 *            title of the cover page
 */
@Override
public void setCover(File image, String title) {
    // Add the cover image to the manifest
    Item item = addItem(COVER_IMAGE_ID, null, image, null, null, false, false, true);
    item.setTitle(title);
    // Point to the cover using a meta tag
    addMeta(COVER_ID, COVER_IMAGE_ID);
    opfPackage.setGenerateCoverHTML(true);
}
Also used : Item(org.eclipse.mylyn.docs.epub.opf.Item)

Example 14 with Item

use of org.eclipse.mylyn.docs.epub.opf.Item in project mylyn.docs by eclipse.

the class Publication method writeContent.

/**
 * Copies all items part of the publication into the OEPBS folder unless the item in question will be generated.
 *
 * @param rootFolder
 *            the folder to copy into.
 * @throws IOException
 */
private void writeContent(File rootFolder) throws IOException {
    // $NON-NLS-1$
    log(Messages.getString("OPSPublication.22"), Severity.INFO, indent);
    EList<Item> items = opfPackage.getManifest().getItems();
    for (Item item : items) {
        if (!item.isGenerated()) {
            File source = new File(item.getFile());
            File destination = new File(rootFolder.getAbsolutePath() + File.separator + item.getHref());
            if (!EPUBFileUtil.copy(source, destination)) {
                log(// $NON-NLS-1$
                MessageFormat.format(// $NON-NLS-1$
                Messages.getString("Publication.0"), item.getHref()), Severity.WARNING, indent + 1);
            }
        }
    }
}
Also used : Item(org.eclipse.mylyn.docs.epub.opf.Item) File(java.io.File)

Example 15 with Item

use of org.eclipse.mylyn.docs.epub.opf.Item in project mylyn.docs by eclipse.

the class Publication method includeReferencedResources.

/**
 * Iterates over all XHTML (non-generated) files in the manifest attempting to determine referenced resources such
 * as image files and adds these to the manifest. This method is not recursive, so items added through this
 * mechanism will not be searched for more content to add.
 *
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
private void includeReferencedResources() throws ParserConfigurationException, SAXException, IOException {
    // $NON-NLS-1$
    log(Messages.getString("OPSPublication.23"), Severity.INFO, indent++);
    EList<Item> manifestItems = opfPackage.getManifest().getItems();
    // Compose a list of file references
    HashMap<File, List<File>> references = new HashMap<File, List<File>>();
    for (Item item : manifestItems) {
        // Only parse XHTML-files and files that are not generated
        if (item.getMedia_type().equals(MIMETYPE_XHTML) && !item.isGenerated()) {
            File source = null;
            if (item.getSourcePath() != null) {
                source = new File(item.getSourcePath());
                log(// $NON-NLS-1$
                MessageFormat.format(Messages.getString("OPSPublication.24"), source), // $NON-NLS-1$
                Severity.VERBOSE, indent);
                references.put(source, ReferenceScanner.parse(item));
            } else {
                source = new File(item.getFile());
                log(// $NON-NLS-1$
                MessageFormat.format(Messages.getString("OPSPublication.25"), source), // $NON-NLS-1$
                Severity.VERBOSE, indent);
            }
            references.put(source, ReferenceScanner.parse(item));
        } else {
            log(// $NON-NLS-1$
            MessageFormat.format(Messages.getString("OPSPublication.26"), item.getFile()), // $NON-NLS-1$
            Severity.DEBUG, indent);
        }
    }
    indent--;
    // Add all referenced items to the manifest
    for (File root : references.keySet()) {
        List<File> files = references.get(root);
        for (File file : files) {
            // Determine a sub-folder to place the resource in.
            File relativePath = new File(EPUBFileUtil.getRelativePath(root, file));
            // same folder as "root".
            if (relativePath.isAbsolute()) {
                relativePath = new File(EMPTY_STRING);
            }
            // Add those files that does not already exist in the manifest
            boolean add = true;
            EList<Item> items = getPackage().getManifest().getItems();
            for (Item item : items) {
                if (item.getFile().equals(file.getAbsolutePath())) {
                    add = false;
                }
            }
            if (add) {
                try {
                    addItem(null, null, file, relativePath.getParent(), null, false, false, false);
                } catch (Exception e) {
                    throw new RuntimeException(// $NON-NLS-1$
                    String.format(// $NON-NLS-1$
                    "Could not add file referenced from \"%1$s\", %2$s", // $NON-NLS-1$
                    root, e.getMessage()), e);
                }
            }
        }
    }
    indent--;
}
Also used : Item(org.eclipse.mylyn.docs.epub.opf.Item) HashMap(java.util.HashMap) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

Item (org.eclipse.mylyn.docs.epub.opf.Item)18 File (java.io.File)13 ArrayList (java.util.ArrayList)3 Itemref (org.eclipse.mylyn.docs.epub.opf.Itemref)3 Test (org.junit.Test)3 FileReader (java.io.FileReader)2 FileWriter (java.io.FileWriter)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 ValidationMessage (org.eclipse.mylyn.docs.epub.core.ValidationMessage)2 InputSource (org.xml.sax.InputSource)2 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 Writer (java.io.Writer)1 List (java.util.List)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 EList (org.eclipse.emf.common.util.EList)1 URI (org.eclipse.emf.common.util.URI)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1