Java Code Examples for org.beetl.core.Configuration#defaultConfiguration()

The following examples show how to use org.beetl.core.Configuration#defaultConfiguration() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PairDLTest.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public GroupTemplate getGt()
{
	ClasspathResourceLoader rs = new ClasspathResourceLoader("/template");
	Configuration cfg;
	try
	{
		cfg = Configuration.defaultConfiguration();
	}
	catch (IOException e)
	{
		throw new RuntimeException(e);
	}
	cfg.setStatementEnd("%>");
	cfg.setStatementStart("<%");

	GroupTemplate gt = new GroupTemplate(rs, cfg);
	return gt;
}
 
Example 2
Source File: SingleDLTest.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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;
}
 
Example 3
Source File: BeetlTest.java    From seed with Apache License 2.0 6 votes vote down vote up
@Test
public void buildFromFile() throws IOException {
    /*
    <!DOCTYPE HTML>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>beetl demo</title>
    </head>
    <body>
        长篇架空武侠小说:${book},真的忍心烂尾么??
    </body>
    </html>
    */
    //使用模板文件位置实例化模板对象
    GroupTemplate groupTemplate = new GroupTemplate(new FileResourceLoader("C:/Users/Jadyer/Desktop/"), Configuration.defaultConfiguration());
    //设置模板所需的共享变量
    Map<String, Object> sharedVars = new HashMap<>();
    sharedVars.put("book", "英雄志");
    groupTemplate.setSharedVars(sharedVars);
    //模板解析成真实的HTML到文件(注意:模板文件source.html必须存在,dest.html不存在时beetl会自动创建并填充内容,dest.html存在时beetl会覆盖该文件)
    groupTemplate.getTemplate("source.html").renderTo(FileUtils.openOutputStream(new File("C:/Users/Jadyer/Desktop/dest.html")));
    //生成PDF文件
    //WkhtmltopdfUtil.convert(null, "C:/Users/Jadyer/Desktop/dest.html", "C:/Users/Jadyer/Desktop/dest.pdf");
}
 
Example 4
Source File: BeetlTest.java    From seed with Apache License 2.0 6 votes vote down vote up
@Test
public void buildFromString() throws IOException {
    GroupTemplate groupTemplate = new GroupTemplate(new StringTemplateResourceLoader(), Configuration.defaultConfiguration());
    //设置模板字符串
    Template template = groupTemplate.getTemplate("<!DOCTYPE HTML>\n" +
            "<html>\n" +
            "<head>\n" +
            "    <meta charset=\"UTF-8\">\n" +
            "    <title>beetl demo</title>\n" +
            "</head>\n" +
            "<body>\n" +
            "    长篇架空武侠小说:${book},真的忍心烂尾么??\n" +
            "</body>\n" +
            "</html>");
    //设置模板所需的共享变量
    Map<String, Object> sharedVars = new HashMap<>();
    sharedVars.put("book", "英雄志");
    template.binding(sharedVars);
    //template.binding("book", "野狗志");
    //解析模板字符串
    String data = template.render();
    System.out.println(data);
}
 
Example 5
Source File: AlbianBeetlTemplateService.java    From Albianj2 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void loading() throws AlbianParserException {
    try {
        FileResourceLoader resourceLoader = new FileResourceLoader(c.getRootPath(), c.getCharset());
        Configuration cfg = Configuration.defaultConfiguration();
        cfg.setHtmlTagSupport(true);
        gt = new GroupTemplate(resourceLoader, cfg);
        StringTemplateResourceLoader rl = new StringTemplateResourceLoader();
        sgt = new GroupTemplate(rl, cfg);
        Map<String, CustomTagConfigurtion> ctcs = c.getCustomTags();
        for (Map.Entry<String, CustomTagConfigurtion> entry : ctcs.entrySet()) {
            CustomTagConfigurtion ctc = entry.getValue();
            Class<? extends Tag> cla = (Class<? extends Tag>) AlbianClassLoader.getInstance().loadClass(ctc.getFullClassname());
            if (null != cla) {
                gt.registerTag(ctc.getName(), cla);
                sgt.registerTag(ctc.getName(), cla);
            }
        }

    } catch (Exception e) {
        throw new AlbianParserException(e);
    }

}
 
Example 6
Source File: GroupTemplateTest.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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());

	}
 
Example 7
Source File: GroupTemplateTest.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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());

	}
 
Example 8
Source File: ServletGroupTemplate.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private ServletGroupTemplate()
{

	try
	{
		Configuration cfg = Configuration.defaultConfiguration();
		WebAppResourceLoader resourceLoader = new WebAppResourceLoader();
		groupTemplate = new GroupTemplate(resourceLoader, cfg);
	}
	catch (Exception ex)
	{
		ex.printStackTrace();
		throw new RuntimeException("加载GroupTemplate失败", ex);

	}

}
 
Example 9
Source File: RegisterFunctionPackage.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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);

}
 
Example 10
Source File: NativeTest.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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);

}
 
Example 11
Source File: CompositeResourceLoaderTest.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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);

}
 
Example 12
Source File: HelloBeetl.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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);

}
 
Example 13
Source File: BeetlViewMaker.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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");
}
 
Example 14
Source File: MetaCopyTest.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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");
	}
 
Example 15
Source File: BeetlTemplateUtil.java    From smart-doc with Apache License 2.0 5 votes vote down vote up
/**
 * Get Beetl template by file name
 *
 * @param templateName template name
 * @return Beetl Template Object
 */
public static Template getByName(String templateName) {
    try {
        ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader("/template/");
        Configuration cfg = Configuration.defaultConfiguration();
        GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
        return gt.getTemplate(templateName);
    } catch (IOException e) {
        throw new RuntimeException("Can't get Beetl template.");
    }
}
 
Example 16
Source File: BeetlTemplateUtil.java    From ApplicationPower with Apache License 2.0 5 votes vote down vote up
/**
 * @param path
 * @return
 */
private static GroupTemplate getGroupTemplate(String path) {
    try {
        ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader("/" + path + "/");
        Configuration cfg = Configuration.defaultConfiguration();
        GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
        return gt;
    } catch (IOException e) {
        throw new RuntimeException("获取模板异常");
    }
}
 
Example 17
Source File: RegisterFunctionByFile.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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);
gt.registerFunctionPackage("t", new FunctionPackage());
Template t = gt.getTemplate("/s32/functionPackage.html");	
String str = t.render();		
System.out.println(str);

}
 
Example 18
Source File: FilePathRL.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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);

}
 
Example 19
Source File: GenerateJmx.java    From testing_platform with Apache License 2.0 4 votes vote down vote up
public static void generate(RunPlanBean rpb) {
    try {
        System.out.println(rpb.toString());

        String root = TPController.class.getClassLoader().getResource("").getPath() + "beetl";

        FileResourceLoader resourceLoader = new FileResourceLoader(root, "utf-8");
        Configuration cfg = Configuration.defaultConfiguration();
        GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
        Template t = gt.getTemplate("/http_request.jmx");

        t.binding("v_duration", rpb.getDuration());
        t.binding("v_users_num", rpb.getUsersNum());
        t.binding("v_ramp_up", rpb.getRampUp());
        TestPlanBean tpb = rpb.getTestPlanBean(); // TODO: wangc@2017/3/13  不够优雅,改成解析成map,直接binding map 
        if (null != tpb) {
            t.binding("v_server_name", tpb.getServerNameIp());
            t.binding("v_port", tpb.getPortNum());
            t.binding("v_prol", tpb.getProtocol());
            t.binding("v_path", tpb.getPath());
        } else {
            t.binding("v_server_name", "111");
            t.binding("v_port", "222");
            t.binding("v_prol", "333");
            t.binding("v_path", "444");
        }

        String jmxRoot = System.getProperty("user.dir")+JMX_PATH;//系统(项目)路径,结尾无斜杠
        //jmx在系统(项目)中的存在路径,斜杠开头,结尾无斜杠
        
        String fullPath = jmxRoot+StringUtils.creAndGetDir(jmxRoot); //返回的应该是  c:/jmx/2017/03  ,并创建这个目录
        
        //文件名,斜杠开头
        String fileName = new StringBuilder(File.separator)
                .append(JMX_NAME_PREFIX)
                .append(StringUtils.getDate("yyyyMMddHHmmssSSS"))
                .append(JMX_NAME_SUFFIX)
                .toString();

        String jmxFilePath = fullPath+fileName;
        rpb.setJmxPath(jmxFilePath);
        OutputStream ops = new FileOutputStream(jmxFilePath);
        t.renderTo(ops);

    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 20
Source File: WordprocessingMLBeetlTemplate.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
protected synchronized 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);
       GroupTemplate engine = new GroupTemplate(loader, cfg);
       // 设置模板引擎,减少重复初始化消耗
       this.setEngine(engine);
       return engine;
}