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);
}
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);
}
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);
}
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;
}
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());
}
Aggregations