小鱼的空气

记录我所思

用FreeMarker自动生成代码或配置文件

FreeMarker是一个很有用的模板工具,使用也很简单。
可以用来自动生成代码或配置文件。下面的类是我以前用来产生Action类的工具,当然后来不用了,只一个基类就OK。要产生其它文件原理一样,仅当抛砖。

package com.gdsoftpark.common.core.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * Action代码产生器,根据Action类模板用FreeMarker产生Action代码.
 * <br>模板例子请参照config目录下的action.ftl
 * 
@author HuangYu
 
*/

public class ActionCodeGenerator {

    
private Configuration cfg;
    
private String templateDir = "C:/codegen/template";
    
private String templateName = "action.ftl";
    
private String outputDir = "C:/codegen/output";
    
    
public ActionCodeGenerator() throws IOException{
        init();
    }


    
public void setOutputDir(String outputDir) {
        
this.outputDir = outputDir;
    }


    
public void setTemplateDir(String templateDir) throws IOException {
        
this.templateDir = templateDir;
        cfg.setDirectoryForTemplateLoading(
new File(templateDir));
    }


    
public void setTemplateName(String templateName) {
        
this.templateName = templateName;
    }

    
    
public void init() throws IOException {
        
// Initialize the FreeMarker configuration;
        
// - Create a configuration instance
        cfg = new Configuration();
        File templateDir 
= new File(this.templateDir);
        cfg.setDirectoryForTemplateLoading(templateDir);
        cfg.setLocale(Locale.CHINA);
        cfg.setDefaultEncoding(
"GBK");
    }

    
    
/**
     * 根据Model类产生Action类代码,如UserAction.java
     * 
@param modelName Model类的名字,如:User
     
*/

    
public void generate(String modelName) throws IOException{
        
// Build the data-model
        Map<String, Object> data = new HashMap<String, Object>();
        data.put(
"T", modelName);
        
        
// Get the templat object
        Template template = cfg.getTemplate(templateName);
        
        org.apache.commons.io.FileUtils.forceMkdir(
new File(outputDir));
        File output 
= new File(outputDir, modelName + "Action.java");
        Writer writer 
= new FileWriter(output);
        
        
// Merge the data-model and the template
        try {
            template.process(data, writer);
        }
 catch (TemplateException e) {
            e.printStackTrace();
        }

    }

  
    
/**
     * 测试,根据Model类生成相应的Action
     
*/

    
public static void main(String[] args) throws IOException {
        ActionCodeGenerator gen 
= new ActionCodeGenerator();
        
        
if(args.length>0){//产生一个类
            gen.generate(args[0]);
        }
 else {// 产生多个类
            File modelClassDir = new File("C:/codegen/model");
            String[] models 
= modelClassDir.list();
            
for (String model : models) {
                String modelName 
= StringUtils.substringBeforeLast(model, ".");
                gen.generate(modelName);
            }

        }

        System.out.println(
"代码已生成,位置:"+gen.outputDir);
    }


}

posted on 2007-04-13 11:50 小鱼 阅读(3072) 评论(1)  编辑  收藏

评论

# re: 用FreeMarker自动生成代码或配置文件 2010-04-01 15:56 jordan shoes

有些提单正面没有预先印就的类似已装上船的条款,这种提单便称为备运提单。备运提单转化为已装船提单的方式有两种:(1)在提单的空白处加“已装船”批注或加盖类似内容的图章。例如“Shipped on Board”,有的只加“On Board”,然后加装船日期并加提单签发的签字或简签。所谓简签,是指签字人以最简单的签字形式通常只签本人姓名中的一个单词或一个字母来代替正式签字  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2010年4月>
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

导航

统计

常用链接

留言簿(3)

我参与的团队

随笔档案

文章档案

搜索

最新评论