Search in sources :

Example 16 with Configuration

use of org.beetl.core.Configuration in project hello-world by haoziapple.

the class BasicUse 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);
}
Also used : Configuration(org.beetl.core.Configuration) StringTemplateResourceLoader(org.beetl.core.resource.StringTemplateResourceLoader) GroupTemplate(org.beetl.core.GroupTemplate) GroupTemplate(org.beetl.core.GroupTemplate) Template(org.beetl.core.Template)

Example 17 with Configuration

use of org.beetl.core.Configuration 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 18 with Configuration

use of org.beetl.core.Configuration in project docx4j-template by vindell.

the class WordprocessingMLBeetlTemplate method getInternalEngine.

protected GroupTemplate getInternalEngine() throws IOException {
    ClasspathResourceLoader loader = new ClasspathResourceLoader();
    // 加载默认参数
    Configuration cfg = Configuration.defaultConfiguration();
    // 模板字符集
    cfg.setCharset(Docx4jProperties.getProperty("docx4j.beetl.charset", Docx4jConstants.DEFAULT_CHARSETNAME));
    // 模板占位起始符号
    cfg.setPlaceholderStart(Docx4jProperties.getProperty("docx4j.beetl.placeholderStart", "${"));
    // 模板占位结束符号
    cfg.setPlaceholderEnd(Docx4jProperties.getProperty("docx4j.beetl.placeholderEnd", "<%"));
    // 控制语句起始符号
    cfg.setStatementStart(Docx4jProperties.getProperty("docx4j.beetl.statementStart", "%>"));
    // 控制语句结束符号
    cfg.setStatementEnd(Docx4jProperties.getProperty("docx4j.beetl.statementEnd", "}"));
    // 是否允许html tag,在web编程中,有可能用到html tag,最好允许
    cfg.setHtmlTagSupport(Docx4jProperties.getProperty("docx4j.beetl.htmlTagSupport", false));
    // html tag 标示符号
    cfg.setHtmlTagFlag(Docx4jProperties.getProperty("docx4j.beetl.htmlTagFlag", "#"));
    // html 绑定的属性,如&lt;aa var="customer">
    cfg.setHtmlTagBindingAttribute(Docx4jProperties.getProperty("docx4j.beetl.htmlTagBindingAttribute", "var"));
    // 是否允许直接调用class
    cfg.setNativeCall(Docx4jProperties.getProperty("docx4j.beetl.nativeCall", false));
    // 输出模式,默认是字符集输出,改成byte输出提高性能
    cfg.setDirectByteOutput(Docx4jProperties.getProperty("docx4j.beetl.directByteOutput", true));
    // 严格mvc应用,只有变态的的人才打开此选项
    cfg.setStrict(Docx4jProperties.getProperty("docx4j.beetl.strict", false));
    // 是否忽略客户端的网络异常
    cfg.setIgnoreClientIOError(Docx4jProperties.getProperty("docx4j.beetl.ignoreClientIOError", true));
    // 错误处理类
    cfg.setErrorHandlerClass(Docx4jProperties.getProperty("docx4j.beetl.errorHandlerClass", "org.beetl.core.ConsoleErrorHandler"));
    // 资源参数
    Map<String, String> resourceMap = cfg.getResourceMap();
    // classpath 跟路径
    resourceMap.put("root", Docx4jProperties.getProperty("docx4j.beetl.resource.root", "/"));
    // 是否检测文件变化
    resourceMap.put("autoCheck", Docx4jProperties.getProperty("docx4j.beetl.resource.autoCheck", "true"));
    // 自定义脚本方法文件位置
    resourceMap.put("functionRoot", Docx4jProperties.getProperty("docx4j.beetl.resource.functionRoot", "functions"));
    // 自定义脚本方法文件的后缀
    resourceMap.put("functionSuffix", Docx4jProperties.getProperty("docx4j.beetl.resource.functionSuffix", "html"));
    // 自定义标签文件位置
    resourceMap.put("tagRoot", Docx4jProperties.getProperty("docx4j.beetl.resource.tagRoot", "htmltag"));
    // 自定义标签文件后缀
    resourceMap.put("tagSuffix", Docx4jProperties.getProperty("docx4j.beetl.resource.tagSuffix", "tag"));
    cfg.setResourceMap(resourceMap);
    return new GroupTemplate(loader, cfg);
}
Also used : Configuration(org.beetl.core.Configuration) ClasspathResourceLoader(org.beetl.core.resource.ClasspathResourceLoader) GroupTemplate(org.beetl.core.GroupTemplate)

Example 19 with Configuration

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

the class SingleDLTest method getGt.

public GroupTemplate getGt() {
    ClasspathResourceLoader rs = new ClasspathResourceLoader("/template");
    Configuration cfg;
    try {
        cfg = Configuration.defaultConfiguration();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    cfg.setStatementEnd(null);
    cfg.setStatementStart("@");
    GroupTemplate gt = new GroupTemplate(rs, cfg);
    return gt;
}
Also used : Configuration(org.beetl.core.Configuration) ClasspathResourceLoader(org.beetl.core.resource.ClasspathResourceLoader) IOException(java.io.IOException) GroupTemplate(org.beetl.core.GroupTemplate)

Example 20 with Configuration

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

the class NativeTest method testSecurity.

@Test
public void testSecurity() throws Exception {
    StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
    Configuration cfg = Configuration.defaultConfiguration();
    GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
    Template t = gt.getTemplate("hello,${@java.lang.System.currentTimeMillis()}");
    String str = t.render();
    AssertJUnit.assertEquals("hello,", str);
}
Also used : Configuration(org.beetl.core.Configuration) StringTemplateResourceLoader(org.beetl.core.resource.StringTemplateResourceLoader) GroupTemplate(org.beetl.core.GroupTemplate) GroupTemplate(org.beetl.core.GroupTemplate) Template(org.beetl.core.Template) Test(org.testng.annotations.Test)

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