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;
}
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;
}
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));
}
}
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");
}
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");
}
Aggregations