diff --git a/src/main/java/xyz/wbsite/dbtool/javafx/ctrl/OptionApiController.java b/src/main/java/xyz/wbsite/dbtool/javafx/ctrl/OptionApiController.java index b9fac975..9d1b36f9 100644 --- a/src/main/java/xyz/wbsite/dbtool/javafx/ctrl/OptionApiController.java +++ b/src/main/java/xyz/wbsite/dbtool/javafx/ctrl/OptionApiController.java @@ -59,6 +59,8 @@ public class OptionApiController { @FXML private List data = new ArrayList<>(); + private List domainList = new ArrayList<>(); + public Button getSelectModulePath() { return selectModulePath; } @@ -187,6 +189,13 @@ public class OptionApiController { this.apis = apis; } + public List getDomainList() { + return domainList; + } + + public void setDomainList(List domainList) { + this.domainList = domainList; + } public void load() { File moduleFile = new File(modulePath.getText()); @@ -201,6 +210,19 @@ public class OptionApiController { } Dialog.showProgress("扫描API..."); + String absolutePath = apiPath.get(0).getAbsolutePath(); + String separator = Tool.replaceSeparator(absolutePath, "#"); + + Pattern compile = Pattern.compile(".*src#main#java#(.*)#action#api"); + Matcher matcher = compile.matcher(separator); + if (matcher.find()) { + String group = matcher.group(1); + String[] split = group.split("#"); + for (String s : split) { + domainList.add(s); + } + } + for (File file : apiPath.get(0).listFiles()) { String module = file.getName(); for (File tar : file.listFiles()) { diff --git a/src/main/java/xyz/wbsite/dbtool/javafx/manger/ProjectManager.java b/src/main/java/xyz/wbsite/dbtool/javafx/manger/ProjectManager.java index 3a51a00f..8ab6c63d 100644 --- a/src/main/java/xyz/wbsite/dbtool/javafx/manger/ProjectManager.java +++ b/src/main/java/xyz/wbsite/dbtool/javafx/manger/ProjectManager.java @@ -271,14 +271,14 @@ public class ProjectManager { }.start(); } - public void generateApi(File module, File api, List apis) { + public void generateApi(File module, File api, List domainList, List apis) { if (module.exists()) { Dialog.showProgress("生成中..."); new Thread() { @Override public void run() { boolean mkdirs = api.mkdirs(); - ApiCallable apiCallable = new ApiCallable(module,api, apis); + ApiCallable apiCallable = new ApiCallable(module,api,domainList, apis); Future submit = service.submit(apiCallable); try { Boolean b = (Boolean) submit.get(); diff --git a/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/ApiCallable.java b/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/ApiCallable.java index 93c68b39..27266859 100644 --- a/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/ApiCallable.java +++ b/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/ApiCallable.java @@ -6,12 +6,15 @@ import xyz.wbsite.dbtool.javafx.manger.ProjectManager; import xyz.wbsite.dbtool.javafx.po.AbstractDBmapper; import xyz.wbsite.dbtool.javafx.po.Api; import xyz.wbsite.dbtool.javafx.tool.EntityReader; -import xyz.wbsite.dbtool.javafx.tool.JavaClassReader; +import xyz.wbsite.dbtool.javafx.tool.RequestReader; +import xyz.wbsite.dbtool.javafx.tool.ResponseReader; import xyz.wbsite.dbtool.javafx.tool.Tool; import java.io.File; import java.io.IOException; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; import java.util.concurrent.Callable; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -21,14 +24,16 @@ public class ApiCallable implements Callable { private File module; private File apiFile; private List apiList; + private List domainList; private Tool tool = new Tool(); private FreeMarkerManager freeMarkerManager; - public ApiCallable(File module, File apiFile, List apiList) { + public ApiCallable(File module, File apiFile, List domainList, List apiList) { this.module = module; this.apiFile = apiFile; this.apiList = apiList; + this.domainList = domainList; this.freeMarkerManager = ManagerFactory.getFreeMarkerManager(); } @@ -56,10 +61,7 @@ public class ApiCallable implements Callable { sbtest.append("test" + File.separator); sbtest.append("java" + File.separator); - JavaClassReader javaClass = new JavaClassReader(apiList.get(0).getRequestFile()); - String[] split = javaClass.getDomainName().split("\\."); - - for (String s1 : split) { + for (String s1 : domainList) { sbmain.append(s1 + File.separator); sbtest.append(s1 + File.separator); } @@ -80,45 +82,26 @@ public class ApiCallable implements Callable { File entity = new File(apiModule, "ent"); entity.mkdirs(); - List entList = new ArrayList<>(); try { //region 生成request - JavaClassReader javaClassReader = new JavaClassReader(api.getRequestFile()); - for (int i = 0; i < javaClassReader.getImportList().size(); i++) { - String s = javaClassReader.getImportList().get(i); + RequestReader requestReader = new RequestReader(api.getRequestFile()); + for (int i = 0; i < requestReader.getImportList().size(); i++) { + String s = requestReader.getImportList().get(i); if (s.contains(".module.entity.")) { - javaClassReader.getImportList().set(i, s); + requestReader.getImportList().set(i, s); } } - { - HashMap ctx = new HashMap(); - ctx.put("package", "package " + javaClassReader.getDomainName() + "." + javaClassReader.getModuleName() + "." + "request;"); - ctx.put("domain", javaClassReader.getDomainName()); - ctx.put("module", javaClassReader.getModuleName()); - ctx.put("target", api.getTarget()); - ctx.put("method", api.getMethod()); - ctx.put("importList", javaClassReader.getImportList()); - ctx.put("annotation", javaClassReader.getAnnotationList()); - ctx.put("className", javaClassReader.getClassName().replaceAll("Request", "")); - ctx.put("body", javaClassReader.getBody()); - ctx.put("tool", tool); - ctx.put("hasList", javaClassReader.isHasList()); - ctx.put("findOrSearchflag", javaClassReader.getFindOrSearchflag()); - File file = new File(apiRequest.getAbsolutePath(), api.getRequestFile().getName()); - - freeMarkerManager.outputTemp(file, "Java_api/module/req/request.java", ctx); - System.out.println("生成文件" + file.getName() + "成功"); - } - //endregion + List entNames = new ArrayList<>(); - for (String s : javaClassReader.getDeptEntList()) { + for (String s : requestReader.getDeptEntList()) { Pattern compile = Pattern.compile("(.*\\.ent)\\.(.*)"); Matcher matcher = compile.matcher(s); if (matcher.find()) { String Package = matcher.group(1); String EntName = matcher.group(2); + entNames.add(EntName); List files = Tool.find(this.module, EntName + ".java"); @@ -126,7 +109,7 @@ public class ApiCallable implements Callable { EntityReader entReader = new EntityReader(file); if (Package.equals(entReader.getPackageName())) { HashMap ctx = new HashMap(); - ctx.put("package", "package " + entReader.getDomainName() + "." + entReader.getModuleName() + "." + "request;"); + ctx.put("package", "package " + entReader.getDomainName() + "." + entReader.getModuleName() + "." + "req;"); ctx.put("domain", entReader.getDomainName()); ctx.put("module", entReader.getModuleName()); ctx.put("importList", entReader.getImportList()); @@ -134,32 +117,94 @@ public class ApiCallable implements Callable { ctx.put("className", entReader.getClassName().replaceAll("Entity", "")); ctx.put("body", entReader.getBody()); ctx.put("tool", tool); - File EntFile = new File(entity.getAbsolutePath(), EntName); + File EntFile = new File(entity.getAbsolutePath(), EntName + ".java"); freeMarkerManager.outputTemp(EntFile, "Java_api/module/ent/entity.java", ctx); System.out.println("生成文件" + EntName + "成功"); + } + } + + } + } + + List reqNames = new ArrayList<>(); + for (String s : requestReader.getDeptReqList()) { + Pattern compile = Pattern.compile("(.*\\.req)\\.(.*)"); + Matcher matcher = compile.matcher(s); + + if (matcher.find()) { + String Package = matcher.group(1); + String EntName = matcher.group(2); + reqNames.add(EntName); + + List files = Tool.find(this.module, EntName + ".java"); + + for (File file : files) { + RequestReader reqReader = new RequestReader(file); + if (Package.equals(reqReader.getPackageName())) { + HashMap ctx = new HashMap(); + ctx.put("package", "package " + requestReader.getDomainName() + "." + requestReader.getModuleName() + "." + "req;"); + ctx.put("domain", requestReader.getDomainName()); + ctx.put("module", requestReader.getModuleName()); + ctx.put("target", api.getTarget()); + ctx.put("method", api.getMethod()); + ctx.put("importList", requestReader.getImportList()); + ctx.put("entNames", entNames); + ctx.put("annotation", requestReader.getAnnotationList()); + ctx.put("className", requestReader.getClassName().replaceAll("Request", "")); + ctx.put("body", requestReader.getBody()); + ctx.put("tool", tool); + ctx.put("hasList", requestReader.isHasList()); + ctx.put("findOrSearchflag", requestReader.getFindOrSearchflag()); + File reqfile = new File(apiRequest.getAbsolutePath(), api.getRequestFile().getName()); + + freeMarkerManager.outputTemp(reqfile, "Java_api/module/req/request.java", ctx); + System.out.println("生成文件" + reqfile.getName() + "成功"); } } } } + + { + HashMap ctx = new HashMap(); + ctx.put("package", "package " + requestReader.getDomainName() + "." + requestReader.getModuleName() + "." + "request;"); + ctx.put("domain", requestReader.getDomainName()); + ctx.put("module", requestReader.getModuleName()); + ctx.put("target", api.getTarget()); + ctx.put("method", api.getMethod()); + ctx.put("importList", requestReader.getImportList()); + ctx.put("entNames", entNames); + ctx.put("reqNames", reqNames); + ctx.put("annotation", requestReader.getAnnotationList()); + ctx.put("className", requestReader.getClassName().replaceAll("Request", "")); + ctx.put("body", requestReader.getBody()); + ctx.put("tool", tool); + ctx.put("hasList", requestReader.isHasList()); + ctx.put("findOrSearchflag", requestReader.getFindOrSearchflag()); + File file = new File(apiRequest.getAbsolutePath(), api.getRequestFile().getName()); + + freeMarkerManager.outputTemp(file, "Java_api/module/req/request.java", ctx); + System.out.println("生成文件" + file.getName() + "成功"); + } + //endregion } catch (IOException e) { e.printStackTrace(); } try { //region 生成response - JavaClassReader javaClassReader = new JavaClassReader(api.getResponseFile()); - for (int i = 0; i < javaClassReader.getImportList().size(); i++) { - String import_ = javaClassReader.getImportList().get(i); + ResponseReader responseReader = new ResponseReader(api.getResponseFile()); + for (int i = 0; i < responseReader.getImportList().size(); i++) { + String import_ = responseReader.getImportList().get(i); if (import_.contains(".module.entity.")) { - javaClassReader.getImportList().set(i, import_); + responseReader.getImportList().set(i, import_); } } List entNames = new ArrayList<>(); - for (String s : javaClassReader.getDeptEntList()) { + for (String s : responseReader.getDeptEntList()) { Pattern compile = Pattern.compile("(.*\\.ent)\\.(.*)"); Matcher matcher = compile.matcher(s); @@ -182,7 +227,7 @@ public class ApiCallable implements Callable { ctx.put("className", entReader.getClassName().replaceAll("Entity", "")); ctx.put("body", entReader.getBody()); ctx.put("tool", tool); - File EntFile = new File(entity.getAbsolutePath(), EntName+".java"); + File EntFile = new File(entity.getAbsolutePath(), EntName + ".java"); freeMarkerManager.outputTemp(EntFile, "Java_api/module/ent/entity.java", ctx); System.out.println("生成文件" + EntName + "成功"); } @@ -193,17 +238,17 @@ public class ApiCallable implements Callable { { HashMap ctx = new HashMap(); - ctx.put("package", "package " + javaClassReader.getDomainName() + "." + javaClassReader.getModuleName() + "." + "request;"); - ctx.put("domain", javaClassReader.getDomainName()); - ctx.put("module", javaClassReader.getModuleName()); - ctx.put("importList", javaClassReader.getImportList()); + ctx.put("package", "package " + responseReader.getDomainName() + "." + responseReader.getModuleName() + "." + "request;"); + ctx.put("domain", responseReader.getDomainName()); + ctx.put("module", responseReader.getModuleName()); + ctx.put("importList", responseReader.getImportList()); ctx.put("entNames", entNames); - ctx.put("annotation", javaClassReader.getAnnotationList()); - ctx.put("className", javaClassReader.getClassName().replaceAll("Response", "")); - ctx.put("body", javaClassReader.getBody()); + ctx.put("annotation", responseReader.getAnnotationList()); + ctx.put("className", responseReader.getClassName().replaceAll("Response", "")); + ctx.put("body", responseReader.getBody()); ctx.put("tool", tool); - ctx.put("hasList", javaClassReader.isHasList()); - ctx.put("Tclass", javaClassReader.getTclass()); + ctx.put("hasList", responseReader.isHasList()); + ctx.put("Tclass", responseReader.getTclass()); File file = new File(apiResponse.getAbsolutePath(), api.getResponseFile().getName().replaceAll("Request", "Response")); freeMarkerManager.outputTemp(file, "Java_api/module/rsp/response.java", ctx); @@ -215,122 +260,6 @@ public class ApiCallable implements Callable { } catch (IOException e) { e.printStackTrace(); } - -// for (String s : api.getDepReq()) { -// try { -// File f = new File(apiFile.getAbsolutePath(), s + ".java"); -// if (!f.exists()) { -// System.err.println("文件" + f.getAbsolutePath() + "不存在"); -// continue; -// } -// JavaClassReader javaClassReader = new JavaClassReader(f); -// for (int i = 0; i < javaClassReader.getImportList().size(); i++) { -// String import_ = javaClassReader.getImportList().get(i); -// if (import_.contains(".module.entity.")) { -// javaClassReader.getImportList().set(i, import_.replaceAll("\\.api\\.entity\\.", ".entity.")); -// } -// } -// -// Method method = new Method(); -// method.setStringMethod(api.getMethod()); -// method.setRequest(javaClassReader.getClassName()); -// method.setTarget(Tool.getRequestTarget(javaClassReader.getClassName())); -// method.setMethod(Tool.getRequestAction(javaClassReader.getClassName())); -// method.setManager(Tool.getRequestTarget(javaClassReader.getClassName()) + "Manager"); -// -// { -// HashMap ctx = new HashMap(); -// ctx.put("package", "package " + javaClassReader.getDomainName() + "." + javaClassReader.getModuleName() + "." + "request;"); -// ctx.put("domain", javaClassReader.getDomainName()); -// ctx.put("module", javaClassReader.getModuleName()); -// ctx.put("importList", javaClassReader.getImportList()); -// ctx.put("annotation", javaClassReader.getAnnotationList()); -// ctx.put("className", javaClassReader.getClassName().replaceAll("Request", "")); -// ctx.put("body", javaClassReader.getBody()); -// ctx.put("tool", tool); -// ctx.put("hasList", javaClassReader.isHasList()); -// ctx.put("findOrSearchflag", javaClassReader.getFindOrSearchflag()); -// File file = new File(request.getAbsolutePath(), f.getName()); -// -// freeMarkerManager.outputTemp(file, "Java_api/module/req/request.java", ctx); -// System.out.println("生成文件" + file.getName() + "成功"); -// } -// //endregion -// } catch (IOException e) { -// e.printStackTrace(); -// } -// -// try { -// File f = new File(rsp.getAbsolutePath(), s.replaceAll("Request", "Response.java")); -// if (!f.exists()) { -// System.err.println("文件" + f.getAbsolutePath() + "不存在"); -// } -// JavaClassReader javaClassReader = new JavaClassReader(f); -// for (int i = 0; i < javaClassReader.getImportList().size(); i++) { -// String import_ = javaClassReader.getImportList().get(i); -// if (import_.contains(".module.entity.")) { -// javaClassReader.getImportList().set(i, import_.replaceAll("\\.api\\.entity\\.", ".entity.")); -// } -// } -// -// { -// HashMap ctx = new HashMap(); -// ctx.put("package", "package " + javaClassReader.getDomainName() + "." + javaClassReader.getModuleName() + "." + "request;"); -// ctx.put("domain", javaClassReader.getDomainName()); -// ctx.put("module", javaClassReader.getModuleName()); -// ctx.put("importList", javaClassReader.getImportList()); -// ctx.put("annotation", javaClassReader.getAnnotationList()); -// ctx.put("className", javaClassReader.getClassName().replaceAll("Response", "")); -// ctx.put("body", javaClassReader.getBody()); -// ctx.put("tool", tool); -// ctx.put("hasList", javaClassReader.isHasList()); -// ctx.put("Tclass", javaClassReader.getTclass()); -// File file = new File(response.getAbsolutePath(), f.getName()); -// -// freeMarkerManager.outputTemp(file, "Java_api/module/rsp/response.java", ctx); -// System.out.println("生成文件" + api.getResponseFile().getName() + "成功"); -// } -// //endregion -// } catch (IOException e) { -// e.printStackTrace(); -// } -// } -// -// for (String s : api.getDepEnt()) { -// -// try { -// File f = new File(ent.getAbsolutePath(), s + ".java"); -// if (!f.exists()) { -// System.out.println("文件" + f.getAbsolutePath() + "不存在"); -// continue; -// } -// JavaClassReader javaClassReader = new JavaClassReader(f); -// for (int i = 0; i < javaClassReader.getImportList().size(); i++) { -// String import_ = javaClassReader.getImportList().get(i); -// if (import_.contains(".module.entity.")) { -// javaClassReader.getImportList().set(i, import_.replaceAll("\\.api\\.entity\\.", ".entity.")); -// } -// } -// -// { -// HashMap ctx = new HashMap(); -// ctx.put("package", "package " + javaClassReader.getDomainName() + "." + javaClassReader.getModuleName() + "." + "request;"); -// ctx.put("domain", javaClassReader.getDomainName()); -// ctx.put("module", javaClassReader.getModuleName()); -// ctx.put("importList", javaClassReader.getImportList()); -// ctx.put("annotation", javaClassReader.getAnnotationList()); -// ctx.put("className", javaClassReader.getClassName().replaceAll("Entity", "")); -// ctx.put("body", javaClassReader.getBody()); -// ctx.put("tool", tool); -// File file = new File(entity.getAbsolutePath(), f.getName()); -// freeMarkerManager.outputTemp(file, "Java_api/module/ent/entity.java", ctx); -// System.out.println("生成文件" + file.getName() + "成功"); -// } -// //endregion -// } catch (IOException e) { -// e.printStackTrace(); -// } -// } } } @@ -346,8 +275,7 @@ public class ApiCallable implements Callable { { System.out.println("生成基础类"); HashMap ctx = new HashMap(); - ctx.put("domain", javaClass.getDomainName()); - ctx.put("module", javaClass.getModuleName()); + ctx.put("domain", String.join(".", domainList)); { File file = new File(domain, "ApiEntity.java"); freeMarkerManager.outputTemp(file, "/Java_api/ApiEntity.java", ctx); @@ -435,8 +363,7 @@ public class ApiCallable implements Callable { System.out.println("生成模块:Test"); HashMap ctx = new HashMap(); ctx.put("tool", tool); - ctx.put("domain", javaClass.getDomainName()); - ctx.put("module", javaClass.getModuleName()); + ctx.put("domain", String.join(".", domainList)); ctx.put("apiList", apiList); freeMarkerManager.outputTemp(new File(testdomain.getAbsolutePath(), "ApiTest.java"), "Java_api/ApiTest.java", ctx); } diff --git a/src/main/java/xyz/wbsite/dbtool/javafx/tool/Dialog.java b/src/main/java/xyz/wbsite/dbtool/javafx/tool/Dialog.java index 147beb1b..a1b234d8 100644 --- a/src/main/java/xyz/wbsite/dbtool/javafx/tool/Dialog.java +++ b/src/main/java/xyz/wbsite/dbtool/javafx/tool/Dialog.java @@ -406,10 +406,7 @@ public class Dialog { String projectName = ""; // 尝试查找Windows下的项目名称 String path = file.getAbsolutePath(); - String pathReplace = path.replaceAll("\\\\", "#"); - if (pathReplace.equals(path)) { - pathReplace = path.replaceAll("/", "#"); - } + String pathReplace = Tool.replaceSeparator(path, "#"); Pattern compile = Pattern.compile("#([^#]*)#src#main#java#"); Matcher matcher = compile.matcher(pathReplace); if (matcher.find()) { @@ -523,7 +520,7 @@ public class Dialog { if (new File(module).exists()) { Dialog.showProgress("生成中..."); - dBmanger.generateApi(new File(module), new File(api), controller.getData()); + dBmanger.generateApi(new File(module), new File(api),controller.getDomainList(), controller.getData()); Dialog.stopPopup(); Platform.runLater(new Runnable() { @Override diff --git a/src/main/java/xyz/wbsite/dbtool/javafx/tool/EntityReader.java b/src/main/java/xyz/wbsite/dbtool/javafx/tool/EntityReader.java index 4ae6d4b3..005c902b 100644 --- a/src/main/java/xyz/wbsite/dbtool/javafx/tool/EntityReader.java +++ b/src/main/java/xyz/wbsite/dbtool/javafx/tool/EntityReader.java @@ -42,24 +42,22 @@ public class EntityReader { StringBuffer sb = new StringBuffer(); while ((line = bufferedReader.readLine()) != null) { if (line.matches("\\s*package\\s(.*);")) { - Pattern compile = Pattern.compile("\\s*package\\s(.*);"); - Matcher matcher = compile.matcher(line); - if (matcher.find()) { - packageName = matcher.group(1); + { + Pattern compile = Pattern.compile("\\s*package\\s(.*);"); + Matcher matcher = compile.matcher(line); + if (matcher.find()) { + packageName = matcher.group(1); + } } - String s = line.replaceAll("package ", ""); - s = s.replaceAll(";", ""); - String[] split = s.split("\\."); - - if ("req".equals(split[split.length - 1]) || "rsp".equals(split[split.length - 1]) || "ent".equals(split[split.length - 1])) { - moduleName = split[split.length - 2]; - for (int i = 0; i < split.length - 2; i++) { - domainName += split[i] + "."; + { + Pattern compile = Pattern.compile("\\s*package\\s(.*)\\.module\\.(.*).ent;"); + Matcher matcher = compile.matcher(line); + if (matcher.find()) { + domainName = matcher.group(1); + moduleName = matcher.group(2); } - domainName = domainName.substring(0, domainName.length() - 1); } - continue; } if (line.matches("import\\s.*")) { @@ -137,13 +135,13 @@ public class EntityReader { } } Iterator iterator = body.iterator(); - while (iterator.hasNext()){ + while (iterator.hasNext()) { String next = iterator.next(); - if (next.contains("@ColumnName")){ + if (next.contains("@ColumnName")) { iterator.remove(); } - if (next.contains("@ColumnDescription")){ + if (next.contains("@ColumnDescription")) { iterator.remove(); } } diff --git a/src/main/java/xyz/wbsite/dbtool/javafx/tool/JavaEnumReader.java b/src/main/java/xyz/wbsite/dbtool/javafx/tool/JavaEnumReader.java deleted file mode 100644 index b3bba1d1..00000000 --- a/src/main/java/xyz/wbsite/dbtool/javafx/tool/JavaEnumReader.java +++ /dev/null @@ -1,153 +0,0 @@ -package xyz.wbsite.dbtool.javafx.tool; - -import java.io.*; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class JavaEnumReader { - private File javaClass; - - private String packageName; - private String domainName; - private String moduleName; - private List annotationList; - private String className; - private List body; - - public JavaEnumReader(File javaClass) throws IOException { - this.javaClass = javaClass; - annotationList = new ArrayList<>(); - domainName = ""; - read(); - } - - private void read() throws IOException { - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(javaClass), "utf-8")); - - int step = 1;//1-读取package,2-读取import,3-读取类注释,4-读取类名、父类,5-读取类体 - - String line = null; - StringBuffer zs = new StringBuffer(); - StringBuffer sb = new StringBuffer(); - while ((line = bufferedReader.readLine()) != null) { - if (line.startsWith("package")) { - packageName = line; - - String s = line.replaceAll("package ", ""); - s = s.replaceAll(";", ""); - String[] split = s.split("\\."); - - if ("enums".equals(split[split.length - 1])) { - moduleName = split[split.length - 2]; - for (int i = 0; i < split.length - 2; i++) { - domainName += split[i] + "."; - } - domainName = domainName.substring(0, domainName.length() - 1); - } - - continue; - } - - if (line.contains("/**") || line.contains("*") || line.contains("*/")) { - if (zs != null) { - zs.append(line + "\n"); - annotationList.add(line); - if (line.contains("*/")) { - zs = null; - } - } - } - if (line.contains("enum")) { - line = line.replaceAll("\\{", " { "); - line = line.replaceAll("}", " } "); - line = line.replaceAll("\\s+", " "); - String[] split = line.split("\\s"); - for (int i = 0; i < split.length; i++) { - if ("enum".equals(split[i])) { - className = split[i + 1]; - } - } - } - sb.append(line + "\n"); - } - - body = new ArrayList<>(Arrays.asList(sb.substring(sb.indexOf("{")).split("\n"))); - - for (int i = 0; i < body.size(); i++) { - if (body.get(i).contains("{")) { - body.set(i, body.get(i).replaceAll("\\{", "")); - break; - } - } - for (int i = body.size() - 1; i > 0; i--) { - if (body.get(i).contains("}")) { - body.set(i, body.get(i).replaceAll("\\}", "")); - break; - } - } - - bufferedReader.close(); - } - - public String getPackageName() { - return packageName; - } - - public void setPackageName(String packageName) { - this.packageName = packageName; - } - - public String getDomainName() { - return domainName; - } - - public void setDomainName(String domainName) { - this.domainName = domainName; - } - - public String getModuleName() { - return moduleName; - } - - public void setModuleName(String moduleName) { - this.moduleName = moduleName; - } - - public List getAnnotationList() { - return annotationList; - } - - public void setAnnotationList(List annotationList) { - this.annotationList = annotationList; - } - - - public String getClassName() { - return className; - } - - public void setClassName(String className) { - this.className = className; - } - - public List getBody() { - return body; - } - - public void setBody(List body) { - this.body = body; - } - - public File getJavaClass() { - return javaClass; - } - - public void setJavaClass(File javaClass) { - this.javaClass = javaClass; - } - - public static void main(String[] args) throws IOException { - JavaEnumReader javaClassReader = new JavaEnumReader(new File("C:\\dbtool\\example\\src\\main\\java\\com\\example\\example\\enums\\Type.java")); - } -} diff --git a/src/main/java/xyz/wbsite/dbtool/javafx/tool/JavaClassReader.java b/src/main/java/xyz/wbsite/dbtool/javafx/tool/RequestReader.java similarity index 87% rename from src/main/java/xyz/wbsite/dbtool/javafx/tool/JavaClassReader.java rename to src/main/java/xyz/wbsite/dbtool/javafx/tool/RequestReader.java index 2f4c9c07..8881901d 100644 --- a/src/main/java/xyz/wbsite/dbtool/javafx/tool/JavaClassReader.java +++ b/src/main/java/xyz/wbsite/dbtool/javafx/tool/RequestReader.java @@ -5,7 +5,7 @@ import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; -public class JavaClassReader { +public class RequestReader { private File javaClass; private String packageName; @@ -22,7 +22,7 @@ public class JavaClassReader { private String findOrSearchflag = "0"; private String Tclass = null; - public JavaClassReader(File javaClass) throws IOException { + public RequestReader(File javaClass) throws IOException { this.javaClass = javaClass; importList = new ArrayList<>(); annotationList = new ArrayList<>(); @@ -42,24 +42,22 @@ public class JavaClassReader { StringBuffer sb = new StringBuffer(); while ((line = bufferedReader.readLine()) != null) { if (line.matches("\\s*package\\s(.*);")) { - Pattern compile = Pattern.compile("\\s*package\\s(.*);"); - Matcher matcher = compile.matcher(line); - if (matcher.find()) { - packageName = matcher.group(1); + { + Pattern compile = Pattern.compile("\\s*package\\s(.*);"); + Matcher matcher = compile.matcher(line); + if (matcher.find()) { + packageName = matcher.group(1); + } } - String s = line.replaceAll("package ", ""); - s = s.replaceAll(";", ""); - String[] split = s.split("\\."); - - if ("req".equals(split[split.length - 1]) || "rsp".equals(split[split.length - 1]) || "ent".equals(split[split.length - 1])) { - moduleName = split[split.length - 2]; - for (int i = 0; i < split.length - 2; i++) { - domainName += split[i] + "."; + { + Pattern compile = Pattern.compile("\\s*package\\s(.*)\\.module\\.(.*).req;"); + Matcher matcher = compile.matcher(line); + if (matcher.find()) { + domainName = matcher.group(1); + moduleName = matcher.group(2); } - domainName = domainName.substring(0, domainName.length() - 1); } - continue; } if (line.matches("import\\s.*")) { @@ -265,7 +263,7 @@ public class JavaClassReader { } public static void main(String[] args) throws IOException { - JavaClassReader javaClassReader = new JavaClassReader(new File("D:\\wangbing\\Project\\dbtool\\target\\project\\EXAMPLE-WEB\\src\\main\\java\\com\\example\\module\\system\\rsp\\DictFindResponse.java")); + RequestReader requestReader = new RequestReader(new File("D:\\wangbing\\Project\\dbtool\\target\\project\\EXAMPLE-WEB\\src\\main\\java\\com\\example\\module\\system\\rsp\\DictFindResponse.java")); System.out.println(); } diff --git a/src/main/java/xyz/wbsite/dbtool/javafx/tool/ResponseReader.java b/src/main/java/xyz/wbsite/dbtool/javafx/tool/ResponseReader.java new file mode 100644 index 00000000..a32e5984 --- /dev/null +++ b/src/main/java/xyz/wbsite/dbtool/javafx/tool/ResponseReader.java @@ -0,0 +1,270 @@ +package xyz.wbsite.dbtool.javafx.tool; + +import java.io.*; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class ResponseReader { + private File javaClass; + + private String packageName; + private String domainName; + private String moduleName; + private List annotationList; + private List importList; + private Set deptReqList; + private Set deptEntList; + private String className; + private String fatherName; + private List body; + private boolean hasList = false; + private String findOrSearchflag = "0"; + private String Tclass = null; + + public ResponseReader(File javaClass) throws IOException { + this.javaClass = javaClass; + importList = new ArrayList<>(); + annotationList = new ArrayList<>(); + deptReqList = new HashSet<>(); + deptEntList = new HashSet<>(); + domainName = ""; + read(); + } + + private void read() throws IOException { + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(javaClass), "utf-8")); + + int step = 1;//1-读取package,2-读取import,3-读取类注释,4-读取类名、父类,5-读取类体 + + String line = null; + StringBuffer zs = new StringBuffer(); + StringBuffer sb = new StringBuffer(); + while ((line = bufferedReader.readLine()) != null) { + if (line.matches("\\s*package\\s(.*);")) { + { + Pattern compile = Pattern.compile("\\s*package\\s(.*);"); + Matcher matcher = compile.matcher(line); + if (matcher.find()) { + packageName = matcher.group(1); + } + } + + { + Pattern compile = Pattern.compile("\\s*package\\s(.*)\\.module\\.(.*).rsp;"); + Matcher matcher = compile.matcher(line); + if (matcher.find()) { + domainName = matcher.group(1); + moduleName = matcher.group(2); + } + } + continue; + } + if (line.matches("import\\s.*")) { + if (line.contains(".ent.")) { + Pattern compile = Pattern.compile("import\\s+(.*\\.ent\\..*);"); + Matcher matcher = compile.matcher(line); + if (matcher.find()) { + String group = matcher.group(1); + deptEntList.add(group); + } + } else if (line.contains(".req.")) { + Pattern compile = Pattern.compile("import\\s+(.*\\.req\\..*);"); + Matcher matcher = compile.matcher(line); + if (matcher.find()) { + String group = matcher.group(1); + deptReqList.add(group); + } + } else if (!line.contains("frame")) { + importList.add(line); + } + continue; + } + if (line.contains("/**") || line.contains("*") || line.contains("*/")) { + if (zs != null) { + zs.append(line + "\n"); + annotationList.add(line); + if (line.contains("*/")) { + zs = null; + } + } + } + if (line.contains("class")) { + line = line.replaceAll("\\{", " { "); + line = line.replaceAll("}", " } "); + line = line.replaceAll("\\s+", " "); + String[] split = line.split("\\s"); + for (int i = 0; i < split.length; i++) { + if ("class".equals(split[i])) { + className = split[i + 1]; + } + if ("extends".equals(split[i])) { + fatherName = split[i + 1]; + + if (fatherName.contains("FindResponse") || fatherName.contains("GetAllResponse")) { + hasList = true; + + Pattern pattern = Pattern.compile("<(.*?)>"); + Matcher matcher = pattern.matcher(fatherName); + if (matcher.find()) { + String group = matcher.group(1); + Tclass = group; + } + } + if (fatherName.contains("FindRequest")) { + findOrSearchflag = "1"; + } else if (fatherName.contains("SearchRequest")) { + findOrSearchflag = "2"; + } + } + } + } + sb.append(line + "\n"); + } + + body = new ArrayList<>(Arrays.asList(sb.substring(sb.indexOf("{")).split("\n"))); + + for (int i = 0; i < body.size(); i++) { + if (body.get(i).contains("{")) { + body.set(i, body.get(i).replaceAll("\\{", "")); + String s = body.get(i); + if (s.length() <= 1) { + body.remove(i); + } + break; + } + } + for (int i = body.size() - 1; i >= 0; i--) { + if (body.get(i).contains("}")) { + body.set(i, body.get(i).replaceAll("}", "")); + break; + } + } + if (body.size() > 1 && "".equals(body.get(0))) { + body.remove(0); + } + if (body.size() > 1 && "".equals(body.get(body.size() - 1))) { + body.remove(body.size() - 1); + } + + bufferedReader.close(); + } + + public String getPackageName() { + return packageName; + } + + public void setPackageName(String packageName) { + this.packageName = packageName; + } + + public String getDomainName() { + return domainName; + } + + public void setDomainName(String domainName) { + this.domainName = domainName; + } + + public String getModuleName() { + return moduleName; + } + + public void setModuleName(String moduleName) { + this.moduleName = moduleName; + } + + public List getAnnotationList() { + return annotationList; + } + + public void setAnnotationList(List annotationList) { + this.annotationList = annotationList; + } + + public List getImportList() { + return importList; + } + + public void setImportList(List importList) { + this.importList = importList; + } + + public String getClassName() { + return className; + } + + public void setClassName(String className) { + this.className = className; + } + + public String getFatherName() { + return fatherName; + } + + public void setFatherName(String fatherName) { + this.fatherName = fatherName; + } + + public List getBody() { + return body; + } + + public void setBody(List body) { + this.body = body; + } + + public File getJavaClass() { + return javaClass; + } + + public void setJavaClass(File javaClass) { + this.javaClass = javaClass; + } + + public boolean isHasList() { + return hasList; + } + + public void setHasList(boolean hasList) { + this.hasList = hasList; + } + + public String getTclass() { + return Tclass; + } + + public String getFindOrSearchflag() { + return findOrSearchflag; + } + + public void setFindOrSearchflag(String findOrSearchflag) { + this.findOrSearchflag = findOrSearchflag; + } + + public void setTclass(String tclass) { + Tclass = tclass; + } + + public Set getDeptReqList() { + return deptReqList; + } + + public void setDeptReqList(Set deptReqList) { + this.deptReqList = deptReqList; + } + + public Set getDeptEntList() { + return deptEntList; + } + + public void setDeptEntList(Set deptEntList) { + this.deptEntList = deptEntList; + } + + public static void main(String[] args) throws IOException { + ResponseReader requestReader = new ResponseReader(new File("D:\\wangbing\\Project\\dbtool\\target\\project\\EXAMPLE-WEB\\src\\main\\java\\com\\example\\module\\system\\rsp\\DictFindResponse.java")); + + System.out.println(); + } +} diff --git a/src/main/java/xyz/wbsite/dbtool/javafx/tool/Tool.java b/src/main/java/xyz/wbsite/dbtool/javafx/tool/Tool.java index f05544e3..63671de1 100644 --- a/src/main/java/xyz/wbsite/dbtool/javafx/tool/Tool.java +++ b/src/main/java/xyz/wbsite/dbtool/javafx/tool/Tool.java @@ -65,6 +65,7 @@ public class Tool { public static String uuid() { return UUID.randomUUID().toString(); } + /** * FileCreateRequest -> file * @@ -384,6 +385,21 @@ public class Tool { return false; } + /** + * 替换路径分割符号 + * + * @param path + * @param sep + * @return + */ + public static String replaceSeparator(String path, String sep) { + String pathReplace = path.replaceAll("\\\\", "#"); + if (pathReplace.equals(path)) { + pathReplace = path.replaceAll("/", "#"); + } + return pathReplace; + } + public static void exchange(List list, int i, int j) { Object o1 = list.get(i); Object o2 = list.get(j); diff --git a/src/main/resources/modules/Java_api/module/req/request.java b/src/main/resources/modules/Java_api/module/req/request.java index f6435eec..ed22f2e3 100644 --- a/src/main/resources/modules/Java_api/module/req/request.java +++ b/src/main/resources/modules/Java_api/module/req/request.java @@ -5,6 +5,12 @@ package ${domain}.${module}.req; ${i} +<#list entNames as entName> +import ${domain}.${module}.ent.${entName}; + +<#list reqNames as reqName> +import ${domain}.${module}.ent.${reqName}; + import ${domain}.${module}.rsp.${className}Response; import ${domain}.ApiRequest; <#if findOrSearchflag=='1'>