org.beetl.core.resource.ClasspathResourceLoader Java Examples

The following examples show how to use org.beetl.core.resource.ClasspathResourceLoader. 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: 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 #2
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 #3
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 #4
Source File: ViewConfiguration.java    From java-platform with Apache License 2.0 6 votes vote down vote up
@Bean
public CompositeResourceLoader viewResourceLoader() {
	CompositeResourceLoader compositeResourceLoader = new CompositeResourceLoader();
	compositeResourceLoader.addResourceLoader(new StartsWithMatcher(VIEW_PREFIX_CLASSPATH).withoutPrefix(),
			new ClasspathResourceLoader("/views"));
	try {
		compositeResourceLoader.addResourceLoader(new StartsWithMatcher(VIEW_PREFIX_TEMPLATE),
				new WebAppResourceLoader(new ServletContextResource(servletContext, templateLocation).getFile().getAbsolutePath()));

		compositeResourceLoader.addResourceLoader(new StartsWithMatcher("/WEB-INF").withPrefix(),
				new WebAppResourceLoader(servletContext.getRealPath(".")));
	} catch (IOException e) {
		e.printStackTrace();
	}
	return compositeResourceLoader;
}
 
Example #5
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 #6
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 #7
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 #8
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 #9
Source File: SQLBeetlRender.java    From weed3 with Apache License 2.0 5 votes vote down vote up
private void initForRuntime() {
    try {
        ClasspathResourceLoader loader = new ClasspathResourceLoader(this.getClass().getClassLoader(), _baseUri);
        gt = new GroupTemplate(loader, cfg);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}
 
Example #10
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 #11
Source File: BeetlTemplateUtil.java    From ApplicationPower with Apache License 2.0 5 votes vote down vote up
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("获取模板异常");

    }
}
 
Example #12
Source File: BeanConfig.java    From WeBASE-Codegen-Monkey with Apache License 2.0 5 votes vote down vote up
/**
 * Beetl render template.
 * 
 * @return GroupTemplate
 * @throws IOException
 */
@Bean
public GroupTemplate getGroupTemplateInstance() throws IOException {
    ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader("");
    org.beetl.core.Configuration cfg = org.beetl.core.Configuration.defaultConfiguration();
    GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
    return gt;
}
 
Example #13
Source File: BeetlTemplateUtil.java    From smart-doc 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("Can't get Beetl template.");
    }
}
 
Example #14
Source File: CoreBeanConfig.java    From WeBASE-Collect-Bee with Apache License 2.0 5 votes vote down vote up
/**
 * Beetl render template.
 * 
 * @return GroupTemplate
 * @throws IOException
 */
@Bean
public static GroupTemplate getGroupTEmplateInstance() throws IOException {
    ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader("");
    org.beetl.core.Configuration cfg = org.beetl.core.Configuration.defaultConfiguration();
    GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
    return gt;
}
 
Example #15
Source File: BeetlConfiguration.java    From seed with Apache License 2.0 4 votes vote down vote up
@Bean(initMethod="init", name="beetlConfig")
public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
    BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
    beetlGroupUtilConfiguration.setResourceLoader(new ClasspathResourceLoader(BeetlConfiguration.class.getClassLoader(), "templates/"));
    return beetlGroupUtilConfiguration;
}
 
Example #16
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;
}
 
Example #17
Source File: BeetlConfiguration.java    From seed with Apache License 2.0 4 votes vote down vote up
@Bean(initMethod="init", name="beetlConfig")
public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
    BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
    beetlGroupUtilConfiguration.setResourceLoader(new ClasspathResourceLoader(BeetlConfiguration.class.getClassLoader(), "templates/"));
    return beetlGroupUtilConfiguration;
}
 
Example #18
Source File: BeetlConfiguration.java    From seed with Apache License 2.0 4 votes vote down vote up
@Bean(initMethod="init", name="beetlConfig")
public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
    BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
    beetlGroupUtilConfiguration.setResourceLoader(new ClasspathResourceLoader(BeetlConfiguration.class.getClassLoader(), "templates/"));
    return beetlGroupUtilConfiguration;
}
 
Example #19
Source File: BeetlConfiguration.java    From seed with Apache License 2.0 4 votes vote down vote up
@Bean(initMethod="init", name="beetlConfig")
public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
    BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
    beetlGroupUtilConfiguration.setResourceLoader(new ClasspathResourceLoader(BeetlConfiguration.class.getClassLoader(), "templates/"));
    return beetlGroupUtilConfiguration;
}
 
Example #20
Source File: Test.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
public static void main(String[] args) throws Exception {
	


	ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader("org/beetl/core/lab/");
	Configuration cfg = Configuration.defaultConfiguration();

	cfg.setDirectByteOutput(true);
	cfg.getResourceMap().put("tagRoot", "");
	cfg.getPkgList().add("org.beetl.core.lab.");

	GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
	gt.registerFunction("bigString", new TestFun());
	gt.registerTag("form",FormTag.class);
	 cfg.setStatementStart("@");
	 cfg.setStatementEnd(null);




	List list = new ArrayList();
	// list.add(new TestUser("abc"));

	HashMap map = new HashMap();
	map.put("key", 123);
	// gt.enableStrict();

	for (int i = 0; i < 1; i++) {

		Template t = gt.getTemplate("/hello.txt");
		// TestUser user = new TestUser("jo");
		// user.lover = new TestUser("dddd");
		// user.friends = list;
		// t.binding("user",user);
		t.binding("$page", new HashMap());
		Long a = 12342343434l;
		Object c = a;
		t.binding("x", a);
		t.binding("y", c);
		// t.binding("user", new TestUser("def"));
		String str = t.render();
		System.out.println(str);



	}

}