Search in sources :

Example 1 with Context

use of org.xwiki.rendering.macro.include.IncludeMacroParameters.Context 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

DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)1 DocumentDisplayerParameters (org.xwiki.display.internal.DocumentDisplayerParameters)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 Block (org.xwiki.rendering.block.Block)1 MacroBlock (org.xwiki.rendering.block.MacroBlock)1 MacroMarkerBlock (org.xwiki.rendering.block.MacroMarkerBlock)1 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)1 XDOM (org.xwiki.rendering.block.XDOM)1 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)1 Context (org.xwiki.rendering.macro.include.IncludeMacroParameters.Context)1 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)1