Search in sources :

Example 16 with XDOMOfficeDocument

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

the class DefaultOfficeResourceViewer method getView.

private OfficeDocumentView getView(ResourceReference reference, AttachmentReference attachmentReference, Map<String, ?> parameters) throws Exception {
    // Search the cache.
    String cacheKey = getCacheKey(attachmentReference.getDocumentReference(), attachmentReference.getName(), parameters);
    AttachmentOfficeDocumentView view = this.attachmentCache.get(cacheKey);
    // It's possible that the attachment has been deleted. We need to catch such events and cleanup the cache.
    DocumentReference documentReference = attachmentReference.getDocumentReference();
    if (!this.documentAccessBridge.getAttachmentReferences(documentReference).contains(attachmentReference)) {
        // If a cached view exists, flush it.
        if (view != null) {
            this.attachmentCache.remove(cacheKey);
        }
        throw new Exception(String.format("Attachment [%s] does not exist.", attachmentReference));
    }
    // Check if the view has expired.
    String currentVersion = this.documentAccessBridge.getAttachmentVersion(attachmentReference);
    if (view != null && !currentVersion.equals(view.getVersion())) {
        // Flush the cached view.
        this.attachmentCache.remove(cacheKey);
        view = null;
    }
    // If a view in not available, build one and cache it.
    if (view == null) {
        XDOMOfficeDocument xdomOfficeDocument = createXDOM(attachmentReference, parameters);
        String attachmentVersion = this.documentAccessBridge.getAttachmentVersion(attachmentReference);
        XDOM xdom = xdomOfficeDocument.getContentDocument();
        // We use only the file name from the resource reference because the rest of the information is specified by
        // the owner document reference. This way we ensure the path to the temporary files doesn't contain
        // redundant information and so it remains as small as possible (considering that the path length is limited
        // on some environments).
        Set<File> temporaryFiles = processImages(xdom, xdomOfficeDocument.getArtifacts(), attachmentReference.getDocumentReference(), attachmentReference.getName(), parameters);
        view = new AttachmentOfficeDocumentView(reference, attachmentReference, attachmentVersion, xdom, temporaryFiles);
        this.attachmentCache.set(cacheKey, view);
    }
    // transformations must be executed even when the XDOM is taken from the cache.
    return view;
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) File(java.io.File) DocumentReference(org.xwiki.model.reference.DocumentReference) InitializationException(org.xwiki.component.phase.InitializationException) CacheException(org.xwiki.cache.CacheException) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument)

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