Search in sources :

Example 6 with Item

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

the class MarkupToOPS method parse.

/**
 * Parses the markup file and populates the publication with the result.
 *
 * @param ops
 *            the publication the content will be added to
 * @param markupFile
 *            the WikiText markup file
 * @return the temporary folder used for generating the HTML from markup
 * @since 2.0
 */
public File parse(Publication ops, File markupFile) throws IOException, FileNotFoundException {
    if (markupLanguage == null) {
        // $NON-NLS-1$
        throw new IllegalStateException("must set markupLanguage");
    }
    // Create a temporary working folder
    // $NON-NLS-1$
    File workingFolder = File.createTempFile("wikitext_", null);
    if (workingFolder.delete() && workingFolder.mkdirs()) {
        // $NON-NLS-1$
        File htmlFile = new File(workingFolder.getAbsolutePath() + File.separator + "markup.html");
        FileWriter out = new FileWriter(htmlFile);
        HtmlDocumentBuilder builder = new HtmlDocumentBuilder(out) {

            @Override
            protected XmlStreamWriter createXmlStreamWriter(Writer out) {
                return super.createFormattingXmlStreamWriter(out);
            }
        };
        List<Item> stylesheets = ops.getItemsByMIMEType(Publication.MIMETYPE_CSS);
        for (Item item : stylesheets) {
            File file = new File(item.getFile());
            Stylesheet css = new Stylesheet(file);
            builder.addCssStylesheet(css);
        }
        // Make sure we get the correct XHTML header
        builder.setEmitDtd(true);
        builder.setHtmlDtd(// $NON-NLS-1$
        "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
        builder.setXhtmlStrict(true);
        MarkupParser markupParser = new MarkupParser();
        markupParser.setBuilder(builder);
        markupParser.setMarkupLanguage(markupLanguage);
        markupParser.parse(new FileReader(markupFile));
        ops.setGenerateToc(true);
        ops.setIncludeReferencedResources(true);
        Item item = ops.addItem(htmlFile);
        item.setSourcePath(markupFile.getAbsolutePath());
    }
    return workingFolder;
}
Also used : Item(org.eclipse.mylyn.docs.epub.opf.Item) FileWriter(java.io.FileWriter) FileReader(java.io.FileReader) File(java.io.File) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder) XmlStreamWriter(org.eclipse.mylyn.wikitext.util.XmlStreamWriter) FileWriter(java.io.FileWriter) Writer(java.io.Writer) Stylesheet(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder.Stylesheet) MarkupParser(org.eclipse.mylyn.wikitext.parser.MarkupParser)

Example 7 with Item

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

the class EclipseTocImporter method importTocs.

private static void importTocs(Publication oebps, File rootFile, Node root) throws ParserConfigurationException, SAXException, IOException, DOMException, URISyntaxException {
    NodeList childNodes = root.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        // Link to another table of contents
        NamedNodeMap attributes = node.getAttributes();
        if ("link".equals(node.getNodeName())) {
            // $NON-NLS-1$
            if (attributes != null) {
                // $NON-NLS-1$
                Node toc = attributes.getNamedItem("toc");
                File tocFile = new File(rootFile.getParentFile(), toc.getNodeValue());
                importFile(oebps, rootFile, tocFile);
            }
        }
        if ("topic".equals(node.getNodeName())) {
            // $NON-NLS-1$
            if (attributes != null) {
                // $NON-NLS-1$
                Node href = attributes.getNamedItem("href");
                if (href != null) {
                    String nodeValue = href.getNodeValue();
                    if (nodeValue.contains("#")) {
                        // $NON-NLS-1$
                        // $NON-NLS-1$
                        nodeValue = nodeValue.substring(0, nodeValue.lastIndexOf("#"));
                    }
                    File hrefFile = new File(rootFile.getParentFile(), nodeValue);
                    // Determine whether or not the file is already
                    // present. We expect there to be no other files with
                    // the same name already in the manifest.
                    EList<Item> items = oebps.getPackage().getManifest().getItems();
                    boolean found = false;
                    for (Item item : items) {
                        if (item.getFile() != null) {
                            File t = new File(item.getFile());
                            if (t.getName().equals(hrefFile.getName())) {
                                found = true;
                            }
                        }
                    }
                    if (!found) {
                        oebps.addItem(hrefFile);
                    }
                }
            }
        }
        importTocs(oebps, rootFile, childNodes.item(i));
    }
}
Also used : Item(org.eclipse.mylyn.docs.epub.opf.Item) NamedNodeMap(org.w3c.dom.NamedNodeMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) File(java.io.File)

Example 8 with Item

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

the class TestOPSPublication method test_Bug358671_Illegal_Fallback.

/**
 * Test method for <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=358671">bug 358671</a>: Add support for
 * fallback items
 * <p>
 * This method tests for the exception that shall be raised when an illegal item has been added with an illegal
 * fallback item. Which fallback items that are allowed is specified by the OPS version.
 * </p>
 *
 * @throws Exception
 */
@Test
public final void test_Bug358671_Illegal_Fallback() throws Exception {
    epub.add(oebps);
    Item item = oebps.addItem(new File("testdata/OPF-Tests/Bug_358671/illegal-type.html"));
    item.setFallback("fallback");
    oebps.addItem("fallback", null, new File("testdata/OPF-Tests/Bug_358671/illegal-type.html"), null, null, true, true, false);
    epub.pack(epubFile);
    ValidationMessage msg = oebps.getValidationMessages().get(0);
    assertEquals(Severity.WARNING, msg.getSeverity());
    assertEquals(true, msg.getMessage().equals("Item \"illegal-type.html\" is not a core media type and specifies a non-core media fallback item."));
}
Also used : Item(org.eclipse.mylyn.docs.epub.opf.Item) ValidationMessage(org.eclipse.mylyn.docs.epub.core.ValidationMessage) File(java.io.File) Test(org.junit.Test)

Example 9 with Item

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

the class TestOPSPublication method test_Bug358671_Legal_Fallback.

/**
 * Test method for <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=358671">bug 358671</a>: Add support for
 * fallback items
 * <p>
 * This method tests for the warning that shall be issued when an illegal item has been added with a legal fallback
 * item.
 * </p>
 *
 * @throws Exception
 */
@Test
public final void test_Bug358671_Legal_Fallback() throws Exception {
    epub.add(oebps);
    Item item = oebps.addItem(new File("testdata/OPF-Tests/Bug_358671/illegal-type.html"));
    item.setFallback("fallback");
    oebps.addItem("fallback", null, new File("testdata/plain-page.xhtml"), null, null, true, true, false);
    epub.pack(epubFile);
    assertEquals(3, oebps.getPackage().getManifest().getItems().size());
    assertEquals(1, oebps.getValidationMessages().size());
    ValidationMessage msg = oebps.getValidationMessages().get(0);
    assertEquals(Severity.WARNING, msg.getSeverity());
    assertEquals(true, msg.getMessage().equals("Item \"illegal-type.html\" is not a core media type but a legal fallback item has been specified."));
}
Also used : Item(org.eclipse.mylyn.docs.epub.opf.Item) ValidationMessage(org.eclipse.mylyn.docs.epub.core.ValidationMessage) File(java.io.File) Test(org.junit.Test)

Example 10 with Item

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

the class TestPublication method test_Bug376312.

/**
 * Test method for <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=376312">bug 376312</a>: [epub] Automatic
 * inclusion of detected resources may add the same resource twice or more
 * <p>
 * File A references file C as do file B. File C references file A. Before the fix there would be two instances of
 * file C.
 * </p>
 *
 * @throws Exception
 */
public final void test_Bug376312() throws Exception {
    oebps.setIncludeReferencedResources(true);
    oebps.addItem(new File("testdata/circular/file-a.xhtml"));
    oebps.addItem(new File("testdata/circular/file-b.xhtml"));
    epub.add(oebps);
    epub.pack(epubFile);
    EList<Item> items = oebps.getPackage().getManifest().getItems();
    // File A, B, C and the NCX
    assertEquals(4, items.size());
}
Also used : Item(org.eclipse.mylyn.docs.epub.opf.Item) File(java.io.File)

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