@Override public String format( String projectName, String path, String revision, String abbrRev, ConfigSection cfg, InputStream raw) throws IOException { // Docx4J tries to load some resources dynamically. This fails if the Gerrit // core classloader is used since it doesn't see the resources that are // contained in the plugin jar. To make the resource loading work we // must set a context classloader on the current thread. ClassLoader loader = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); WordprocessingMLPackage p = Docx4J.load(raw); HTMLSettings htmlSettings = Docx4J.createHTMLSettings(); htmlSettings.setWmlPackage(p); Docx4jProperties.setProperty("docx4j.Convert.Out.HTML.OutputMethodXML", true); try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { Docx4J.toHTML(htmlSettings, out, Docx4J.FLAG_EXPORT_PREFER_XSL); String html = out.toString(UTF_8.name()); return util.applyCss(html, NAME, projectName); } } catch (Docx4JException e) { throw new IOException(e); } finally { Thread.currentThread().setContextClassLoader(loader); } }
/** Convert the document to HTML */ public static void toHTML( WordprocessingMLPackage wmlPackage, String imageDirPath, String imageTargetUri, OutputStream outputStream) throws Docx4JException { HTMLSettings settings = createHTMLSettings(); settings.setWmlPackage(wmlPackage); if (imageDirPath != null) { settings.setImageDirPath(imageDirPath); } if (imageTargetUri != null) { settings.setImageTargetUri(imageTargetUri); } toHTML(settings, outputStream, FLAG_NONE); }