Search in sources :

Example 11 with Configuration

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

the class RegisterFunctionPackage 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("/s32/pagefunction.html");
    String str = t.render();
    System.out.println(str);
}
Also used : Configuration(org.beetl.core.Configuration) FileResourceLoader(org.beetl.core.resource.FileResourceLoader) GroupTemplate(org.beetl.core.GroupTemplate) GroupTemplate(org.beetl.core.GroupTemplate) Template(org.beetl.core.Template)

Example 12 with Configuration

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

the class BeetlGroupUtilConfiguration method initGroupTemplate.

private void initGroupTemplate() throws IOException {
    // 配置数据加载
    Configuration configuration = null;
    // 如果都未设置,取默认的配置
    if ((configProperties == null) && (configFileResource == null)) {
        configuration = Configuration.defaultConfiguration();
    } else {
        Properties properties = new Properties();
        if (configFileResource != null) {
            InputStream in = null;
            try {
                // 如果指定了配置文件,先加载配置文件
                in = configFileResource.getInputStream();
                properties.load(in);
            } catch (IOException ex) {
                throw ex;
            } finally {
                if (in != null) {
                    in.close();
                    in = null;
                }
            }
        }
        if (configProperties != null) {
            for (Entry<Object, Object> entry : configProperties.entrySet()) {
                String key = (String) entry.getKey();
                String value = (String) entry.getValue();
                properties.setProperty(key, value);
            }
        }
        // 使用配置项配置properties
        configuration = new Configuration(properties);
    }
    // 如果未指定,返回
    if (resourceLoader != null) {
        groupTemplate = new GroupTemplate(resourceLoader, configuration);
    } else {
        WebAppResourceLoader defaultLoader = new WebAppResourceLoader(root);
        groupTemplate = new GroupTemplate(defaultLoader, configuration);
    }
    if (errorHandler != null) {
        groupTemplate.setErrorHandler(errorHandler);
    }
    // 设置共享变量
    if (sharedVars != null) {
        groupTemplate.setSharedVars(sharedVars);
    }
}
Also used : Configuration(org.beetl.core.Configuration) InputStream(java.io.InputStream) IOException(java.io.IOException) GroupTemplate(org.beetl.core.GroupTemplate) Properties(java.util.Properties) WebAppResourceLoader(org.beetl.core.resource.WebAppResourceLoader)

Example 13 with Configuration

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

the class JFinal3BeetlRenderFactory method config.

public void config(ResourceLoader rs) {
    if (groupTemplate != null) {
        groupTemplate.close();
    }
    try {
        Configuration cfg = Configuration.defaultConfiguration();
        groupTemplate = new GroupTemplate(rs, cfg);
    } catch (IOException e) {
        throw new RuntimeException("加载GroupTemplate失败", e);
    }
}
Also used : Configuration(org.beetl.core.Configuration) GroupTemplate(org.beetl.core.GroupTemplate) IOException(java.io.IOException)

Example 14 with Configuration

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

the class BeetlViewMaker method init.

public void init() throws IOException {
    log.debug("beetl init ....");
    Configuration cfg = Configuration.defaultConfiguration();
    Properties prop = new Properties();
    InputStream ins = Configuration.class.getResourceAsStream("/beetl.properties");
    if (ins != null) {
        log.debug("found beetl.properties, loading ...");
        try {
            prop.load(ins);
        } finally {
            Streams.safeClose(ins);
        }
    }
    if (!prop.containsKey(Configuration.RESOURCE_LOADER)) {
        // 默认选用WebAppResourceLoader,除非用户自定义了RESOURCE_LOADER
        log.debug("no custom RESOURCE_LOADER found , select WebAppResourceLoader");
        cfg.setResourceLoader(WebAppResourceLoader.class.getName());
    }
    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视图就能正常工作了
        cfg.setDirectByteOutput(true);
    }
    if (!prop.containsKey(Configuration.ERROR_HANDLER)) {
        // 没有自定义ERROR_HANDLER,用定制的
        cfg.setErrorHandlerClass(LogErrorHandler.class.getName());
    }
    groupTemplate = new GroupTemplate(cfg);
    render = new WebRender(groupTemplate);
    log.debug("beetl init complete");
}
Also used : WebRender(org.beetl.ext.web.WebRender) Configuration(org.beetl.core.Configuration) InputStream(java.io.InputStream) GroupTemplate(org.beetl.core.GroupTemplate) Properties(java.util.Properties) WebAppResourceLoader(org.beetl.core.resource.WebAppResourceLoader)

Example 15 with Configuration

use of org.beetl.core.Configuration in project swift by luastar.

the class BeetlUtils method init.

private void init() {
    try {
        ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();
        Configuration cfg = Configuration.defaultConfiguration();
        groupTemplate = new GroupTemplate(resourceLoader, cfg);
    } catch (Exception e) {
        logger.error("转换为文件时异常", e);
    }
}
Also used : Configuration(org.beetl.core.Configuration) ClasspathResourceLoader(org.beetl.core.resource.ClasspathResourceLoader) GroupTemplate(org.beetl.core.GroupTemplate)

Aggregations

Configuration (org.beetl.core.Configuration)23 GroupTemplate (org.beetl.core.GroupTemplate)22 Template (org.beetl.core.Template)13 ClasspathResourceLoader (org.beetl.core.resource.ClasspathResourceLoader)10 FileResourceLoader (org.beetl.core.resource.FileResourceLoader)7 IOException (java.io.IOException)6 Properties (java.util.Properties)4 HashMap (java.util.HashMap)3 StringTemplateResourceLoader (org.beetl.core.resource.StringTemplateResourceLoader)3 InputStream (java.io.InputStream)2 WebAppResourceLoader (org.beetl.core.resource.WebAppResourceLoader)2 Program (org.beetl.core.statement.Program)2 ProgramMetaData (org.beetl.core.statement.ProgramMetaData)2 Test (org.testng.annotations.Test)2 ToolUtil (com.ikoori.vip.server.core.util.ToolUtil)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 StringWriter (java.io.StringWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URL (java.net.URL)1