• 国际化(提取代码中文)


    项目在国际化时,需要提取代码中文,特此写了类似爬虫的工具。

    设置好directory点击运行。

    public class MatchCounter {
        public List<String> typeList = new ArrayList<String>(Arrays.asList("private", "public", "protected"));
        public List<String> skipDireList = new ArrayList<String>(Arrays.asList("eship-common", "eship-dubbo-core", "entity"));//不需要扫描的目录
        public List<String> skipFileList = new ArrayList<String>(Arrays.asList("MatchCounter.java"));//不需要扫描的目录
    
        public static void main(String args[]) throws Exception {
            String directory = "C:\Workspaces\parcels\eship-finance-center";
            //String directory = "C:\Workspaces\parcels";
            String excelPath = "c://1111.xlsx";
    
            MatchCounter dataArrayList = new MatchCounter();
            List<Bean> allBeanList = dataArrayList.call(new File(directory));
    
            System.out.println(JSON.toJSONString(allBeanList));
            if (allBeanList.size() > 0) {
                System.out.println("============开始导出=====================");
                ExcelData excelData = new ExcelData();
                List<String> titles = new ArrayList<>();
                titles.add("子系统");
                titles.add("类名");
                titles.add("方法名");
                titles.add("作者");
                titles.add("描述");
                titles.add("原始提示文字");
                titles.add("最终提示中文");
                titles.add("最终提示英文");
                excelData.setTitles(titles);
    
                List<List<Object>> rows = new ArrayList<>();
                for (Bean bean : allBeanList) {
                    List<Object> row = new ArrayList<>();
                    row.add(bean.getProductName());
                    row.add(bean.getClassName());
                    row.add(bean.getMethodName());
                    row.add("");
                    row.add("");
                    row.add(bean.getRows());
                    row.add("");
                    row.add("");
                    rows.add(row);
                }
                excelData.setRows(rows);
    
                ExportExcelUtils.exportExcel(null, excelPath, excelData);
                System.out.println("导出成功");
            }
        }
    
        public List<Bean> call(File directoryFile) throws Exception {
            List<Bean> beanList = new ArrayList<Bean>();
    
            File[] files = directoryFile.listFiles();
            for (File file : files) {
                if (file.isDirectory() && !skipDireList.contains(file.getName())) {
                    List<Bean> b2List = this.call(file);
                    beanList.addAll(b2List);
                } else if (file.getName().endsWith(".java") && !skipFileList.contains(file.getName())) {
                    beanList.addAll(search(file));
                }
            }
    
            return beanList;
        }
    
        public List<Bean> search(File file) throws Exception {
            //项目名称
            String productName = "";
            List<String> productSplit = new ArrayList<String>(Arrays.asList(file.getPath().split("\\")));
            productName = productSplit.get(productSplit.indexOf("src") - 1);
    
            //当前文件的BeanList
            List<Bean> beanSubList = new ArrayList<Bean>();
    
            //直接跳过
            String skipFileMatch = "^(\s*)\w*(\s)*(interface|@interface)[\w\W]*\{";
            //String keyword = "^((?!/\*\*|/\*|//|@|.info).)*[\u4e00-\u9fa5]+";
            String keyword = "^((?!/\*\*|/\*|//|@|.info).)*[\u4e00-\u9fa5]+[\w\W]*";
            String match1 = "^(\s*)/\*.*";
            String match2 = "^(\s*)[\w\W]*\*/";//"\*/";
            String match3 = "^(\s*)\w*(\s)*(class|interface|@interface|enum)\s";//class
            String match4 = "^(\s*)(public|private|protected)((?!class|enum).)*\{(\s*)$";//method
    
            BufferedReader inScanner = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
    
            Boolean flag = true;
            String thisClassName = "";
            StringBuilder chinaContent = new StringBuilder();   //中文内容
            Boolean isAdd = false;
    
            String string;  //当前行
            while ((string = inScanner.readLine()) != null) {
                //处理接口、自定义注解等需要跳过的类
                if (Pattern.matches(skipFileMatch, string)) {
                    return new ArrayList<Bean>();
                }
                //是否是/*开头
                if (Pattern.matches(match1, string)) {
                    flag = false;
                }
                //是否是*/结尾
                if (!flag && Pattern.matches(match2, string)) {
                    flag = true;
                }
                //主体内容
                if (flag) {
                    //匹配到类
                    if (Pattern.matches(match3, string)) {
                        Integer index = string.indexOf(" final class ") > 0 ? 3 : 2;
                        if (!typeList.contains(string.trim().split(" ")[0])) {
                            index = index - 1;
                        }
                        String className = string.trim().split(" ")[index];
                        if (className.indexOf("<") > 0) {
                            className = className.substring(0, className.indexOf("<"));
                        }
                        thisClassName = className;
                        System.out.println("类名:" + className);
    
                        if (string.indexOf(" enum ") > 0) { //如果是枚举类型
                            Bean bean = new Bean(productName, thisClassName, "", "");
                            beanSubList.add(bean);
                        }
                        continue;
                    }
                    //匹配到方法
                    if (Pattern.matches(match4, string)) {
                        //构造方法
                        if (StringUtils.isNotBlank(thisClassName) && new ArrayList(Arrays.asList(string.split(" |\("))).contains(thisClassName)) {
                            return new ArrayList<Bean>();
                        }
    
                        String stringTrim = string.trim();
                        String methodNmae = stringTrim.substring(0, stringTrim.indexOf("(")).trim();
                        methodNmae = methodNmae.substring(methodNmae.lastIndexOf(" "));
                        System.out.println("方法名:" + methodNmae);
    
                        if (chinaContent.toString().indexOf(" static String ") > 0 | chinaContent.toString().indexOf(" static final ") > 0 | chinaContent.toString().indexOf(" final static ") > 0) {  //常量
                            Bean bean = new Bean(productName, thisClassName, "", chinaContent.toString());
                            beanSubList.add(bean);
                        }
                        if (StringUtils.isNotBlank(chinaContent.toString())) {
                            //旧方法
                            Bean bOld = beanSubList.get(beanSubList.size() - 1);
                            bOld.setRows(chinaContent.substring(0, chinaContent.length() - 1));
                        } else if (isAdd) {
                            beanSubList.remove(beanSubList.size() - 1);
                        }
                        //新方法初始化内容字段
                        chinaContent = new StringBuilder();
                        //新方法初始化列表
                        Bean bean = new Bean(productName, thisClassName, methodNmae, "");
                        beanSubList.add(bean);
                        isAdd = true;
                        continue;
                    }
                    //匹配到中文行
                    if (Pattern.matches(keyword, string)) {
                        chinaContent.append(string.trim()).append("
    ");
                    }
                }
            }
            if (StringUtils.isNotBlank(chinaContent.toString())) {
                //旧方法
                Bean bOld = beanSubList.get(beanSubList.size() - 1);
                bOld.setRows(chinaContent.toString());
            } else if (isAdd) {
                beanSubList.remove(beanSubList.size() - 1);
            }
    
            inScanner.close();
    
            return beanSubList;
        }
    
        @Data
        public class Bean {
            /**
             * 项目名称
             */
            private String productName;
            /**
             * 类名称
             */
            private String className;
            /**
             * 方法名称
             */
            private String methodName;
            /**
             * 数据
             */
            private String rows;
    
            public Bean(String productName, String className, String methodName, String rows) {
                this.productName = productName;
                this.className = className;
                this.methodName = methodName;
                this.rows = rows;
            }
        }
    
        public void other() {
            /*ExecutorService pool = Executors.newCachedThreadPool();//线程池
            MatchCounter dataArrayList = new MatchCounter(new File(directory), pool);
            Future<ArrayList<String>> resultFuture = pool.submit(dataArrayList);//获取结果
    
            System.out.println("开始睡眠");
            Thread.sleep(10000);
            System.out.println("结束睡眠");
            //输出结果
            int i = 0;
            FileOutputStream fos = new FileOutputStream("c:\tttt.txt", true);
            for (String string : resultFuture.get()) {
                i++;
                fos.write(string.getBytes());
                System.out.print(string);
                if (i % 4 == 0) {
                    System.out.println();
                    fos.write("
    ".getBytes());
                }
            }
            fos.close();
            pool.shutdown();
            System.exit(0);*/
        }
    }
  • 相关阅读:
    数据库操作
    jquery 第一章
    算法很美 第一章
    python 学习第四天
    python学习第三天
    python学习第二天
    学习python的第一天
    C#-线程
    C#-流、存储
    C#-集合
  • 原文地址:https://www.cnblogs.com/yifanSJ/p/12515520.html
Copyright © 2020-2023  润新知