use of org.xwiki.rendering.macro.parameter.MacroParameterException in project xwiki-platform by xwiki.
the class DefaultWikiMacro method validate.
/**
* Check validity of the given macro parameters and content.
*
* @param parameters the macro parameters
* @param macroContent the macro content
* @throws MacroExecutionException given parameters of content is invalid
*/
private void validate(WikiMacroParameters parameters, String macroContent) throws MacroExecutionException {
// First verify that all mandatory parameters are provided.
// Note that we currently verify automatically mandatory parameters in Macro Transformation but for the moment
// this is only checked for Java-based macros. Hence why we need to check here too.
Map<String, ParameterDescriptor> parameterDescriptors = getDescriptor().getParameterDescriptorMap();
for (ParameterDescriptor parameterDescriptor : parameterDescriptors.values()) {
Object parameterValue = parameters.get(parameterDescriptor.getId());
if (parameterDescriptor.isMandatory() && (null == parameterValue)) {
throw new MacroParameterException(String.format("Parameter [%s] is mandatory", parameterDescriptor.getId()));
}
// Set default parameter value if applicable.
Object parameterDefaultValue = parameterDescriptor.getDefaultValue();
if (parameterValue == null && parameterDefaultValue != null) {
parameters.set(parameterDescriptor.getId(), parameterDefaultValue);
}
}
// Verify the a macro content is not empty if it was declared mandatory.
if (getDescriptor().getContentDescriptor() != null && getDescriptor().getContentDescriptor().isMandatory()) {
if (macroContent == null || macroContent.length() == 0) {
throw new MacroExecutionException("Missing macro content: this macro requires content (a body)");
}
}
}
Aggregations