读写文件 mvel 渲染

Hello, my name is @{name.toUpperCase()}

print(java.lang.System.getProperty("user.name"));
var FileUtils = Java.type("org.apache.commons.io.FileUtils");
var File = Java.type("java.io.File");
var TemplateRuntime = Java.type("org.mvel2.templates.TemplateRuntime");

function readFileToString(fileName, encoding) {
	var file = new File(fileName);
	return FileUtils.readFileToString(file, encoding);

}
function writeStringToFile(fileName, content, encoding) {
	var file = new File(fileName);
	return FileUtils.writeStringToFile(file, content, encoding);
}
function rendertmpl() {
	var template = readFileToString("a.txt","utf-8");
	var vars = {
		"name" : "Michael",
		"bb" : "Michael"
	};
	var out = TemplateRuntime.eval(template, vars);
	//print(out);
	writeStringToFile("b.txt",out,"utf-8");
}
rendertmpl();


@foreach{tm : tms} 
 - @{tm["tableName"]}
 - @{tm["modelName"]}
   @foreach{cm : tm["cms"]} 
   - @{cm["columnName"]}
   - @{cm["columnClass"]}
   - @{cm["columnComment"]}
   @end{}
@end{}



import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mvel2.templates.CompiledTemplate;
import org.mvel2.templates.TemplateCompiler;
import org.mvel2.templates.TemplateRuntime;

public class App {
	public static void main(String[] args) {
		String fileName = "a.txt";
		File file = new File(fileName);
		String template = "";
		try {
			template = org.apache.commons.io.FileUtils.readFileToString(file, "utf-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
		Map<String,Object> cm= new HashMap<String, Object>();
		cm.put("columnName", "columnNamev");
		cm.put("columnClass", "columnClassV");
		cm.put("columnComment", "columnCommentV");
		List<Map<String,Object>> cms = new ArrayList<Map<String,Object>>();
		cms.add(cm);

		Map<String, Object> tm = new HashMap<String, Object>();
		tm.put("tableName", "tableNameV");
		tm.put("modelName", "modelNameV");
		tm.put("cms", cms);
		
		Map<String, Object> vars = new HashMap<String, Object>();
		List<Map<String,Object>> tms = new ArrayList<Map<String,Object>>();
		tms.add(tm);
		
		vars.put("tms", tms);

		String output = (String) TemplateRuntime.eval(template, vars);
		System.out.println(output);
		 // compile the template
		CompiledTemplate compiled = TemplateCompiler.compileTemplate(template);
		
		// execute the template
		//String output = (String) TemplateRuntime.execute(compiled, vars);

		System.out.println(java.lang.System.getProperty("java.class.path"));

		/*String fileName = "a.txt";
		File file = new File(fileName);
		String fileContent = "";
		try {
			fileContent = org.apache.commons.io.FileUtils.readFileToString(
					file, "utf-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("");
		fileContent += "Helloworld";
		try {
			org.apache.commons.io.FileUtils.writeStringToFile(file,
					fileContent, "utf-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
		*/
	}
}



jjs -cp lib\commons-io-2.4.jar;lib\mvel2-2.2.7.Final.jar mvel.js

猜你喜欢

转载自snowelf.iteye.com/blog/2258394
今日推荐