Search in sources :

Example 11 with MetaDataBlock

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

the class CurrentMacroEntityReferenceResolver method resolve.

@Override
public EntityReference resolve(String representation, EntityType entityType, Object... parameters) {
    // There must one parameter and it must be of type Block
    if (parameters.length != 1 || !(parameters[0] instanceof Block)) {
        throw new IllegalArgumentException(String.format("You must pass one parameter of type [%s]", Block.class.getName()));
    }
    Block currentBlock = (Block) parameters[0];
    EntityReference result;
    MetaDataBlock metaDataBlock = currentBlock.getFirstBlock(new MetadataBlockMatcher(MetaData.BASE), Block.Axes.ANCESTOR);
    // If no Source MetaData was found resolve against the current entity as a failsafe solution.
    if (metaDataBlock == null) {
        result = this.currentEntityReferenceResolver.resolve(representation, entityType);
    } else {
        String sourceMetaData = (String) metaDataBlock.getMetaData().getMetaData(MetaData.BASE);
        result = this.currentEntityReferenceResolver.resolve(representation, entityType, this.currentEntityReferenceResolver.resolve(sourceMetaData, EntityType.DOCUMENT));
    }
    return result;
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference) MetadataBlockMatcher(org.xwiki.rendering.block.match.MetadataBlockMatcher) Block(org.xwiki.rendering.block.Block) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock)

Example 12 with MetaDataBlock

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

the class CurrentMacroEntityReferenceResolverTest method resolveWhenMetaDataBlock.

@Test
public void resolveWhenMetaDataBlock() throws Exception {
    DocumentReference baseReference = new DocumentReference("basewiki", "basespace", "basepage");
    EntityReference expectedReference = new AttachmentReference("file", baseReference);
    EntityReferenceResolver<String> currentEntityReferenceResolver = mocker.getInstance(EntityReferenceResolver.TYPE_STRING, "current");
    when(currentEntityReferenceResolver.resolve("basewiki:basespace.basepage", EntityType.DOCUMENT)).thenReturn(baseReference);
    when(currentEntityReferenceResolver.resolve("file", EntityType.ATTACHMENT, baseReference)).thenReturn(expectedReference);
    Block wordBlock = new WordBlock("whatever");
    MetaData metaData = new MetaData(Collections.<String, Object>singletonMap(MetaData.BASE, "basewiki:basespace.basepage"));
    new XDOM(Arrays.<Block>asList(new MetaDataBlock(Arrays.<Block>asList(wordBlock), metaData)));
    Assert.assertEquals(expectedReference, mocker.getComponentUnderTest().resolve("file", EntityType.ATTACHMENT, wordBlock));
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XDOM(org.xwiki.rendering.block.XDOM) MetaData(org.xwiki.rendering.listener.MetaData) WordBlock(org.xwiki.rendering.block.WordBlock) EntityReference(org.xwiki.model.reference.EntityReference) WordBlock(org.xwiki.rendering.block.WordBlock) Block(org.xwiki.rendering.block.Block) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) DocumentReference(org.xwiki.model.reference.DocumentReference) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) Test(org.junit.Test)

Example 13 with MetaDataBlock

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

the class DisplayMacro method execute.

@Override
public List<Block> execute(DisplayMacroParameters 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 display.");
    }
    DocumentReference includedReference = resolve(context.getCurrentMacroBlock(), parameters);
    checkRecursiveDisplay(context.getCurrentMacroBlock(), includedReference);
    if (!this.documentAccessBridge.isDocumentViewable(includedReference)) {
        throw new MacroExecutionException("Current user [" + this.documentAccessBridge.getCurrentUserReference() + "] doesn't have view rights on document [" + this.defaultEntityReferenceSerializer.serialize(includedReference) + "]");
    }
    // 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.
    // Display the content in an isolated execution and transformation context.
    DocumentDisplayerParameters displayParameters = new DocumentDisplayerParameters();
    displayParameters.setContentTransformed(true);
    displayParameters.setExecutionContextIsolated(displayParameters.isContentTransformed());
    displayParameters.setSectionId(parameters.getSection());
    displayParameters.setTransformationContextIsolated(displayParameters.isContentTransformed());
    displayParameters.setTargetSyntax(context.getTransformationContext().getTargetSyntax());
    displayParameters.setContentTranslated(true);
    Stack<Object> references = this.displaysBeingExecuted.get();
    if (references == null) {
        references = new Stack<Object>();
        this.displaysBeingExecuted.set(references);
    }
    references.push(includedReference);
    XDOM result;
    try {
        result = this.documentDisplayer.display(documentBridge, displayParameters);
    } catch (Exception e) {
        throw new MacroExecutionException(e.getMessage(), e);
    } finally {
        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);
    metadata.getMetaData().addMetaData(MetaData.BASE, source);
    return Arrays.<Block>asList(metadata);
}
Also used : DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) MacroBlock(org.xwiki.rendering.block.MacroBlock) Block(org.xwiki.rendering.block.Block) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock)

Example 14 with MetaDataBlock

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

the class IncludeMacroTest method testIncludeMacroInsideSourceMetaDataBlockAndWithRelativeDocumentReferencePassed.

@Test
public void testIncludeMacroInsideSourceMetaDataBlockAndWithRelativeDocumentReferencePassed() throws Exception {
    String expected = "beginDocument\n" + "beginMetaData [[source]=[wiki:space.relativePage][syntax]=[XWiki 2.0]]\n" + "beginParagraph\n" + "onWord [content]\n" + "endParagraph\n" + "endMetaData [[source]=[wiki:space.relativePage][syntax]=[XWiki 2.0]]\n" + "endDocument";
    IncludeMacroParameters parameters = new IncludeMacroParameters();
    parameters.setReference("relativePage");
    final MacroTransformationContext macroContext = createMacroTransformationContext("whatever", false);
    // Add a Source MetaData Block as a parent of the include 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");
    final DocumentModelBridge mockDocument = getMockery().mock(DocumentModelBridge.class);
    getMockery().checking(new Expectations() {

        {
            oneOf(mockDocumentReferenceResolver).resolve("relativePage", macroContext.getCurrentMacroBlock());
            will(returnValue(resolvedReference));
            oneOf(mockSetup.bridge).isDocumentViewable(resolvedReference);
            will(returnValue(true));
            oneOf(mockSetup.bridge).getDocumentInstance(resolvedReference);
            will(returnValue(mockDocument));
            oneOf(mockSetup.bridge).getTranslatedDocumentInstance(resolvedReference);
            will(returnValue(mockDocument));
            oneOf(mockSetup.bridge).getCurrentDocumentReference();
            will(returnValue(sourceReference));
            oneOf(mockDocument).getXDOM();
            will(returnValue(getXDOM("content")));
            oneOf(mockDocument).getSyntax();
            will(returnValue(Syntax.XWIKI_2_0));
            oneOf(mockDocument).getDocumentReference();
            will(returnValue(resolvedReference));
            allowing(mockDocument).getRealLanguage();
            will(returnValue(""));
        }
    });
    List<Block> blocks = this.includeMacro.execute(parameters, null, macroContext);
    assertBlocks(expected, blocks, this.rendererFactory);
}
Also used : Expectations(org.jmock.Expectations) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) MetaData(org.xwiki.rendering.listener.MetaData) IncludeMacroParameters(org.xwiki.rendering.macro.include.IncludeMacroParameters) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) 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) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) Test(org.junit.Test)

Example 15 with MetaDataBlock

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

the class IncludeMacroTest method testIncludeMacroWhenIncludingDocumentWithRelativeReferences.

/**
 * Verify that relative links returned by the Include macro as wrapped with a MetaDataBlock.
 */
@Test
public void testIncludeMacroWhenIncludingDocumentWithRelativeReferences() throws Exception {
    String expected = "beginDocument\n" + "beginMetaData [[base]=[includedWiki:includedSpace.includedPage]" + "[source]=[includedWiki:includedSpace.includedPage][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]=[includedWiki:includedSpace.includedPage]" + "[source]=[includedWiki:includedSpace.includedPage][syntax]=[XWiki 2.0]]\n" + "endDocument";
    final DocumentReference includedDocumentReference = new DocumentReference("includedWiki", "includedSpace", "includedPage");
    setUpDocumentMock("includedWiki:includedSpace.includedPage", includedDocumentReference, "[[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(includedDocumentReference));
            oneOf(mockSetup.bridge).popDocumentFromContext(with(any(Map.class)));
        }
    });
    IncludeMacroParameters parameters = new IncludeMacroParameters();
    parameters.setReference("includedWiki:includedSpace.includedPage");
    parameters.setContext(Context.NEW);
    List<Block> blocks = this.includeMacro.execute(parameters, null, createMacroTransformationContext("whatever", false));
    assertBlocks(expected, blocks, this.rendererFactory);
}
Also used : Expectations(org.jmock.Expectations) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) IncludeMacroParameters(org.xwiki.rendering.macro.include.IncludeMacroParameters) Block(org.xwiki.rendering.block.Block) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) 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)

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