Search in sources :

Example 1 with TemplateModel

use of freemarker.template.TemplateModel in project head by mifos.

the class DateFormatter method exec.

/**
     * @param args
     *            a list of arguments passed in from FTL in this order:
     *            <ul>
     *            <li>date object to be formatted. It can be an instance of either java.util.Date,
     *            org.joda.time.DateTime or org.joda.time.LocalDate</li>
     *            <li>pattern: date format pattern see {@link java.text.SimpleDateFormat}</li>
     *            <li>locale: an instance of java.util.Locale</li>
     *            </ul>
     */
@Override
public Object exec(List args) throws TemplateModelException {
    if (args.size() != 3) {
        throw new IllegalArgumentException("Wrong arguments");
    }
    Object date = DeepUnwrap.unwrap((TemplateModel) args.get(0));
    String pattern = (String) DeepUnwrap.unwrap((TemplateModel) args.get(1));
    Locale locale = (Locale) DeepUnwrap.unwrap((TemplateModel) args.get(2));
    if (date instanceof LocalDate) {
        date = ((LocalDate) date).toDateTimeAtStartOfDay();
    }
    String formatted = "";
    if (date instanceof DateTime) {
        formatted = DateTimeFormat.forPattern(pattern).withLocale(locale).print((DateTime) date);
    } else if (date instanceof Date) {
        formatted = new SimpleDateFormat(pattern, locale).format((Date) date);
    } else if (date != null) {
        throw new IllegalArgumentException("Unsupported date type: " + date.getClass());
    }
    return formatted;
}
Also used : Locale(java.util.Locale) TemplateModel(freemarker.template.TemplateModel) LocalDate(org.joda.time.LocalDate) SimpleDateFormat(java.text.SimpleDateFormat) DateTime(org.joda.time.DateTime) LocalDate(org.joda.time.LocalDate) Date(java.util.Date)

Example 2 with TemplateModel

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

the class ItemFacetModel method getInternalHashModel.

private TemplateHashModel getInternalHashModel() throws TemplateModelException {
    if (!cacheSet) {
        T obj = facet.get(id);
        TemplateModel wrapped = WRAPPER_FACET.wrap(id, obj);
        if (wrapped instanceof TemplateHashModel) {
            cache = (TemplateHashModel) wrapped;
            cacheSet = true;
        }
    }
    return cache;
}
Also used : TemplateHashModel(freemarker.template.TemplateHashModel) TemplateModel(freemarker.template.TemplateModel)

Example 3 with TemplateModel

use of freemarker.template.TemplateModel 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 4 with TemplateModel

use of freemarker.template.TemplateModel 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 5 with TemplateModel

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

Aggregations

TemplateModel (freemarker.template.TemplateModel)108 TemplateModelException (freemarker.template.TemplateModelException)26 Map (java.util.Map)23 TemplateScalarModel (freemarker.template.TemplateScalarModel)18 Test (org.junit.Test)17 ArrayList (java.util.ArrayList)15 TemplateException (freemarker.template.TemplateException)13 HashMap (java.util.HashMap)13 SimpleScalar (freemarker.template.SimpleScalar)11 IOException (java.io.IOException)11 List (java.util.List)11 Template (freemarker.template.Template)9 TemplateSequenceModel (freemarker.template.TemplateSequenceModel)8 TemplateTransformModel (freemarker.template.TemplateTransformModel)8 SimpleHash (freemarker.template.SimpleHash)7 SimpleSequence (freemarker.template.SimpleSequence)7 Writer (java.io.Writer)7 Iterator (java.util.Iterator)7 Environment (freemarker.core.Environment)6 TemplateHashModel (freemarker.template.TemplateHashModel)6