Search in sources :

Example 6 with MacroTransformationContext

use of org.xwiki.rendering.transformation.MacroTransformationContext in project xwiki-platform by xwiki.

the class ClassLoadingTest method registerComponents.

@Override
protected void registerComponents() throws Exception {
    super.registerComponents();
    this.mockSetup = new ScriptMockSetup(getComponentManager());
    this.macro = getComponentManager().getInstance(Macro.class, "groovy");
    this.context = new MacroTransformationContext();
    // The script macro checks the current block (which is a macro block) to see what engine to use
    this.context.setCurrentMacroBlock(new MacroBlock("groovy", Collections.<String, String>emptyMap(), false));
    // Set the syntax since the script macro needs it to parse the script result using that syntax
    this.context.setSyntax(Syntax.XWIKI_2_0);
}
Also used : ScriptMockSetup(org.xwiki.rendering.macro.script.ScriptMockSetup) Macro(org.xwiki.rendering.macro.Macro) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 7 with MacroTransformationContext

use of org.xwiki.rendering.transformation.MacroTransformationContext in project xwiki-platform by xwiki.

the class SecurityTest method executeGroovyMacro.

private void executeGroovyMacro(String script, boolean restricted) throws Exception {
    Macro macro = getComponentManager().getInstance(Macro.class, "groovy");
    JSR223ScriptMacroParameters parameters = new JSR223ScriptMacroParameters();
    MacroTransformationContext context = new MacroTransformationContext();
    context.getTransformationContext().setRestricted(restricted);
    context.setSyntax(Syntax.XWIKI_2_1);
    // The script macro checks the current block (which is a macro block) to see what engine to use.
    context.setCurrentMacroBlock(new MacroBlock("groovy", Collections.<String, String>emptyMap(), false));
    macro.execute(parameters, script, context);
}
Also used : Macro(org.xwiki.rendering.macro.Macro) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) JSR223ScriptMacroParameters(org.xwiki.rendering.macro.script.JSR223ScriptMacroParameters) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 8 with MacroTransformationContext

use of org.xwiki.rendering.transformation.MacroTransformationContext 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)

Example 9 with MacroTransformationContext

use of org.xwiki.rendering.transformation.MacroTransformationContext in project xwiki-platform by xwiki.

the class NestedScriptMacroValidatorTest method buildContext.

/**
 * Build a chain of nested macros ({@link MacroMarkerBlock} blocks) and put them into a macro transformation
 * context. The chain will be the only child of a top level XDOM, the last child will be the current
 * {@link MacroBlock}.
 *
 * @param chain list of nested macro names (starting with parent)
 * @return an initialized macro transformation context
 */
private MacroTransformationContext buildContext(String... chain) {
    MacroTransformationContext context = new MacroTransformationContext();
    if (chain == null || chain.length < 1) {
        context.setXDOM(new XDOM(new LinkedList<Block>()));
        return context;
    }
    Map<String, String> parameters = new HashMap<String, String>();
    MacroBlock child = new MacroBlock(chain[chain.length - 1], parameters, false);
    Block current = child;
    for (int i = chain.length - 2; i >= 0; i--) {
        Block parent = new MacroMarkerBlock(chain[i], parameters, Collections.singletonList(current), false);
        current = parent;
    }
    XDOM root = new XDOM(Collections.singletonList(current));
    context.setXDOM(root);
    context.setCurrentMacroBlock(child);
    return context;
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) HashMap(java.util.HashMap) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) MacroBlock(org.xwiki.rendering.block.MacroBlock) Block(org.xwiki.rendering.block.Block) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) LinkedList(java.util.LinkedList) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 10 with MacroTransformationContext

use of org.xwiki.rendering.transformation.MacroTransformationContext in project xwiki-platform by xwiki.

the class NestedScriptMacroValidatorTest method testNoNestedScriptInHtml.

@Test
public void testNoNestedScriptInHtml() throws Exception {
    MacroTransformationContext context = buildContext("script", "html", "script");
    CancelableEvent event = new ScriptEvaluatingEvent();
    this.validator.onEvent(event, context, null);
    Assert.assertTrue(event.isCanceled());
}
Also used : ScriptEvaluatingEvent(org.xwiki.script.event.ScriptEvaluatingEvent) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) CancelableEvent(org.xwiki.observation.event.CancelableEvent) Test(org.junit.Test)

Aggregations

MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)41 Test (org.junit.Test)30 MacroBlock (org.xwiki.rendering.block.MacroBlock)17 DocumentReference (org.xwiki.model.reference.DocumentReference)15 Block (org.xwiki.rendering.block.Block)13 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)10 XDOM (org.xwiki.rendering.block.XDOM)10 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)10 Expectations (org.jmock.Expectations)9 HashMap (java.util.HashMap)6 MacroMarkerBlock (org.xwiki.rendering.block.MacroMarkerBlock)6 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)6 MetaData (org.xwiki.rendering.listener.MetaData)6 CacheMacroParameters (org.xwiki.rendering.macro.cache.CacheMacroParameters)6 IncludeMacroParameters (org.xwiki.rendering.macro.include.IncludeMacroParameters)6 ContextMacroParameters (org.xwiki.rendering.macro.context.ContextMacroParameters)5 Transformation (org.xwiki.rendering.transformation.Transformation)5 TransformationContext (org.xwiki.rendering.transformation.TransformationContext)5 StringWriter (java.io.StringWriter)4 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)4