/** * Method to overwrite to indicate the script engine name. * * @param parameters the macro parameters. * @param context the context of the macro transformation. * @return the name of the script engine to use. */ protected String getScriptEngineName(P parameters, MacroTransformationContext context) { return context.getCurrentMacroBlock().getId().toLowerCase(); }
/** * Method to overwrite to indicate the script engine name. * * @param parameters the macro parameters. * @param context the context of the macro transformation. * @return the name of the script engine to use. */ protected String getScriptEngineName(P parameters, MacroTransformationContext context) { return context.getCurrentMacroBlock().getId().toLowerCase(); }
@Override protected String getScriptEngineName(DefaultScriptMacroParameters parameters, MacroTransformationContext context) { String engineName; if (this.language == null) { String macroName = context.getCurrentMacroBlock().getId().toLowerCase(); if ("script".equals(macroName)) { engineName = parameters.getLanguage(); } else { engineName = macroName; } } else { engineName = this.language; } return engineName; } }
/** * {@inheritDoc} * * @see org.xwiki.rendering.macro.script.AbstractJSR223ScriptMacro#getScriptEngineName(org.xwiki.rendering.macro.script.JSR223ScriptMacroParameters, * org.xwiki.rendering.transformation.MacroTransformationContext) */ @Override protected String getScriptEngineName(DefaultScriptMacroParameters parameters, MacroTransformationContext context) { String engineName; if (this.language == null) { String macroName = context.getCurrentMacroBlock().getId().toLowerCase(); if ("script".equals(macroName)) { engineName = parameters.getLanguage(); } else { engineName = macroName; } } else { engineName = this.language; } return engineName; } }
/** * @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; }
/** * Wrap the output of a macro block with a {@link MacroMarkerBlock}. * * @param macroBlockToWrap the block that should be replaced * @param newBlocks list of blocks to wrap * @return the wrapper */ private Block wrapInMacroMarker(MacroBlock macroBlockToWrap, List<Block> newBlocks) { return new MacroMarkerBlock(macroBlockToWrap.getId(), macroBlockToWrap.getParameters(), macroBlockToWrap .getContent(), newBlocks, macroBlockToWrap.isInline()); }
public List<String> getIncludedPages(XWikiContext context) { if (is10Syntax()) { return getIncludedPagesForXWiki10Syntax(getContent(), context); } else { // Find all include macros listed on the page XDOM dom = getXDOM(); List<String> result = new ArrayList<String>(); for (MacroBlock macroBlock : dom.getChildrenByType(MacroBlock.class, true)) { // - Add each document pointed to by the include macro // - Also add all the included pages found in the velocity macro when using the deprecated #include* // macros // This should be removed when we fully drop support for the XWiki Syntax 1.0 but for now we want to // play // nice with people migrating from 1.0 to 2.0 syntax if (macroBlock.getId().equalsIgnoreCase("include")) { String documentName = macroBlock.getParameters().get("document"); if (documentName.indexOf('.') == -1) { documentName = getSpace() + "." + documentName; } result.add(documentName); } else if (macroBlock.getId().equalsIgnoreCase("velocity") && !StringUtils.isEmpty(macroBlock.getContent())) { // Try to find matching content inside each velocity macro result.addAll(getIncludedPagesForXWiki10Syntax(macroBlock.getContent(), context)); } } return result; } }
/** * Wrap the output of a macro block with a {@link MacroMarkerBlock}. * * @param macroBlockToWrap the block that should be replaced * @param newBlocks list of blocks to wrap * @return the wrapper */ private Block wrapInMacroMarker(MacroBlock macroBlockToWrap, List<Block> newBlocks) { return new MacroMarkerBlock(macroBlockToWrap.getId(), macroBlockToWrap.getParameters(), macroBlockToWrap.getContent(), newBlocks, macroBlockToWrap.isInline()); }
/** * @param xdom the {@link XDOM} to convert * @return an inline version of the passed {@link XDOM} */ private XDOM convertToInline(XDOM xdom) { List<Block> blocks = new ArrayList<Block>(xdom.getChildren()); // TODO: use inline parser instead if (!blocks.isEmpty()) { this.parserUtils.removeTopLevelParagraph(blocks); // Make sure included macro is inline when script macro itself is inline Block block = blocks.get(0); if (block instanceof MacroBlock) { MacroBlock macro = (MacroBlock) block; if (!macro.isInline()) { blocks.set(0, new MacroBlock(macro.getId(), macro.getParameters(), macro.getContent(), true)); } } xdom.setChildren(blocks); } return xdom; }
/** * Wrap the output of a macro block with a {@link org.xwiki.rendering.block.MacroMarkerBlock}. * * @param macroBlockToWrap the block that should be replaced * @param newBlocks list of blocks to wrap * @return the wrapper */ private Block wrapInMacroMarker(MacroBlock macroBlockToWrap, List<Block> newBlocks) { return new MacroMarkerBlock(macroBlockToWrap.getId(), macroBlockToWrap.getParameters(), macroBlockToWrap.getContent(), newBlocks, macroBlockToWrap.isInline()); } }
/** * Clone and filter wiki macro content depending of the context. * * @param context the macro execution context * @return the cleaned wiki macro content */ private XDOM prepareWikiMacroContent() { XDOM xdom = this.wikimacro.getContent().clone(); // Macro code segment is always parsed into a separate xdom document. Now if this code segment starts with // another macro block, it will always be interpreted as a block macro regardless of the current wiki macro's // context (because as far as the nested macro is concerned, it starts on a new line). This will introduce // unnecessary paragraph elements when the wiki macro is used inline, so we need to force such opening macro // blocks to behave as inline macros if the wiki macro is used inline. if (this.inline) { List<Block> children = xdom.getChildren(); if (!children.isEmpty() && children.get(0) instanceof MacroBlock) { MacroBlock old = (MacroBlock) children.get(0); MacroBlock replacement = new MacroBlock(old.getId(), old.getParameters(), old.getContent(), true); xdom.replaceChild(replacement, old); } } return xdom; }
/** * {@inheritDoc} * * @see org.xwiki.rendering.block.AbstractBlock#traverse(org.xwiki.rendering.listener.Listener) */ public void traverse(Listener listener) { // Don't do anything here since we want the Macro Transformer component to take in charge // Macro execution. This is because Macro execution is a complex process that involves: // * computing the order in which the macros should be evaluated. For example the TOC macro // should evaluate last since other macros can contribute headers/sections blocks. // * some macros need to modify blocks in the XDOM object // * macro execution is a multi-pass process // In essence the Macro Transformer will replace all MacroBlock blocks with other Blocks // generated from the execution of the Macros when XDOM.traverse() is called there // won't be any MacroBlock.traverse() method called at all. // Note: We're calling the event to let other listener downstream decide what to do with it. // In practice as described above this method will never get called when the whole rendering // process is executed. This does get called during our unit tests though. listener.onMacro(getId(), getParameters(), getContent(), isInline()); } }
result.set(0, new MacroBlock(macro.getId(), macro.getParameters(), macro.getContent(), true));
result.set(0, new MacroBlock(macro.getId(), macro.getParameters(), macro.getContent(), true));
@Override protected void check(CancelableEvent event, MacroTransformationContext context, ScriptMacroParameters parameters) { MacroId currentMacroId = new MacroId(context.getCurrentMacroBlock().getId()); try { MacroPermissionPolicy mpp = this.componentManager.getInstance(MacroPermissionPolicy.class, currentMacroId.getId()); if (!mpp.hasPermission(parameters, context)) { event.cancel(String.format("The execution of the [%s] script macro is not allowed." + " Check the rights of its last author or the parameters if it's rendered from another script.", currentMacroId)); } } catch (ComponentLookupException e) { // Policy not found for macro, check permission using backward compatibility check backwardCompatibilityCheck(currentMacroId, event); } }
/** * {@inheritDoc} * * @see org.xwiki.rendering.internal.macro.script.AbstractScriptCheckerListener#check(org.xwiki.observation.event.CancelableEvent, org.xwiki.rendering.transformation.MacroTransformationContext, org.xwiki.rendering.macro.script.ScriptMacroParameters) */ @Override protected void check(CancelableEvent event, MacroTransformationContext context, ScriptMacroParameters parameters) { try { MacroId currentMacro = new MacroId(context.getCurrentMacroBlock().getId()); if (!(macroManager.getMacro(currentMacro) instanceof PrivilegedScriptMacro)) { // no special permission needed return; } // with not protected script engine, we are testing if the current dcument's author has "programming" right if (!this.documentAccessBridge.hasProgrammingRights()) { event.cancel("You don't have the right to execute this script"); } } catch (MacroLookupException exception) { // should not happen, this method was called from that macro } } }
if ("include".equals(macroBlock.getId())) { String documentName = macroBlock.getParameter("document"); if (documentName != null) {
context.getCurrentMacroBlock().getId(), extractSourceContentReference(context.getCurrentMacroBlock()), parentId, extractSourceContentReference(parent)));