Search in sources :

Example 46 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 47 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 48 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 49 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)

Example 50 with TemplateModelException

use of freemarker.template.TemplateModelException in project siddhi by wso2.

the class FormatDescriptionMethod method exec.

@Override
public Object exec(List args) throws TemplateModelException {
    if (args.size() != 1) {
        throw new TemplateModelException("There should be a single argument of type string " + "passed to format description method");
    }
    SimpleScalar arg1 = (SimpleScalar) args.get(0);
    String inputString = arg1.getAsString();
    // Replacing spaces that should not be considered in text wrapping with non breaking spaces
    inputString = replaceLeadingSpaces(inputString);
    inputString = inputString.replaceAll("<", "&lt;");
    inputString = inputString.replaceAll(">", "&gt;");
    // Replacing new line characters
    inputString = replaceNewLines(inputString);
    inputString = inputString.replaceAll("\t", "&nbsp;&nbsp;&nbsp;&nbsp;");
    inputString = inputString.replaceAll("```([^```]*)```", "<pre>$1</pre>");
    inputString = inputString.replaceAll("`([^`]*)`", "<code>$1</code>");
    return inputString;
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) SimpleScalar(freemarker.template.SimpleScalar)

Aggregations

TemplateModelException (freemarker.template.TemplateModelException)87 TemplateModel (freemarker.template.TemplateModel)24 IOException (java.io.IOException)21 TemplateScalarModel (freemarker.template.TemplateScalarModel)18 SimpleScalar (freemarker.template.SimpleScalar)17 BeanModel (freemarker.ext.beans.BeanModel)14 Environment (freemarker.core.Environment)13 Writer (java.io.Writer)13 ArrayList (java.util.ArrayList)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 Map (java.util.Map)10 List (java.util.List)9 SimpleNumber (freemarker.template.SimpleNumber)7 TemplateHashModel (freemarker.template.TemplateHashModel)7 Iterator (java.util.Iterator)6 freemarker.core._TemplateModelException (freemarker.core._TemplateModelException)5 TemplateHashModelEx (freemarker.template.TemplateHashModelEx)5 TemplateMethodModelEx (freemarker.template.TemplateMethodModelEx)5 TemplateModelIterator (freemarker.template.TemplateModelIterator)5 UMAException (com.ibm.uma.UMAException)4