private void generateInc(VelocityContext context,String templateFile,String outFile) throws Exception {
try {
String encoding = DEFAULT_OUT_FILE_ENCODING;
VelocityEngine ve = getEngine();
Template t = ve.getTemplate(templateFile);
StringWriter writer = new StringWriter();
t.merge(context, writer);
if(backupOldInc){
backupOldInc(distsPath,backupPath,outFile);
}
writeOutFile(writer,outFile,encoding);
log.info("update " + outFile + "(" + encoding + ") with new value ");
} catch (Exception e) {
log.warn("Error when generating overview inc file", e);
throw e;
}
}
/**
* 根据指定的字符编码返回Velocity Engine。
* @param encoding 字符编码.Velocity的input 和 output都为此编码。
*/
private VelocityEngine getEngine() throws Exception{
String encoding = DEFAULT_OUT_FILE_ENCODING;
if(vEngine == null){
vEngine = new VelocityEngine();
Properties p = new Properties();
p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, templatesPath);
p.setProperty(Velocity.INPUT_ENCODING, encoding);
p.setProperty(Velocity.OUTPUT_ENCODING, encoding);
vEngine.init(p);
}
return vEngine;
}
/**
* 获取默认的Velocity Context.在默认的Velocity Context中默认设置了DateFormat/NumberFormat/Locale以及当前的日期。
*/
private VelocityContext getDefaultContent(){
VelocityContext context = new VelocityContext();
context.put("date", new DateTool());
context.put("number", new INumberTool());
context.put("enLocale", Locale.ENGLISH);
context.put("gbLocale", Locale.CHINESE);
context.put("b5Locale", Locale.TAIWAN);
context.put("today", new Date());
return context;
}