Search in sources :

Example 16 with Item

use of org.eclipse.mylyn.docs.epub.opf.Item 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);
    }
}
Also used : Item(org.eclipse.mylyn.docs.epub.opf.Item) FileWriter(java.io.FileWriter) IOException(java.io.IOException) Itemref(org.eclipse.mylyn.docs.epub.opf.Itemref) File(java.io.File)

Example 17 with Item

use of org.eclipse.mylyn.docs.epub.opf.Item 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;
}
Also used : Item(org.eclipse.mylyn.docs.epub.opf.Item) Itemref(org.eclipse.mylyn.docs.epub.opf.Itemref)

Example 18 with Item

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

the class TestPublication method testGetItemsByMIMEType.

/**
 * Test method for {@link org.eclipse.mylyn.docs.epub.core.Publication#getItemsByMIMEType(java.lang.String)} .
 */
@Test
public final void testGetItemsByMIMEType() {
    Item i_in_1 = oebps.addItem(new File("testdata/images/subfolder-drawing.svg"));
    Item i_in_2 = oebps.addItem(new File("testdata/plain-page.xhtml"));
    List<Item> i_out_1 = oebps.getItemsByMIMEType("image/svg+xml");
    assertEquals(1, i_out_1.size());
    assertEquals(i_in_1, i_out_1.get(0));
    List<Item> i_out_2 = oebps.getItemsByMIMEType("application/xhtml+xml");
    assertEquals(1, i_out_2.size());
    assertEquals(i_in_2, i_out_2.get(0));
}
Also used : Item(org.eclipse.mylyn.docs.epub.opf.Item) File(java.io.File) Test(org.junit.Test)

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