rapid_generator uml

Process procedure:

GeneratorMain:

public static void main(String[] args) throws Exception {
        GeneratorFacade g = new GeneratorFacade();
//        g.printAllTableNames();                //打印数据库中的表名称
        
        g.deleteOutRootDir();                            //删除生成器的输出目录
//        g.generateByTable("table_name","template");    //通过数据库表生成文件,template为模板的根目录
        g.generateByAllTable("template");    //自动搜索数据库中的所有表并生成文件,template为模板的根目录
//        g.generateByClass(Blog.class,"template_clazz");
         
//        g.deleteByTable("table_name", "template"); //删除生成的文件
        //打开文件夹
        Runtime.getRuntime().exec("cmd.exe /c start "+GeneratorProperties.getRequiredProperty("outRoot"));
    }

GeneratorFacade:

public void generateByAllTable(String templateRootDir) throws Exception {
        new ProcessUtils().processByAllTable(templateRootDir,false);
    }

ProcessUtils:

public void processByAllTable(String templateRootDir,boolean isDelete) throws Exception {
            List<Table> tables = TableFactory.getInstance().getAllTables();
            List exceptions = new ArrayList();
            for(int i = 0; i < tables.size(); i++ ) {
                try {
                    processByTable(getGenerator(templateRootDir),tables.get(i),isDelete);
                }catch(GeneratorException ge) {
                    exceptions.addAll(ge.getExceptions());
                }
            }
            PrintUtils.printExceptionsSumary("",getGenerator(templateRootDir).getOutRootDir(),exceptions);
        }
  public void processByTable(String tableName,String templateRootDir,boolean isDelete) throws Exception {
            if("*".equals(tableName)) {
                if(isDelete)
                    deleteByAllTable(templateRootDir);
                else
                    generateByAllTable(templateRootDir);
                return;
            }
            Generator g = getGenerator(templateRootDir);
            Table table = TableFactory.getInstance().getTable(tableName);
            try {
                processByTable(g,table,isDelete);
            }catch(GeneratorException ge) {
                PrintUtils.printExceptionsSumary(ge.getMessage(),getGenerator(templateRootDir).getOutRootDir(),ge.getExceptions());
            }
        

GeneratorFacade:

    public void processByTable(Generator g, Table table,boolean isDelete) throws Exception {
            GeneratorModel m = GeneratorModelUtils.newFromTable(table);
            PrintUtils.printBeginProcess(table.getSqlName()+" => "+table.getClassName(),isDelete);
            if(isDelete)
                g.deleteBy(m.templateModel,m.filePathModel);
            else 
                g.generateBy(m.templateModel,m.filePathModel);
        }
GeneratorModelUtils:
public static GeneratorModel newFromTable(Table table) {
            Map templateModel = new HashMap();
            templateModel.put("table", table);
            setShareVars(templateModel);
            
            Map filePathModel = new HashMap();
            setShareVars(filePathModel);
            filePathModel.putAll(BeanHelper.describe(table));
            return new GeneratorModel(templateModel,filePathModel);
        }

Generator:

public Generator generateBy(Map templateModel,Map filePathModel) throws Exception {
        processTemplateRootDirs(templateModel, filePathModel,false);
        return this;
    }
private void processTemplateRootDirs(Map templateModel,Map filePathModel,boolean isDelete) throws Exception {
        if(StringHelper.isBlank(getOutRootDir())) throw new IllegalStateException("'outRootDir' property must be not null.");
        if(templateRootDirs.size() == 0) throw new IllegalStateException("'templateRootDirs' cannot empty");
        GeneratorException ge = new GeneratorException("generator occer error, Generator BeanInfo:"+BeanHelper.describe(this));
        for(int i = 0; i < this.templateRootDirs.size(); i++) {
            File templateRootDir = (File)templateRootDirs.get(i);
            List<Exception> exceptions = scanTemplatesAndProcess(templateRootDir,templateModel,filePathModel,isDelete);
            ge.addAll(exceptions); 
        }
        if(!ge.exceptions.isEmpty()) throw ge;
    }

 


private List<Exception> scanTemplatesAndProcess(File templateRootDir, Map templateModel,Map filePathModel,boolean isDelete) throws Exception {
        if(templateRootDir == null) throw new IllegalStateException("'templateRootDir' must be not null");
        GLogger.println("-------------------load template from templateRootDir = '"+templateRootDir.getAbsolutePath()+"' outRootDir:"+new File(outRootDir).getAbsolutePath());
        
         List srcFiles = FileHelper.searchAllNotIgnoreFile(templateRootDir);
        
        List<Exception> exceptions = new ArrayList();
        for(int i = 0; i < srcFiles.size(); i++) {
            File srcFile = (File)srcFiles.get(i);
            try {
                if(isDelete){
                    new TemplateProcessor().executeDelete(templateRootDir, templateModel,filePathModel, srcFile);
                }else {
                    new TemplateProcessor().executeGenerate(templateRootDir, templateModel,filePathModel, srcFile);
                }
            }catch(Exception e) {
                if (ignoreTemplateGenerateException) {
                    GLogger.warn("iggnore generate error,template is:" + srcFile+" cause:"+e);
                    exceptions.add(e);
                } else {
                    throw e;
                }
            }
        }
        return exceptions;
    }

TemplateProcessor:

private void executeGenerate(File templateRootDir,Map templateModel, Map filePathModel ,File srcFile) throws SQLException, IOException,TemplateException {
            String templateFile = FileHelper.getRelativePath(templateRootDir, srcFile);
            if(GeneratorHelper.isIgnoreTemplateProcess(srcFile, templateFile,includes,excludes)) {
                return;
            }
            
            if(isCopyBinaryFile && FileHelper.isBinaryFile(srcFile)) {
                String outputFilepath = proceeForOutputFilepath(filePathModel, templateFile);
                GLogger.println("[copy binary file by extention] from:"+srcFile+" => "+new File(getOutRootDir(),outputFilepath));
                IOHelper.copyAndClose(new FileInputStream(srcFile), new FileOutputStream(new File(getOutRootDir(),outputFilepath)));
                return;
            }
            
            try {
                String outputFilepath = proceeForOutputFilepath(filePathModel,templateFile);
                
                initGeneratorControlProperties(srcFile,outputFilepath);
                processTemplateForGeneratorControl(templateModel, templateFile);
                
                if(gg.isIgnoreOutput()) {
                    GLogger.println("[not generate] by gg.isIgnoreOutput()=true on template:"+templateFile);
                    return;
                }
                
                if(StringHelper.isNotBlank(gg.getOutputFile())) {
                    generateNewFileOrInsertIntoFile(templateFile,gg.getOutputFile(), templateModel);
                }
            }catch(Exception e) {
                throw new RuntimeException("generate oucur error,templateFile is:" + templateFile+" => "+ gg.getOutputFile()+" cause:"+e, e);
            }
        }

Generator:

    private void initGeneratorControlProperties(File srcFile,String outputFile) throws SQLException {
            gg.setSourceFile(srcFile.getAbsolutePath());
            gg.setSourceFileName(srcFile.getName());
            gg.setSourceDir(srcFile.getParent());
            gg.setOutRoot(getOutRootDir());
            gg.setOutputEncoding(outputEncoding);
            gg.setSourceEncoding(sourceEncoding);
            gg.setMergeLocation(GENERATOR_INSERT_LOCATION);
            gg.setOutputFile(outputFile);
        }
private void processTemplateForGeneratorControl(Map templateModel,String templateFile) throws IOException, TemplateException {
            templateModel.put("gg", gg);
            Template template = getFreeMarkerTemplate(templateFile);
            template.process(templateModel, IOHelper.NULL_WRITER);
        }
private void generateNewFileOrInsertIntoFile( String templateFile,String outputFilepath, Map templateModel) throws Exception {
            Template template = getFreeMarkerTemplate(templateFile);
            template.setOutputEncoding(gg.getOutputEncoding());
            
            File absoluteOutputFilePath = FileHelper.parentMkdir(outputFilepath);
            if(absoluteOutputFilePath.exists()) {
                StringWriter newFileContentCollector = new StringWriter();
                if(GeneratorHelper.isFoundInsertLocation(gg,template, templateModel, absoluteOutputFilePath, newFileContentCollector)) {
                    GLogger.println("[insert]\t generate content into:"+outputFilepath);
                    IOHelper.saveFile(absoluteOutputFilePath, newFileContentCollector.toString(),gg.getOutputEncoding());
                    return;
                }
            }
            
            if(absoluteOutputFilePath.exists() && !gg.isOverride()) {
                GLogger.println("[not generate]\t by gg.isOverride()=false and outputFile exist:"+outputFilepath);
                return;
            }
            
            GLogger.println("[generate]\t template:"+templateFile+" ==> "+outputFilepath);
            FreemarkerHelper.processTemplate(template, templateModel, absoluteOutputFilePath,gg.getOutputEncoding());
        }
    }

 

 

 

posted on 2014-10-19 17:57  woody-nd  阅读(513)  评论(0)    收藏  举报

导航