Search in sources :

Example 11 with MacroExecutionException

use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.

the class AbstractJSR223ScriptMacro method evaluateBlock.

@Override
protected List<Block> evaluateBlock(P parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    if (StringUtils.isEmpty(content)) {
        return Collections.emptyList();
    }
    String engineName = getScriptEngineName(parameters, context);
    List<Block> result;
    if (engineName != null) {
        try {
            ScriptEngine engine = getScriptEngine(engineName);
            if (engine != null) {
                result = evaluateBlock(engine, parameters, content, context);
            } else {
                throw new MacroExecutionException("Can't find script engine with name [" + engineName + "]");
            }
        } catch (ScriptException e) {
            throw new MacroExecutionException("Failed to evaluate Script Macro for content [" + content + "]", e);
        }
    } else {
        // If no language identifier is provided, don't evaluate content
        result = parseScriptResult(content, parameters, context);
    }
    return result;
}
Also used : ScriptException(javax.script.ScriptException) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) Block(org.xwiki.rendering.block.Block) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) ScriptEngine(javax.script.ScriptEngine)

Example 12 with MacroExecutionException

use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.

the class AbstractScriptMacro method parseScriptResult.

/**
 * Convert script result as a {@link Block} list.
 *
 * @param content the script result to parse.
 * @param parameters the macro parameters.
 * @param context the context of the macro transformation.
 * @return the {@link Block}s.
 * @throws MacroExecutionException Failed to find source parser.
 * @since 2.1M1
 */
protected List<Block> parseScriptResult(String content, P parameters, MacroTransformationContext context) throws MacroExecutionException {
    List<Block> result;
    if (parameters.isWiki()) {
        result = parseSourceSyntax(content, context);
    } else {
        try {
            result = this.plainTextParser.parse(new StringReader(content)).getChildren();
        } catch (ParseException e) {
            // String.
            throw new MacroExecutionException("Failed to parse link label as plain text", e);
        }
    }
    // 3) If in inline mode remove any top level paragraph
    if (context.isInline()) {
        this.parserUtils.removeTopLevelParagraph(result);
        // TODO: use inline parser instead
        if (!result.isEmpty() && result.get(0) instanceof MacroBlock && !((MacroBlock) result.get(0)).isInline()) {
            MacroBlock macro = (MacroBlock) result.get(0);
            result.set(0, new MacroBlock(macro.getId(), macro.getParameters(), macro.getContent(), true));
        }
    }
    return result;
}
Also used : StringReader(java.io.StringReader) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) MacroBlock(org.xwiki.rendering.block.MacroBlock) Block(org.xwiki.rendering.block.Block) ParseException(org.xwiki.rendering.parser.ParseException) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 13 with MacroExecutionException

use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.

the class ContextMacro method execute.

@Override
public List<Block> execute(ContextMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    if (parameters.getDocument() == null) {
        throw new MacroExecutionException("You must specify a 'document' parameter pointing to the document to " + "set in the context as the current document.");
    }
    DocumentReference referencedDocReference = this.macroDocumentReferenceResolver.resolve(parameters.getDocument(), context.getCurrentMacroBlock());
    boolean currentContextHasProgrammingRights = this.documentAccessBridge.hasProgrammingRights();
    List<Block> result;
    try {
        Map<String, Object> backupObjects = new HashMap<>();
        try {
            this.documentAccessBridge.pushDocumentInContext(backupObjects, referencedDocReference);
            // error since it would be a security breach otherwise.
            if (this.documentAccessBridge.hasProgrammingRights() && !currentContextHasProgrammingRights) {
                throw new MacroExecutionException("Current document must have programming rights since the " + "context document provided [" + parameters.getDocument() + "] has programming rights.");
            }
            MetaData metadata = new MetaData();
            metadata.addMetaData(MetaData.SOURCE, parameters.getDocument());
            metadata.addMetaData(MetaData.BASE, parameters.getDocument());
            XDOM xdom = this.contentParser.parse(content, context, false, metadata, false);
            // Configure the  Transformation Context depending on the mode asked.
            if (parameters.getTransformationContext() == TransformationContextMode.DOCUMENT || parameters.getTransformationContext() == TransformationContextMode.TRANSFORMATIONS) {
                // Apply the transformations but with a Transformation Context having the XDOM of the passed
                // document so that macros execute on the passed document's XDOM (e.g. the TOC macro will generate
                // the toc for the passed document instead of the current document).
                DocumentModelBridge referencedDoc = this.documentAccessBridge.getTranslatedDocumentInstance(referencedDocReference);
                XDOM referencedXDOM = referencedDoc.getXDOM();
                if (parameters.getTransformationContext() == TransformationContextMode.TRANSFORMATIONS) {
                    // Get the XDOM from the referenced doc but with Transformations applied so that all macro are
                    // executed and contribute XDOM elements.
                    // IMPORTANT: This can be dangerous since it means executing macros, and thus also script macros
                    // defined in the referenced document. To be used with caution.
                    TransformationContext referencedTxContext = new TransformationContext(referencedXDOM, referencedDoc.getSyntax());
                    this.transformationManager.performTransformations(referencedXDOM, referencedTxContext);
                }
                // Now execute transformation on the context macro content but with the referenced XDOM in the
                // Transformation context!
                TransformationContext txContext = new TransformationContext(referencedXDOM, referencedDoc.getSyntax());
                this.transformationManager.performTransformations(xdom, txContext);
            }
            // Keep metadata so that the result stay associated to context properties when inserted in the parent
            // XDOM
            result = Arrays.asList((Block) new MetaDataBlock(xdom.getChildren(), xdom.getMetaData()));
        } finally {
            this.documentAccessBridge.popDocumentFromContext(backupObjects);
        }
    } catch (Exception e) {
        if (e instanceof MacroExecutionException) {
            throw (MacroExecutionException) e;
        } else {
            throw new MacroExecutionException(String.format("Failed to render page in the context of [%s]", referencedDocReference), e);
        }
    }
    return result;
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) HashMap(java.util.HashMap) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) MetaData(org.xwiki.rendering.listener.MetaData) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) Block(org.xwiki.rendering.block.Block) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) DocumentReference(org.xwiki.model.reference.DocumentReference) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock)

Example 14 with MacroExecutionException

use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.

the class ContextMacroTest method executeWithReferencedDocumentHavingProgrammingRightsButNotTheCallingDocument.

@Test
public void executeWithReferencedDocumentHavingProgrammingRightsButNotTheCallingDocument() throws Exception {
    MacroTransformationContext macroContext = new MacroTransformationContext();
    MacroBlock macroBlock = new MacroBlock("context", Collections.emptyMap(), false);
    macroContext.setCurrentMacroBlock(macroBlock);
    DocumentReferenceResolver<String> resolver = this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "macro");
    when(resolver.resolve("wiki:space.page", macroBlock)).thenReturn(new DocumentReference("wiki", "space", "page"));
    DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
    when(dab.hasProgrammingRights()).thenReturn(false).thenReturn(true);
    ContextMacroParameters parameters = new ContextMacroParameters();
    parameters.setDocument("wiki:space.page");
    try {
        this.mocker.getComponentUnderTest().execute(parameters, "", macroContext);
        fail("Should have thrown an exception");
    } catch (MacroExecutionException expected) {
        assertEquals("Current document must have programming rights since the context document provided [" + "wiki:space.page] has programming rights.", expected.getMessage());
    }
}
Also used : MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) ContextMacroParameters(org.xwiki.rendering.macro.context.ContextMacroParameters) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroBlock(org.xwiki.rendering.block.MacroBlock) Test(org.junit.Test)

Example 15 with MacroExecutionException

use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.

the class CodeMacro method parseContent.

@Override
protected List<Block> parseContent(CodeMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    List<Block> result;
    try {
        if (LANGUAGE_NONE.equalsIgnoreCase(parameters.getLanguage())) {
            if (StringUtils.isEmpty(content)) {
                result = Collections.emptyList();
            } else {
                result = this.plainTextParser.parse(new StringReader(content)).getChildren().get(0).getChildren();
            }
        } else {
            result = highlight(parameters, content);
        }
    } catch (Exception e) {
        throw new MacroExecutionException("Failed to highlight content", e);
    }
    Map<String, String> formatParameters = new LinkedHashMap<String, String>();
    formatParameters.put("class", "code");
    if (context.isInline()) {
        result = Arrays.<Block>asList(new FormatBlock(result, Format.NONE, formatParameters));
    } else {
        result = Arrays.<Block>asList(new GroupBlock(result, formatParameters));
    }
    return result;
}
Also used : StringReader(java.io.StringReader) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) GroupBlock(org.xwiki.rendering.block.GroupBlock) FormatBlock(org.xwiki.rendering.block.FormatBlock) Block(org.xwiki.rendering.block.Block) GroupBlock(org.xwiki.rendering.block.GroupBlock) FormatBlock(org.xwiki.rendering.block.FormatBlock) ParseException(org.xwiki.rendering.parser.ParseException) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)48 Test (org.junit.Test)12 Block (org.xwiki.rendering.block.Block)12 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)10 DocumentReference (org.xwiki.model.reference.DocumentReference)9 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)7 MacroBlock (org.xwiki.rendering.block.MacroBlock)7 XDOM (org.xwiki.rendering.block.XDOM)7 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)6 Expectations (org.jmock.Expectations)5 DocumentDisplayerParameters (org.xwiki.display.internal.DocumentDisplayerParameters)5 StringReader (java.io.StringReader)4 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)4 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)4 IncludeMacroParameters (org.xwiki.rendering.macro.include.IncludeMacroParameters)4 HashMap (java.util.HashMap)3 AttachmentReference (org.xwiki.model.reference.AttachmentReference)3 GroupBlock (org.xwiki.rendering.block.GroupBlock)3 MacroMarkerBlock (org.xwiki.rendering.block.MacroMarkerBlock)3 TableBlock (org.xwiki.rendering.block.TableBlock)3