Search in sources :

Example 6 with TemplateMethodModelEx

use of freemarker.template.TemplateMethodModelEx 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 7 with TemplateMethodModelEx

use of freemarker.template.TemplateMethodModelEx 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 8 with TemplateMethodModelEx

use of freemarker.template.TemplateMethodModelEx 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)

Example 9 with TemplateMethodModelEx

use of freemarker.template.TemplateMethodModelEx 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)

Example 10 with TemplateMethodModelEx

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

the class TLDParsingTest method testTldParser.

@Test
public void testTldParser() throws Exception {
    URL url = getClass().getResource("TLDParsingTest.tld");
    TaglibFactory.TldParserForTaglibBuilding tldParser = new TaglibFactory.TldParserForTaglibBuilding(wrapper);
    InputSource is = new InputSource();
    InputStream input = url.openStream();
    is.setByteStream(input);
    is.setSystemId(url.toString());
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(false);
    factory.setValidating(false);
    XMLReader reader = factory.newSAXParser().getXMLReader();
    reader.setContentHandler(tldParser);
    reader.setErrorHandler(tldParser);
    reader.parse(is);
    input.close();
    assertEquals(1, tldParser.getListeners().size());
    assertTrue(tldParser.getListeners().get(0) instanceof ExampleContextListener);
    Map tagsAndFunctions = tldParser.getTagsAndFunctions();
    assertEquals(4, tagsAndFunctions.size());
    JspTagModelBase tag = (JspTagModelBase) tagsAndFunctions.get("setStringAttributeTag");
    assertNotNull(tag);
    tag = (JspTagModelBase) tagsAndFunctions.get("setStringAttributeTag2");
    assertNotNull(tag);
    TemplateMethodModelEx function = (TemplateMethodModelEx) tagsAndFunctions.get("toUpperCase");
    assertNotNull(function);
    TemplateScalarModel result = (TemplateScalarModel) function.exec(Arrays.asList(new TemplateModel[] { new SimpleScalar("abc") }));
    assertEquals("ABC", result.getAsString());
    function = (TemplateMethodModelEx) tagsAndFunctions.get("toUpperCase2");
    assertNotNull(function);
    result = (TemplateScalarModel) function.exec(Arrays.asList(new TemplateModel[] { new SimpleScalar("abc") }));
    assertEquals("ABC", result.getAsString());
}
Also used : InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) TemplateModel(freemarker.template.TemplateModel) SimpleScalar(freemarker.template.SimpleScalar) URL(java.net.URL) TemplateMethodModelEx(freemarker.template.TemplateMethodModelEx) TemplateScalarModel(freemarker.template.TemplateScalarModel) Map(java.util.Map) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) Test(org.junit.Test)

Aggregations

TemplateMethodModelEx (freemarker.template.TemplateMethodModelEx)11 TemplateHashModel (freemarker.template.TemplateHashModel)8 TemplateScalarModel (freemarker.template.TemplateScalarModel)7 TemplateModel (freemarker.template.TemplateModel)6 TemplateModelException (freemarker.template.TemplateModelException)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)4 List (java.util.List)3 Map (java.util.Map)3 TemplateException (freemarker.template.TemplateException)2 TemplateNumberModel (freemarker.template.TemplateNumberModel)2 TemplateSequenceModel (freemarker.template.TemplateSequenceModel)2 IOException (java.io.IOException)2 TemplateHTMLOutputModel (freemarker.core.TemplateHTMLOutputModel)1 MalformedTemplateNameException (freemarker.template.MalformedTemplateNameException)1 SimpleHash (freemarker.template.SimpleHash)1 SimpleScalar (freemarker.template.SimpleScalar)1 Template (freemarker.template.Template)1 TemplateDirectiveBody (freemarker.template.TemplateDirectiveBody)1 TemplateDirectiveModel (freemarker.template.TemplateDirectiveModel)1