Search in sources :

Example 21 with MacroExecutionException

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);
    }
}
Also used : MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) ParseException(java.text.ParseException) Date(java.util.Date)

Example 22 with MacroExecutionException

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.");
        }
    }
}
Also used : MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) TableRowBlock(org.xwiki.rendering.block.TableRowBlock)

Example 23 with MacroExecutionException

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);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) IOException(java.io.IOException) File(java.io.File)

Example 24 with MacroExecutionException

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;
}
Also used : MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) File(java.io.File) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) IOException(java.io.IOException)

Example 25 with MacroExecutionException

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);
}
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) MacroBlock(org.xwiki.rendering.block.MacroBlock) Block(org.xwiki.rendering.block.Block) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock)

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