Search in sources :

Example 6 with MacroExecutionException

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

the class DisplayMacroTest method testDisplayMacroWhenInvalidSectionSpecified.

@Test
public void testDisplayMacroWhenInvalidSectionSpecified() throws Exception {
    DisplayMacroParameters parameters = new DisplayMacroParameters();
    parameters.setSection("unknown");
    try {
        runDisplayMacro(parameters, "content");
        Assert.fail("Should have raised an exception");
    } catch (MacroExecutionException expected) {
        Assert.assertEquals("Cannot find section [unknown] in document [wiki:Space.DisplayedPage]", expected.getMessage());
    }
}
Also used : MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) DisplayMacroParameters(org.xwiki.rendering.macro.display.DisplayMacroParameters) Test(org.junit.Test)

Example 7 with MacroExecutionException

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

the class DashboardMacroTest method executeWhenInsideDashboardMacro.

@Test
public void executeWhenInsideDashboardMacro() throws Exception {
    BeanManager beanManager = this.mocker.getInstance(BeanManager.class);
    BeanDescriptor descriptor = mock(BeanDescriptor.class);
    when(beanManager.getBeanDescriptor(any())).thenReturn(descriptor);
    when(descriptor.getProperties()).thenReturn(Collections.emptyList());
    Execution execution = this.mocker.getInstance(Execution.class);
    ExecutionContext ec = new ExecutionContext();
    when(execution.getContext()).thenReturn(ec);
    ec.setProperty("dashboardMacroCalls", 1);
    DashboardMacroParameters parameters = new DashboardMacroParameters();
    MacroTransformationContext macroContext = new MacroTransformationContext();
    try {
        this.mocker.getComponentUnderTest().execute(parameters, "", macroContext);
        fail("Exception should have been raised here");
    } catch (MacroExecutionException expected) {
        assertEquals("Dashboard macro recursion detected. Don't call the Dashboard macro inside of itself...", expected.getMessage());
    }
}
Also used : Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) BeanDescriptor(org.xwiki.properties.BeanDescriptor) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) DashboardMacroParameters(org.xwiki.rendering.macro.dashboard.DashboardMacroParameters) BeanManager(org.xwiki.properties.BeanManager) Test(org.junit.Test)

Example 8 with MacroExecutionException

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

the class FormulaMacro method render.

/**
 * Renders the formula using the specified renderer.
 *
 * @param formula the formula text
 * @param inline is the formula supposed to be used inline or as a block-level element
 * @param fontSize the specified font size
 * @param imageType the specified resulting image type
 * @param rendererHint the hint for the renderer to use
 * @return the resulting block holding the generated image, or {@code null} in case of an error.
 * @throws MacroExecutionException if no renderer exists for the passed hint or if that rendered failed to render
 *         the formula
 * @throws IllegalArgumentException if the formula is not valid, according to the LaTeX syntax
 */
private Block render(String formula, boolean inline, FontSize fontSize, Type imageType, String rendererHint) throws MacroExecutionException, IllegalArgumentException {
    try {
        FormulaRenderer renderer = this.manager.getInstance(FormulaRenderer.class, rendererHint);
        String imageName = renderer.process(formula, inline, fontSize, imageType);
        // TODO: HACK!!
        // We're going through the getAttachmentURL() API so that when the PdfURLFactory is used, the generated
        // image is saved and then embedded in the exported PDF thanks to PDFURIResolver. In the future we need
        // to remove this hack by introduce a proper Resource for generated image (say TemporaryResource),
        // implement a TemporaryResourceSerializer<URL> and introduce a ResourceLoader interface and have it
        // implemented for TemporaryResource...
        AttachmentReference attachmentReference = new AttachmentReference(imageName, this.dab.getCurrentDocumentReference());
        String url = this.dab.getAttachmentURL(attachmentReference, false);
        // Note that we have to replace the download action by the tex action since the getAttachmentURL() API
        // will use the "download" action but when the generated URL is called by the browser it needs to point to
        // the TexAction...
        url = url.replace("/download/", "/tex/");
        // TODO: end HACK!!
        ResourceReference imageReference = new ResourceReference(url, ResourceType.URL);
        ImageBlock result = new ImageBlock(imageReference, false);
        // Set the alternative text for the image to be the original formula
        result.setParameter("alt", formula);
        return result;
    } catch (Exception e) {
        throw new MacroExecutionException(String.format("Failed to render formula using the [%s] renderer", rendererHint), e);
    }
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ImageBlock(org.xwiki.rendering.block.ImageBlock) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) FormulaRenderer(org.xwiki.formula.FormulaRenderer) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException)

Example 9 with MacroExecutionException

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

the class IncludeMacro method execute.

@Override
public List<Block> execute(IncludeMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    // Step 1: Perform checks.
    if (parameters.getReference() == null) {
        throw new MacroExecutionException("You must specify a 'reference' parameter pointing to the entity to include.");
    }
    DocumentReference includedReference = resolve(context.getCurrentMacroBlock(), parameters);
    checkRecursiveInclusion(context.getCurrentMacroBlock(), includedReference);
    if (!this.documentAccessBridge.isDocumentViewable(includedReference)) {
        throw new MacroExecutionException(String.format("Current user [%s] doesn't have view rights on document [%s]", this.documentAccessBridge.getCurrentUserReference(), this.defaultEntityReferenceSerializer.serialize(includedReference)));
    }
    Context parametersContext = parameters.getContext();
    // Step 2: Retrieve the included document.
    DocumentModelBridge documentBridge;
    try {
        documentBridge = this.documentAccessBridge.getDocumentInstance(includedReference);
    } catch (Exception e) {
        throw new MacroExecutionException("Failed to load Document [" + this.defaultEntityReferenceSerializer.serialize(includedReference) + "]", e);
    }
    // Step 3: Display the content of the included document.
    // Check the value of the "context" parameter.
    // 
    // If CONTEXT_NEW then display the content in an isolated execution and transformation context.
    // 
    // if CONTEXT_CURRENT then display the content without performing any transformations (we don't want any Macro
    // to be executed at this stage since they should be executed by the currently running Macro Transformation.
    DocumentDisplayerParameters displayParameters = new DocumentDisplayerParameters();
    displayParameters.setContentTransformed(parametersContext == Context.NEW);
    displayParameters.setExecutionContextIsolated(displayParameters.isContentTransformed());
    displayParameters.setSectionId(parameters.getSection());
    displayParameters.setTransformationContextIsolated(displayParameters.isContentTransformed());
    displayParameters.setTransformationContextRestricted(context.getTransformationContext().isRestricted());
    displayParameters.setTargetSyntax(context.getTransformationContext().getTargetSyntax());
    displayParameters.setContentTranslated(true);
    Stack<Object> references = this.inclusionsBeingExecuted.get();
    if (parametersContext == Context.NEW) {
        if (references == null) {
            references = new Stack<Object>();
            this.inclusionsBeingExecuted.set(references);
        }
        references.push(includedReference);
    }
    XDOM result;
    try {
        result = this.documentDisplayer.display(documentBridge, displayParameters);
    } catch (Exception e) {
        throw new MacroExecutionException(e.getMessage(), e);
    } finally {
        if (parametersContext == Context.NEW) {
            references.pop();
        }
    }
    // Step 4: Wrap Blocks in a MetaDataBlock with the "source" meta data specified so that we know from where the
    // content comes and "base" meta data so that reference are properly resolved
    MetaDataBlock metadata = new MetaDataBlock(result.getChildren(), result.getMetaData());
    String source = this.defaultEntityReferenceSerializer.serialize(includedReference);
    metadata.getMetaData().addMetaData(MetaData.SOURCE, source);
    if (parametersContext == Context.NEW) {
        metadata.getMetaData().addMetaData(MetaData.BASE, source);
    }
    return Arrays.<Block>asList(metadata);
}
Also used : MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) Context(org.xwiki.rendering.macro.include.IncludeMacroParameters.Context) DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) Block(org.xwiki.rendering.block.Block) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) MacroBlock(org.xwiki.rendering.block.MacroBlock) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock)

Example 10 with MacroExecutionException

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

the class IncludeMacro method checkRecursiveInclusion.

/**
 * Protect form recursive inclusion.
 *
 * @param currrentBlock the child block to check
 * @param documentReference the reference of the document being included
 * @throws MacroExecutionException recursive inclusion has been found
 */
private void checkRecursiveInclusion(Block currrentBlock, DocumentReference documentReference) throws MacroExecutionException {
    // Check for parent context=new macros
    Stack<Object> references = this.inclusionsBeingExecuted.get();
    if (references != null && references.contains(documentReference)) {
        throw new MacroExecutionException("Found recursive inclusion of document [" + documentReference + "]");
    }
    // Check for parent context=current macros
    Block parentBlock = currrentBlock.getParent();
    if (parentBlock != null) {
        if (parentBlock instanceof MacroMarkerBlock) {
            MacroMarkerBlock parentMacro = (MacroMarkerBlock) parentBlock;
            if (isRecursive(parentMacro, documentReference)) {
                throw new MacroExecutionException("Found recursive inclusion of document [" + documentReference + "]");
            }
        }
        checkRecursiveInclusion(parentBlock, documentReference);
    }
}
Also used : MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) Block(org.xwiki.rendering.block.Block) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) MacroBlock(org.xwiki.rendering.block.MacroBlock)

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