use of org.xwiki.officeimporter.OfficeImporterException in project xwiki-platform by xwiki.
the class DefaultPresentationBuilder method buildPresentationXDOM.
/**
* Parses the given HTML text into an XDOM tree.
*
* @param html the HTML text to parse
* @param targetDocumentReference specifies the document where the presentation will be imported; we use the target
* document reference to get the syntax of the target document and to set the {@code BASE} meta data on
* the created XDOM
* @return a XDOM tree
* @throws OfficeImporterException if parsing the given HTML fails
*/
protected XDOM buildPresentationXDOM(String html, DocumentReference targetDocumentReference) throws OfficeImporterException {
try {
ComponentManager contextComponentManager = this.contextComponentManagerProvider.get();
String syntaxId = this.documentAccessBridge.getTranslatedDocumentInstance(targetDocumentReference).getSyntax().toIdString();
BlockRenderer renderer = contextComponentManager.getInstance(BlockRenderer.class, syntaxId);
Map<String, String> galleryParameters = Collections.emptyMap();
ExpandedMacroBlock gallery = new ExpandedMacroBlock("gallery", galleryParameters, renderer, false, contextComponentManager);
gallery.addChild(this.xhtmlParser.parse(new StringReader(html)));
XDOM xdom = new XDOM(Collections.singletonList((Block) gallery));
// Make sure (image) references are resolved relative to the target document reference.
xdom.getMetaData().addMetaData(MetaData.BASE, entityReferenceSerializer.serialize(targetDocumentReference));
return xdom;
} catch (Exception e) {
throw new OfficeImporterException("Failed to build presentation XDOM.", e);
}
}
use of org.xwiki.officeimporter.OfficeImporterException 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.OfficeImporterException 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);
}
}
use of org.xwiki.officeimporter.OfficeImporterException in project xwiki-platform by xwiki.
the class DefaultXDOMOfficeDocumentBuilder method build.
@Override
public XDOMOfficeDocument build(XHTMLOfficeDocument xhtmlOfficeDocument) throws OfficeImporterException {
Document xhtmlDoc = xhtmlOfficeDocument.getContentDocument();
HTMLUtils.stripHTMLEnvelope(xhtmlDoc);
XDOM xdom = null;
try {
xdom = this.xHtmlParser.parse(new StringReader(HTMLUtils.toString(xhtmlDoc)));
} catch (ParseException ex) {
throw new OfficeImporterException("Error: Could not parse xhtml office content.", ex);
}
return new XDOMOfficeDocument(xdom, xhtmlOfficeDocument.getArtifacts(), this.componentManager);
}
Aggregations