Search in sources :

Example 1 with ParameterDescriptor

use of org.xwiki.rendering.macro.descriptor.ParameterDescriptor in project xwiki-platform by xwiki.

the class WikiMacroDescriptorTest method testGetParameterDescriptorMapInCorrectOrder.

/**
 * Ensure that the parameter descriptor map returned has the same order as the order in which parameter
 * descriptors are passed to the Wiki Macro descriptor. This is useful for example to ensure that the
 * WYSIWYG editor will display the wiki macro parameter in the same order as the Wiki Macro Object order.
 */
@org.junit.Test
public void testGetParameterDescriptorMapInCorrectOrder() {
    List<WikiMacroParameterDescriptor> paramDescriptors = Arrays.asList(new WikiMacroParameterDescriptor("id1", "description1", true), new WikiMacroParameterDescriptor("id2", "description2", true));
    WikiMacroDescriptor descriptor = new WikiMacroDescriptor("name", "description", "category", WikiMacroVisibility.GLOBAL, new DefaultContentDescriptor(), paramDescriptors);
    Map<String, ParameterDescriptor> result = descriptor.getParameterDescriptorMap();
    Iterator<String> it = result.keySet().iterator();
    Assert.assertEquals("id1", it.next());
    Assert.assertEquals("id2", it.next());
}
Also used : DefaultContentDescriptor(org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor) ParameterDescriptor(org.xwiki.rendering.macro.descriptor.ParameterDescriptor)

Example 2 with ParameterDescriptor

use of org.xwiki.rendering.macro.descriptor.ParameterDescriptor 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

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