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;
}
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;
}
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);
}
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);
}
}
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);
}
Aggregations