use of org.xwiki.rendering.macro.script.ScriptMacro in project xwiki-platform by xwiki.
the class NestedScriptMacroValidatorListener method check.
@Override
protected void check(CancelableEvent event, MacroTransformationContext context, ScriptMacroParameters parameters) {
// Traverse the XDOM tree up to the root
if (context.getCurrentMacroBlock() != null) {
MacroMarkerBlock parent = context.getCurrentMacroBlock().getFirstBlock(new ClassBlockMatcher(MacroMarkerBlock.class), Block.Axes.ANCESTOR);
while (parent != null) {
String parentId = parent.getId();
try {
Macro<?> macro = this.macroManager.getMacro(new MacroId(parentId));
if (macro instanceof ScriptMacro) {
// Find the
event.cancel(String.format("Nested scripts are not allowed. Current Script Macro [%s] " + "(source [%s]) is executed inside Script Macro [%s] (source [%s])", context.getCurrentMacroBlock().getId(), extractSourceContentReference(context.getCurrentMacroBlock()), parentId, extractSourceContentReference(parent)));
} else if (macro instanceof NestedScriptMacroEnabled) {
// This macro has the right to produce script macro whatever the parent.
return;
} else if ("include".equals(parentId)) {
// use NestedScriptMacroEnabled, we should maybe find something more generic
return;
}
} catch (MacroLookupException exception) {
// Shouldn't happen, the parent macro was already successfully executed earlier
}
parent = parent.getFirstBlock(new ClassBlockMatcher(MacroMarkerBlock.class), Block.Axes.ANCESTOR);
}
}
}
Aggregations