use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.
the class AxisConfigurator method setDateLimits.
/**
* Set the limits of a date axis.
*
* @param axis The axis.
* @param index The index of the axis.
* @throws MacroExecutionException if the parameters could not be parsed as dates.
*/
private void setDateLimits(DateAxis axis, int index) throws MacroExecutionException {
try {
if (axisLowerLimit[index] != null) {
Date date = localeConfiguration.getDateFormat().parse(StringUtils.trim(axisLowerLimit[index]));
axis.setMinimumDate(date);
}
if (axisUpperLimit[index] != null) {
Date date = localeConfiguration.getDateFormat().parse(StringUtils.trim(axisUpperLimit[index]));
axis.setMaximumDate(date);
}
} catch (ParseException e) {
throw new MacroExecutionException("Invalid date in axis bound.", e);
}
}
use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.
the class AbstractTableBlockDataSource method buildDataset.
/**
* Build a category dataset.
*
* @param tableBlock The table block to parse.
* @param dataRange The data range.
* @param datasetBuilder The dataset builder.
* @throws MacroExecutionException if there are any errors.
*/
private void buildDataset(TableBlock tableBlock, int[] dataRange, TableDatasetBuilder datasetBuilder) throws MacroExecutionException {
int startRow = dataRange[0];
int startColumn = dataRange[1];
int endRow = dataRange[2];
int endColumn = dataRange[3];
if (startRow == 0 && datasetBuilder.forceRowHeadings()) {
startRow = 1;
}
if (startColumn == 0 && datasetBuilder.forceColumnHeadings()) {
startColumn = 1;
}
getRowKeys(tableBlock, startRow, endRow, startColumn, datasetBuilder);
getColumnKeys(tableBlock, startColumn, endColumn, startRow, datasetBuilder);
for (int i = startRow; i <= endRow; i++) {
if (i < tableBlock.getChildren().size()) {
TableRowBlock tableRow = (TableRowBlock) tableBlock.getChildren().get(i);
for (int j = startColumn; j <= endColumn; j++) {
if (j < tableRow.getChildren().size()) {
Number value = cellContentAsNumber((TableCellBlock) tableRow.getChildren().get(j));
datasetBuilder.setValue(i - startRow, j - startColumn, value);
} else {
throw new MacroExecutionException("Data range (columns) overflow.");
}
}
} else {
throw new MacroExecutionException("Data range (rows) overflow.");
}
}
}
use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.
the class TemporaryChartImageWriter method writeImage.
@Override
public void writeImage(ImageId imageId, byte[] imageData) throws MacroExecutionException {
File imageFile = getStorageLocation(imageId);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(imageFile);
fos.write(imageData);
fos.close();
} catch (IOException e) {
throw new MacroExecutionException("Failed to write the generated chart image", e);
} finally {
IOUtils.closeQuietly(fos);
}
}
use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.
the class TemporaryChartImageWriter method getStorageLocation.
/**
* Compute the location where to store the generated chart image.
*
* @param imageId the image id that we use to generate a unique storage location
* @return the location where to store the generated chart image
* @throws MacroExecutionException if an error happened when computing the location
*/
protected File getStorageLocation(ImageId imageId) throws MacroExecutionException {
File directory;
try {
String currentWiki = URLEncoder.encode(getCurrentWiki(), DEFAULT_ENCODING);
// TODO: We need to decide if it's ok to use the the hardcoded "space/page" or if we want to use the
// current document in which case we need to extract it from the XDOM. The reason I haven't done it
// by default is because it takes more time and the image id seems unique enough to not cause collisions.
directory = new File(this.environment.getTemporaryDirectory(), String.format("temp/%s/%s/%s/%s", MODULE_NAME, currentWiki, SPACE, PAGE));
directory.mkdirs();
} catch (Exception e) {
// Should not happen since UTF8 encoding should always be present
throw new MacroExecutionException("Failed to compute chart image location", e);
}
File locationFile = new File(directory, String.format("%s.png", imageId.getId()));
return locationFile;
}
use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.
the class DisplayMacro method execute.
@Override
public List<Block> execute(DisplayMacroParameters 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 display.");
}
DocumentReference includedReference = resolve(context.getCurrentMacroBlock(), parameters);
checkRecursiveDisplay(context.getCurrentMacroBlock(), includedReference);
if (!this.documentAccessBridge.isDocumentViewable(includedReference)) {
throw new MacroExecutionException("Current user [" + this.documentAccessBridge.getCurrentUserReference() + "] doesn't have view rights on document [" + this.defaultEntityReferenceSerializer.serialize(includedReference) + "]");
}
// 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.
// Display the content in an isolated execution and transformation context.
DocumentDisplayerParameters displayParameters = new DocumentDisplayerParameters();
displayParameters.setContentTransformed(true);
displayParameters.setExecutionContextIsolated(displayParameters.isContentTransformed());
displayParameters.setSectionId(parameters.getSection());
displayParameters.setTransformationContextIsolated(displayParameters.isContentTransformed());
displayParameters.setTargetSyntax(context.getTransformationContext().getTargetSyntax());
displayParameters.setContentTranslated(true);
Stack<Object> references = this.displaysBeingExecuted.get();
if (references == null) {
references = new Stack<Object>();
this.displaysBeingExecuted.set(references);
}
references.push(includedReference);
XDOM result;
try {
result = this.documentDisplayer.display(documentBridge, displayParameters);
} catch (Exception e) {
throw new MacroExecutionException(e.getMessage(), e);
} finally {
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);
metadata.getMetaData().addMetaData(MetaData.BASE, source);
return Arrays.<Block>asList(metadata);
}
Aggregations