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