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;
}
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));
}
}
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."));
}
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."));
}
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());
}
Aggregations