/** * {@inheritDoc} * * @see org.xwiki.rendering.block.Block#getChildrenByType(java.lang.Class, boolean) */ public <T extends Block> List<T> getChildrenByType(Class<T> blockClass, boolean recurse) { List<T> typedBlocks = new ArrayList<T>(); for (Block block : getChildren()) { if (blockClass.isAssignableFrom(block.getClass())) { typedBlocks.add(blockClass.cast(block)); } if (recurse && !block.getChildren().isEmpty()) { typedBlocks.addAll(block.getChildrenByType(blockClass, true)); } } return typedBlocks; }
/** * {@inheritDoc} * * @see org.xwiki.rendering.transformation.Transformation#transform(org.xwiki.rendering.block.Block, * org.xwiki.rendering.transformation.TransformationContext) */ public void transform(Block rootBlock, TransformationContext context) throws TransformationException { // Create a macro execution context with all the information required for macros. MacroTransformationContext macroContext = new MacroTransformationContext(context); macroContext.setTransformation(this); // Counter to prevent infinite recursion if a macro generates the same macro for example. int executions = 0; List<MacroBlock> macroBlocks = rootBlock.getChildrenByType(MacroBlock.class, true); while (!macroBlocks.isEmpty() && executions < this.maxMacroExecutions) { transformOnce(rootBlock, macroContext, context.getSyntax()); // TODO: Make this less inefficient by caching the blocks list. macroBlocks = rootBlock.getChildrenByType(MacroBlock.class, true); executions++; } }
/** * @return the macro with the highest priority for the passed syntax or null if no macro is found */ private MacroHolder getHighestPriorityMacro(Block rootBlock, Syntax syntax) { List<MacroHolder> macroHolders = new ArrayList<MacroHolder>(); // 1) Sort the macros by priority to find the highest priority macro to execute for (MacroBlock macroBlock : rootBlock.getChildrenByType(MacroBlock.class, true)) { try { Macro< ? > macro = this.macroManager.getMacro(new MacroId(macroBlock.getId(), syntax)); macroHolders.add(new MacroHolder(macro, macroBlock)); } catch (MacroLookupException e) { // Macro cannot be found. Generate an error message instead of the macro execution result. // TODO: make it internationalized generateError(macroBlock, "Unknown macro: " + macroBlock.getId(), "The \"" + macroBlock.getId() + "\" macro is not in the list of registered macros. Verify the " + "spelling or contact your administrator."); getLogger().debug("Failed to locate the [" + macroBlock.getId() + "] macro. Ignoring it."); } } // Sort the Macros by priority Collections.sort(macroHolders); return macroHolders.size() > 0 ? macroHolders.get(0) : null; }