use of freemarker.template.TemplateModel in project entando-core by entando.
the class ControllerServlet method initFreemarker.
protected void initFreemarker(HttpServletRequest request, HttpServletResponse response, RequestContext reqCtx) throws TemplateModelException {
Configuration config = new Configuration();
DefaultObjectWrapper wrapper = new DefaultObjectWrapper();
config.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);
config.setObjectWrapper(wrapper);
config.setTemplateExceptionHandler(TemplateExceptionHandler.DEBUG_HANDLER);
TemplateModel templateModel = this.createModel(wrapper, this.getServletContext(), request, response);
ExecutorBeanContainer ebc = new ExecutorBeanContainer(config, templateModel);
reqCtx.addExtraParam(SystemConstants.EXTRAPAR_EXECUTOR_BEAN_CONTAINER, ebc);
}
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");
}
use of freemarker.template.TemplateModel in project wombat by PLOS.
the class TemplateModelUtil method getAsList.
static ImmutableList<TemplateModel> getAsList(TemplateModel value) throws TemplateModelException {
if (value == null)
return ImmutableList.of();
if (value instanceof TemplateSequenceModel) {
ImmutableList.Builder<TemplateModel> builder = ImmutableList.builder();
TemplateSequenceModel sequenceModel = (TemplateSequenceModel) value;
int size = sequenceModel.size();
for (int i = 0; i < size; i++) {
builder.add(sequenceModel.get(i));
}
return builder.build();
} else {
return ImmutableList.of(value);
}
}
use of freemarker.template.TemplateModel in project freemarker by apache.
the class DynamicKeyName method _eval.
@Override
TemplateModel _eval(Environment env) throws TemplateException {
TemplateModel targetModel = target.eval(env);
if (targetModel == null) {
if (env.isClassicCompatible()) {
return null;
} else {
throw InvalidReferenceException.getInstance(target, env);
}
}
TemplateModel keyModel = keyExpression.eval(env);
if (keyModel == null) {
if (env.isClassicCompatible()) {
keyModel = TemplateScalarModel.EMPTY_STRING;
} else {
keyExpression.assertNonNull(null, env);
}
}
if (keyModel instanceof TemplateNumberModel) {
int index = keyExpression.modelToNumber(keyModel, env).intValue();
return dealWithNumericalKey(targetModel, index, env);
}
if (keyModel instanceof TemplateScalarModel) {
String key = EvalUtil.modelToString((TemplateScalarModel) keyModel, keyExpression, env);
return dealWithStringKey(targetModel, key, env);
}
if (keyModel instanceof RangeModel) {
return dealWithRangeKey(targetModel, (RangeModel) keyModel, env);
}
throw new UnexpectedTypeException(keyExpression, keyModel, "number, range, or string", new Class[] { TemplateNumberModel.class, TemplateScalarModel.class, Range.class }, env);
}
Aggregations