Search in sources :

Example 1 with MetaDataBlock

use of org.xwiki.rendering.block.MetaDataBlock in project xwiki-platform by xwiki.

the class DefaultOfficeResourceViewer method maybeFixExpandedMacroAncestor.

private void maybeFixExpandedMacroAncestor(Block block) {
    ExpandedMacroBlock expandedMacro = block.getFirstBlock(new ClassBlockMatcher(ExpandedMacroBlock.class), Block.Axes.ANCESTOR_OR_SELF);
    if (expandedMacro != null) {
        Block parent = expandedMacro.getParent();
        if (!(parent instanceof MetaDataBlock) || !((MetaDataBlock) parent).getMetaData().contains(MODULE_NAME)) {
            MetaDataBlock metaData = new MetaDataBlock(Collections.<Block>emptyList());
            // Use a syntax that supports relative path resource references (we use relative paths to include the
            // temporary files).
            metaData.getMetaData().addMetaData(MetaData.SYNTAX, Syntax.XWIKI_2_1);
            metaData.getMetaData().addMetaData(MODULE_NAME, true);
            parent.replaceChild(metaData, expandedMacro);
            metaData.addChild(expandedMacro);
        }
    }
}
Also used : ExpandedMacroBlock(org.xwiki.rendering.block.ExpandedMacroBlock) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) ExpandedMacroBlock(org.xwiki.rendering.block.ExpandedMacroBlock) ImageBlock(org.xwiki.rendering.block.ImageBlock) Block(org.xwiki.rendering.block.Block) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock)

Example 2 with MetaDataBlock

use of org.xwiki.rendering.block.MetaDataBlock 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 3 with MetaDataBlock

use of org.xwiki.rendering.block.MetaDataBlock in project xwiki-platform by xwiki.

the class DisplayMacroTest method testDisplayMacroWhenDisplayingDocumentWithRelativeReferences.

/**
 * Verify that relative links returned by the display macro as wrapped with a MetaDataBlock.
 */
@Test
public void testDisplayMacroWhenDisplayingDocumentWithRelativeReferences() throws Exception {
    String expected = "beginDocument\n" + "beginMetaData [[base]=[displayedWiki:displayedSpace.displayedPage]" + "[source]=[displayedWiki:displayedSpace.displayedPage][syntax]=[XWiki 2.0]]\n" + "beginParagraph\n" + "beginLink [Typed = [false] Type = [doc] Reference = [page]] [false]\n" + "endLink [Typed = [false] Type = [doc] Reference = [page]] [false]\n" + "onSpace\n" + "beginLink [Typed = [true] Type = [attach] Reference = [test.png]] [false]\n" + "endLink [Typed = [true] Type = [attach] Reference = [test.png]] [false]\n" + "onSpace\n" + "onImage [Typed = [false] Type = [attach] Reference = [test.png]] [true]\n" + "endParagraph\n" + "endMetaData [[base]=[displayedWiki:displayedSpace.displayedPage]" + "[source]=[displayedWiki:displayedSpace.displayedPage][syntax]=[XWiki 2.0]]\n" + "endDocument";
    final DocumentReference displayedDocumentReference = new DocumentReference("displayedWiki", "displayedSpace", "displayedPage");
    setUpDocumentMock("displayedWiki:displayedSpace.displayedPage", displayedDocumentReference, "[[page]] [[attach:test.png]] image:test.png");
    getMockery().checking(new Expectations() {

        {
            oneOf(mockSetup.bridge).isDocumentViewable(with(any(DocumentReference.class)));
            will(returnValue(true));
            oneOf(mockSetup.bridge).pushDocumentInContext(with(any(Map.class)), with(any(DocumentModelBridge.class)));
            oneOf(mockSetup.bridge).getCurrentDocumentReference();
            will(returnValue(displayedDocumentReference));
            oneOf(mockSetup.bridge).popDocumentFromContext(with(any(Map.class)));
        }
    });
    DisplayMacroParameters parameters = new DisplayMacroParameters();
    parameters.setReference("displayedWiki:displayedSpace.displayedPage");
    List<Block> blocks = this.displayMacro.execute(parameters, null, createMacroTransformationContext("whatever", false));
    assertBlocks(expected, blocks, this.rendererFactory);
}
Also used : Expectations(org.jmock.Expectations) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) DisplayMacroParameters(org.xwiki.rendering.macro.display.DisplayMacroParameters) Block(org.xwiki.rendering.block.Block) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) MacroBlock(org.xwiki.rendering.block.MacroBlock) HashMap(java.util.HashMap) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 4 with MetaDataBlock

use of org.xwiki.rendering.block.MetaDataBlock in project xwiki-platform by xwiki.

the class DisplayMacroTest method testDisplayMacroInsideBaseMetaDataBlockAndWithRelativeDocumentReferencePassed.

@Test
public void testDisplayMacroInsideBaseMetaDataBlockAndWithRelativeDocumentReferencePassed() throws Exception {
    String expected = "beginDocument\n" + "beginMetaData [[base]=[wiki:space.relativePage][source]=[wiki:space.relativePage][syntax]=[XWiki 2.0]]\n" + "beginParagraph\n" + "onWord [content]\n" + "endParagraph\n" + "endMetaData [[base]=[wiki:space.relativePage][source]=[wiki:space.relativePage][syntax]=[XWiki 2.0]]\n" + "endDocument";
    DisplayMacroParameters parameters = new DisplayMacroParameters();
    parameters.setReference("relativePage");
    MacroTransformationContext macroContext = createMacroTransformationContext("whatever", false);
    // Add a Source MetaData Block as a parent of the display Macro block.
    new MetaDataBlock(Collections.<Block>singletonList(macroContext.getCurrentMacroBlock()), new MetaData(Collections.<String, Object>singletonMap(MetaData.BASE, "wiki:space.page")));
    final DocumentReference sourceReference = new DocumentReference("wiki", "space", "page");
    final DocumentReference resolvedReference = new DocumentReference("wiki", "space", "relativePage");
    setUpDocumentMock("relativePage", resolvedReference, "content");
    getMockery().checking(new Expectations() {

        {
            allowing(mockDocumentReferenceResolver).resolve(with("wiki:space.page"), with(IsArray.array(any(MacroBlock.class))));
            will(returnValue(sourceReference));
            allowing(mockDocumentReferenceResolver).resolve(with("relativePage"), with(IsArray.array(any(MacroBlock.class))));
            will(returnValue(resolvedReference));
            oneOf(mockSetup.bridge).isDocumentViewable(resolvedReference);
            will(returnValue(true));
            oneOf(mockSetup.bridge).pushDocumentInContext(with(any(Map.class)), with(same(mockDocument)));
            oneOf(mockSetup.bridge).getCurrentDocumentReference();
            will(returnValue(resolvedReference));
            oneOf(mockSetup.bridge).popDocumentFromContext(with(any(Map.class)));
        }
    });
    List<Block> blocks = this.displayMacro.execute(parameters, null, macroContext);
    assertBlocks(expected, blocks, this.rendererFactory);
}
Also used : Expectations(org.jmock.Expectations) MetaData(org.xwiki.rendering.listener.MetaData) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) DisplayMacroParameters(org.xwiki.rendering.macro.display.DisplayMacroParameters) Block(org.xwiki.rendering.block.Block) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) MacroBlock(org.xwiki.rendering.block.MacroBlock) HashMap(java.util.HashMap) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) Test(org.junit.Test)

Example 5 with MetaDataBlock

use of org.xwiki.rendering.block.MetaDataBlock in project xwiki-platform by xwiki.

the class IncludeMacro method execute.

@Override
public List<Block> execute(IncludeMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    // Step 1: Perform checks.
    if (parameters.getReference() == null) {
        throw new MacroExecutionException("You must specify a 'reference' parameter pointing to the entity to include.");
    }
    DocumentReference includedReference = resolve(context.getCurrentMacroBlock(), parameters);
    checkRecursiveInclusion(context.getCurrentMacroBlock(), includedReference);
    if (!this.documentAccessBridge.isDocumentViewable(includedReference)) {
        throw new MacroExecutionException(String.format("Current user [%s] doesn't have view rights on document [%s]", this.documentAccessBridge.getCurrentUserReference(), this.defaultEntityReferenceSerializer.serialize(includedReference)));
    }
    Context parametersContext = parameters.getContext();
    // Step 2: Retrieve the included document.
    DocumentModelBridge documentBridge;
    try {
        documentBridge = this.documentAccessBridge.getDocumentInstance(includedReference);
    } catch (Exception e) {
        throw new MacroExecutionException("Failed to load Document [" + this.defaultEntityReferenceSerializer.serialize(includedReference) + "]", e);
    }
    // Step 3: Display the content of the included document.
    // Check the value of the "context" parameter.
    // 
    // If CONTEXT_NEW then display the content in an isolated execution and transformation context.
    // 
    // if CONTEXT_CURRENT then display the content without performing any transformations (we don't want any Macro
    // to be executed at this stage since they should be executed by the currently running Macro Transformation.
    DocumentDisplayerParameters displayParameters = new DocumentDisplayerParameters();
    displayParameters.setContentTransformed(parametersContext == Context.NEW);
    displayParameters.setExecutionContextIsolated(displayParameters.isContentTransformed());
    displayParameters.setSectionId(parameters.getSection());
    displayParameters.setTransformationContextIsolated(displayParameters.isContentTransformed());
    displayParameters.setTransformationContextRestricted(context.getTransformationContext().isRestricted());
    displayParameters.setTargetSyntax(context.getTransformationContext().getTargetSyntax());
    displayParameters.setContentTranslated(true);
    Stack<Object> references = this.inclusionsBeingExecuted.get();
    if (parametersContext == Context.NEW) {
        if (references == null) {
            references = new Stack<Object>();
            this.inclusionsBeingExecuted.set(references);
        }
        references.push(includedReference);
    }
    XDOM result;
    try {
        result = this.documentDisplayer.display(documentBridge, displayParameters);
    } catch (Exception e) {
        throw new MacroExecutionException(e.getMessage(), e);
    } finally {
        if (parametersContext == Context.NEW) {
            references.pop();
        }
    }
    // Step 4: Wrap Blocks in a MetaDataBlock with the "source" meta data specified so that we know from where the
    // content comes and "base" meta data so that reference are properly resolved
    MetaDataBlock metadata = new MetaDataBlock(result.getChildren(), result.getMetaData());
    String source = this.defaultEntityReferenceSerializer.serialize(includedReference);
    metadata.getMetaData().addMetaData(MetaData.SOURCE, source);
    if (parametersContext == Context.NEW) {
        metadata.getMetaData().addMetaData(MetaData.BASE, source);
    }
    return Arrays.<Block>asList(metadata);
}
Also used : MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) Context(org.xwiki.rendering.macro.include.IncludeMacroParameters.Context) DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) Block(org.xwiki.rendering.block.Block) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) MacroBlock(org.xwiki.rendering.block.MacroBlock) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock)

Aggregations

MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)17 Block (org.xwiki.rendering.block.Block)11 DocumentReference (org.xwiki.model.reference.DocumentReference)10 MacroBlock (org.xwiki.rendering.block.MacroBlock)8 Test (org.junit.Test)7 HashMap (java.util.HashMap)6 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)6 XDOM (org.xwiki.rendering.block.XDOM)6 MetadataBlockMatcher (org.xwiki.rendering.block.match.MetadataBlockMatcher)5 MetaData (org.xwiki.rendering.listener.MetaData)5 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)5 Expectations (org.jmock.Expectations)4 MacroMarkerBlock (org.xwiki.rendering.block.MacroMarkerBlock)4 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)4 Map (java.util.Map)3 EntityReference (org.xwiki.model.reference.EntityReference)3 DocumentDisplayerParameters (org.xwiki.display.internal.DocumentDisplayerParameters)2 AttachmentReference (org.xwiki.model.reference.AttachmentReference)2 ExpandedMacroBlock (org.xwiki.rendering.block.ExpandedMacroBlock)2 ImageBlock (org.xwiki.rendering.block.ImageBlock)2