Search in sources :

Example 1 with OfficeImporterException

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);
    }
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) ExpandedMacroBlock(org.xwiki.rendering.block.ExpandedMacroBlock) ComponentManager(org.xwiki.component.manager.ComponentManager) StringReader(java.io.StringReader) Block(org.xwiki.rendering.block.Block) ExpandedMacroBlock(org.xwiki.rendering.block.ExpandedMacroBlock) OfficeImporterException(org.xwiki.officeimporter.OfficeImporterException) OfficeImporterException(org.xwiki.officeimporter.OfficeImporterException) OfficeConverterException(org.xwiki.officeimporter.converter.OfficeConverterException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer)

Example 2 with OfficeImporterException

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);
}
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 3 with OfficeImporterException

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);
    }
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) OfficeImporterException(org.xwiki.officeimporter.OfficeImporterException) OfficeConverterException(org.xwiki.officeimporter.converter.OfficeConverterException)

Example 4 with OfficeImporterException

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);
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) StringReader(java.io.StringReader) OfficeImporterException(org.xwiki.officeimporter.OfficeImporterException) ParseException(org.xwiki.rendering.parser.ParseException) XHTMLOfficeDocument(org.xwiki.officeimporter.document.XHTMLOfficeDocument) Document(org.w3c.dom.Document) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument)

Aggregations

OfficeImporterException (org.xwiki.officeimporter.OfficeImporterException)4 OfficeConverterException (org.xwiki.officeimporter.converter.OfficeConverterException)3 InputStream (java.io.InputStream)2 StringReader (java.io.StringReader)2 HashMap (java.util.HashMap)2 Document (org.w3c.dom.Document)2 XHTMLOfficeDocument (org.xwiki.officeimporter.document.XHTMLOfficeDocument)2 XDOM (org.xwiki.rendering.block.XDOM)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Map (java.util.Map)1 ComponentManager (org.xwiki.component.manager.ComponentManager)1 XDOMOfficeDocument (org.xwiki.officeimporter.document.XDOMOfficeDocument)1 Block (org.xwiki.rendering.block.Block)1 ExpandedMacroBlock (org.xwiki.rendering.block.ExpandedMacroBlock)1 ParseException (org.xwiki.rendering.parser.ParseException)1 BlockRenderer (org.xwiki.rendering.renderer.BlockRenderer)1 HTMLCleanerConfiguration (org.xwiki.xml.html.HTMLCleanerConfiguration)1