Search in sources :

Example 1 with RootFile

use of org.eclipse.mylyn.docs.epub.ocf.RootFile in project mylyn.docs by eclipse.

the class EPUB method unpack.

/**
 * Unpacks the given EPUB file into the specified destination and populates the data model with the content. Note
 * that when the destination folder already exists or is empty the file EPUB will not be unpacked or verified, but
 * the contents of the destination will be treated as an already unpacked EPUB. If this behaviour is not desired one
 * should take steps to delete the folder prior to unpacking.
 * <p>
 * When performing the unpacking, the modification date of the destination folder will be set to the modification
 * date of the source EPUB. Additionally the contents of the EPUB will retain the original modification date if set.
 * </p>
 * <p>
 * Multiple OPS root files in the publication will populate the OCF container instance with one {@link Publication}
 * for each as expected. The contents of the data model starting with the OCF container will be replaced. If the
 * publication is in an unsupported version it will not be added to the data model.
 * </p>
 *
 * @param epubFile
 *            the EPUB file to unpack
 * @param rootFolder
 *            the destination folder
 * @throws Exception
 * @see {@link #unpack(File)} when destination is not interesting
 * @see {@link #getContainer()} to obtain the container instance
 * @see {@link #getOPSPublications()} to get a list of all contained OPS publications
 */
public void unpack(File epubFile, File rootFolder) throws Exception {
    if (!isEPUB(epubFile)) {
        // $NON-NLS-1$
        throw new IllegalArgumentException(MessageFormat.format("{0} is not an EPUB file", epubFile));
    }
    if (!rootFolder.exists() || rootFolder.list().length == 0) {
        EPUBFileUtil.unzip(epubFile, rootFolder);
    }
    readOCF(rootFolder);
    EList<RootFile> rootFiles = ocfContainer.getRootfiles().getRootfiles();
    for (RootFile rootFile : rootFiles) {
        if (rootFile.getMediaType().equals(MIMETYPE_OEBPS)) {
            File root = new File(rootFolder.getAbsolutePath() + File.separator + rootFile.getFullPath());
            switch(readPublicationVersion(root)) {
                case V2:
                    Publication ops2 = Publication.getVersion2Instance(logger);
                    ops2.unpack(root);
                    rootFile.setPublication(ops2);
                    break;
                case V3:
                    Publication ops3 = Publication.getVersion3Instance();
                    ops3.unpack(root);
                    rootFile.setPublication(ops3);
                    break;
                default:
                    log(// $NON-NLS-1$
                    MessageFormat.format("Unsupported OEBPS version in root file {0}", rootFile.getFullPath()), Severity.WARNING);
                    break;
            }
        }
    }
}
Also used : RootFile(org.eclipse.mylyn.docs.epub.ocf.RootFile) RootFile(org.eclipse.mylyn.docs.epub.ocf.RootFile) File(java.io.File)

Example 2 with RootFile

use of org.eclipse.mylyn.docs.epub.ocf.RootFile in project mylyn.docs by eclipse.

the class TestEPUB method testPackFail.

/**
 * Test method for {@link org.eclipse.mylyn.docs.epub.core.EPUB#pack(java.io.File)}.
 * <ul>
 * <li>Shall throw exception when unknown publication type is added.</li>
 * </ul>
 *
 * @throws Exception
 */
@Test
public final void testPackFail() throws Exception {
    EPUB epub = new EPUB();
    File drawing = new File("testdata/drawing.svg");
    epub.add(drawing, "image/svg+xml");
    Container container = epub.getContainer();
    RootFiles rootfiles = container.getRootfiles();
    EList<RootFile> files = rootfiles.getRootfiles();
    files.get(0).setPublication(null);
    try {
        epub.pack(epubFile);
        fail();
    } catch (Exception e) {
    }
}
Also used : Container(org.eclipse.mylyn.docs.epub.ocf.Container) EPUB(org.eclipse.mylyn.docs.epub.core.EPUB) RootFiles(org.eclipse.mylyn.docs.epub.ocf.RootFiles) RootFile(org.eclipse.mylyn.docs.epub.ocf.RootFile) RootFile(org.eclipse.mylyn.docs.epub.ocf.RootFile) File(java.io.File) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with RootFile

use of org.eclipse.mylyn.docs.epub.ocf.RootFile in project mylyn.docs by eclipse.

the class TestEPUB method testAddOPSPublication.

/**
 * Test method for {@link org.eclipse.mylyn.docs.epub.core.EPUB#add(org.eclipse.mylyn.docs.epub.core.Publication)} .
 * <ul>
 * <li>Container shall hold more than one OPS publication</li>
 * <li>OPS structures shall follow naming conventions.</li>
 * <li>OPS MIME-type shall be correct</li>
 * <li>Rootfile object shall be correct.</li>
 * </ul>
 *
 * @throws Exception
 */
@Test
public final void testAddOPSPublication() throws Exception {
    EPUB epub = new EPUB();
    Publication oebps1 = new OPSPublication();
    Publication oebps2 = new OPSPublication();
    epub.add(oebps1);
    epub.add(oebps2);
    Container container = epub.getContainer();
    RootFiles rootfiles = container.getRootfiles();
    EList<RootFile> files = rootfiles.getRootfiles();
    assertEquals(true, files.get(0).getFullPath().equals("OEBPS/content.opf"));
    assertEquals(true, files.get(1).getFullPath().equals("OEBPS_1/content.opf"));
    assertEquals(true, files.get(0).getMediaType().equals("application/oebps-package+xml"));
    assertEquals(true, files.get(1).getMediaType().equals("application/oebps-package+xml"));
    assertEquals(true, files.get(0).getPublication() == oebps1);
    assertEquals(true, files.get(1).getPublication() == oebps2);
}
Also used : Container(org.eclipse.mylyn.docs.epub.ocf.Container) EPUB(org.eclipse.mylyn.docs.epub.core.EPUB) Publication(org.eclipse.mylyn.docs.epub.core.Publication) OPSPublication(org.eclipse.mylyn.docs.epub.core.OPSPublication) RootFiles(org.eclipse.mylyn.docs.epub.ocf.RootFiles) OPSPublication(org.eclipse.mylyn.docs.epub.core.OPSPublication) RootFile(org.eclipse.mylyn.docs.epub.ocf.RootFile) Test(org.junit.Test)

Example 4 with RootFile

use of org.eclipse.mylyn.docs.epub.ocf.RootFile in project mylyn.docs by eclipse.

the class EPUB method add.

/**
 * Adds a new publication (or root file) to the EPUB. Use {@link #add(Publication)} when adding an OEBPS
 * publication.
 * <p>
 * Note that while an {@link EPUB} can technically contain multiple instances of an {@link Publication}, in practice
 * reading systems does not support this.
 * </p>
 *
 * @param file
 *            the publication to add
 * @param type
 *            the MIME type of the publication
 * @see #add(Publication)
 */
public void add(File file, String type) {
    String name = type.substring(type.lastIndexOf('/') + 1, type.length()).toUpperCase();
    RootFiles rootFiles = ocfContainer.getRootfiles();
    int count = rootFiles.getRootfiles().size();
    if (count >= 1) {
        // $NON-NLS-1$
        log("Multiple root files is unsupported by most reading systems!", Severity.WARNING);
    }
    // $NON-NLS-1$
    String rootFileName = count > 0 ? name + "_" + count : name;
    rootFileName += File.separator + file.getName();
    RootFile rootFile = OCFFactory.eINSTANCE.createRootFile();
    rootFile.setFullPath(rootFileName);
    rootFile.setMediaType(type);
    rootFile.setPublication(file);
    rootFiles.getRootfiles().add(rootFile);
    log(// $NON-NLS-1$
    MessageFormat.format(// $NON-NLS-1$
    Messages.getString("EPUB.1"), // $NON-NLS-1$
    rootFile.getFullPath(), rootFile.getMediaType()), Severity.VERBOSE);
}
Also used : RootFiles(org.eclipse.mylyn.docs.epub.ocf.RootFiles) RootFile(org.eclipse.mylyn.docs.epub.ocf.RootFile)

Example 5 with RootFile

use of org.eclipse.mylyn.docs.epub.ocf.RootFile in project mylyn.docs by eclipse.

the class EPUB method getOPSPublications.

/**
 * Returns a list of all <i>OPS publications</i> contained within the EPUB. Publications in unsupported versions
 * will not be returned. However their existence can still be determined by looking at the
 * {@link Container#getRootfiles()} result.
 *
 * @return a list of all OPS publications
 * @see {@link #getContainer()} for obtaining the root file container
 */
public List<Publication> getOPSPublications() {
    ArrayList<Publication> publications = new ArrayList<Publication>();
    EList<RootFile> rootFiles = ocfContainer.getRootfiles().getRootfiles();
    for (RootFile rootFile : rootFiles) {
        if (rootFile.getMediaType().equals(MIMETYPE_OEBPS)) {
            // May be null if the publications is in an unsupported format.
            if (rootFile.getPublication() != null) {
                publications.add((Publication) rootFile.getPublication());
            }
        }
    }
    return publications;
}
Also used : ArrayList(java.util.ArrayList) RootFile(org.eclipse.mylyn.docs.epub.ocf.RootFile)

Aggregations

RootFile (org.eclipse.mylyn.docs.epub.ocf.RootFile)7 RootFiles (org.eclipse.mylyn.docs.epub.ocf.RootFiles)5 File (java.io.File)3 EPUB (org.eclipse.mylyn.docs.epub.core.EPUB)3 Container (org.eclipse.mylyn.docs.epub.ocf.Container)3 Test (org.junit.Test)3 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 OPSPublication (org.eclipse.mylyn.docs.epub.core.OPSPublication)1 Publication (org.eclipse.mylyn.docs.epub.core.Publication)1