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