Search in sources :

Example 1 with TemplateNumberModel

use of freemarker.template.TemplateNumberModel in project pcgen by PCGen.

the class LoopDirective method execute.

@SuppressWarnings("rawtypes")
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
    // Check if no parameters were given:
    int fromVal = 0;
    Integer toVal = null;
    int step = 1;
    for (Object entryObj : params.entrySet()) {
        Map.Entry entry = (Entry) entryObj;
        String paramName = (String) entry.getKey();
        TemplateModel paramValue = (TemplateModel) entry.getValue();
        switch(paramName) {
            case "from":
                if (!(paramValue instanceof TemplateNumberModel)) {
                    throw new TemplateModelException("The \"" + paramName + "\" parameter " + "must be a number.");
                }
                fromVal = ((TemplateNumberModel) paramValue).getAsNumber().intValue();
                break;
            case "to":
                if (!(paramValue instanceof TemplateNumberModel)) {
                    throw new TemplateModelException("The \"" + paramName + "\" parameter " + "must be a number.");
                }
                toVal = ((TemplateNumberModel) paramValue).getAsNumber().intValue();
                break;
            case "step":
                if (!(paramValue instanceof TemplateNumberModel)) {
                    throw new TemplateModelException("The \"" + paramName + "\" parameter " + "must be a number.");
                }
                step = ((TemplateNumberModel) paramValue).getAsNumber().intValue();
                if (step == 0) {
                    throw new TemplateModelException("The \"" + paramName + "\" parameter must not be 0.");
                }
                break;
        }
    }
    if (toVal == null) {
        throw new TemplateModelException("The \"to\" parameter must be provided.");
    }
    if (body == null) {
        throw new TemplateModelException("This directive must have content.");
    }
    if (step > 0) {
        for (int i = fromVal; i <= toVal; i += step) {
            // Set the loop variable, if there is one:
            if (loopVars.length > 0) {
                loopVars[0] = new SimpleNumber(i);
            }
            if (loopVars.length > 1) {
                loopVars[1] = i + step <= toVal ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE;
            }
            // Executes the nested body (same as <#nested> in FTL). In this
            // case we don't provide a special writer as the parameter:
            body.render(env.getOut());
        }
    } else {
        for (int i = fromVal; i >= toVal; i += step) {
            // Set the loop variable, if there is one:
            if (loopVars.length > 0) {
                loopVars[0] = new SimpleNumber(i);
            }
            if (loopVars.length > 1) {
                loopVars[1] = i + step >= toVal ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE;
            }
            // Executes the nested body (same as <#nested> in FTL). In this
            // case we don't provide a special writer as the parameter:
            body.render(env.getOut());
        }
    }
}
Also used : Entry(java.util.Map.Entry) TemplateModelException(freemarker.template.TemplateModelException) Entry(java.util.Map.Entry) SimpleNumber(freemarker.template.SimpleNumber) TemplateNumberModel(freemarker.template.TemplateNumberModel) TemplateModel(freemarker.template.TemplateModel) Map(java.util.Map)

Aggregations

SimpleNumber (freemarker.template.SimpleNumber)1 TemplateModel (freemarker.template.TemplateModel)1 TemplateModelException (freemarker.template.TemplateModelException)1 TemplateNumberModel (freemarker.template.TemplateNumberModel)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1