Search in sources :

Example 1 with XDOMOfficeDocument

use of org.xwiki.officeimporter.document.XDOMOfficeDocument in project xwiki-platform by xwiki.

the class DefaultXDOMOfficeDocumentBuilder method build.

@Override
public XDOMOfficeDocument build(InputStream officeFileStream, String officeFileName, DocumentReference reference, boolean filterStyles) throws OfficeImporterException {
    XDOMOfficeDocument xdomOfficeDocument = build(this.xhtmlOfficeDocumentBuilder.build(officeFileStream, officeFileName, reference, filterStyles));
    // Make sure references are resolved relative to the target document reference.
    xdomOfficeDocument.getContentDocument().getMetaData().addMetaData(MetaData.BASE, entityReferenceSerializer.serialize(reference));
    return xdomOfficeDocument;
}
Also used : XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument)

Example 2 with XDOMOfficeDocument

use of org.xwiki.officeimporter.document.XDOMOfficeDocument in project xwiki-platform by xwiki.

the class OfficeAttachmentImporter method convertAttachmentContent.

/**
 * Converts the content of the specified office file to wiki syntax.
 *
 * @param attachmentReference specifies the office file whose content should be converted
 * @param filterStyles controls whether styles are filtered when converting the HTML produced by the office server
 *            to wiki syntax
 * @return the annotated XHTML text obtained from the specified office document
 * @throws Exception if converting the content of the specified attachment fails
 */
private String convertAttachmentContent(AttachmentReference attachmentReference, boolean filterStyles) throws Exception {
    InputStream officeFileStream = documentAccessBridge.getAttachmentContent(attachmentReference);
    String officeFileName = attachmentReference.getName();
    DocumentReference targetDocRef = attachmentReference.getDocumentReference();
    XDOMOfficeDocument xdomOfficeDocument;
    if (isPresentation(attachmentReference.getName())) {
        xdomOfficeDocument = presentationBuilder.build(officeFileStream, officeFileName, targetDocRef);
    } else {
        xdomOfficeDocument = documentBuilder.build(officeFileStream, officeFileName, targetDocRef, filterStyles);
    }
    // Attach the images extracted from the imported office document to the target wiki document.
    for (Map.Entry<String, byte[]> artifact : xdomOfficeDocument.getArtifacts().entrySet()) {
        AttachmentReference artifactReference = new AttachmentReference(artifact.getKey(), targetDocRef);
        documentAccessBridge.setAttachmentContent(artifactReference, artifact.getValue());
    }
    return xdomOfficeDocument.getContentAsString("annotatedxhtml/1.0");
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) InputStream(java.io.InputStream) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument)

Example 3 with XDOMOfficeDocument

use of org.xwiki.officeimporter.document.XDOMOfficeDocument in project xwiki-platform by xwiki.

the class DefaultOfficeResourceViewer method getView.

private OfficeDocumentView getView(ResourceReference resourceReference, Map<String, ?> parameters) throws Exception {
    DocumentReference ownerDocument = getOwnerDocument(parameters);
    String serializedResourceReference = this.resourceReferenceSerializer.serialize(resourceReference);
    // Search the cache.
    String cacheKey = getCacheKey(ownerDocument, serializedResourceReference, parameters);
    OfficeDocumentView view = this.externalCache.get(cacheKey);
    // If a view in not available, build one and cache it.
    if (view == null) {
        XDOMOfficeDocument xdomOfficeDocument = createXDOM(ownerDocument, resourceReference, parameters);
        XDOM xdom = xdomOfficeDocument.getContentDocument();
        Set<File> temporaryFiles = processImages(xdom, xdomOfficeDocument.getArtifacts(), ownerDocument, serializedResourceReference, parameters);
        view = new OfficeDocumentView(resourceReference, xdom, temporaryFiles);
        this.externalCache.set(cacheKey, view);
    }
    return view;
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) File(java.io.File) DocumentReference(org.xwiki.model.reference.DocumentReference) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument)

Example 4 with XDOMOfficeDocument

use of org.xwiki.officeimporter.document.XDOMOfficeDocument in project xwiki-platform by xwiki.

the class DefaultOfficeResourceViewerTest method testViewExistingOfficeAttachmentWithCacheMiss.

/**
 * Tests creating a view for an existing office attachment.
 *
 * @throws Exception if an error occurs
 */
@Test
public void testViewExistingOfficeAttachmentWithCacheMiss() throws Exception {
    when(attachmentCache.get(CACHE_KEY)).thenReturn(null);
    when(documentAccessBridge.getAttachmentReferences(ATTACHMENT_REFERENCE.getDocumentReference())).thenReturn(Arrays.asList(ATTACHMENT_REFERENCE));
    when(documentAccessBridge.getAttachmentVersion(ATTACHMENT_REFERENCE)).thenReturn(ATTACHMENT_VERSION);
    ByteArrayInputStream attachmentContent = new ByteArrayInputStream(new byte[256]);
    when(documentAccessBridge.getAttachmentContent(ATTACHMENT_REFERENCE)).thenReturn(attachmentContent);
    XDOMOfficeDocument xdomOfficeDocument = new XDOMOfficeDocument(new XDOM(new ArrayList<Block>()), new HashMap<String, byte[]>(), mocker);
    when(officeDocumentBuilder.build(attachmentContent, ATTACHMENT_REFERENCE.getName(), ATTACHMENT_REFERENCE.getDocumentReference(), false)).thenReturn(xdomOfficeDocument);
    mocker.getComponentUnderTest().createView(ATTACHMENT_RESOURCE_REFERENCE, DEFAULT_VIEW_PARAMETERS);
    verify(attachmentCache).set(eq(CACHE_KEY), notNull(AttachmentOfficeDocumentView.class));
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument) Test(org.junit.Test)

Example 5 with XDOMOfficeDocument

use of org.xwiki.officeimporter.document.XDOMOfficeDocument in project xwiki-platform by xwiki.

the class DefaultOfficeResourceViewerTest method testViewANewVersionOfAnExistingOfficeAttachment.

/**
 * Tests creating a view for an office attachment that has been viewed in past and whose version has been
 * incremented.
 *
 * @throws Exception if an error occurs.
 */
@Test
public void testViewANewVersionOfAnExistingOfficeAttachment() throws Exception {
    AttachmentOfficeDocumentView officeDocumentView = new AttachmentOfficeDocumentView(ATTACHMENT_RESOURCE_REFERENCE, ATTACHMENT_REFERENCE, ATTACHMENT_VERSION, new XDOM(new ArrayList<Block>()), new HashSet<File>());
    when(attachmentCache.get(CACHE_KEY)).thenReturn(officeDocumentView);
    when(documentAccessBridge.getAttachmentReferences(ATTACHMENT_REFERENCE.getDocumentReference())).thenReturn(Arrays.asList(ATTACHMENT_REFERENCE));
    when(documentAccessBridge.getAttachmentVersion(ATTACHMENT_REFERENCE)).thenReturn("2.1");
    ByteArrayInputStream attachmentContent = new ByteArrayInputStream(new byte[256]);
    when(documentAccessBridge.getAttachmentContent(ATTACHMENT_REFERENCE)).thenReturn(attachmentContent);
    XDOMOfficeDocument xdomOfficeDocument = new XDOMOfficeDocument(new XDOM(new ArrayList<Block>()), new HashMap<String, byte[]>(), mocker);
    when(officeDocumentBuilder.build(attachmentContent, ATTACHMENT_REFERENCE.getName(), ATTACHMENT_REFERENCE.getDocumentReference(), false)).thenReturn(xdomOfficeDocument);
    Assert.assertNotNull(mocker.getComponentUnderTest().createView(ATTACHMENT_RESOURCE_REFERENCE, DEFAULT_VIEW_PARAMETERS));
    verify(attachmentCache).remove(CACHE_KEY);
    verify(attachmentCache).set(eq(CACHE_KEY), notNull(AttachmentOfficeDocumentView.class));
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) File(java.io.File) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument) Test(org.junit.Test)

Aggregations

XDOMOfficeDocument (org.xwiki.officeimporter.document.XDOMOfficeDocument)16 DocumentReference (org.xwiki.model.reference.DocumentReference)10 Test (org.junit.Test)9 XDOM (org.xwiki.rendering.block.XDOM)9 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InputStream (java.io.InputStream)5 File (java.io.File)3 HashMap (java.util.HashMap)3 AttachmentReference (org.xwiki.model.reference.AttachmentReference)3 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 Expectations (org.jmock.Expectations)2 Document (org.w3c.dom.Document)2 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)2 AbstractOfficeImporterTest (org.xwiki.officeimporter.internal.AbstractOfficeImporterTest)2 TargetDocumentDescriptor (org.xwiki.officeimporter.splitter.TargetDocumentDescriptor)2 ExpandedMacroBlock (org.xwiki.rendering.block.ExpandedMacroBlock)2 ClassBlockMatcher (org.xwiki.rendering.block.match.ClassBlockMatcher)2 Reader (java.io.Reader)1 Type (java.lang.reflect.Type)1