Search in sources :

Example 1 with MacroLookupException

use of org.xwiki.rendering.macro.MacroLookupException 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);
        }
    }
}
Also used : ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) MacroId(org.xwiki.rendering.macro.MacroId) ScriptMacro(org.xwiki.rendering.macro.script.ScriptMacro) MacroLookupException(org.xwiki.rendering.macro.MacroLookupException)

Aggregations

MacroMarkerBlock (org.xwiki.rendering.block.MacroMarkerBlock)1 ClassBlockMatcher (org.xwiki.rendering.block.match.ClassBlockMatcher)1 MacroId (org.xwiki.rendering.macro.MacroId)1 MacroLookupException (org.xwiki.rendering.macro.MacroLookupException)1 ScriptMacro (org.xwiki.rendering.macro.script.ScriptMacro)1