org.beetl.core.resource.FileResourceLoader Java Examples

The following examples show how to use org.beetl.core.resource.FileResourceLoader. 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: 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 #2
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 #3
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 #4
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 #5
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 #6
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 #7
Source File: IncludeJSPTag.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected String getPrefix()
{

	ResourceLoader rs = ctx.gt.getResourceLoader();
	String loaderRoot = null;
	if (rs instanceof FileResourceLoader)
	{
		loaderRoot = ((FileResourceLoader) rs).getRoot();
	}
	else
	{
		return "";
	}

	File loaderFileRoot = new File(loaderRoot);

	if (loaderFileRoot.equals(root))
	{
		return "";
	}
	else
	{
		int len = root.getAbsolutePath().length();
		return loaderFileRoot.getAbsolutePath().substring(len);

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