Search in sources :

Example 11 with TemplateHashModel

use of freemarker.template.TemplateHashModel in project freemarker by apache.

the class EnumModelsTest method modelCaching.

@Test
public void modelCaching() throws Exception {
    BeansWrapper bw = new BeansWrapper(Configuration.VERSION_2_3_21);
    TemplateHashModel enums = bw.getEnumModels();
    TemplateHashModel e = (TemplateHashModel) enums.get(E.class.getName());
    assertNotNull(e);
    assertNotNull(e.get("A"));
    assertNotNull(e.get("B"));
    assertNull(e.get("C"));
    try {
        enums.get("no.such.ClassExists");
        fail();
    } catch (TemplateModelException ex) {
        assertTrue(ex.getCause() instanceof ClassNotFoundException);
    }
    TemplateModel a = e.get("A");
    assertTrue(a instanceof TemplateScalarModel);
    assertTrue(a instanceof TemplateHashModel);
    assertEquals(((TemplateScalarModel) a).getAsString(), "ts:A");
    TemplateMethodModelEx nameMethod = (TemplateMethodModelEx) ((TemplateHashModel) a).get("name");
    assertEquals(((TemplateScalarModel) nameMethod.exec(new ArrayList())).getAsString(), "A");
    assertSame(e, enums.get(E.class.getName()));
    bw.clearClassIntrospecitonCache();
    TemplateHashModel eAfterClean = (TemplateHashModel) enums.get(E.class.getName());
    assertNotSame(e, eAfterClean);
    assertSame(eAfterClean, enums.get(E.class.getName()));
    assertNotNull(eAfterClean.get("A"));
    assertNotNull(eAfterClean.get("B"));
    assertNull(eAfterClean.get("C"));
}
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)

Example 12 with TemplateHashModel

use of freemarker.template.TemplateHashModel in project freemarker by apache.

the class FineTuneMethodAppearanceTest method assertSSubvariableValue.

private void assertSSubvariableValue(Boolean replaceExistingProperty, boolean preferGetS, String expectedValue) throws TemplateModelException {
    DefaultObjectWrapper ow = new DefaultObjectWrapper(Configuration.VERSION_2_3_27);
    ow.setMethodAppearanceFineTuner(new PropertyReplacementMethodAppearanceFineTuner(replaceExistingProperty, preferGetS));
    assertEquals(expectedValue, ((TemplateScalarModel) ((TemplateHashModel) ow.wrap(new PropertyReplacementTestBean())).get("s")).getAsString());
}
Also used : TemplateHashModel(freemarker.template.TemplateHashModel) DefaultObjectWrapper(freemarker.template.DefaultObjectWrapper)

Example 13 with TemplateHashModel

use of freemarker.template.TemplateHashModel in project freemarker by apache.

the class FineTuneMethodAppearanceTest method checkIfProperlyWrapped.

private void checkIfProperlyWrapped(TemplateModel tm) throws TemplateModelException {
    TemplateHashModel thm = (TemplateHashModel) tm;
    assertEquals("v1", ((TemplateScalarModel) thm.get("v1")).getAsString());
    assertEquals("v2()", ((TemplateScalarModel) thm.get("v2")).getAsString());
    assertEquals("getV3()", ((TemplateScalarModel) thm.get("v3")).getAsString());
    assertTrue(thm.get("getV3") instanceof TemplateMethodModelEx);
}
Also used : TemplateHashModel(freemarker.template.TemplateHashModel) TemplateMethodModelEx(freemarker.template.TemplateMethodModelEx)

Example 14 with TemplateHashModel

use of freemarker.template.TemplateHashModel in project freemarker by apache.

the class RmiDebugModelImpl method get.

public DebugModel[] get(String[] keys) throws TemplateModelException, RemoteException {
    DebugModel[] dm = new DebugModel[keys.length];
    TemplateHashModel h = (TemplateHashModel) model;
    for (int i = 0; i < keys.length; i++) {
        dm[i] = getDebugModel(h.get(keys[i]));
    }
    return dm;
}
Also used : DebugModel(freemarker.debug.DebugModel) TemplateHashModel(freemarker.template.TemplateHashModel)

Example 15 with TemplateHashModel

use of freemarker.template.TemplateHashModel in project freemarker by apache.

the class _ObjectBuilderSettingEvaluator method setJavaBeanProperties.

private void setJavaBeanProperties(Object bean, List namedParamNames, List namedParamValues) throws _ObjectBuilderSettingEvaluationException {
    if (namedParamNames.isEmpty()) {
        return;
    }
    final Class cl = bean.getClass();
    Map /*<String,Method>*/
    beanPropSetters;
    try {
        PropertyDescriptor[] propDescs = Introspector.getBeanInfo(cl).getPropertyDescriptors();
        beanPropSetters = new HashMap(propDescs.length * 4 / 3, 1.0f);
        for (int i = 0; i < propDescs.length; i++) {
            PropertyDescriptor propDesc = propDescs[i];
            final Method writeMethod = propDesc.getWriteMethod();
            if (writeMethod != null) {
                beanPropSetters.put(propDesc.getName(), writeMethod);
            }
        }
    } catch (Exception e) {
        throw new _ObjectBuilderSettingEvaluationException("Failed to inspect " + cl.getName() + " class", e);
    }
    TemplateHashModel beanTM = null;
    for (int i = 0; i < namedParamNames.size(); i++) {
        String name = (String) namedParamNames.get(i);
        if (!beanPropSetters.containsKey(name)) {
            throw new _ObjectBuilderSettingEvaluationException("The " + cl.getName() + " class has no writeable JavaBeans property called " + StringUtil.jQuote(name) + ".");
        }
        Method beanPropSetter = (Method) beanPropSetters.put(name, null);
        if (beanPropSetter == null) {
            throw new _ObjectBuilderSettingEvaluationException("JavaBeans property " + StringUtil.jQuote(name) + " is set twice.");
        }
        try {
            if (beanTM == null) {
                TemplateModel wrappedObj = env.getObjectWrapper().wrap(bean);
                if (!(wrappedObj instanceof TemplateHashModel)) {
                    throw new _ObjectBuilderSettingEvaluationException("The " + cl.getName() + " class is not a wrapped as TemplateHashModel.");
                }
                beanTM = (TemplateHashModel) wrappedObj;
            }
            TemplateModel m = beanTM.get(beanPropSetter.getName());
            if (m == null) {
                throw new _ObjectBuilderSettingEvaluationException("Can't find " + beanPropSetter + " as FreeMarker method.");
            }
            if (!(m instanceof TemplateMethodModelEx)) {
                throw new _ObjectBuilderSettingEvaluationException(StringUtil.jQuote(beanPropSetter.getName()) + " wasn't a TemplateMethodModelEx.");
            }
            List /*TemplateModel*/
            args = new ArrayList();
            args.add(env.getObjectWrapper().wrap(namedParamValues.get(i)));
            ((TemplateMethodModelEx) m).exec(args);
        } catch (Exception e) {
            throw new _ObjectBuilderSettingEvaluationException("Failed to set " + StringUtil.jQuote(name), e);
        }
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) TemplateModel(freemarker.template.TemplateModel) TemplateModelException(freemarker.template.TemplateModelException) InvocationTargetException(java.lang.reflect.InvocationTargetException) TemplateHashModel(freemarker.template.TemplateHashModel) TemplateMethodModelEx(freemarker.template.TemplateMethodModelEx) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

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