velocity封装类(初始化、生成模板文件接口)

 

    就直接把实现工具类放上来,有时间再好好分析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;
  }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值