就直接把实现工具类放上来,有时间再好好分析velocity,好久没动了。给个好开始2012,博客开始活跃。
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
/**
* 生成文件的类
* @ClassName: VelocityUtil
* @author slk
* @date 2011-7-22 下午05:53:19
*/
public class VelocityUtil {
private VelocityEngine velocityEngine;
public final static String GBK="GBK";
public final static String UTF_8="UTF-8";
private Template template;
private VelocityContext velocityContext;
private boolean isverify=true;//参数如果都不为空,就为true
private VelocityAttribute attribute;//为VelocityUtil的值对象
private String encoding=UTF_8;//设置生成文件及读取文件的编码
public VelocityUtil(VelocityAttribute attribute){
this.attribute=attribute;
init();
}
public VelocityUtil(VelocityAttribute attribute,String encoding){
this.attribute=attribute;
this.encoding=encoding;
init();
}
/**
* 设置VelocityEngine的参数
* @param key
* @param value
* @return
* @see
* @author slk
* @date 2011-7-22 下午05:50:26
*/
public VelocityUtil setProperty(String key,Object value){
this.velocityEngine.setProperty(key, value);
return this;
}
/**
* 初始化类的一些必须值
* @see
* @author slk
* @date 2011-7-22 下午05:50:52
*/
private void init(){
this.velocityEngine = new VelocityEngine();
if(isverify=this.isAttribute()){
this.velocityEngine.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, this.attribute.getTemplatePath());
//处理中文问题
this.velocityEngine.setProperty(Velocity.ENCODING_DEFAULT,encoding);
this.velocityEngine.setProperty(Velocity.INPUT_ENCODING,encoding);
this.velocityEngine.setProperty(Velocity.OUTPUT_ENCODING,encoding);
//初始化模板
try {
this.velocityEngine.init();
this.template = velocityEngine.getTemplate(this.attribute.getTemplateFileName());
this.velocityContext = new VelocityContext();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 设置在模板文件里的参数变量及值
* @param key
* @param value
* @return
* @see
* @author slk
* @date 2011-7-22 下午05:51:17
*/
public VelocityUtil put(String key,Object value){
if(isverify){
velocityContext.put(key, value);
}
return this;
}
/**
* 生成文件的方法
* @return
* @see
* @author slk
* @date 2011-7-22 下午05:51:53
*/
public boolean executegenerateFile(){
boolean success=false;
Writer mywriter = null;
if(isverify){
try {
File file=new File(this.attribute.getGeneratePath()+"/"+this.attribute.getGenerateFileName());
if(!file.exists()){
File pfile=file.getParentFile();
if(!pfile.exists()){
pfile.mkdirs();
}
}
mywriter = new PrintWriter(file.getAbsoluteFile(),encoding);
this.template.merge(this.velocityContext, mywriter);
mywriter.flush();
success=true;
} catch (Exception e) {
e.printStackTrace();
}finally{
if(mywriter!=null){
try {
mywriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
return success;
}
/**
* 对VelocityAttribute的字段值判断是否为空值
* @return
* @see
* @author slk
* @date 2011-7-22 下午05:52:09
*/
private boolean isAttribute(){
boolean success=true;
if(this.attribute!=null){
if(attribute.getGenerateFileName()==null||attribute.getGenerateFileName().equals("")){
success=false;
}else if(attribute.getTemplateFileName()==null||attribute.getTemplateFileName().equals("")){
success=false;
}else if(attribute.getGeneratePath()==null||attribute.getGeneratePath().equals("")){
success=false;
}else if(attribute.getTemplatePath()==null||attribute.getTemplatePath().equals("")){
success=false;
}
}else{
success=false;
}
return success;
}
}
属性类:
**
* 是VelocityUtil的值对象
* @ClassName: VelocityAttribute
* @author slk
* @date 2011-7-22 下午05:52:55
*/
public class VelocityAttribute {
private String generateFileName;//生成文件后名称
private String templateFileName;//生成文件的模板文件名
private String templatePath;//生成文件的模板路
private String generatePath;//生成文件的存放路径
public String getGenerateFileName() {
return generateFileName;
}
public void setGenerateFileName(String generateFileName) {
this.generateFileName = generateFileName;
}
public String getTemplateFileName() {
return templateFileName;
}
public void setTemplateFileName(String templateFileName) {
this.templateFileName = templateFileName;
}
public String getTemplatePath() {
return templatePath;
}
public void setTemplatePath(String templatePath) {
this.templatePath = templatePath;
}
public String getGeneratePath() {
return generatePath;
}
public void setGeneratePath(String generatePath) {
this.generatePath = generatePath;
}
}