Search in sources :

Example 6 with XDOMOfficeDocument

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

the class DefaultOfficeResourceViewerTest method testViewPresentation.

@Test
public void testViewPresentation() throws Exception {
    AttachmentResourceReference attachResourceRef = new AttachmentResourceReference("xwiki:Some.Page@presentation.odp");
    DocumentReference documentReference = new DocumentReference("wiki", "Some", "Page");
    AttachmentReference attachmentReference = new AttachmentReference("presentation.odp", documentReference);
    AttachmentReferenceResolver<String> attachmentReferenceResolver = mocker.getInstance(AttachmentReferenceResolver.TYPE_STRING, "current");
    when(attachmentReferenceResolver.resolve(attachResourceRef.getReference())).thenReturn(attachmentReference);
    when(documentAccessBridge.getAttachmentReferences(attachmentReference.getDocumentReference())).thenReturn(Arrays.asList(attachmentReference));
    when(documentAccessBridge.getAttachmentVersion(attachmentReference)).thenReturn("3.2");
    ByteArrayInputStream attachmentContent = new ByteArrayInputStream(new byte[256]);
    when(documentAccessBridge.getAttachmentContent(attachmentReference)).thenReturn(attachmentContent);
    ResourceReference imageReference = new ResourceReference("slide0.png", ResourceType.URL);
    ExpandedMacroBlock galleryMacro = new ExpandedMacroBlock("gallery", Collections.singletonMap("width", "300px"), null, false);
    galleryMacro.addChild(new ImageBlock(imageReference, true));
    XDOM xdom = new XDOM(Collections.<Block>singletonList(galleryMacro));
    Map<String, byte[]> artifacts = Collections.singletonMap("slide0.png", new byte[8]);
    XDOMOfficeDocument xdomOfficeDocument = new XDOMOfficeDocument(xdom, artifacts, mocker);
    PresentationBuilder presentationBuilder = mocker.getInstance(PresentationBuilder.class);
    when(presentationBuilder.build(attachmentContent, attachmentReference.getName(), documentReference)).thenReturn(xdomOfficeDocument);
    Map<String, ?> viewParameters = Collections.singletonMap("ownerDocument", documentReference);
    TemporaryResourceReference temporaryResourceReference = new TemporaryResourceReference("officeviewer", Arrays.asList(String.valueOf(viewParameters.hashCode()), "slide0.png"), documentReference);
    Type type = new DefaultParameterizedType(null, ResourceReferenceSerializer.class, TemporaryResourceReference.class, ExtendedURL.class);
    ResourceReferenceSerializer<TemporaryResourceReference, ExtendedURL> urlTemporaryResourceReferenceSerializer = mocker.getInstance(type, "standard/tmp");
    ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("url", "to", "slide0.png"));
    when(urlTemporaryResourceReferenceSerializer.serialize(temporaryResourceReference)).thenReturn(extendedURL);
    XDOM output = this.mocker.getComponentUnderTest().createView(attachResourceRef, viewParameters);
    ImageBlock imageBlock = (ImageBlock) output.getBlocks(new ClassBlockMatcher(ImageBlock.class), Block.Axes.DESCENDANT).get(0);
    assertEquals("/url/to/slide0.png", imageBlock.getReference().getReference());
    galleryMacro = (ExpandedMacroBlock) output.getBlocks(new ClassBlockMatcher(ExpandedMacroBlock.class), Block.Axes.DESCENDANT).get(0);
    assertFalse(galleryMacro.getParent() instanceof XDOM);
    assertEquals(Syntax.XWIKI_2_1, ((MetaDataBlock) galleryMacro.getParent()).getMetaData().getMetaData(MetaData.SYNTAX));
    TemporaryResourceStore store = mocker.getInstance(TemporaryResourceStore.class);
    verify(store).createTemporaryFile(eq(temporaryResourceReference), any(InputStream.class));
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XDOM(org.xwiki.rendering.block.XDOM) ImageBlock(org.xwiki.rendering.block.ImageBlock) AttachmentResourceReference(org.xwiki.rendering.listener.reference.AttachmentResourceReference) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ExtendedURL(org.xwiki.url.ExtendedURL) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) ResourceType(org.xwiki.rendering.listener.reference.ResourceType) Type(java.lang.reflect.Type) TemporaryResourceReference(org.xwiki.resource.temporary.TemporaryResourceReference) ByteArrayInputStream(java.io.ByteArrayInputStream) ExpandedMacroBlock(org.xwiki.rendering.block.ExpandedMacroBlock) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) PresentationBuilder(org.xwiki.officeimporter.builder.PresentationBuilder) TemporaryResourceStore(org.xwiki.resource.temporary.TemporaryResourceStore) TemporaryResourceReference(org.xwiki.resource.temporary.TemporaryResourceReference) AttachmentResourceReference(org.xwiki.rendering.listener.reference.AttachmentResourceReference) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) DocumentReference(org.xwiki.model.reference.DocumentReference) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) Test(org.junit.Test)

Example 7 with XDOMOfficeDocument

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

the class OfficeImporterScriptServiceTest method saveWithAppend.

@Test
public void saveWithAppend() throws Exception {
    XDOMOfficeDocument doc = mock(XDOMOfficeDocument.class);
    DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
    String syntaxId = "test/1.0";
    when(documentAccessBridge.isDocumentEditable(documentReference)).thenReturn(true);
    when(documentAccessBridge.exists(documentReference)).thenReturn(true);
    DocumentModelBridge document = mock(DocumentModelBridge.class);
    when(documentAccessBridge.getTranslatedDocumentInstance(documentReference)).thenReturn(document);
    when(document.getSyntax()).thenReturn(new Syntax(new SyntaxType("test", "Test"), "1.0"));
    when(documentAccessBridge.getDocumentContent(documentReference, null)).thenReturn("before");
    when(doc.getContentAsString(syntaxId)).thenReturn("after");
    assertTrue(officeImporterScriptService.save(doc, documentReference, syntaxId, null, null, true));
    verify(documentAccessBridge).setDocumentContent(documentReference, "before\nafter", "Updated by office importer.", false);
}
Also used : DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) SyntaxType(org.xwiki.rendering.syntax.SyntaxType) Syntax(org.xwiki.rendering.syntax.Syntax) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 8 with XDOMOfficeDocument

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

the class OfficeImporterScriptServiceTest method saveWithOverwrite.

@Test
public void saveWithOverwrite() throws Exception {
    XDOMOfficeDocument doc = mock(XDOMOfficeDocument.class);
    DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
    DocumentReference parentReference = new DocumentReference("wiki", "Space", "Parent");
    String syntaxId = "test/1.0";
    String title = "Office Document Title";
    String content = "Office Document Content";
    String fileName = "logo.png";
    byte[] fileContent = new byte[] { 65, 82 };
    when(documentAccessBridge.isDocumentEditable(documentReference)).thenReturn(true);
    when(doc.getContentAsString(syntaxId)).thenReturn(content);
    when(doc.getArtifacts()).thenReturn(Collections.singletonMap(fileName, fileContent));
    assertTrue(officeImporterScriptService.save(doc, documentReference, syntaxId, parentReference, title, false));
    verify(documentAccessBridge).setDocumentSyntaxId(documentReference, syntaxId);
    verify(documentAccessBridge).setDocumentContent(documentReference, content, "Created by office importer.", false);
    verify(documentAccessBridge).setDocumentParentReference(documentReference, parentReference);
    verify(documentAccessBridge).setDocumentTitle(documentReference, title);
    verify(documentAccessBridge).setAttachmentContent(new AttachmentReference(fileName, documentReference), fileContent);
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 9 with XDOMOfficeDocument

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

the class DefaultPresentationBuilderTest method build.

@Test
public void build() throws Exception {
    DocumentReference documentReference = new DocumentReference("wiki", Arrays.asList("Path", "To"), "Page");
    when(this.entityReferenceSerializer.serialize(documentReference)).thenReturn("wiki:Path.To.Page");
    DocumentModelBridge document = mock(DocumentModelBridge.class);
    DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
    when(dab.getTranslatedDocumentInstance(documentReference)).thenReturn(document);
    when(document.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
    InputStream officeFileStream = new ByteArrayInputStream("Presentation content".getBytes());
    Map<String, byte[]> artifacts = new HashMap<String, byte[]>();
    byte[] firstSlide = "first slide".getBytes();
    byte[] secondSlide = "second slide".getBytes();
    artifacts.put("img0.jpg", firstSlide);
    artifacts.put("img0.html", new byte[0]);
    artifacts.put("text0.html", new byte[0]);
    artifacts.put("img1.jpg", secondSlide);
    artifacts.put("img1.html", new byte[0]);
    artifacts.put("text1.html", new byte[0]);
    when(this.officeConverter.convert(Collections.singletonMap("file.odp", officeFileStream), "file.odp", "img0.html")).thenReturn(artifacts);
    HTMLCleanerConfiguration config = mock(HTMLCleanerConfiguration.class);
    when(this.officeHTMLCleaner.getDefaultConfiguration()).thenReturn(config);
    Document xhtmlDoc = XMLUtils.createDOMDocument();
    xhtmlDoc.appendChild(xhtmlDoc.createElement("html"));
    String presentationHTML = "<p><img src=\"file-slide0.jpg\"/></p><p><img src=\"file-slide1.jpg\"/></p>";
    when(this.officeHTMLCleaner.clean(any(Reader.class), eq(config))).then(returnMatchingDocument(presentationHTML, xhtmlDoc));
    XDOM galleryContent = new XDOM(Collections.<Block>emptyList());
    when(this.xhtmlParser.parse(any(Reader.class))).thenReturn(galleryContent);
    XDOMOfficeDocument result = this.mocker.getComponentUnderTest().build(officeFileStream, "file.odp", documentReference);
    verify(config).setParameters(Collections.singletonMap("targetDocument", "wiki:Path.To.Page"));
    Map<String, byte[]> expectedArtifacts = new HashMap<String, byte[]>();
    expectedArtifacts.put("file-slide0.jpg", firstSlide);
    expectedArtifacts.put("file-slide1.jpg", secondSlide);
    assertEquals(expectedArtifacts, result.getArtifacts());
    assertEquals("wiki:Path.To.Page", result.getContentDocument().getMetaData().getMetaData(MetaData.BASE));
    List<ExpandedMacroBlock> macros = result.getContentDocument().getBlocks(new ClassBlockMatcher(ExpandedMacroBlock.class), Block.Axes.CHILD);
    Assert.assertEquals(1, macros.size());
    Assert.assertEquals("gallery", macros.get(0).getId());
    Assert.assertEquals(galleryContent, macros.get(0).getChildren().get(0));
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) Reader(java.io.Reader) Document(org.w3c.dom.Document) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) ByteArrayInputStream(java.io.ByteArrayInputStream) ExpandedMacroBlock(org.xwiki.rendering.block.ExpandedMacroBlock) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) DocumentReference(org.xwiki.model.reference.DocumentReference) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument) HTMLCleanerConfiguration(org.xwiki.xml.html.HTMLCleanerConfiguration) Test(org.junit.Test)

Example 10 with XDOMOfficeDocument

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

the class OfficeAttachmentImporterTest method toHTML.

@Test
public void toHTML() throws Exception {
    when(this.authorization.hasAccess(Right.EDIT, attachmentReference)).thenReturn(true);
    when(this.documentAccessBridge.getAttachmentVersion(attachmentReference)).thenReturn("1.3");
    when(this.officeServer.getState()).thenReturn(ServerState.CONNECTED);
    InputStream attachmentContent = mock(InputStream.class);
    when(this.documentAccessBridge.getAttachmentContent(attachmentReference)).thenReturn(attachmentContent);
    XDOMOfficeDocumentBuilder documentBuilder = this.mocker.getInstance(XDOMOfficeDocumentBuilder.class);
    XDOMOfficeDocument xdomOfficeDocument = mock(XDOMOfficeDocument.class);
    when(documentBuilder.build(attachmentContent, "my.doc", attachmentReference.getDocumentReference(), true)).thenReturn(xdomOfficeDocument);
    when(xdomOfficeDocument.getArtifacts()).thenReturn(Collections.emptyMap());
    when(xdomOfficeDocument.getContentAsString("annotatedxhtml/1.0")).thenReturn("test");
    Map<String, Object> parameters = Collections.singletonMap("filterStyles", "true");
    assertEquals("test", this.mocker.getComponentUnderTest().toHTML(attachmentReference, parameters));
}
Also used : InputStream(java.io.InputStream) XDOMOfficeDocumentBuilder(org.xwiki.officeimporter.builder.XDOMOfficeDocumentBuilder) 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