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"));
}
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);
}
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);
}
}
}
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"));
}
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());
}
Aggregations