Search in sources :

Example 1 with MacroParameterException

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)");
        }
    }
}
Also used : ParameterDescriptor(org.xwiki.rendering.macro.descriptor.ParameterDescriptor) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) MacroParameterException(org.xwiki.rendering.macro.parameter.MacroParameterException)

Aggregations

MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)1 ParameterDescriptor (org.xwiki.rendering.macro.descriptor.ParameterDescriptor)1 MacroParameterException (org.xwiki.rendering.macro.parameter.MacroParameterException)1