Search in sources :

Example 76 with TemplateModelException

use of freemarker.template.TemplateModelException in project jbehave-core by jbehave.

the class TemplateableOutput method afterStory.

@Override
public void afterStory(boolean givenStory) {
    if (!givenStory) {
        Map<String, Object> model = newDataModel();
        model.put("story", outputStory);
        model.put("keywords", new OutputKeywords(keywords));
        TemplateHashModel enumModels = BeansWrapper.getDefaultInstance().getEnumModels();
        TemplateHashModel escapeEnums;
        try {
            String escapeModeEnum = EscapeMode.class.getCanonicalName();
            escapeEnums = (TemplateHashModel) enumModels.get(escapeModeEnum);
            model.put("EscapeMode", escapeEnums);
        } catch (TemplateModelException e) {
            throw new IllegalArgumentException(e);
        }
        write(file, templatePath, model);
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateHashModel(freemarker.template.TemplateHashModel)

Example 77 with TemplateModelException

use of freemarker.template.TemplateModelException in project ma-core-public by infiniteautomation.

the class MessageFormatDirective method execute.

@Override
public void execute(Environment env, @SuppressWarnings("rawtypes") Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
    TemplateModel key = (TemplateModel) params.get("key");
    String out;
    if (key == null) {
        // No key. Look for a message.
        BeanModel model = (BeanModel) params.get("message");
        if (model == null) {
            if (params.containsKey("message"))
                // The parameter is there, but the value is null.
                out = "";
            else
                // The parameter wasn't given
                throw new TemplateModelException("One of key or message must be provided");
        } else {
            TranslatableMessage message = (TranslatableMessage) model.getWrappedObject();
            if (message == null)
                out = "";
            else
                out = message.translate(translations);
        }
    } else {
        if (key instanceof TemplateScalarModel)
            out = translations.translate(((TemplateScalarModel) key).getAsString());
        else
            throw new TemplateModelException("key must be a string");
    }
    env.getOut().write(out);
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) BeanModel(freemarker.ext.beans.BeanModel) TemplateScalarModel(freemarker.template.TemplateScalarModel) TemplateModel(freemarker.template.TemplateModel) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 78 with TemplateModelException

use of freemarker.template.TemplateModelException in project ma-core-public by infiniteautomation.

the class UsedImagesDirective method execute.

@Override
public void execute(Environment env, @SuppressWarnings("rawtypes") Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
    boolean writeLogo = false;
    TemplateModel logo = (TemplateModel) params.get("logo");
    if (logo instanceof TemplateScalarModel) {
        String s = ((TemplateScalarModel) logo).getAsString();
        if (Boolean.parseBoolean(s)) {
            writeLogo = true;
            if (!imageList.contains(Common.applicationLogo))
                imageList.add(Common.applicationLogo);
            env.getOut().write(Common.applicationLogo);
        }
    }
    if (!writeLogo) {
        TemplateModel src = (TemplateModel) params.get("src");
        if (src instanceof TemplateScalarModel) {
            String s = "images/" + ((TemplateScalarModel) src).getAsString();
            if (!imageList.contains(s))
                imageList.add(s);
            env.getOut().write(s);
        } else
            throw new TemplateModelException("key must be a string");
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateScalarModel(freemarker.template.TemplateScalarModel) TemplateModel(freemarker.template.TemplateModel)

Example 79 with TemplateModelException

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

the class TemplateEngineFreemarkerI18nMethod method exec.

public TemplateModel exec(List args) throws TemplateModelException {
    if (args.size() == 1 && args.get(0) instanceof StringModel && ((StringModel) args.get(0)).getWrappedObject() instanceof ConstraintViolation) {
        ConstraintViolation violation = (ConstraintViolation) ((StringModel) args.get(0)).getWrappedObject();
        String messageValue = messages.get(violation.getMessageKey(), context, result, violation.getMessageParams()).orElse(violation.getDefaultMessage());
        return new SimpleScalar(messageValue);
    } else if (args.size() == 1) {
        String messageKey = ((SimpleScalar) args.get(0)).getAsString();
        String messageValue = messages.get(messageKey, context, result).orElse(messageKey);
        logIfMessageKeyIsMissing(messageKey, messageValue);
        return new SimpleScalar(messageValue);
    } else if (args.size() > 1) {
        List<String> strings = Lists.newArrayList();
        for (Object o : args) {
            // We currently allow only numbers and strings as arguments
            if (o instanceof SimpleScalar) {
                strings.add(((SimpleScalar) o).getAsString());
            } else if (o instanceof SimpleNumber) {
                strings.add(((SimpleNumber) o).toString());
            }
        }
        String messageKey = strings.get(0);
        String messageValue = messages.get(messageKey, context, result, strings.subList(1, strings.size()).toArray()).orElse(messageKey);
        logIfMessageKeyIsMissing(messageKey, messageValue);
        return new SimpleScalar(messageValue);
    } else {
        throw new TemplateModelException("Using i18n without any key is not possible.");
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) SimpleNumber(freemarker.template.SimpleNumber) ConstraintViolation(ninja.validation.ConstraintViolation) StringModel(freemarker.ext.beans.StringModel) SimpleScalar(freemarker.template.SimpleScalar)

Example 80 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)

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