use of freemarker.template.TemplateHashModel in project engine by craftercms.
the class CrafterFreeMarkerView method buildTemplateModel.
@Override
@SuppressWarnings("deprecation")
protected SimpleHash buildTemplateModel(final Map<String, Object> model, final HttpServletRequest request, final HttpServletResponse response) {
AllHttpScopesAndAppContextHashModel templateModel = new AllHttpScopesAndAppContextHashModel(getObjectWrapper(), applicationContextAccessor, getServletContext(), request);
HttpSessionHashModel sessionModel = createSessionModel(request, response);
HttpRequestHashModel requestModel = new HttpRequestHashModel(request, response, getObjectWrapper());
HttpRequestParametersHashModel requestParamsModel = new HttpRequestParametersHashModel(request);
Map<String, String> cookies = createCookieMap(request);
templateModel.put(KEY_APPLICATION_CAP, servletContextHashModel);
templateModel.put(KEY_APPLICATION, servletContextHashModel);
templateModel.put(KEY_SESSION_CAP, sessionModel);
templateModel.put(KEY_SESSION, sessionModel);
templateModel.put(KEY_REQUEST_CAP, requestModel);
templateModel.put(KEY_REQUEST, requestModel);
templateModel.put(KEY_REQUEST_PARAMS_CAP, requestParamsModel);
templateModel.put(KEY_REQUEST_PARAMS, requestParamsModel);
templateModel.put(KEY_APP_CONTEXT_CAP, applicationContextAccessor);
templateModel.put(KEY_APP_CONTEXT, applicationContextAccessor);
templateModel.put(KEY_COOKIES_CAP, cookies);
templateModel.put(KEY_COOKIES, cookies);
Authentication auth = SecurityUtils.getAuthentication(request);
if (auth != null) {
templateModel.put(KEY_AUTH_CAP, auth);
templateModel.put(KEY_AUTH, auth);
templateModel.put(KEY_PROFILE_CAP, auth.getProfile());
templateModel.put(KEY_PROFILE, auth.getProfile());
}
SiteContext siteContext = SiteContext.getCurrent();
Configuration siteConfig = siteContext.getConfig();
Locale locale = LocaleContextHolder.getLocale();
TemplateHashModel staticModels = BeansWrapper.getDefaultInstance().getStaticModels();
TemplateHashModel enumModels = BeansWrapper.getDefaultInstance().getEnumModels();
templateModel.put(KEY_STATICS_CAP, staticModels);
templateModel.put(KEY_STATICS, staticModels);
templateModel.put(KEY_ENUMS_CAP, enumModels);
templateModel.put(KEY_ENUMS, enumModels);
templateModel.put(KEY_SITE_CONTEXT_CAP, siteContext);
templateModel.put(KEY_SITE_CONTEXT, siteContext);
templateModel.put(KEY_LOCALE_CAP, locale);
templateModel.put(KEY_LOCALE, locale);
if (siteConfig != null) {
templateModel.put(KEY_SITE_CONFIG, siteConfig);
templateModel.put(KEY_SITE_CONFIG_CAP, siteConfig);
}
templateModel.putAll(model);
ObjectFactory<SimpleHash> componentModelFactory = new ObjectFactory<SimpleHash>() {
public SimpleHash getObject() {
return buildTemplateModel(model, request, response);
}
};
RenderComponentDirective renderComponentDirective = new RenderComponentDirective();
renderComponentDirective.setSiteItemService(siteItemService);
renderComponentDirective.setModelFactory(componentModelFactory);
renderComponentDirective.setTemplateXPathQuery(componentTemplateXPathQuery);
renderComponentDirective.setTemplateNamePrefix(componentTemplateNamePrefix);
renderComponentDirective.setTemplateNameSuffix(componentTemplateNameSuffix);
renderComponentDirective.setIncludeElementName(componentIncludeElementName);
renderComponentDirective.setScriptResolver(componentScriptResolver);
renderComponentDirective.setServletContext(getServletContext());
ExecuteControllerDirective executeControllerDirective = new ExecuteControllerDirective();
executeControllerDirective.setServletContext(getServletContext());
templateModel.put(RENDER_COMPONENT_DIRECTIVE_NAME, renderComponentDirective);
templateModel.put(EXECUTE_CONTROLLER_DIRECTIVE_NAME, executeControllerDirective);
return templateModel;
}
use of freemarker.template.TemplateHashModel in project freemarker by apache.
the class DynamicKeyName method dealWithNumericalKey.
private TemplateModel dealWithNumericalKey(TemplateModel targetModel, int index, Environment env) throws TemplateException {
if (targetModel instanceof TemplateSequenceModel) {
TemplateSequenceModel tsm = (TemplateSequenceModel) targetModel;
int size;
try {
size = tsm.size();
} catch (Exception e) {
size = Integer.MAX_VALUE;
}
return index < size ? tsm.get(index) : null;
}
try {
String s = target.evalAndCoerceToPlainText(env);
try {
return new SimpleScalar(s.substring(index, index + 1));
} catch (IndexOutOfBoundsException e) {
if (index < 0) {
throw new _MiscTemplateException("Negative index not allowed: ", Integer.valueOf(index));
}
if (index >= s.length()) {
throw new _MiscTemplateException("String index out of range: The index was ", Integer.valueOf(index), " (0-based), but the length of the string is only ", Integer.valueOf(s.length()), ".");
}
throw new RuntimeException("Can't explain exception", e);
}
} catch (NonStringException e) {
throw new UnexpectedTypeException(target, targetModel, "sequence or " + NonStringException.STRING_COERCABLE_TYPES_DESC, NUMERICAL_KEY_LHO_EXPECTED_TYPES, (targetModel instanceof TemplateHashModel ? "You had a numberical value inside the []. Currently that's only supported for " + "sequences (lists) and strings. To get a Map item with a non-string key, " + "use myMap?api.get(myKey)." : null), env);
}
}
use of freemarker.template.TemplateHashModel in project freemarker by apache.
the class BeansWrapperMiscTest method booleansTest.
@Test
public void booleansTest() throws Exception {
final BeansWrapper bw = new BeansWrapper();
assertTrue(((TemplateBooleanModel) bw.wrap(Boolean.TRUE)).getAsBoolean());
assertFalse(((TemplateBooleanModel) bw.wrap(Boolean.FALSE)).getAsBoolean());
TemplateHashModel tm = (TemplateHashModel) bw.wrap(Boolean.TRUE);
assertNotNull(tm.get("hashCode"));
assertNotNull(tm.get("class"));
bw.setExposureLevel(BeansWrapper.EXPOSE_PROPERTIES_ONLY);
assertNull(tm.get("hashCode"));
assertNotNull(tm.get("class"));
bw.setExposureLevel(BeansWrapper.EXPOSE_NOTHING);
assertNull(tm.get("hashCode"));
assertNull(tm.get("class"));
bw.setExposureLevel(BeansWrapper.EXPOSE_ALL);
assertNotNull(tm.get("hashCode"));
assertNotNull(tm.get("class"));
assertSame(tm, bw.wrap(Boolean.TRUE));
}
use of freemarker.template.TemplateHashModel in project freemarker by apache.
the class BeansWrapperSingletonsTest method exposesFields.
private boolean exposesFields(BeansWrapper bw) throws TemplateModelException {
TemplateHashModel thm = (TemplateHashModel) bw.wrap(new C());
TemplateScalarModel r = (TemplateScalarModel) thm.get("foo");
if (r == null)
return false;
assertEquals("FOO", r.getAsString());
return true;
}
use of freemarker.template.TemplateHashModel in project freemarker by apache.
the class StaticModelsTest method modelCaching.
@Test
public void modelCaching() throws Exception {
BeansWrapper bw = new BeansWrapper(Configuration.VERSION_2_3_21);
TemplateHashModel statics = bw.getStaticModels();
TemplateHashModel s = (TemplateHashModel) statics.get(S.class.getName());
assertNotNull(s);
assertNotNull(s.get("F"));
assertNotNull(s.get("m"));
try {
s.get("x");
fail();
} catch (TemplateModelException e) {
assertThat(e.getMessage(), containsString("No such key"));
}
try {
statics.get("no.such.ClassExists");
fail();
} catch (TemplateModelException e) {
assertTrue(e.getCause() instanceof ClassNotFoundException);
}
TemplateModel f = s.get("F");
assertTrue(f instanceof TemplateScalarModel);
assertEquals(((TemplateScalarModel) f).getAsString(), "F OK");
TemplateModel m = s.get("m");
assertTrue(m instanceof TemplateMethodModelEx);
assertEquals(((TemplateScalarModel) ((TemplateMethodModelEx) m).exec(new ArrayList())).getAsString(), "m OK");
assertSame(s, statics.get(S.class.getName()));
bw.clearClassIntrospecitonCache();
TemplateHashModel sAfterClean = (TemplateHashModel) statics.get(S.class.getName());
assertNotSame(s, sAfterClean);
assertSame(sAfterClean, statics.get(S.class.getName()));
assertNotNull(sAfterClean.get("F"));
assertNotNull(sAfterClean.get("m"));
}
Aggregations