Search in sources :

Example 1 with Context

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);
}
Also used : Context(org.beetl.core.Context) VirtualAttributeEval(org.beetl.core.VirtualAttributeEval) JSONObject(com.alibaba.fastjson.JSONObject) List(java.util.List) Template(org.beetl.core.Template) Test(org.testng.annotations.Test)

Example 2 with Context

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);
}
Also used : Context(org.beetl.core.Context) User(org.beetl.core.User) VirtualClassAttribute(org.beetl.core.VirtualClassAttribute) JSONObject(com.alibaba.fastjson.JSONObject) List(java.util.List) Template(org.beetl.core.Template) Test(org.testng.annotations.Test)

Example 3 with Context

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);
}
Also used : Context(org.beetl.core.Context) JsonNode(com.fasterxml.jackson.databind.JsonNode) VirtualAttributeEval(org.beetl.core.VirtualAttributeEval) JSONObject(com.alibaba.fastjson.JSONObject) StringTemplateResourceLoader(org.beetl.core.resource.StringTemplateResourceLoader) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Template(org.beetl.core.Template) Test(org.testng.annotations.Test)

Example 4 with Context

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));
}
Also used : Context(org.beetl.core.Context)

Example 5 with Context

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));
}
Also used : Context(org.beetl.core.Context)

Aggregations

Context (org.beetl.core.Context)8 Template (org.beetl.core.Template)5 Test (org.testng.annotations.Test)5 JSONObject (com.alibaba.fastjson.JSONObject)4 VirtualAttributeEval (org.beetl.core.VirtualAttributeEval)3 List (java.util.List)2 StringTemplateResourceLoader (org.beetl.core.resource.StringTemplateResourceLoader)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayList (java.util.ArrayList)1 Function (org.beetl.core.Function)1 User (org.beetl.core.User)1 VirtualClassAttribute (org.beetl.core.VirtualClassAttribute)1