博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
rapid_generator uml
阅读量:7170 次
发布时间:2019-06-29

本文共 9445 字,大约阅读时间需要 31 分钟。

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
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
exceptions = scanTemplatesAndProcess(templateRootDir,templateModel,filePathModel,isDelete); ge.addAll(exceptions); } if(!ge.exceptions.isEmpty()) throw ge; }
 

 

 
private List
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
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());        }    }

 

 

 

转载于:https://www.cnblogs.com/woodynd/p/4035185.html

你可能感兴趣的文章
设计模式-单例模式
查看>>
8. String to Integer (atoi)
查看>>
cocos2dx内存优化
查看>>
Arguments
查看>>
鲁棒图(Robustness Diagram)
查看>>
2011TG初赛
查看>>
css3各种loading写法
查看>>
android 获取资源文件 R.drawable中的图片转换为drawable、bitmap(转载)
查看>>
GOOGLE卫星地图URL中的Tile位置编码算法
查看>>
python3中如何区分一个函数和方法
查看>>
文件I/O操作函数 lseek()
查看>>
datepicker使用
查看>>
关于纠正《Hive权威指南》中的结论~“hive在使用set自定义变量时,hivevar命名空间是可选的”~的论证...
查看>>
移动端遇到的问题小结--video
查看>>
【算法学习笔记】02.wikioi1205 单词翻转
查看>>
Codeforces Round #304 (Div.2)
查看>>
查看mysql binlog日志
查看>>
Spring中ioc的实现原理
查看>>
关于移动端使用swiper做图片文字轮播的思考
查看>>
我的Java开发学习之旅------>在Dos环境下Java内部类的编译和运行
查看>>