use of org.beetl.core.Context in project beetl2.0 by javamonkey.
the class VirtualAttributeTest method testVirtualAttribute.
@Test
public void testVirtualAttribute() throws Exception {
gt.registerVirtualAttributeEval(new VirtualAttributeEval() {
@Override
public Object eval(Object o, String attributeName, Context ctx) {
// TODO Auto-generated method stub
return attributeName;
}
@Override
public boolean isSupport(Class c, String attributeName) {
if (attributeName.equals("hello"))
return true;
else
return false;
}
});
List list = User.getTestUsers();
Template t = gt.getTemplate("/va/va_virtual_template.html");
this.bind(t, "list", list, "user", new Object());
String str = t.render();
AssertJUnit.assertEquals(this.getFileContent("/va/va_virtual_expected.html"), str);
t = gt.getTemplate("/va/va_virtual_template.html");
this.bind(t, "list", list, "user", new Object());
str = t.render();
AssertJUnit.assertEquals(this.getFileContent("/va/va_virtual_expected.html"), str);
}
use of org.beetl.core.Context in project beetl2.0 by javamonkey.
the class VirtualAttributeTest method testVirtualClasAttribute.
@Test
public void testVirtualClasAttribute() throws Exception {
gt.registerVirtualAttributeClass(User.class, new VirtualClassAttribute() {
@Override
public String eval(Object o, String attributeName, Context ctx) {
User user = (User) o;
if (user.getAge() < 10) {
return "young";
} else {
return "old";
}
}
});
List list = User.getTestUsers();
User user = User.getTestUser();
Template t = gt.getTemplate("/va/class_virtual_template.html");
this.bind(t, "list", list, "user", user);
String str = t.render();
AssertJUnit.assertEquals(this.getFileContent("/va/class_virtual_expected.html"), str);
t = gt.getTemplate("/va/class_virtual_template.html");
this.bind(t, "list", list, "user", user);
str = t.render();
AssertJUnit.assertEquals(this.getFileContent("/va/class_virtual_expected.html"), str);
}
use of org.beetl.core.Context in project beetl2.0 by javamonkey.
the class VirtualAttributeTest method testJacksonVirtualAttribute.
@Test
public void testJacksonVirtualAttribute() throws Exception {
ObjectMapper mapper = new ObjectMapper();
String json = "{\"name\":\"lijz\",\"id\":10}";
JsonNode node = mapper.readTree(json);
gt.registerVirtualAttributeEval(new VirtualAttributeEval() {
@Override
public Object eval(Object o, String attributeName, Context ctx) {
JsonNode node = (JsonNode) o;
return node.get(attributeName);
}
@Override
public boolean isSupport(Class c, String attributeName) {
if (JsonNode.class.isAssignableFrom(c))
return true;
else
return false;
}
});
// String str = "${json.name}"; jaskson 支持get方法,因此不用虚拟属性也能访问
String str = "${json.~name}";
Template template = gt.getTemplate(str, new StringTemplateResourceLoader());
template.binding("json", node);
String ret = template.render();
AssertJUnit.assertEquals("\"lijz\"", ret);
}
use of org.beetl.core.Context in project beetl2.0 by javamonkey.
the class CheckExistFunction method main.
public static void main(String[] args) {
CheckExistFunction fn = new CheckExistFunction();
Context ctx = new Context();
ctx.set("list", null);
System.out.println(fn.call(new Object[] { "list" }, ctx));
}
use of org.beetl.core.Context in project beetl2.0 by javamonkey.
the class ParseDouble method main.
public static void main(String[] args) {
Double d = 1232323232323.89;
System.out.println(d);
String str = d.toString();
double c = Double.parseDouble(str);
System.out.println(c);
ParseDouble pDouble = new ParseDouble();
Context ctx = new Context();
System.out.println(pDouble.call(new Object[] { -01. }, ctx));
System.out.println(pDouble.call(new Object[] { 2332.23213 }, ctx));
System.out.println(pDouble.call(new Object[] { "-1.023" }, ctx));
System.out.println(pDouble.call(new Object[] { "abcd" }, ctx));
}
Aggregations