Search in sources :

Example 1 with TemplateModelException

use of freemarker.template.TemplateModelException in project android by JetBrains.

the class FmCompareVersionsMethod method exec.

@Override
public TemplateModel exec(List args) throws TemplateModelException {
    if (args.size() != 2) {
        throw new TemplateModelException("Wrong arguments");
    }
    GradleVersion lhs = GradleVersion.parse(args.get(0).toString());
    GradleVersion rhs = GradleVersion.parse(args.get(1).toString());
    return new SimpleNumber(lhs.compareTo(rhs));
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) SimpleNumber(freemarker.template.SimpleNumber) GradleVersion(com.android.ide.common.repository.GradleVersion)

Example 2 with TemplateModelException

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

the class IndentDirective method execute.

@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
    int numSpaces = -1;
    boolean unpad = false;
    boolean wrap = false;
    for (Object o : params.entrySet()) {
        Map.Entry ent = (Map.Entry) o;
        String paramName = (String) ent.getKey();
        TemplateModel paramValue = (TemplateModel) ent.getValue();
        switch(paramName) {
            case PARAM_NAME_SPACES:
                numSpaces = getIntParam(paramName, paramValue);
                break;
            case PARAM_NAME_UNPAD:
                unpad = getBooleanParam(paramName, paramValue);
                break;
            case PARAM_NAME_WRAP:
                wrap = getBooleanParam(paramName, paramValue);
                break;
            default:
                throw new TemplateModelException("Unsupported parameter: " + paramName);
        }
    }
    if (numSpaces < 0) {
        throw new TemplateModelException("The required \"" + PARAM_NAME_SPACES + "\" parameter is missing.");
    }
    if (body == null) {
        throw new TemplateModelException("Indent requires a body");
    } else {
        // Executes the nested body (same as <#nested> in FTL). In this
        // case we don't provide a special writer as the parameter:
        StringWriter sw = new StringWriter();
        body.render(sw);
        String fixedup = LicenseUtil.process(sw.toString(), unpad, wrap);
        IOUtils.copy(new StringReader(fixedup), new IndentingWriter(env.getOut(), numSpaces));
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) TemplateModel(freemarker.template.TemplateModel) Map(java.util.Map)

Example 3 with TemplateModelException

use of freemarker.template.TemplateModelException in project wombat by PLOS.

the class TemplateModelUtil method getAsMap.

static ImmutableMap<String, TemplateModel> getAsMap(TemplateModel value) throws TemplateModelException {
    if (value == null)
        return ImmutableMap.of();
    if (value instanceof TemplateHashModelEx) {
        TemplateHashModelEx ftlHash = (TemplateHashModelEx) value;
        ImmutableMap.Builder<String, TemplateModel> builder = ImmutableMap.builder();
        for (TemplateModelIterator iterator = ftlHash.keys().iterator(); iterator.hasNext(); ) {
            String key = iterator.next().toString();
            builder.put(key, ftlHash.get(key));
        }
        return builder.build();
    }
    throw new TemplateModelException("Hash type expected");
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateModelIterator(freemarker.template.TemplateModelIterator) TemplateHashModelEx(freemarker.template.TemplateHashModelEx) TemplateModel(freemarker.template.TemplateModel) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 4 with TemplateModelException

use of freemarker.template.TemplateModelException in project wombat by PLOS.

the class TemplateModelUtil method getAsMultimap.

static ImmutableListMultimap<String, TemplateModel> getAsMultimap(TemplateModel value) throws TemplateModelException {
    if (value == null)
        return ImmutableListMultimap.of();
    if (value instanceof TemplateHashModelEx) {
        TemplateHashModelEx ftlHash = (TemplateHashModelEx) value;
        ImmutableListMultimap.Builder<String, TemplateModel> builder = ImmutableListMultimap.builder();
        for (TemplateModelIterator iterator = ftlHash.keys().iterator(); iterator.hasNext(); ) {
            String key = iterator.next().toString();
            TemplateModel model = ftlHash.get(key);
            if (model instanceof TemplateSequenceModel) {
                TemplateSequenceModel sequenceModel = (TemplateSequenceModel) model;
                int size = sequenceModel.size();
                for (int i = 0; i < size; i++) {
                    builder.put(key, sequenceModel.get(i));
                }
            } else {
                builder.put(key, model);
            }
        }
        return builder.build();
    }
    throw new TemplateModelException("Hash type expected");
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateSequenceModel(freemarker.template.TemplateSequenceModel) TemplateModelIterator(freemarker.template.TemplateModelIterator) TemplateHashModelEx(freemarker.template.TemplateHashModelEx) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) TemplateModel(freemarker.template.TemplateModel)

Example 5 with TemplateModelException

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

the class GetOptionalTemplateMethod method exec.

public Object exec(List args) throws TemplateModelException {
    final int argCnt = args.size();
    if (argCnt < 1 || argCnt > 2) {
        throw _MessageUtil.newArgCntError(methodName, argCnt, 1, 2);
    }
    final Environment env = Environment.getCurrentEnvironment();
    if (env == null) {
        throw new IllegalStateException("No freemarer.core.Environment is associated to the current thread.");
    }
    final String absTemplateName;
    {
        TemplateModel arg = (TemplateModel) args.get(0);
        if (!(arg instanceof TemplateScalarModel)) {
            throw _MessageUtil.newMethodArgMustBeStringException(methodName, 0, arg);
        }
        String templateName = EvalUtil.modelToString((TemplateScalarModel) arg, null, env);
        try {
            absTemplateName = env.toFullTemplateName(env.getCurrentTemplate().getName(), templateName);
        } catch (MalformedTemplateNameException e) {
            throw new _TemplateModelException(e, "Failed to convert template path to full path; see cause exception.");
        }
    }
    final TemplateHashModelEx options;
    if (argCnt > 1) {
        TemplateModel arg = (TemplateModel) args.get(1);
        if (!(arg instanceof TemplateHashModelEx)) {
            throw _MessageUtil.newMethodArgMustBeExtendedHashException(methodName, 1, arg);
        }
        options = (TemplateHashModelEx) arg;
    } else {
        options = null;
    }
    String encoding = null;
    boolean parse = true;
    if (options != null) {
        final KeyValuePairIterator kvpi = TemplateModelUtils.getKeyValuePairIterator(options);
        while (kvpi.hasNext()) {
            final KeyValuePair kvp = kvpi.next();
            final String optName;
            {
                TemplateModel optNameTM = kvp.getKey();
                if (!(optNameTM instanceof TemplateScalarModel)) {
                    throw _MessageUtil.newMethodArgInvalidValueException(methodName, 1, "All keys in the options hash must be strings, but found ", new _DelayedAOrAn(new _DelayedFTLTypeDescription(optNameTM)));
                }
                optName = ((TemplateScalarModel) optNameTM).getAsString();
            }
            final TemplateModel optValue = kvp.getValue();
            if (OPTION_ENCODING.equals(optName)) {
                encoding = getStringOption(OPTION_ENCODING, optValue);
            } else if (OPTION_PARSE.equals(optName)) {
                parse = getBooleanOption(OPTION_PARSE, optValue);
            } else {
                throw _MessageUtil.newMethodArgInvalidValueException(methodName, 1, "Unsupported option ", new _DelayedJQuote(optName), "; valid names are: ", new _DelayedJQuote(OPTION_ENCODING), ", ", new _DelayedJQuote(OPTION_PARSE), ".");
            }
        }
    }
    final Template template;
    try {
        template = env.getTemplateForInclusion(absTemplateName, encoding, parse, true);
    } catch (IOException e) {
        throw new _TemplateModelException(e, "I/O error when trying to load optional template ", new _DelayedJQuote(absTemplateName), "; see cause exception");
    }
    SimpleHash result = new SimpleHash(env.getObjectWrapper());
    result.put(RESULT_EXISTS, template != null);
    // conveniently provided like in <@optTemp.include!myDefaultMacro />.
    if (template != null) {
        result.put(RESULT_INCLUDE, new TemplateDirectiveModel() {

            public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
                if (!params.isEmpty()) {
                    throw new TemplateException("This directive supports no parameters.", env);
                }
                if (loopVars.length != 0) {
                    throw new TemplateException("This directive supports no loop variables.", env);
                }
                if (body != null) {
                    throw new TemplateException("This directive supports no nested content.", env);
                }
                env.include(template);
            }
        });
        result.put(RESULT_IMPORT, new TemplateMethodModelEx() {

            public Object exec(List args) throws TemplateModelException {
                if (!args.isEmpty()) {
                    throw new TemplateModelException("This method supports no parameters.");
                }
                try {
                    return env.importLib(template, null);
                } catch (IOException e) {
                    throw new _TemplateModelException(e, "Failed to import loaded template; see cause exception");
                } catch (TemplateException e) {
                    throw new _TemplateModelException(e, "Failed to import loaded template; see cause exception");
                }
            }
        });
    }
    return result;
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateModel(freemarker.template.TemplateModel) MalformedTemplateNameException(freemarker.template.MalformedTemplateNameException) Template(freemarker.template.Template) TemplateMethodModelEx(freemarker.template.TemplateMethodModelEx) List(java.util.List) KeyValuePairIterator(freemarker.template.TemplateHashModelEx2.KeyValuePairIterator) KeyValuePair(freemarker.template.TemplateHashModelEx2.KeyValuePair) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) TemplateDirectiveBody(freemarker.template.TemplateDirectiveBody) TemplateHashModelEx(freemarker.template.TemplateHashModelEx) SimpleHash(freemarker.template.SimpleHash) TemplateScalarModel(freemarker.template.TemplateScalarModel) Map(java.util.Map) TemplateDirectiveModel(freemarker.template.TemplateDirectiveModel)

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