use of org.beetl.core.Template 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.Template in project beetl2.0 by javamonkey.
the class MetaCopyTest method main.
public static void main(String[] args) throws Exception {
String home = System.getProperty("user.dir") + File.separator + "template" + File.separator;
Configuration cf = Configuration.defaultConfiguration();
FileResourceLoader rs = new FileResourceLoader(home, cf.getCharset());
GroupTemplate gt = new GroupTemplate(rs, cf);
Template t = gt.getTemplate("/helloworld.html");
Program p = gt.getProgram("/helloworld.html");
ProgramMetaData old = p.metaData;
ProgramMetaData copy = old.copy();
System.out.println("ok");
}
use of org.beetl.core.Template in project beetl2.0 by javamonkey.
the class ClasspathRL method main.
public static void main(String[] args) throws Exception {
ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();
URL url = resourceLoader.getClass().getResource("/org/beetl/sample/s01/hello.txt");
Configuration cfg = Configuration.defaultConfiguration();
GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
Template t = gt.getTemplate("/org/beetl/sample/s01/hello.txt");
String str = t.render();
System.out.println(str);
}
use of org.beetl.core.Template in project beetl2.0 by javamonkey.
the class FilePathRL method main.
public static void main(String[] args) throws Exception {
String root = System.getProperty("user.dir") + File.separator + "template";
FileResourceLoader resourceLoader = new FileResourceLoader(root, "utf-8");
Configuration cfg = Configuration.defaultConfiguration();
GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
Template t = gt.getTemplate("/s01/hello.txt");
String str = t.render();
System.out.println(str);
}
use of org.beetl.core.Template in project beetl2.0 by javamonkey.
the class HelloBeetl method main.
public static void main(String[] args) throws Exception {
StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
Configuration cfg = Configuration.defaultConfiguration();
GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
Template t = gt.getTemplate("hello,${name}");
t.binding("name", "beetl");
String str = t.render();
System.out.println(str);
}
Aggregations