Search in sources :

Example 16 with TemplateHashModel

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;
}
Also used : Locale(java.util.Locale) HttpRequestHashModel(org.craftercms.engine.util.freemarker.HttpRequestHashModel) Configuration(org.apache.commons.configuration2.Configuration) SiteContext(org.craftercms.engine.service.context.SiteContext) ExecuteControllerDirective(org.craftercms.engine.freemarker.ExecuteControllerDirective) RenderComponentDirective(org.craftercms.engine.freemarker.RenderComponentDirective) HttpSessionHashModel(freemarker.ext.servlet.HttpSessionHashModel) TemplateHashModel(freemarker.template.TemplateHashModel) ObjectFactory(org.springframework.beans.factory.ObjectFactory) Authentication(org.craftercms.security.authentication.Authentication) SimpleHash(freemarker.template.SimpleHash) HttpRequestParametersHashModel(freemarker.ext.servlet.HttpRequestParametersHashModel)

Example 17 with TemplateHashModel

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);
    }
}
Also used : TemplateSequenceModel(freemarker.template.TemplateSequenceModel) TemplateHashModel(freemarker.template.TemplateHashModel) SimpleScalar(freemarker.template.SimpleScalar) TemplateException(freemarker.template.TemplateException)

Example 18 with TemplateHashModel

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));
}
Also used : TemplateHashModel(freemarker.template.TemplateHashModel) Test(org.junit.Test)

Example 19 with TemplateHashModel

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;
}
Also used : TemplateHashModel(freemarker.template.TemplateHashModel) TemplateScalarModel(freemarker.template.TemplateScalarModel)

Example 20 with TemplateHashModel

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"));
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateHashModel(freemarker.template.TemplateHashModel) TemplateMethodModelEx(freemarker.template.TemplateMethodModelEx) ArrayList(java.util.ArrayList) TemplateScalarModel(freemarker.template.TemplateScalarModel) TemplateModel(freemarker.template.TemplateModel) Test(org.junit.Test)

Aggregations

TemplateHashModel (freemarker.template.TemplateHashModel)26 TemplateScalarModel (freemarker.template.TemplateScalarModel)9 TemplateMethodModelEx (freemarker.template.TemplateMethodModelEx)8 TemplateModelException (freemarker.template.TemplateModelException)8 Test (org.junit.Test)8 TemplateSequenceModel (freemarker.template.TemplateSequenceModel)7 TemplateModel (freemarker.template.TemplateModel)6 TemplateNumberModel (freemarker.template.TemplateNumberModel)4 TemplateException (freemarker.template.TemplateException)3 Version (freemarker.template.Version)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 BeansWrapper (freemarker.ext.beans.BeansWrapper)2 Configuration (freemarker.template.Configuration)2 SimpleScalar (freemarker.template.SimpleScalar)2 IOException (java.io.IOException)2 URL (java.net.URL)2 MultiTemplateLoader (freemarker.cache.MultiTemplateLoader)1 StringTemplateLoader (freemarker.cache.StringTemplateLoader)1 TemplateLoader (freemarker.cache.TemplateLoader)1