Search in sources :

Example 6 with OfficeConverter

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

the class DefaultOfficeViewerScriptServiceTest method isMimeTypeSupported.

@Test
public void isMimeTypeSupported() throws Exception {
    OfficeServer officeServer = mocker.getInstance(OfficeServer.class);
    OfficeConverter officeConverter = mock(OfficeConverter.class);
    when(officeServer.getConverter()).thenReturn(officeConverter);
    when(officeConverter.getFormatRegistry()).thenReturn(new DefaultDocumentFormatRegistry());
    for (String mediaType : Arrays.asList("application/vnd.oasis.opendocument.text", "application/msword", "application/vnd.oasis.opendocument.presentation", "application/vnd.ms-powerpoint", "application/vnd.oasis.opendocument.spreadsheet", "application/vnd.ms-excel")) {
        Assert.assertTrue(mocker.getComponentUnderTest().isMimeTypeSupported(mediaType));
    }
    for (String mediaType : Arrays.asList("foo/bar", "application/pdf")) {
        Assert.assertFalse(mocker.getComponentUnderTest().isMimeTypeSupported(mediaType));
    }
}
Also used : DefaultDocumentFormatRegistry(org.artofsolving.jodconverter.document.DefaultDocumentFormatRegistry) OfficeServer(org.xwiki.officeimporter.server.OfficeServer) OfficeConverter(org.xwiki.officeimporter.converter.OfficeConverter) Test(org.junit.Test)

Example 7 with OfficeConverter

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

the class OfficeExporter method exportXHTML.

/**
 * Converts the given XHTML to the specified format and writes the result to the output stream.
 *
 * @param xhtml the XHTML to be exported
 * @param out where to write the export result
 * @param format the office format to convert the XHTML to
 * @param context the XWiki context, used to access the mapping between images embedded in the given XHTML and their
 *            location on the file system
 * @throws XWikiException if the conversion fails
 */
private void exportXHTML(String xhtml, OutputStream out, DocumentFormat format, XWikiContext context) throws XWikiException {
    String html = applyXSLT(xhtml, getOfficeExportXSLT(context));
    String inputFileName = "export_input.html";
    String outputFileName = "export_output." + format.getExtension();
    Map<String, InputStream> inputStreams = new HashMap<String, InputStream>();
    // We assume that the HTML was generated using the XWiki encoding.
    Charset charset = Charset.forName(context.getWiki().getEncoding());
    inputStreams.put(inputFileName, new ByteArrayInputStream(html.getBytes(charset)));
    addEmbeddedObjects(inputStreams, context);
    OfficeConverter officeConverter = this.officeServer.getConverter();
    try {
        Map<String, byte[]> ouput = officeConverter.convert(inputStreams, inputFileName, outputFileName);
        out.write(ouput.values().iterator().next());
    } catch (Exception e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_EXPORT, XWikiException.ERROR_XWIKI_APP_SEND_RESPONSE_EXCEPTION, String.format("Exception while exporting to %s (%s).", format.getName(), format.getExtension()), e);
    }
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) OfficeConverter(org.xwiki.officeimporter.converter.OfficeConverter) XWikiException(com.xpn.xwiki.XWikiException) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

OfficeConverter (org.xwiki.officeimporter.converter.OfficeConverter)7 DocumentFormat (org.artofsolving.jodconverter.document.DocumentFormat)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 DefaultDocumentFormatRegistry (org.artofsolving.jodconverter.document.DefaultDocumentFormatRegistry)2 Test (org.junit.Test)2 OfficeServer (org.xwiki.officeimporter.server.OfficeServer)2 XWikiException (com.xpn.xwiki.XWikiException)1 FileInputStream (java.io.FileInputStream)1 Charset (java.nio.charset.Charset)1 Expectations (org.jmock.Expectations)1 Before (org.junit.Before)1 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)1 Cache (org.xwiki.cache.Cache)1 CacheManager (org.xwiki.cache.CacheManager)1 CacheConfiguration (org.xwiki.cache.config.CacheConfiguration)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 XDOMOfficeDocumentBuilder (org.xwiki.officeimporter.builder.XDOMOfficeDocumentBuilder)1 XDOMOfficeDocument (org.xwiki.officeimporter.document.XDOMOfficeDocument)1