Java Code Examples for org.beetl.core.GroupTemplate#getTemplate()

The following examples show how to use org.beetl.core.GroupTemplate#getTemplate() . 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: BeetlTemplateUtil.java    From smart-doc with Apache License 2.0 6 votes vote down vote up
/**
 * Batch bind binding value to Beetl templates and return all file rendered,
 * Map key is file name,value is file content
 *
 * @param path   path
 * @param params params
 * @return map
 */
public static Map<String, String> getTemplatesRendered(String path, Map<String, Object> params) {
    Map<String, String> templateMap = new HashMap<>();
    File[] files = FileUtil.getResourceFolderFiles(path);
    GroupTemplate gt = getGroupTemplate(path);
    for (File f : files) {
        if (f.isFile()) {
            String fileName = f.getName();
            Template tp = gt.getTemplate(fileName);
            if (null != params) {
                tp.binding(params);
            }
            templateMap.put(fileName, tp.render());
        }
    }
    return templateMap;
}
 
Example 2
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 3
Source File: SharedVars.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 {
	
ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();		
Configuration cfg = Configuration.defaultConfiguration();
GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
Map<String,Object> shared = new HashMap<String,Object>();
shared.put("name", "beetl");
gt.setSharedVars(shared);
Template t = gt.getTemplate("/org/beetl/sample/s0208/t1.txt");	
String str = t.render();
System.out.println(str);
t = gt.getTemplate("/org/beetl/sample/s0208/t2.txt");	
str = t.render();
System.out.println(str);


}
 
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: JavaCodeGen.java    From springboot-plus with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void make(Target target, Entity entity) {
	GroupTemplate gt = target.getGroupTemplate();
	Template template = gt.getTemplate("/java/dao.java");
	template.binding("entity", entity);
	template.binding("target", target);
	template.binding("package", gen.basePackage+".dao");
	template.binding("basePackage", gen.basePackage);
	String content = template.render();
	target.flush(this, content);
}
 
Example 6
Source File: JavaCodeGen.java    From springboot-plus with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void make(Target target, Entity entity) {
	GroupTemplate gt = target.getGroupTemplate();
	Template template = gt.getTemplate("/java/service.java");
	template.binding("entity", entity);
	template.binding("target", target);
	template.binding("package", gen.basePackage+".service");
	template.binding("basePackage", gen.basePackage);
	String content = template.render();
	target.flush(this, content);
}
 
Example 7
Source File: JavaCodeGen.java    From springboot-plus with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void make(Target target, Entity entity) {
	GroupTemplate gt = target.getGroupTemplate();
	Template template = gt.getTemplate("/java/controller.java");
	template.binding("entity", entity);
	template.binding("target", target);
	template.binding("package", gen.basePackage+".web");
	template.binding("basePackage", gen.basePackage);
	String content = template.render();
	target.flush(this, content);
}
 
Example 8
Source File: ClasspathRL.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 {
	
	ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();		
	URL url = resourceLoader.getClass().getResource("/org/beetl/sample/s01/hello.txt");
	Configuration cfg = Configuration.defaultConfiguration();
	GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
	Template t = gt.getTemplate("/org/beetl/sample/s01/hello.txt");	
	String str = t.render();
	System.out.println(str);

}
 
Example 9
Source File: HtmlGen.java    From springboot-plus with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void make(Target target, Entity entity) {
	GroupTemplate gt = target.getGroupTemplate();
	Template template = gt.getTemplate("/html/index.html");
	template.binding("entity", entity);
	template.binding("target", target);
	String content = template.render();
	target.flush(this, content);
}
 
Example 10
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 11
Source File: JavaCodeGen.java    From springboot-plus with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void make(Target target, Entity entity) {
	GroupTemplate gt = target.getGroupTemplate();
	Template template = gt.getTemplate("/java/pojo.java");
	template.binding("entity", entity);
	template.binding("target", target);
	template.binding("package", gen.basePackage+".entity");
	template.binding("className", entity.getName());
	List<Map<String,Object>> attrs = new ArrayList<Map<String,Object>>();
	for(Attribute attr:entity.getList()) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("comment", attr.getComment());
		map.put("type", attr.getJavaType());
		map.put("name", attr.getName());
		map.put("methodName", BaseTarget.upperFirst(attr.getName()));
		map.put("isId", attr.isId());
		map.put("dictType", attr.getDictType());
		attrs.add(map);
		
	}
	template.binding("attrs", attrs);
	String srcHead ="";
	srcHead+="import java.math.*;"+JavaCodeGen.CR;
	srcHead+="import java.util.Date;"+JavaCodeGen.CR;
	
	srcHead+="import java.sql.Timestamp;"+JavaCodeGen.CR;
	template.binding("imports", srcHead);
	template.binding("comment", entity.getComment());
	String content = template.render();
	target.flush(this, content);
}
 
Example 12
Source File: JSGen.java    From springboot-plus with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void make(Target target, Entity entity) {
	GroupTemplate gt = target.getGroupTemplate();
	Template template = gt.getTemplate("/js/del.js");
	template.binding("entity", entity);
	template.binding("target", target);
	String content = template.render();
	target.flush(this, content);
		
}
 
Example 13
Source File: MdGen.java    From springboot-plus with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void make(Target target, Entity entity) {
	this.entity = entity;
	GroupTemplate gt = target.getGroupTemplate();
	Template template = gt.getTemplate("/md/entity.md");
	template.binding("entity", entity);
	template.binding("target", target);
	String content = template.render();
	target.flush(this, content);

}
 
Example 14
Source File: JSGen.java    From springboot-plus with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void make(Target target, Entity entity) {
	GroupTemplate gt = target.getGroupTemplate();
	Template template = gt.getTemplate("/js/edit.js");
	template.binding("entity", entity);
	template.binding("target", target);
	String content = template.render();
	target.flush(this, content);
	
}
 
Example 15
Source File: JSGen.java    From springboot-plus with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void make(Target target, Entity entity) {
	GroupTemplate gt = target.getGroupTemplate();
	Template template = gt.getTemplate("/js/index.js");
	template.binding("entity", entity);
	template.binding("target", target);
	String content = template.render();
	target.flush(this, content);
}
 
Example 16
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 17
Source File: PairDLTest.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testSimple() throws Exception
{

	GroupTemplate newGt = getGt();
	Template t = newGt.getTemplate("/text/simple_pair_template.html");
	String str = t.render();
	AssertJUnit.assertEquals(this.getFileContent("/text/simple_pair_expected.html"), str);

}
 
Example 18
Source File: ErrorTest.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 {
	
	ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();		
	Configuration cfg = Configuration.defaultConfiguration();
	GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
	Template t = gt.getTemplate("/org/beetl/sample/s0125/error1.txt");	
	String str = t.render();
	
	t = gt.getTemplate("/org/beetl/sample/s0125/error2.txt");	
	str = t.render();
	System.out.println(str);

}
 
Example 19
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 20
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();
    }
}