Search in sources :

Example 26 with MacroBlock

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

the class OfficeMacroImporter method buildXDOM.

/**
 * Builds an XDOM with a single block, the office macro.
 *
 * @param attachmentReference the office file to be viewed
 * @param filterStyles whether to filter text styles such as font, color, alignment, margins, etc.
 * @return the XDOM that can be rendered to view the office file
 */
public XDOM buildXDOM(AttachmentReference attachmentReference, boolean filterStyles) {
    Map<String, String> macroParams = new HashMap<String, String>();
    macroParams.put("attachment", attachmentReference.getName());
    if (!filterStyles) {
        macroParams.put("filterStyles", "false");
    }
    MacroBlock officeMacro = new MacroBlock("office", macroParams, false);
    XDOM xdom = new XDOM(Collections.<Block>singletonList(officeMacro));
    // Since we're generating an XDOM block we need to set up the required MetaData information
    // Set the BASE MetaData
    xdom.getMetaData().addMetaData(MetaData.BASE, entityReferenceSerializer.serialize(attachmentReference.getDocumentReference()));
    // Set the SYNTAX MetaData
    try {
        DocumentModelBridge document = documentAccessBridge.getTranslatedDocumentInstance(attachmentReference.getDocumentReference());
        xdom.getMetaData().addMetaData(MetaData.SYNTAX, document.getSyntax());
    } catch (Exception e) {
        throw new RuntimeException(String.format("Failed to compute Syntax for the document containing attachment [%s]", attachmentReference), e);
    }
    return xdom;
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) HashMap(java.util.HashMap) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 27 with MacroBlock

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

the class VelocityMacroSecurityTest method testRestrictedByContext.

@Test(expected = MacroExecutionException.class)
public void testRestrictedByContext() throws Exception {
    VelocityMacroParameters params = new VelocityMacroParameters();
    MacroTransformationContext context = new MacroTransformationContext();
    context.setSyntax(Syntax.XWIKI_2_0);
    context.setCurrentMacroBlock(new MacroBlock("velocity", Collections.<String, String>emptyMap(), false));
    context.setId("page1");
    // Restrict the transformation context.
    context.getTransformationContext().setRestricted(true);
    when(authorizationManager.hasAccess(Right.SCRIPT)).thenReturn(true);
    mocker.getComponentUnderTest().execute(params, "#macro(testMacrosAreLocal)mymacro#end", context);
}
Also used : MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroBlock(org.xwiki.rendering.block.MacroBlock) Test(org.junit.Test)

Example 28 with MacroBlock

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

the class VelocityMacroSecurityTest method testRestrictedByRights.

@Test(expected = MacroExecutionException.class)
public void testRestrictedByRights() throws Exception {
    VelocityMacroParameters params = new VelocityMacroParameters();
    MacroTransformationContext context = new MacroTransformationContext();
    context.setSyntax(Syntax.XWIKI_2_0);
    context.setCurrentMacroBlock(new MacroBlock("velocity", Collections.<String, String>emptyMap(), false));
    context.setId("page1");
    context.getTransformationContext().setRestricted(false);
    // Restrict the SCRIPT right.
    when(authorizationManager.hasAccess(Right.SCRIPT)).thenReturn(false);
    mocker.getComponentUnderTest().execute(params, "#macro(testMacrosAreLocal)mymacro#end", context);
    verify(authorizationManager.hasAccess(Right.SCRIPT));
}
Also used : MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroBlock(org.xwiki.rendering.block.MacroBlock) Test(org.junit.Test)

Example 29 with MacroBlock

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

the class DefaultWikiMacro method execute.

@Override
public List<Block> execute(WikiMacroParameters parameters, String macroContent, MacroTransformationContext context) throws MacroExecutionException {
    validate(parameters, macroContent);
    // Parse the wiki macro content.
    XDOM xdom = prepareWikiMacroContent(context);
    // Prepare macro context.
    Map<String, Object> macroBinding = new HashMap<>();
    macroBinding.put(MACRO_PARAMS_KEY, parameters);
    macroBinding.put(MACRO_CONTENT_KEY, macroContent);
    macroBinding.put(MACRO_DESCRIPTOR_KEY, getDescriptor());
    macroBinding.put(MACRO_CONTEXT_KEY, context);
    macroBinding.put(MACRO_RESULT_KEY, null);
    // Extension point to add more wiki macro bindings
    try {
        List<WikiMacroBindingInitializer> bindingInitializers = this.componentManager.getInstanceList(WikiMacroBindingInitializer.class);
        for (WikiMacroBindingInitializer bindingInitializer : bindingInitializers) {
            bindingInitializer.initialize(this.macroDocumentReference, parameters, macroContent, context, macroBinding);
        }
    } catch (ComponentLookupException e) {
    // TODO: we should probably log something but that should never happen
    }
    // Execute the macro
    ObservationManager observation = null;
    try {
        observation = this.componentManager.getInstance(ObservationManager.class);
    } catch (ComponentLookupException e) {
    // TODO: maybe log something
    }
    // Get XWiki context
    Map<String, Object> xwikiContext = null;
    try {
        Execution execution = this.componentManager.getInstance(Execution.class);
        ExecutionContext econtext = execution.getContext();
        if (econtext != null) {
            xwikiContext = (Map<String, Object>) execution.getContext().getProperty("xwikicontext");
        }
    } catch (ComponentLookupException e) {
    // TODO: maybe log something
    }
    try {
        Transformation macroTransformation = this.componentManager.getInstance(Transformation.class, MACRO_HINT);
        if (xwikiContext != null) {
            // Place macro context inside xwiki context ($xcontext.macro).
            xwikiContext.put(MACRO_KEY, macroBinding);
        }
        MacroBlock wikiMacroBlock = context.getCurrentMacroBlock();
        MacroMarkerBlock wikiMacroMarker = new MacroMarkerBlock(wikiMacroBlock.getId(), wikiMacroBlock.getParameters(), wikiMacroBlock.getContent(), xdom.getChildren(), wikiMacroBlock.isInline());
        // Make sure to use provided metadatas
        MetaDataBlock metaDataBlock = new MetaDataBlock(Collections.<Block>singletonList(wikiMacroMarker), xdom.getMetaData());
        // Make sure the context XDOM contains the wiki macro content
        wikiMacroBlock.getParent().replaceChild(metaDataBlock, wikiMacroBlock);
        // "Emulate" the fact that wiki macro block is still part of the XDOM (what is in the XDOM is a
        // MacroMarkerBlock and MacroTransformationContext current macro block only support MacroBlock so we can't
        // switch it without breaking some APIs)
        wikiMacroBlock.setParent(metaDataBlock.getParent());
        wikiMacroBlock.setNextSiblingBlock(metaDataBlock.getNextSibling());
        wikiMacroBlock.setPreviousSiblingBlock(metaDataBlock.getPreviousSibling());
        try {
            if (observation != null) {
                observation.notify(STARTEXECUTION_EVENT, this, macroBinding);
            }
            // Perform internal macro transformations.
            TransformationContext txContext = new TransformationContext(context.getXDOM(), this.syntax);
            txContext.setId(context.getId());
            RenderingContext renderingContext = componentManager.getInstance(RenderingContext.class);
            ((MutableRenderingContext) renderingContext).transformInContext(macroTransformation, txContext, wikiMacroMarker);
        } finally {
            // Restore context XDOM to its previous state
            metaDataBlock.getParent().replaceChild(wikiMacroBlock, metaDataBlock);
        }
        return extractResult(wikiMacroMarker.getChildren(), macroBinding, context);
    } catch (Exception ex) {
        throw new MacroExecutionException("Error while performing internal macro transformations", ex);
    } finally {
        if (xwikiContext != null) {
            xwikiContext.remove(MACRO_KEY);
        }
        if (observation != null) {
            observation.notify(ENDEXECUTION_EVENT, this);
        }
    }
}
Also used : RenderingContext(org.xwiki.rendering.transformation.RenderingContext) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext) Transformation(org.xwiki.rendering.transformation.Transformation) XDOM(org.xwiki.rendering.block.XDOM) HashMap(java.util.HashMap) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) ObservationManager(org.xwiki.observation.ObservationManager) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) MacroParameterException(org.xwiki.rendering.macro.parameter.MacroParameterException) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext) MacroBlock(org.xwiki.rendering.block.MacroBlock) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock)

Example 30 with MacroBlock

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

the class MacroBlockDumperTest method testDumpMacroMarkerBlockWithSource.

@Test
public void testDumpMacroMarkerBlockWithSource() throws Exception {
    Block macro1 = new MacroBlock("macro", params, "content", false);
    Block macro2 = new MacroMarkerBlock("macro", params, "content", Collections.<Block>emptyList(), false);
    XDOM xdom1 = new XDOM(Collections.singletonList(macro1));
    XDOM xdom2 = new XDOM(Collections.singletonList(macro2));
    xdom1.getMetaData().addMetaData(MetaData.SOURCE, "source");
    xdom2.getMetaData().addMetaData(MetaData.SOURCE, "source");
    byte[] dump1 = dumper.dump(macro1);
    byte[] dump2 = dumper.dump(macro2);
    assertThat(dump1, equalTo(dump2));
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) MacroBlock(org.xwiki.rendering.block.MacroBlock) WordBlock(org.xwiki.rendering.block.WordBlock) Block(org.xwiki.rendering.block.Block) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) 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