use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.
the class IncludeMacroTest method testIncludeMacroWithRecursiveIncludeContextNew.
@Test
public void testIncludeMacroWithRecursiveIncludeContextNew() throws Exception {
final DocumentDisplayer mockDocumentDisplayer = getMockery().mock(DocumentDisplayer.class);
this.includeMacro.setDocumentAccessBridge(mockSetup.bridge);
this.includeMacro.setDocumentDisplayer(mockDocumentDisplayer);
final MacroTransformationContext macroContext = createMacroTransformationContext("wiki:space.page", false);
final IncludeMacroParameters parameters = new IncludeMacroParameters();
parameters.setReference("wiki:space.page");
parameters.setContext(Context.NEW);
getMockery().checking(new Expectations() {
{
allowing(mockDocumentReferenceResolver).resolve("wiki:space.page", macroContext.getCurrentMacroBlock());
will(returnValue(new DocumentReference("wiki", "space", "page")));
allowing(mockSetup.bridge).isDocumentViewable(with(any(DocumentReference.class)));
will(returnValue(true));
allowing(mockSetup.bridge).getDocumentInstance(with(any(DocumentReference.class)));
will(returnValue(null));
allowing(mockDocumentDisplayer).display(with(same((DocumentModelBridge) null)), with(any(DocumentDisplayerParameters.class)));
will(new CustomAction("recursively call the include macro again") {
@Override
public Object invoke(Invocation invocation) throws Throwable {
try {
includeMacro.execute(parameters, null, macroContext);
} catch (Exception expected) {
if (expected.getMessage().contains("Found recursive inclusion")) {
throw new ExpectedRecursiveInclusionException();
}
}
return true;
}
});
}
});
try {
this.includeMacro.execute(parameters, null, macroContext);
Assert.fail("The include macro hasn't checked the recursive inclusion");
} catch (MacroExecutionException expected) {
if (!(expected.getCause() instanceof ExpectedRecursiveInclusionException)) {
throw expected;
}
}
}
use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.
the class IncludeMacroTest method testIncludeMacroWithRecursiveIncludeContextCurrent.
@Test
public void testIncludeMacroWithRecursiveIncludeContextCurrent() throws Exception {
this.includeMacro.setDocumentAccessBridge(mockSetup.bridge);
final MacroTransformationContext macroContext = createMacroTransformationContext("wiki:space.page", false);
// Add an Include Macro MarkerBlock as a parent of the include Macro block since this is what would have
// happened if an Include macro is included in another Include macro.
final MacroMarkerBlock includeMacroMarker = new MacroMarkerBlock("include", Collections.singletonMap("reference", "space.page"), Collections.<Block>singletonList(macroContext.getCurrentMacroBlock()), false);
getMockery().checking(new Expectations() {
{
allowing(mockDocumentReferenceResolver).resolve("wiki:space.page", macroContext.getCurrentMacroBlock());
will(returnValue(new DocumentReference("wiki", "space", "page")));
allowing(mockDocumentReferenceResolver).resolve("space.page", includeMacroMarker);
will(returnValue(new DocumentReference("wiki", "space", "page")));
}
});
IncludeMacroParameters parameters = new IncludeMacroParameters();
parameters.setReference("wiki:space.page");
parameters.setContext(Context.CURRENT);
try {
this.includeMacro.execute(parameters, null, macroContext);
Assert.fail("The include macro hasn't checked the recursive inclusion");
} catch (MacroExecutionException expected) {
if (!expected.getMessage().startsWith("Found recursive inclusion")) {
throw expected;
}
}
}
use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.
the class UserAvatarMacro method execute.
@Override
public List<Block> execute(UserAvatarMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
DocumentReference userReference = this.currentDocumentReferenceResolver.resolve(parameters.getUsername(), new EntityReference(USER_SPACE, EntityType.SPACE));
// Find the avatar attachment name or null if not defined or an error happened when locating it
String fileName = null;
if (this.documentAccessBridge.exists(userReference)) {
Object avatarProperty = this.documentAccessBridge.getProperty(userReference, new DocumentReference(userReference.getWikiReference().getName(), USER_SPACE, "XWikiUsers"), "avatar");
if (avatarProperty != null) {
fileName = avatarProperty.toString();
}
} else {
throw new MacroExecutionException("User [" + this.compactWikiEntityReferenceSerializer.serialize(userReference) + "] is not registered in this wiki");
}
// Initialize with the default avatar.
ResourceReference imageReference = new ResourceReference(this.skinAccessBridge.getSkinFile("icons/xwiki/noavatar.png"), ResourceType.URL);
// Try to use the configured avatar.
if (!StringUtils.isBlank(fileName)) {
AttachmentReference attachmentReference = new AttachmentReference(fileName, userReference);
// Check if the configured avatar file actually exists.
try {
if (documentAccessBridge.getAttachmentVersion(attachmentReference) != null) {
// Use it.
imageReference = new ResourceReference(this.compactWikiEntityReferenceSerializer.serialize(attachmentReference), ResourceType.ATTACHMENT);
}
} catch (Exception e) {
// Log and fallback on default.
logger.warn("Failed to get the avatar for user [{}]: [{}]. Using default.", this.compactWikiEntityReferenceSerializer.serialize(userReference), ExceptionUtils.getRootCauseMessage(e));
}
}
ImageBlock imageBlock = new ImageBlock(imageReference, false);
imageBlock.setParameter("alt", "Picture of " + userReference.getName());
imageBlock.setParameter("title", userReference.getName());
if (parameters.getWidth() != null) {
imageBlock.setParameter("width", String.valueOf(parameters.getWidth()));
}
if (parameters.getHeight() != null) {
imageBlock.setParameter("height", String.valueOf(parameters.getHeight()));
}
List<Block> result = Collections.singletonList(imageBlock);
return context.isInline() ? result : Collections.singletonList(new GroupBlock(result));
}
use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.
the class TableTimeTableXYDatasetBuilder method setParameters.
@Override
public void setParameters(Map<String, String> parameters) throws MacroExecutionException {
String timePeriodTypeString = parameters.get(TIMEPERIOD_TYPE_PARAM);
if (timePeriodTypeString != null) {
this.timePeriodType = TimePeriodType.forName(timePeriodTypeString);
if (this.timePeriodType == null) {
throw new MacroExecutionException(String.format("Invalid time period type [%s].", timePeriodTypeString));
}
}
dataset = new TimeTableXYDataset(localeConfiguration.getTimeZone(), localeConfiguration.getLocale());
}
use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.
the class AxisConfigurator method setNumberLimits.
/**
* Set the limits of a number axis.
*
* @param axis The axis.
* @param index The index of the axis
* @throws MacroExecutionException if the parameters could not be parsed as numbers.
*/
private void setNumberLimits(ValueAxis axis, int index) throws MacroExecutionException {
try {
if (axisLowerLimit[index] != null) {
Number number = NumberUtils.createNumber(StringUtils.trim(axisLowerLimit[index]));
axis.setLowerBound(number.doubleValue());
}
if (axisUpperLimit[index] != null) {
Number number = NumberUtils.createNumber(StringUtils.trim(axisUpperLimit[index]));
axis.setUpperBound(number.doubleValue());
}
} catch (NumberFormatException e) {
throw new MacroExecutionException("Invalid number in axis bound.", e);
}
}
Aggregations