Search in sources :

Example 41 with MacroExecutionException

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

the class DocumentTableBlockDataSource method computeXDOM.

/**
 * Get the XDOM for the data source.
 *
 * @param context the Macro context from which we can get the XDOM if the source is in the current content
 * @return the XDOM in which the data source is located
 * @throws MacroExecutionException in case of an error getting the XDOM
 */
private XDOM computeXDOM(MacroTransformationContext context) throws MacroExecutionException {
    XDOM xdom;
    // Context and 2) it's going to cause a cycle...
    if (isDefinedChartSourceTheCurrentDocument(context.getCurrentMacroBlock())) {
        xdom = context.getXDOM();
    } else {
        try {
            DocumentModelBridge document = this.docBridge.getDocumentInstance(this.documentReference);
            DocumentDisplayerParameters parameters = new DocumentDisplayerParameters();
            parameters.setContentTranslated(true);
            parameters.setTargetSyntax(context.getTransformationContext().getTargetSyntax());
            parameters.setContentTranslated(true);
            xdom = this.documentDisplayer.display(document, parameters);
        } catch (Exception e) {
            throw new MacroExecutionException(String.format("Error getting Chart table from document [%s]", this.documentReference, e));
        }
    }
    return xdom;
}
Also used : DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException)

Example 42 with MacroExecutionException

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

the class DocumentTableBlockDataSource method getTableBlock.

@Override
protected TableBlock getTableBlock(String macroContent, MacroTransformationContext context) throws MacroExecutionException {
    XDOM xdom = computeXDOM(context);
    // Find the correct table block.
    List<TableBlock> tableBlocks = xdom.getBlocks(new ClassBlockMatcher(TableBlock.class), Block.Axes.DESCENDANT);
    TableBlock result = null;
    this.logger.debug("Table id is [{}], there are [{}] tables in the document [{}]", new Object[] { this.tableId, tableBlocks.size(), this.documentReference });
    if (null != tableId) {
        for (TableBlock tableBlock : tableBlocks) {
            String id = tableBlock.getParameter("id");
            if (null != id && id.equals(this.tableId)) {
                result = tableBlock;
                break;
            }
        }
    } else {
        result = (tableBlocks.size() > 0) ? tableBlocks.get(0) : null;
    }
    if (null == result) {
        throw new MacroExecutionException("Unable to find a matching data table.");
    }
    return result;
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) TableBlock(org.xwiki.rendering.block.TableBlock) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException)

Example 43 with MacroExecutionException

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

the class MacroContentTableBlockDataSource method getTableBlock.

@Override
protected TableBlock getTableBlock(String macroContent, MacroTransformationContext context) throws MacroExecutionException {
    // Since we are using an inline source the macro content cannot be empty/null.
    if (StringUtils.isEmpty(macroContent)) {
        throw new MacroExecutionException("A Chart Macro using an inline source must have a data table defined in " + "its content.");
    }
    // Parse the macro content into an XDOM.
    XDOM xdom = this.macroContentParser.parse(macroContent, context, true, false);
    // Take the first TableBlock found in the macro content.
    List<TableBlock> tableBlocks = xdom.getBlocks(new ClassBlockMatcher(TableBlock.class), Block.Axes.DESCENDANT);
    if (tableBlocks.size() == 0) {
        throw new MacroExecutionException("Unable to locate a suitable data table.");
    }
    return tableBlocks.get(0);
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) TableBlock(org.xwiki.rendering.block.TableBlock) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException)

Example 44 with MacroExecutionException

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

the class ChartMacro method generateChart.

/**
 * Builds the chart image according to the specifications passed in.
 *
 * @param parameters the macro parameters
 * @param content the macro content
 * @param context the macro transformation context, used for example to find out the current document reference
 * @throws MacroExecutionException if an error occurs while generating / saving the chart image
 */
private void generateChart(ChartMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    String source = computeSource(parameters.getSource(), content);
    DataSource dataSource;
    try {
        dataSource = this.componentManager.getInstance(DataSource.class, source);
    } catch (ComponentLookupException e) {
        throw new MacroExecutionException(String.format("Invalid source parameter [%s]", parameters.getSource()), e);
    }
    Map<String, String> sourceParameters = getSourceParameters(parameters, source);
    dataSource.buildDataset(content, sourceParameters, context);
    try {
        this.imageWriter.writeImage(new ImageId(parameters), this.chartGenerator.generate(dataSource.getChartModel(), sourceParameters));
    } catch (ChartGeneratorException e) {
        throw new MacroExecutionException("Error while rendering chart", e);
    }
}
Also used : ChartGeneratorException(org.xwiki.chart.ChartGeneratorException) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) DataSource(org.xwiki.rendering.internal.macro.chart.source.DataSource)

Example 45 with MacroExecutionException

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

the class FormulaMacro method execute.

@Override
public List<Block> execute(FormulaMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    if (StringUtils.isEmpty(content)) {
        throw new MacroExecutionException(CONTENT_MISSING_ERROR);
    }
    String rendererHint = this.configuration.getRenderer();
    FontSize size = parameters.getFontSize();
    Type type = parameters.getImageType();
    Block result;
    try {
        result = render(content, context.isInline(), size, type, rendererHint);
    } catch (MacroExecutionException ex) {
        this.logger.debug("Failed to render content with the [{}] renderer. Falling back to the safe renderer.", rendererHint, ex);
        try {
            result = render(content, context.isInline(), size, type, this.configuration.getSafeRenderer());
        } catch (IllegalArgumentException ex2) {
            throw new MacroExecutionException(WRONG_CONTENT_ERROR);
        }
    } catch (IllegalArgumentException ex) {
        throw new MacroExecutionException(WRONG_CONTENT_ERROR);
    }
    // If no image was generated, just return the original text
    if (result == null) {
        result = new WordBlock(content);
    }
    // Block level formulae should be wrapped in a paragraph element
    if (!context.isInline()) {
        result = new ParagraphBlock(Collections.<Block>singletonList(result));
    }
    return Collections.singletonList(result);
}
Also used : Type(org.xwiki.formula.FormulaRenderer.Type) ResourceType(org.xwiki.rendering.listener.reference.ResourceType) FontSize(org.xwiki.formula.FormulaRenderer.FontSize) WordBlock(org.xwiki.rendering.block.WordBlock) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) Block(org.xwiki.rendering.block.Block) ImageBlock(org.xwiki.rendering.block.ImageBlock) WordBlock(org.xwiki.rendering.block.WordBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock)

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