Search in sources :

Example 6 with TemplateModelException

use of freemarker.template.TemplateModelException in project ninja by ninjaframework.

the class TemplateEngineFreemarkerReverseRouteHelper method computeReverseRoute.

public TemplateModel computeReverseRoute(List args) throws TemplateModelException {
    if (args.size() < 2) {
        throw new TemplateModelException("Please specify at least classname and controller (2 parameters).");
    } else {
        List<String> strings = new ArrayList<>(args.size());
        for (Object o : args) {
            // We currently allow only numbers and strings as arguments
            if (o instanceof String) {
                strings.add((String) o);
            }
            if (o instanceof SimpleScalar) {
                strings.add(((SimpleScalar) o).getAsString());
            } else if (o instanceof SimpleNumber) {
                strings.add(((SimpleNumber) o).toString());
            }
        }
        try {
            Class<?> clazz = Class.forName(strings.get(0));
            Object[] parameterMap = strings.subList(2, strings.size()).toArray();
            String reverseRoute = router.getReverseRoute(clazz, strings.get(1), parameterMap);
            return new SimpleScalar(reverseRoute);
        } catch (ClassNotFoundException ex) {
            throw new TemplateModelException("Error. Cannot find class for String: " + strings.get(0));
        }
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) SimpleNumber(freemarker.template.SimpleNumber) ArrayList(java.util.ArrayList) SimpleScalar(freemarker.template.SimpleScalar)

Example 7 with TemplateModelException

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

the class EquipSetLoopDirective method execute.

@SuppressWarnings("rawtypes")
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
    if (body == null) {
        throw new TemplateModelException("This directive must have content.");
    }
    List<EquipSet> eqSetList = new ArrayList<>();
    EquipSet currSet = null;
    String currIdPath = pc.getCalcEquipSetId();
    for (EquipSet es : pc.getDisplay().getEquipSet()) {
        if (es.getParentIdPath().equals("0")) {
            eqSetList.add(es);
            if (es.getIdPath().equals(currIdPath)) {
                currSet = es;
            }
        }
    }
    for (EquipSet equipSet : eqSetList) {
        pc.setCalcEquipSetId(equipSet.getIdPath());
        pc.setCalcEquipmentList(equipSet.getUseTempMods());
        // 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());
    }
    if (currSet != null) {
        pc.setCalcEquipSetId(currSet.getIdPath());
        pc.setCalcEquipmentList(currSet.getUseTempMods());
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) EquipSet(pcgen.core.character.EquipSet) ArrayList(java.util.ArrayList)

Example 8 with TemplateModelException

use of freemarker.template.TemplateModelException 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)

Example 9 with TemplateModelException

use of freemarker.template.TemplateModelException in project asterixdb by apache.

the class LoadFileDirective method execute.

@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
    String fileParam = null;
    String defaultParam = null;
    boolean trimParam = false;
    for (Object paramObj : params.entrySet()) {
        Map.Entry param = (Map.Entry) paramObj;
        String paramName = (String) param.getKey();
        TemplateModel paramValue = (TemplateModel) param.getValue();
        switch(paramName) {
            case PARAM_FILE:
                if (paramValue instanceof TemplateScalarModel) {
                    fileParam = ((TemplateScalarModel) paramValue).getAsString();
                } else {
                    throw new TemplateModelException(PARAM_FILE + " must be a string");
                }
                break;
            case PARAM_DEFAULT_TEXT:
                if (paramValue instanceof TemplateScalarModel) {
                    defaultParam = ((TemplateScalarModel) paramValue).getAsString();
                } else {
                    throw new TemplateModelException(PARAM_DEFAULT_TEXT + " must be a string");
                }
                break;
            case PARAM_TRIM:
                if (paramValue instanceof TemplateBooleanModel) {
                    trimParam = ((TemplateBooleanModel) paramValue).getAsBoolean();
                } else {
                    throw new TemplateModelException(PARAM_TRIM + " must be a boolean");
                }
                break;
            default:
                throw new TemplateModelException("Unknown param: " + paramName);
        }
    }
    if (fileParam == null) {
        throw new TemplateModelException("The required \"" + PARAM_FILE + "\" parameter" + "is missing.");
    }
    if (body != null) {
        throw new TemplateModelException("Body is not supported by this directive");
    }
    Writer out = env.getOut();
    File baseDir = ((FileTemplateLoader) ((Configuration) env.getTemplate().getParent()).getTemplateLoader()).baseDir;
    File file = new File(baseDir, fileParam);
    if (file.exists()) {
        if (trimParam) {
            LicenseUtil.readAndTrim(out, file);
            out.write('\n');
        } else {
            IOUtils.copy(new FileInputStream(file), out, StandardCharsets.UTF_8);
        }
    } else if (defaultParam != null) {
        out.append(defaultParam).append("\n");
    } else {
        throw new IOException("File not found: " + file);
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateModel(freemarker.template.TemplateModel) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) TemplateScalarModel(freemarker.template.TemplateScalarModel) FileTemplateLoader(freemarker.cache.FileTemplateLoader) Map(java.util.Map) File(java.io.File) TemplateBooleanModel(freemarker.template.TemplateBooleanModel) Writer(java.io.Writer)

Example 10 with TemplateModelException

use of freemarker.template.TemplateModelException in project sling by apache.

the class AdaptToMethod method exec.

@Override
public Object exec(final List arguments) throws TemplateModelException {
    if (arguments.size() != 2) {
        throw new TemplateModelException("wrong number of arguments, expecting 2 (adaptable and adapter type).");
    }
    try {
        final String classname = arguments.get(1).toString();
        final Class<?> clazz = dynamicClassLoaderManager.getDynamicClassLoader().loadClass(classname);
        final TemplateModel templateModel = (TemplateModel) arguments.get(0);
        final Object adaptable = DeepUnwrap.unwrap(templateModel);
        return adapterManager.getAdapter(adaptable, clazz);
    } catch (Exception e) {
        throw new TemplateModelException(e);
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateModel(freemarker.template.TemplateModel) TemplateModelException(freemarker.template.TemplateModelException)

Aggregations

TemplateModelException (freemarker.template.TemplateModelException)10 SimpleNumber (freemarker.template.SimpleNumber)4 TemplateModel (freemarker.template.TemplateModel)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 SimpleScalar (freemarker.template.SimpleScalar)2 Writer (java.io.Writer)2 GradleVersion (com.android.ide.common.repository.GradleVersion)1 FileTemplateLoader (freemarker.cache.FileTemplateLoader)1 StringModel (freemarker.ext.beans.StringModel)1 TemplateBooleanModel (freemarker.template.TemplateBooleanModel)1 TemplateNumberModel (freemarker.template.TemplateNumberModel)1 TemplateScalarModel (freemarker.template.TemplateScalarModel)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 Entry (java.util.Map.Entry)1 ConstraintViolation (ninja.validation.ConstraintViolation)1