Search in sources :

Example 1 with OfficeConverter

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

the class DefaultOfficeResourceViewer method isPresentation.

/**
 * Utility method for checking if a file name corresponds to an office presentation.
 *
 * @param fileName attachment file name
 * @return {@code true} if the file extension represents an office presentation format, {@code false} otherwise
 */
private boolean isPresentation(String fileName) {
    String extension = fileName.substring(fileName.lastIndexOf('.') + 1);
    OfficeConverter officeConverter = this.officeServer.getConverter();
    if (officeConverter != null) {
        DocumentFormat format = officeConverter.getFormatRegistry().getFormatByExtension(extension);
        return format != null && format.getInputFamily() == DocumentFamily.PRESENTATION;
    }
    return false;
}
Also used : DocumentFormat(org.artofsolving.jodconverter.document.DocumentFormat) OfficeConverter(org.xwiki.officeimporter.converter.OfficeConverter)

Example 2 with OfficeConverter

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

the class DefaultOfficeResourceViewerTest method configure.

/**
 * Test fixture.
 *
 * @throws Exception in case of an exception raised during the fixture preparation
 */
@Before
public void configure() throws Exception {
    final CacheManager cacheManager = mocker.getInstance(CacheManager.class);
    attachmentCache = mock(Cache.class, "attachment");
    externalCache = mock(Cache.class, "external");
    when(cacheManager.<OfficeDocumentView>createNewCache(notNull(CacheConfiguration.class))).thenReturn(attachmentCache, externalCache);
    EntityReferenceSerializer<String> entityReferenceSerializer = mocker.getInstance(EntityReferenceSerializer.TYPE_STRING);
    when(entityReferenceSerializer.serialize(ATTACHMENT_REFERENCE)).thenReturn(STRING_ATTACHMENT_REFERENCE);
    when(entityReferenceSerializer.serialize(ATTACHMENT_REFERENCE.getDocumentReference())).thenReturn(STRING_DOCUMENT_REFERENCE);
    AttachmentReferenceResolver<String> attachmentReferenceResolver = mocker.getInstance(AttachmentReferenceResolver.TYPE_STRING, "current");
    when(attachmentReferenceResolver.resolve(STRING_ATTACHMENT_REFERENCE)).thenReturn(ATTACHMENT_REFERENCE);
    this.resourceReferenceSerializer = mocker.getInstance(ResourceReferenceTypeSerializer.class);
    when(this.resourceReferenceSerializer.serialize(ATTACHMENT_RESOURCE_REFERENCE)).thenReturn(STRING_ATTACHMENT_RESOURCE_REFERENCE);
    ConverterManager converterManager = mocker.getInstance(ConverterManager.class);
    when(converterManager.convert(boolean.class, null)).thenReturn(false);
    when(converterManager.convert(DocumentReference.class, ATTACHMENT_REFERENCE.getDocumentReference())).thenReturn(ATTACHMENT_REFERENCE.getDocumentReference());
    documentAccessBridge = mocker.getInstance(DocumentAccessBridge.class);
    officeDocumentBuilder = mocker.getInstance(XDOMOfficeDocumentBuilder.class);
    OfficeServer officeServer = mocker.getInstance(OfficeServer.class);
    OfficeConverter officeConverter = mock(OfficeConverter.class);
    when(officeServer.getConverter()).thenReturn(officeConverter);
    when(officeConverter.getFormatRegistry()).thenReturn(new DefaultDocumentFormatRegistry());
}
Also used : DefaultDocumentFormatRegistry(org.artofsolving.jodconverter.document.DefaultDocumentFormatRegistry) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) OfficeServer(org.xwiki.officeimporter.server.OfficeServer) OfficeConverter(org.xwiki.officeimporter.converter.OfficeConverter) ConverterManager(org.xwiki.properties.ConverterManager) ResourceReferenceTypeSerializer(org.xwiki.rendering.renderer.reference.ResourceReferenceTypeSerializer) CacheManager(org.xwiki.cache.CacheManager) XDOMOfficeDocumentBuilder(org.xwiki.officeimporter.builder.XDOMOfficeDocumentBuilder) CacheConfiguration(org.xwiki.cache.config.CacheConfiguration) Cache(org.xwiki.cache.Cache) Before(org.junit.Before)

Example 3 with OfficeConverter

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

the class OfficeImporterScriptService method isPresentation.

/**
 * Utility method for checking if a file name corresponds to an office presentation.
 *
 * @param officeFileName office file name
 * @return true if the file name / extension represents an office presentation format
 */
private boolean isPresentation(String officeFileName) {
    String extension = officeFileName.substring(officeFileName.lastIndexOf('.') + 1);
    OfficeConverter officeConverter = officeServer.getConverter();
    if (officeConverter != null) {
        DocumentFormat format = officeConverter.getFormatRegistry().getFormatByExtension(extension);
        return format != null && format.getInputFamily() == DocumentFamily.PRESENTATION;
    }
    return false;
}
Also used : DocumentFormat(org.artofsolving.jodconverter.document.DocumentFormat) OfficeConverter(org.xwiki.officeimporter.converter.OfficeConverter)

Example 4 with OfficeConverter

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

the class DefaultXDOMOfficeDocumentBuilderTest method testXDOMOfficeDocumentBuilding.

/**
 * Test {@link OfficeDocument} building.
 */
@Test
public void testXDOMOfficeDocumentBuilding() throws Exception {
    // Create & register a mock document converter to by-pass the office server.
    final InputStream mockOfficeFileStream = new ByteArrayInputStream(new byte[1024]);
    final Map<String, InputStream> mockInput = new HashMap<String, InputStream>();
    mockInput.put(INPUT_FILE_NAME, mockOfficeFileStream);
    final Map<String, byte[]> mockOutput = new HashMap<String, byte[]>();
    mockOutput.put(OUTPUT_FILE_NAME, "<html><head><title></tile></head><body><p><strong>Hello There</strong></p></body></html>".getBytes());
    final OfficeConverter mockDocumentConverter = getMockery().mock(OfficeConverter.class);
    final DocumentReference documentReference = new DocumentReference("xwiki", "Main", "Test");
    getMockery().checking(new Expectations() {

        {
            oneOf(mockOfficeServer).getConverter();
            will(returnValue(mockDocumentConverter));
            allowing(mockDocumentConverter).convert(mockInput, INPUT_FILE_NAME, OUTPUT_FILE_NAME);
            will(returnValue(mockOutput));
            allowing(mockDocumentReferenceResolver).resolve("xwiki:Main.Test");
            will(returnValue(documentReference));
            allowing(mockDefaultStringEntityReferenceSerializer).serialize(documentReference);
            will(returnValue("xwiki:Main.Test"));
        }
    });
    XDOMOfficeDocument document = xdomOfficeDocumentBuilder.build(mockOfficeFileStream, INPUT_FILE_NAME, documentReference, true);
    assertEquals("xwiki:Main.Test", document.getContentDocument().getMetaData().getMetaData(MetaData.BASE));
    assertEquals("**Hello There**", document.getContentAsString());
    assertEquals(0, document.getArtifacts().size());
}
Also used : Expectations(org.jmock.Expectations) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OfficeConverter(org.xwiki.officeimporter.converter.OfficeConverter) DocumentReference(org.xwiki.model.reference.DocumentReference) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument) AbstractOfficeImporterTest(org.xwiki.officeimporter.internal.AbstractOfficeImporterTest) Test(org.junit.Test)

Example 5 with OfficeConverter

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

the class DefaultOfficeViewerScriptService method isConversionSupported.

/**
 * Use this method to check if the unidirectional conversion from a document format (input media type) to another
 * document format (output media type) is supported by this converter.
 *
 * @param inputMediaType the media type of the input document
 * @param outputMediaType the media type of the output document
 * @return {@code true} if a document can be converted from the input media type to the output media type,
 *         {@code false} otherwise
 */
private boolean isConversionSupported(String inputMediaType, String outputMediaType) {
    OfficeConverter converter = this.officeServer.getConverter();
    if (converter != null) {
        DocumentFormat inputFormat = converter.getFormatRegistry().getFormatByMediaType(inputMediaType);
        DocumentFormat outputFormat = converter.getFormatRegistry().getFormatByMediaType(outputMediaType);
        return inputFormat != null && outputFormat != null && outputFormat.getStoreProperties(inputFormat.getInputFamily()) != null;
    } else {
        return false;
    }
}
Also used : DocumentFormat(org.artofsolving.jodconverter.document.DocumentFormat) OfficeConverter(org.xwiki.officeimporter.converter.OfficeConverter)

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