Search in sources :

Example 21 with MacroBlock

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

the class DefaultLinkedResourceHelper method getResourceType.

@Override
public ResourceType getResourceType(Block block) {
    // Determine the reference string and reference type for each block type.
    ResourceType resourceType = null;
    if (block instanceof LinkBlock) {
        LinkBlock linkBlock = (LinkBlock) block;
        ResourceReference linkReference = linkBlock.getReference();
        resourceType = linkReference.getType();
    } else if (block instanceof MacroBlock) {
        // We still have to look at the reference string to see if it is a valid include (i.e. non-recursive).
        String referenceString = block.getParameter(REFERENCE_MACRO_PARAMETER);
        if (StringUtils.isBlank(referenceString)) {
            referenceString = block.getParameter(DOCUMENT_MACRO_PARAMETER);
        }
        if (StringUtils.isBlank(referenceString)) {
            // Skip it.
            return null;
        }
        // FIXME: this may be SPACE once we start hiding "WebHome" from macro reference parameters.
        resourceType = ResourceType.DOCUMENT;
    }
    return resourceType;
}
Also used : LinkBlock(org.xwiki.rendering.block.LinkBlock) ResourceType(org.xwiki.rendering.listener.reference.ResourceType) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 22 with MacroBlock

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

the class DefaultLinkedResourceHelper method getResourceReferenceString.

@Override
public String getResourceReferenceString(Block block) {
    // Determine the reference string and reference type for each block type.
    String referenceString = null;
    if (block instanceof LinkBlock) {
        LinkBlock linkBlock = (LinkBlock) block;
        ResourceReference linkReference = linkBlock.getReference();
        referenceString = linkReference.getReference();
    } else if (block instanceof MacroBlock) {
        referenceString = block.getParameter(REFERENCE_MACRO_PARAMETER);
        if (StringUtils.isBlank(referenceString)) {
            referenceString = block.getParameter(DOCUMENT_MACRO_PARAMETER);
        }
        if (StringUtils.isBlank(referenceString)) {
            // If the reference is not set or is empty, we have a recursive include which is not valid anyway.
            // Skip it.
            referenceString = null;
        }
    }
    return referenceString;
}
Also used : LinkBlock(org.xwiki.rendering.block.LinkBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 23 with MacroBlock

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

the class IncludeMacroTest method createMacroTransformationContext.

private MacroTransformationContext createMacroTransformationContext(String documentName, boolean isInline) {
    MacroTransformationContext context = new MacroTransformationContext();
    MacroBlock includeMacro = new MacroBlock("include", Collections.singletonMap("reference", documentName), isInline);
    context.setCurrentMacroBlock(includeMacro);
    return context;
}
Also used : MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 24 with MacroBlock

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

the class ContextMacroTest method executeOk.

@Test
public void executeOk() throws Exception {
    MacroBlock macroBlock = new MacroBlock("context", Collections.<String, String>emptyMap(), false);
    MacroTransformationContext macroContext = new MacroTransformationContext();
    macroContext.setSyntax(Syntax.XWIKI_2_0);
    macroContext.setCurrentMacroBlock(macroBlock);
    DocumentReferenceResolver<String> resolver = this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "macro");
    DocumentReference referencedDocumentReference = new DocumentReference("wiki", "space", "page");
    when(resolver.resolve("wiki:space.page", macroBlock)).thenReturn(referencedDocumentReference);
    DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
    DocumentModelBridge dmb = mock(DocumentModelBridge.class);
    when(dab.getTranslatedDocumentInstance(referencedDocumentReference)).thenReturn(dmb);
    MacroContentParser parser = this.mocker.getInstance(MacroContentParser.class);
    XDOM xdom = new XDOM(Arrays.asList((Block) new ParagraphBlock(Arrays.asList((Block) new LinkBlock(Collections.emptyList(), new ResourceReference("", ResourceType.DOCUMENT), false)))));
    when(parser.parse(eq(""), same(macroContext), eq(false), any(MetaData.class), eq(false))).thenReturn(xdom);
    ContextMacroParameters parameters = new ContextMacroParameters();
    parameters.setDocument("wiki:space.page");
    // Note: we're not testing the returned value here since this is done in integation tests.
    this.mocker.getComponentUnderTest().execute(parameters, "", macroContext);
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) MacroContentParser(org.xwiki.rendering.macro.MacroContentParser) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) LinkBlock(org.xwiki.rendering.block.LinkBlock) MetaData(org.xwiki.rendering.listener.MetaData) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) LinkBlock(org.xwiki.rendering.block.LinkBlock) Block(org.xwiki.rendering.block.Block) MacroBlock(org.xwiki.rendering.block.MacroBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) ContextMacroParameters(org.xwiki.rendering.macro.context.ContextMacroParameters) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroBlock(org.xwiki.rendering.block.MacroBlock) Test(org.junit.Test)

Example 25 with MacroBlock

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

the class ContextMacroTest method executeWithReferencedDocumentHavingProgrammingRightsAndCallingDocumentToo.

@Test
public void executeWithReferencedDocumentHavingProgrammingRightsAndCallingDocumentToo() throws Exception {
    MacroBlock macroBlock = new MacroBlock("context", Collections.<String, String>emptyMap(), false);
    MacroTransformationContext macroContext = new MacroTransformationContext();
    macroContext.setSyntax(Syntax.XWIKI_2_0);
    macroContext.setCurrentMacroBlock(macroBlock);
    DocumentReferenceResolver<String> resolver = this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "macro");
    DocumentReference referencedDocumentReference = new DocumentReference("wiki", "space", "page");
    when(resolver.resolve("wiki:space.page", macroBlock)).thenReturn(referencedDocumentReference);
    DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
    when(dab.hasProgrammingRights()).thenReturn(true).thenReturn(true);
    DocumentModelBridge dmb = mock(DocumentModelBridge.class);
    when(dab.getTranslatedDocumentInstance(referencedDocumentReference)).thenReturn(dmb);
    MacroContentParser parser = this.mocker.getInstance(MacroContentParser.class);
    when(parser.parse(eq(""), same(macroContext), eq(false), any(MetaData.class), eq(false))).thenReturn(new XDOM(Collections.emptyList()));
    ContextMacroParameters parameters = new ContextMacroParameters();
    parameters.setDocument("wiki:space.page");
    this.mocker.getComponentUnderTest().execute(parameters, "", macroContext);
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) MacroContentParser(org.xwiki.rendering.macro.MacroContentParser) MetaData(org.xwiki.rendering.listener.MetaData) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) ContextMacroParameters(org.xwiki.rendering.macro.context.ContextMacroParameters) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroBlock(org.xwiki.rendering.block.MacroBlock) Test(org.junit.Test)

Aggregations

MacroBlock (org.xwiki.rendering.block.MacroBlock)30 Test (org.junit.Test)14 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)13 XDOM (org.xwiki.rendering.block.XDOM)12 DocumentReference (org.xwiki.model.reference.DocumentReference)9 Block (org.xwiki.rendering.block.Block)8 LinkBlock (org.xwiki.rendering.block.LinkBlock)7 MacroMarkerBlock (org.xwiki.rendering.block.MacroMarkerBlock)7 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)7 HashMap (java.util.HashMap)5 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)5 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)4 MetaData (org.xwiki.rendering.listener.MetaData)4 ContextMacroParameters (org.xwiki.rendering.macro.context.ContextMacroParameters)4 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)3 WordBlock (org.xwiki.rendering.block.WordBlock)3 MacroContentParser (org.xwiki.rendering.macro.MacroContentParser)3 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)3 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 ToString (org.suigeneris.jrcs.util.ToString)2