Search in sources :

Example 1 with OfficeConverterException

use of org.xwiki.officeimporter.converter.OfficeConverterException in project xwiki-platform by xwiki.

the class DefaultXHTMLOfficeDocumentBuilder method build.

@Override
public XHTMLOfficeDocument build(InputStream officeFileStream, String officeFileName, DocumentReference reference, boolean filterStyles) throws OfficeImporterException {
    // Accents seems to cause issues in some conditions
    // See https://jira.xwiki.org/browse/XWIKI-14692
    String cleanedOfficeFileName = StringUtils.stripAccents(officeFileName);
    // Invoke the office document converter.
    Map<String, InputStream> inputStreams = new HashMap<String, InputStream>();
    inputStreams.put(cleanedOfficeFileName, officeFileStream);
    Map<String, byte[]> artifacts;
    // The office converter uses the output file name extension to determine the output format/syntax.
    String outputFileName = StringUtils.substringBeforeLast(cleanedOfficeFileName, ".") + ".html";
    try {
        artifacts = this.officeServer.getConverter().convert(inputStreams, cleanedOfficeFileName, outputFileName);
    } catch (OfficeConverterException ex) {
        String message = "Error while converting document [%s] into html.";
        throw new OfficeImporterException(String.format(message, officeFileName), ex);
    }
    // Prepare the parameters for HTML cleaning.
    Map<String, String> params = new HashMap<String, String>();
    params.put("targetDocument", this.entityReferenceSerializer.serialize(reference));
    // Extract the images that are embedded through the Data URI scheme and add them to the other artifacts so that
    // they end up as attachments.
    params.put("attachEmbeddedImages", "true");
    if (filterStyles) {
        params.put("filterStyles", "strict");
    }
    // Parse and clean the HTML output.
    HTMLCleanerConfiguration configuration = this.officeHtmlCleaner.getDefaultConfiguration();
    configuration.setParameters(params);
    Reader html = getReader(artifacts.remove(outputFileName));
    Document xhtmlDoc = this.officeHtmlCleaner.clean(html, configuration);
    @SuppressWarnings("unchecked") Map<String, byte[]> embeddedImages = (Map<String, byte[]>) xhtmlDoc.getUserData("embeddedImages");
    if (embeddedImages != null) {
        artifacts.putAll(embeddedImages);
    }
    // Return a new XHTMLOfficeDocument instance.
    return new XHTMLOfficeDocument(xhtmlDoc, artifacts);
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) XHTMLOfficeDocument(org.xwiki.officeimporter.document.XHTMLOfficeDocument) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) OfficeConverterException(org.xwiki.officeimporter.converter.OfficeConverterException) XHTMLOfficeDocument(org.xwiki.officeimporter.document.XHTMLOfficeDocument) Document(org.w3c.dom.Document) OfficeImporterException(org.xwiki.officeimporter.OfficeImporterException) HashMap(java.util.HashMap) Map(java.util.Map) HTMLCleanerConfiguration(org.xwiki.xml.html.HTMLCleanerConfiguration)

Example 2 with OfficeConverterException

use of org.xwiki.officeimporter.converter.OfficeConverterException in project xwiki-platform by xwiki.

the class DefaultOfficeConverter method convert.

@Override
public Map<String, byte[]> convert(Map<String, InputStream> inputStreams, String inputFileName, String outputFileName) throws OfficeConverterException {
    // Verify whether an input stream is present for the main input file.
    if (null == inputStreams.get(inputFileName)) {
        String message = "No input stream specified for main input file [%s].";
        throw new OfficeConverterException(String.format(message, inputFileName));
    }
    OfficeConverterFileStorage storage = null;
    try {
        // Prepare temporary storage.
        storage = new OfficeConverterFileStorage(this.workDir, inputFileName, outputFileName);
        // Write out all the input streams.
        FileOutputStream fos = null;
        for (Map.Entry<String, InputStream> entry : inputStreams.entrySet()) {
            try {
                File temp = new File(storage.getInputDir(), entry.getKey());
                fos = new FileOutputStream(temp);
                IOUtils.copy(entry.getValue(), fos);
            } finally {
                IOUtils.closeQuietly(fos);
            }
        }
        // Perform the conversion.
        this.converter.convert(storage.getInputFile(), storage.getOutputFile());
        // Collect all the output artifacts.
        Map<String, byte[]> result = new HashMap<String, byte[]>();
        FileInputStream fis = null;
        for (File file : storage.getOutputDir().listFiles()) {
            try {
                fis = new FileInputStream(file);
                result.put(file.getName(), IOUtils.toByteArray(fis));
            } finally {
                IOUtils.closeQuietly(fis);
            }
        }
        return result;
    } catch (Exception ex) {
        throw new OfficeConverterException("Error while performing conversion.", ex);
    } finally {
        if (!storage.cleanUp()) {
            LOGGER.error("Could not cleanup temporary storage after conversion.");
        }
    }
}
Also used : HashMap(java.util.HashMap) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) OfficeConverterException(org.xwiki.officeimporter.converter.OfficeConverterException) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) FileInputStream(java.io.FileInputStream) OfficeConverterException(org.xwiki.officeimporter.converter.OfficeConverterException)

Example 3 with OfficeConverterException

use of org.xwiki.officeimporter.converter.OfficeConverterException in project xwiki-platform by xwiki.

the class DefaultPresentationBuilder method importPresentation.

/**
 * Invokes the Office Server to convert the given input stream. The result is a map of artifacts including slide
 * images.
 *
 * @param officeFileStream the office presentation byte stream
 * @param officeFileName the name of the office presentation that is being imported
 * @return the map of artifacts created by the Office Server
 * @throws OfficeImporterException if converting the office presentation fails
 */
protected Map<String, byte[]> importPresentation(InputStream officeFileStream, String officeFileName) throws OfficeImporterException {
    Map<String, InputStream> inputStreams = new HashMap<String, InputStream>();
    inputStreams.put(officeFileName, officeFileStream);
    try {
        // artifact displays a screen shot of the first presentation slide.
        return this.officeServer.getConverter().convert(inputStreams, officeFileName, "img0.html");
    } catch (OfficeConverterException e) {
        String message = "Error while converting document [%s] into html.";
        throw new OfficeImporterException(String.format(message, officeFileName), e);
    }
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) OfficeImporterException(org.xwiki.officeimporter.OfficeImporterException) OfficeConverterException(org.xwiki.officeimporter.converter.OfficeConverterException)

Aggregations

InputStream (java.io.InputStream)3 HashMap (java.util.HashMap)3 OfficeConverterException (org.xwiki.officeimporter.converter.OfficeConverterException)3 Map (java.util.Map)2 OfficeImporterException (org.xwiki.officeimporter.OfficeImporterException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 Document (org.w3c.dom.Document)1 XHTMLOfficeDocument (org.xwiki.officeimporter.document.XHTMLOfficeDocument)1 HTMLCleanerConfiguration (org.xwiki.xml.html.HTMLCleanerConfiguration)1