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;
}
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");
}
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;
}
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));
}
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));
}
Aggregations