Search in sources :

Example 6 with FileResourceLoader

use of org.beetl.core.resource.FileResourceLoader in project nutzboot by nutzam.

the class BeetlGroupTemplateStarter method getGroupTemplate.

@IocBean(depose = "close")
public GroupTemplate getGroupTemplate() throws IOException {
    Properties prop = new Properties();
    for (String key : conf.keySet()) {
        if (key.startsWith("beetl.")) {
            prop.put(key.substring("beetl.".length()), conf.get(key));
        }
    }
    System.out.println(prop);
    if (!prop.containsKey(Configuration.RESOURCE_LOADER)) {
        prop.put(Configuration.RESOURCE_LOADER, ClasspathResourceLoader.class.getName());
    }
    if (!prop.containsKey("RESOURCE.autoCheck")) {
        prop.put("RESOURCE.autoCheck", "true");
    }
    if (!prop.containsKey("RESOURCE.root")) {
        prop.put("RESOURCE.root", prop.getProperty("root", "template/"));
    }
    if (!prop.containsKey(Configuration.DIRECT_BYTE_OUTPUT)) {
        // 默认启用DIRECT_BYTE_OUTPUT,除非用户自定义, 一般不会.
        log.debug("no custom DIRECT_BYTE_OUTPUT found , set to true");
        // 当DIRECT_BYTE_OUTPUT为真时, beetl渲染会通过getOutputStream获取输出流
        // 而BeetlView会使用LazyResponseWrapper代理getOutputStream方法
        // 从而实现在模板输出之前,避免真正调用getOutputStream
        // 这样@Fail视图就能正常工作了
        prop.put(Configuration.DIRECT_BYTE_OUTPUT, "true");
    }
    if (!prop.containsKey(Configuration.ERROR_HANDLER)) {
        // 没有自定义ERROR_HANDLER,用定制的
        prop.put(Configuration.ERROR_HANDLER, LogErrorHandler.class.getName());
    }
    Configuration cfg = new Configuration(prop);
    String local = conf.get(PROP_TEMPLATE_ROOT_LOCAL);
    GroupTemplate gt = new GroupTemplate(cfg);
    if (!Strings.isBlank(local)) {
        try {
            if (new File(local).exists()) {
                local = new File(local).getAbsolutePath();
                FileResourceLoader resourceLoader = new FileResourceLoader(local);
                resourceLoader.setAutoCheck(true);
                gt.setResourceLoader(resourceLoader);
                log.debugf("Template Local path=%s is ok, use it", local);
            }
        } catch (Throwable e) {
            log.infof("Template Local path=%s is not vaild, fallback to beetl.RESOURCE.root", local, e);
        }
    }
    return gt;
}
Also used : Configuration(org.beetl.core.Configuration) FileResourceLoader(org.beetl.core.resource.FileResourceLoader) ClasspathResourceLoader(org.beetl.core.resource.ClasspathResourceLoader) GroupTemplate(org.beetl.core.GroupTemplate) Properties(java.util.Properties) File(java.io.File) LogErrorHandler(org.beetl.ext.nutz.LogErrorHandler) IocBean(org.nutz.ioc.loader.annotation.IocBean)

Example 7 with FileResourceLoader

use of org.beetl.core.resource.FileResourceLoader in project beetl2.0 by javamonkey.

the class ResourceLoaderTest method testSimple.

@Test
public void testSimple() throws Exception {
    FileResourceLoader f = new FileResourceLoader();
    boolean exist = f.exist("/build.xml");
    AssertJUnit.assertTrue(exist);
    ClasspathResourceLoader cp = new ClasspathResourceLoader();
    exist = cp.exist("/template/resourceloader/cp.txt");
    AssertJUnit.assertTrue(exist);
}
Also used : FileResourceLoader(org.beetl.core.resource.FileResourceLoader) ClasspathResourceLoader(org.beetl.core.resource.ClasspathResourceLoader) Test(org.testng.annotations.Test)

Example 8 with FileResourceLoader

use of org.beetl.core.resource.FileResourceLoader in project beetl2.0 by javamonkey.

the class CompositeResourceLoaderTest method testSimple.

@Test
public void testSimple() throws Exception {
    Configuration conf = Configuration.defaultConfiguration();
    CompositeResourceLoader loader = new CompositeResourceLoader();
    String home = System.getProperty("user.dir");
    String path1 = home + "/src/test/resources/template/resourceloader/var1";
    String path2 = home + "/src/test/resources/template/resourceloader/var2";
    FileResourceLoader fileLoader1 = new FileResourceLoader(path1);
    FileResourceLoader fileLoader2 = new FileResourceLoader(path2);
    Map data = getData();
    // 根据id加载
    MapResourceLoader mapLoader = new MapResourceLoader(data);
    loader.addResourceLoader(new StartsWithMatcher("http:").withoutPrefix(), fileLoader2);
    loader.addResourceLoader(new StartsWithMatcher("db:").withoutPrefix(), mapLoader);
    loader.addResourceLoader(new AllowAllMatcher(), fileLoader1);
    GroupTemplate gt = new GroupTemplate(loader, conf);
    Template t = gt.getTemplate("/xxx.html");
    t.binding("a", "hello");
    String result = t.render();
    ;
    AssertJUnit.assertEquals("hellohello--file2:hello--db=hello", result);
}
Also used : StartsWithMatcher(org.beetl.core.resource.StartsWithMatcher) Configuration(org.beetl.core.Configuration) AllowAllMatcher(org.beetl.core.resource.AllowAllMatcher) FileResourceLoader(org.beetl.core.resource.FileResourceLoader) GroupTemplate(org.beetl.core.GroupTemplate) CompositeResourceLoader(org.beetl.core.resource.CompositeResourceLoader) Map(java.util.Map) HashMap(java.util.HashMap) GroupTemplate(org.beetl.core.GroupTemplate) Template(org.beetl.core.Template) Test(org.testng.annotations.Test)

Example 9 with FileResourceLoader

use of org.beetl.core.resource.FileResourceLoader in project beetl2.0 by javamonkey.

the class GroupTemplateTest 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();
    cf.setStatementStart("<!--:");
    cf.setStatementEnd("-->");
    FileResourceLoader rs = new FileResourceLoader(home, cf.getCharset());
    GroupTemplate gt = new GroupTemplate(rs, cf);
    List<StockModel> list = StockModel.dummyItems();
    Template t = gt.getTemplate("/helloworld.html");
    t.binding("items", list);
    StringWriter sw = new StringWriter();
    t.renderTo(sw);
    System.out.println(sw.toString());
    // 第二次
    t = gt.getTemplate("/helloworld.html");
    t.binding("items", list);
    sw = new StringWriter();
    t.renderTo(sw);
    System.out.println(sw.toString());
}
Also used : Configuration(org.beetl.core.Configuration) StringWriter(java.io.StringWriter) FileResourceLoader(org.beetl.core.resource.FileResourceLoader) GroupTemplate(org.beetl.core.GroupTemplate) GroupTemplate(org.beetl.core.GroupTemplate) Template(org.beetl.core.Template)

Aggregations

FileResourceLoader (org.beetl.core.resource.FileResourceLoader)9 Configuration (org.beetl.core.Configuration)7 GroupTemplate (org.beetl.core.GroupTemplate)7 Template (org.beetl.core.Template)6 File (java.io.File)2 ClasspathResourceLoader (org.beetl.core.resource.ClasspathResourceLoader)2 Test (org.testng.annotations.Test)2 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 ResourceLoader (org.beetl.core.ResourceLoader)1 AllowAllMatcher (org.beetl.core.resource.AllowAllMatcher)1 CompositeResourceLoader (org.beetl.core.resource.CompositeResourceLoader)1 StartsWithMatcher (org.beetl.core.resource.StartsWithMatcher)1 Program (org.beetl.core.statement.Program)1 ProgramMetaData (org.beetl.core.statement.ProgramMetaData)1 LogErrorHandler (org.beetl.ext.nutz.LogErrorHandler)1 IocBean (org.nutz.ioc.loader.annotation.IocBean)1