小魚的空氣

          記錄我所思

          用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 小魚 閱讀(3090) 評論(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)

          我參與的團隊

          隨筆檔案

          文章檔案

          搜索

          最新評論

          主站蜘蛛池模板: 浮梁县| 五莲县| 伊春市| 汽车| 上高县| 祁门县| 广宗县| 延庆县| 德阳市| 亳州市| 邻水| 内丘县| 五寨县| 招远市| 南溪县| 明光市| 滁州市| 鹤壁市| 芦山县| 河池市| 宣汉县| 新宁县| 珠海市| 会同县| 翁源县| 封丘县| 怀安县| 托克逊县| 丰镇市| 盈江县| 凤山县| 铁岭县| 乌拉特后旗| 玉环县| 安塞县| 大安市| 古丈县| 拉萨市| 石楼县| 伊春市| 旌德县|