use of freemarker.template.TemplateModel in project asterixdb by apache.
the class LoadFileDirective method execute.
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
String fileParam = null;
String defaultParam = null;
boolean trimParam = false;
for (Object paramObj : params.entrySet()) {
Map.Entry param = (Map.Entry) paramObj;
String paramName = (String) param.getKey();
TemplateModel paramValue = (TemplateModel) param.getValue();
switch(paramName) {
case PARAM_FILE:
if (paramValue instanceof TemplateScalarModel) {
fileParam = ((TemplateScalarModel) paramValue).getAsString();
} else {
throw new TemplateModelException(PARAM_FILE + " must be a string");
}
break;
case PARAM_DEFAULT_TEXT:
if (paramValue instanceof TemplateScalarModel) {
defaultParam = ((TemplateScalarModel) paramValue).getAsString();
} else {
throw new TemplateModelException(PARAM_DEFAULT_TEXT + " must be a string");
}
break;
case PARAM_TRIM:
if (paramValue instanceof TemplateBooleanModel) {
trimParam = ((TemplateBooleanModel) paramValue).getAsBoolean();
} else {
throw new TemplateModelException(PARAM_TRIM + " must be a boolean");
}
break;
default:
throw new TemplateModelException("Unknown param: " + paramName);
}
}
if (fileParam == null) {
throw new TemplateModelException("The required \"" + PARAM_FILE + "\" parameter" + "is missing.");
}
if (body != null) {
throw new TemplateModelException("Body is not supported by this directive");
}
Writer out = env.getOut();
File baseDir = ((FileTemplateLoader) ((Configuration) env.getTemplate().getParent()).getTemplateLoader()).baseDir;
File file = new File(baseDir, fileParam);
if (file.exists()) {
if (trimParam) {
LicenseUtil.readAndTrim(out, file);
out.write('\n');
} else {
IOUtils.copy(new FileInputStream(file), out, StandardCharsets.UTF_8);
}
} else if (defaultParam != null) {
out.append(defaultParam).append("\n");
} else {
throw new IOException("File not found: " + file);
}
}
use of freemarker.template.TemplateModel in project engine by craftercms.
the class AllHttpScopesAndAppContextHashModel method get.
@Override
public TemplateModel get(String key) throws TemplateModelException {
// Lookup in page scope
TemplateModel model = super.get(key);
if (model != null) {
return model;
}
// Lookup in request scope
Object obj = request.getAttribute(key);
if (obj != null) {
return wrap(obj);
}
// Lookup in session scope
HttpSession session = request.getSession(false);
if (session != null) {
obj = session.getAttribute(key);
if (obj != null) {
return wrap(obj);
}
}
// Lookup in application scope
obj = context.getAttribute(key);
if (obj != null) {
return wrap(obj);
}
// Lookup in application context
try {
return wrap(applicationContextAccessor.get(key));
} catch (NoSuchBeanDefinitionException e) {
}
// return wrapper's null object (probably null).
return wrap(null);
}
use of freemarker.template.TemplateModel in project engine by craftercms.
the class RenderComponentDirective method execute.
@SuppressWarnings("unchecked")
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException {
TemplateModel componentParam = (TemplateModel) params.get(COMPONENT_PARAM_NAME);
TemplateModel componentPathParam = (TemplateModel) params.get(COMPONENT_PATH_PARAM_NAME);
TemplateModel additionalModelParam = (TemplateModel) params.get(ADDITIONAL_MODEL_PARAM_NAME);
Map<String, Object> additionalModel = null;
SiteItem component;
if (componentParam == null && componentPathParam == null) {
throw new TemplateException("No '" + COMPONENT_PARAM_NAME + "' or '" + COMPONENT_PATH_PARAM_NAME + "' param specified", env);
} else if (componentParam != null) {
component = getComponentFromNode(componentParam, env);
} else {
component = getComponentFromPath(componentPathParam, env);
}
if (additionalModelParam != null) {
additionalModel = unwrap(ADDITIONAL_MODEL_PARAM_NAME, additionalModelParam, Map.class, env);
}
Map<String, Object> templateModel = executeScripts(component, additionalModel, env);
SimpleHash model = getFullModel(component, templateModel, additionalModel);
Template template = getTemplate(component, env);
Writer output = env.getOut();
processComponentTemplate(template, model, output, env);
}
use of freemarker.template.TemplateModel in project sling by apache.
the class AdaptToMethod method exec.
@Override
public Object exec(final List arguments) throws TemplateModelException {
if (arguments.size() != 2) {
throw new TemplateModelException("wrong number of arguments, expecting 2 (adaptable and adapter type).");
}
try {
final String classname = arguments.get(1).toString();
final Class<?> clazz = dynamicClassLoaderManager.getDynamicClassLoader().loadClass(classname);
final TemplateModel templateModel = (TemplateModel) arguments.get(0);
final Object adaptable = DeepUnwrap.unwrap(templateModel);
return adapterManager.getAdapter(adaptable, clazz);
} catch (Exception e) {
throw new TemplateModelException(e);
}
}
use of freemarker.template.TemplateModel in project PublicCMS-preview by sanluan.
the class TemplateDirectiveHandler method reduce.
private Map<String, TemplateModel> reduce() throws TemplateModelException {
Map<String, TemplateModel> reduceMap = new LinkedHashMap<>();
ObjectWrapper objectWrapper = environment.getObjectWrapper();
Namespace namespace = environment.getCurrentNamespace();
Iterator<Entry<String, Object>> iterator = map.entrySet().iterator();
for (int i = 0; iterator.hasNext(); i++) {
Entry<String, Object> entry = iterator.next();
if (i < loopVars.length) {
loopVars[i] = objectWrapper.wrap(entry.getValue());
} else {
String key = entry.getKey();
reduceMap.put(key, namespace.get(key));
namespace.put(key, objectWrapper.wrap(entry.getValue()));
}
}
return reduceMap;
}
Aggregations