From ff278a6d7801eb41720319d4c0f3fadd2d2d40e5 Mon Sep 17 00:00:00 2001 From: wangbing <1919101440@qq.com> Date: Fri, 6 Dec 2019 00:26:42 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: 2defdd8ea98e70df13b0c6ebd251b74ed86a7d64 --- .../javafx/ctrl/OptionApiController.java | 265 +++++--------- .../dbtool/javafx/manger/ProjectManager.java | 6 +- .../javafx/manger/callable/ApiCallable.java | 332 +++++++++--------- .../java/xyz/wbsite/dbtool/javafx/po/Api.java | 82 ++--- .../wbsite/dbtool/javafx/po/ApiMethod.java | 31 ++ .../dbtool/javafx/tool/ApiClassReader.java | 63 ++++ .../xyz/wbsite/dbtool/javafx/tool/Dialog.java | 21 +- .../dbtool/javafx/tool/JavaClassReader.java | 3 - .../xyz/wbsite/dbtool/javafx/tool/Tool.java | 38 ++ .../web/frame/base/FileUploadResponse.java | 34 -- src/main/resources/fxml/apiOption.fxml | 19 +- .../resources/modules/Java_api/ApiTest.java | 6 +- .../module/{entity => ent}/entity.java | 2 +- .../module/{request => req}/request.java | 8 +- .../module/{response => rsp}/response.java | 2 +- src/main/resources/modules/Java_api/pom.xml | 2 +- .../SpringBoot/test/application.properties | 2 +- .../java/filter/Authorizations.ftl | 2 +- .../modules/SpringMVC_Mybatis/pom.ftl | 4 +- 19 files changed, 445 insertions(+), 477 deletions(-) create mode 100644 src/main/java/xyz/wbsite/dbtool/javafx/po/ApiMethod.java create mode 100644 src/main/java/xyz/wbsite/dbtool/javafx/tool/ApiClassReader.java delete mode 100644 src/main/java/xyz/wbsite/dbtool/web/frame/base/FileUploadResponse.java rename src/main/resources/modules/Java_api/module/{entity => ent}/entity.java (84%) rename src/main/resources/modules/Java_api/module/{request => req}/request.java (81%) rename src/main/resources/modules/Java_api/module/{response => rsp}/response.java (90%) 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 7f76c773..5b3c862d 100644 --- a/src/main/java/xyz/wbsite/dbtool/javafx/ctrl/OptionApiController.java +++ b/src/main/java/xyz/wbsite/dbtool/javafx/ctrl/OptionApiController.java @@ -1,9 +1,5 @@ package xyz.wbsite.dbtool.javafx.ctrl; -import xyz.wbsite.dbtool.javafx.view.DBCheckBoxTableCell; -import xyz.wbsite.dbtool.javafx.po.Api; -import xyz.wbsite.dbtool.javafx.tool.Dialog; -import xyz.wbsite.dbtool.javafx.tool.Tool; import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; @@ -15,6 +11,12 @@ import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.control.cell.TextFieldTableCell; import javafx.util.Callback; import javafx.util.converter.DefaultStringConverter; +import xyz.wbsite.dbtool.javafx.po.Api; +import xyz.wbsite.dbtool.javafx.po.ApiMethod; +import xyz.wbsite.dbtool.javafx.tool.ApiClassReader; +import xyz.wbsite.dbtool.javafx.tool.Dialog; +import xyz.wbsite.dbtool.javafx.tool.Tool; +import xyz.wbsite.dbtool.javafx.view.DBCheckBoxTableCell; import java.io.*; import java.util.ArrayList; @@ -39,8 +41,6 @@ public class OptionApiController { @FXML private CheckBox All; @FXML - private CheckBox api; - @FXML private CheckBox create; @FXML private CheckBox delete; @@ -99,14 +99,6 @@ public class OptionApiController { return create; } - public CheckBox getApi() { - return api; - } - - public void setApi(CheckBox api) { - this.api = api; - } - public void setCreate(CheckBox create) { this.create = create; } @@ -196,115 +188,84 @@ public class OptionApiController { } - private static File find(File file, String name) { - if (file == null) { - return null; - } else if (file.equals(name)) { - return file; - } - - if (file.listFiles() != null) { - for (File f : file.listFiles()) { - return find(f, name); - } - } - - return null; - } - public void load() { File moduleFile = new File(modulePath.getText()); data.clear(); if (moduleFile.exists()) { - File apiPath = find(moduleFile, "api"); - - for (File file : apiPath.listFiles()) { - String module = file.getName(); - for (File tar : file.listFiles()) { - String target = tar.getName().replaceAll("Api", ""); - - Api api = new Api(); - api.setModule(module); - api.setTarget(target); + List apiPath = Tool.find(moduleFile, "api"); - } - } - - File reqs = new File(moduleFile, "req"); - - if (!reqs.exists()) { + if (apiPath.size() == 0) { return; } Dialog.showProgress("扫描API..."); - for (File req : reqs.listFiles()) { - Api api = new Api(); - api.setReq(req); - api.setTargetRequest(req.getName().replace(".java", "")); - - //查找依赖ent - List entities = findEntities(req); - for (String entity : entities) { - api.getDepEnt().add(entity); - } - //查找依赖req - List reqss = findReq(req); - for (String string : reqss) { - api.getDepReq().add(string); - } - - //找response - File rsp = new File(moduleFile.getAbsolutePath() + File.separator + "rsp" + File.separator + req.getName().replace("Request", "Response")); - if (rsp.exists()) { - api.setRsp(rsp); - api.setTargetResponse(rsp.getName().replace(".java", "")); - - //查找依赖ent - List entities_ = findEntities(rsp); - for (String entity : entities_) { - api.getDepEnt().add(entity); - } - - //查找依赖req - List reqss_ = findReq(rsp); - for (String string : reqss_) { - api.getDepReq().add(string); - } - } - -// api.setMethod(module + "." + Tool.camelToPoint(req.getName().replaceAll("Request\\.java", ""))); - - - if (api.getTargetRequest().startsWith("Api")) { - api.setCheck(true); - } else { - api.setCheck(false); - } - - if (api.getRsp() == null || !api.getRsp().exists()) { - api.setError("响应不存在"); - api.setCheck(false); - } - for (String s : api.getDepReq()) { - File en = new File(moduleFile.getAbsolutePath() + File.separator + "req" + File.separator + s + ".java"); - if (!en.exists()) { - api.setError("依赖的" + s + "(Request)不存在、"); - api.setCheck(false); - } - } - for (String s : api.getDepEnt()) { - File en = new File(moduleFile.getAbsolutePath() + File.separator + "ent" + File.separator + s + ".java"); - if (!en.exists()) { - api.setError("依赖的" + s + "(Entity)不存在、"); - api.setCheck(false); + for (File file : apiPath.get(0).listFiles()) { + String module = file.getName(); + for (File tar : file.listFiles()) { + try { + String target = tar.getName().replaceAll("Api", "").replaceAll(".java", ""); + ApiClassReader apiClassReader = new ApiClassReader(tar); + + for (ApiMethod apiMethod : apiClassReader.getMethodList()) { + Api api = new Api(); + api.setModule(module); + api.setTarget(target); + api.setRequest(apiMethod.getRequest()); + api.setResponse(apiMethod.getResponse()); + api.setMethod(apiMethod.getMethod()); + + List reqFiles = Tool.find(moduleFile, apiMethod.getRequest() + ".java"); + if (reqFiles.size() == 0) { + api.setError(api.getError() + "未找到请求"); + } else if (reqFiles.size() > 1) { + api.setError(api.getError() + "找到多个请求"); + } else { + api.setRequestFile(reqFiles.get(0)); + //查找依赖ent + List entities = findEntities(reqFiles.get(0)); + for (String entity : entities) { + api.getDepEnt().add(entity); + } + + //查找依赖req + List reqss = findReq(reqFiles.get(0)); + for (String string : reqss) { + api.getDepReq().add(string); + } + } + + List rspfiles = Tool.find(moduleFile, apiMethod.getResponse() + ".java"); + if (rspfiles.size() == 0) { + api.setError(api.getError() + "未找到响应"); + } else if (rspfiles.size() > 1) { + api.setError(api.getError() + "找到多个响应"); + } else { + api.setResponseFile(rspfiles.get(0)); + //查找依赖ent + List entities = findEntities(rspfiles.get(0)); + for (String entity : entities) { + api.getDepEnt().add(entity); + } + + //查找依赖req + List reqss = findReq(rspfiles.get(0)); + for (String string : reqss) { + api.getDepReq().add(string); + } + } + + api.setCheck(true); + data.add(api); + } + } catch (IOException e) { + e.printStackTrace(); } } - - data.add(api); - initData(); } + + initData(); Dialog.stopPopup(); } } @@ -340,37 +301,6 @@ public class OptionApiController { return strings; } - public List findEnums(File file) { - ArrayList strings = new ArrayList<>(); - InputStreamReader read = null;//考虑到编码格式 - try { - read = new InputStreamReader(new FileInputStream(file), "UTF-8"); - BufferedReader bufferedReader = new BufferedReader(read); - String line = null; - while ((line = bufferedReader.readLine()) != null) { - //指定字符串判断处 - if (line.contains(".enums.")) { - Pattern compile = Pattern.compile(".*\\.enums\\.(.*);"); - - Matcher matcher = compile.matcher(line); - - if (matcher.find()) { - String group = matcher.group(1); - strings.add(group); -// System.out.println(group); - } - } - } - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - return strings; - } - public List findReq(File file) { ArrayList strings = new ArrayList<>(); InputStreamReader read = null;//考虑到编码格式 @@ -428,7 +358,6 @@ public class OptionApiController { return strings; } - public void initData() { apis.setEditable(true); apis.setSortPolicy(new Callback() { @@ -448,11 +377,6 @@ public class OptionApiController { @Override public ObservableValue call(Integer param) { super.call(param); -// if (data.get(param).isCheck()) { -// checkBoxTableCell.setInvalid(true); -// } else { -// checkBoxTableCell.setInvalid(false); -// } if (data.get(param).isCheck()) { return new SimpleBooleanProperty(true); } else { @@ -472,19 +396,10 @@ public class OptionApiController { return checkBoxTableCell; } }); - columns.get(1).setCellValueFactory(new PropertyValueFactory("error")); + columns.get(1).setCellValueFactory(new PropertyValueFactory("target")); columns.get(1).setCellFactory(new Callback() { @Override public TableCell call(TableColumn param) { -// param.setOnEditCommit(new EventHandler() { -// @Override -// public void handle(TableColumn.CellEditEvent event) { -// int row = event.getTablePosition().getRow(); -// Api api = data.get(row); -// api.setReqName((String) event.getNewValue()); -// } -// }); - TextFieldTableCell textFieldTableCell = new TextFieldTableCell(new DefaultStringConverter()) { @Override public void updateItem(Object item, boolean empty) { @@ -496,19 +411,10 @@ public class OptionApiController { return textFieldTableCell; } }); - - columns.get(2).setCellValueFactory(new PropertyValueFactory("targetRequest")); + columns.get(2).setCellValueFactory(new PropertyValueFactory("method")); columns.get(2).setCellFactory(new Callback() { @Override public TableCell call(TableColumn param) { -// param.setOnEditCommit(new EventHandler() { -// @Override -// public void handle(TableColumn.CellEditEvent event) { -// int row = event.getTablePosition().getRow(); -// Api api = data.get(row); -// api.setTargetRequest((String) event.getNewValue()); -// } -// }); TextFieldTableCell textFieldTableCell = new TextFieldTableCell(new DefaultStringConverter()) { @Override public void updateItem(Object item, boolean empty) { @@ -519,18 +425,10 @@ public class OptionApiController { return textFieldTableCell; } }); - columns.get(3).setCellValueFactory(new PropertyValueFactory("targetResponse")); + columns.get(3).setCellValueFactory(new PropertyValueFactory("error")); columns.get(3).setCellFactory(new Callback() { @Override public TableCell call(TableColumn param) { -// param.setOnEditCommit(new EventHandler() { -// @Override -// public void handle(TableColumn.CellEditEvent event) { -// int row = event.getTablePosition().getRow(); -// Api api = data.get(row); -// api.setTargetResponse((String) event.getNewValue()); -// } -// }); TextFieldTableCell textFieldTableCell = new TextFieldTableCell(new DefaultStringConverter()) { @Override public void updateItem(Object item, boolean empty) { @@ -541,8 +439,7 @@ public class OptionApiController { return textFieldTableCell; } }); - - columns.get(4).setCellValueFactory(new PropertyValueFactory("method")); + columns.get(4).setCellValueFactory(new PropertyValueFactory("request")); columns.get(4).setCellFactory(new Callback() { @Override public TableCell call(TableColumn param) { @@ -556,6 +453,20 @@ public class OptionApiController { return textFieldTableCell; } }); + columns.get(5).setCellValueFactory(new PropertyValueFactory("response")); + columns.get(5).setCellFactory(new Callback() { + @Override + public TableCell call(TableColumn param) { + TextFieldTableCell textFieldTableCell = new TextFieldTableCell(new DefaultStringConverter()) { + @Override + public void updateItem(Object item, boolean empty) { + super.updateItem(item, empty); + } + }; + textFieldTableCell.setEditable(false); + return textFieldTableCell; + } + }); ObservableList ObservableList = FXCollections.observableArrayList(); ObservableList.addAll(data); apis.setItems(ObservableList); 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 98425cd6..3a51a00f 100644 --- a/src/main/java/xyz/wbsite/dbtool/javafx/manger/ProjectManager.java +++ b/src/main/java/xyz/wbsite/dbtool/javafx/manger/ProjectManager.java @@ -278,11 +278,7 @@ public class ProjectManager { @Override public void run() { boolean mkdirs = api.mkdirs(); - File reqList = new File(module, "req"); - File rspList = new File(module, "rsp"); - File entList = new File(module, "ent"); - File enumsList = new File(module, "enums"); - ApiCallable apiCallable = new ApiCallable(api, reqList, rspList, entList, enumsList, apis); + ApiCallable apiCallable = new ApiCallable(module,api, 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 1d7b6520..e2121183 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 @@ -1,12 +1,11 @@ package xyz.wbsite.dbtool.javafx.manger.callable; -import xyz.wbsite.dbtool.javafx.manger.ProjectManager; import xyz.wbsite.dbtool.javafx.manger.FreeMarkerManager; import xyz.wbsite.dbtool.javafx.manger.ManagerFactory; +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.JavaClassReader; -import xyz.wbsite.dbtool.javafx.tool.JavaEnumReader; import xyz.wbsite.dbtool.javafx.tool.Tool; import java.io.File; @@ -16,55 +15,45 @@ import java.util.concurrent.Callable; public class ApiCallable implements Callable { - private File api; - private File req; - private File rsp; - private File ent; + private File module; + private File apiFile; private List apiList; private Tool tool = new Tool(); private FreeMarkerManager freeMarkerManager; - public ApiCallable(File api, File req, File rsp, File ent, File enums, List apiList) { - this.api = api; - this.req = req; - this.rsp = rsp; - this.ent = ent; + public ApiCallable(File module, File apiFile, List apiList) { + this.module = module; + this.apiFile = apiFile; this.apiList = apiList; this.freeMarkerManager = ManagerFactory.getFreeMarkerManager(); } - private AbstractDBmapper dBmapper; - public Boolean call() throws Exception { - if (!api.exists()) { - api.mkdir(); + if (!apiFile.exists()) { + apiFile.mkdir(); } else { - Tool.clear(api); + Tool.clear(apiFile); } - dBmapper = ProjectManager.dBmapper; + AbstractDBmapper dBmapper = ProjectManager.dBmapper; File domain; File testdomain; - File request; - File response; - File entity; - File enumeration; StringBuffer sbmain = new StringBuffer(""); - sbmain.append(this.api.getPath() + File.separator); + sbmain.append(this.apiFile.getPath() + File.separator); sbmain.append("src" + File.separator); sbmain.append("main" + File.separator); sbmain.append("java" + File.separator); StringBuffer sbtest = new StringBuffer(""); - sbtest.append(this.api.getPath() + File.separator); + sbtest.append(this.apiFile.getPath() + File.separator); sbtest.append("src" + File.separator); sbtest.append("test" + File.separator); sbtest.append("java" + File.separator); - JavaClassReader javaClass = new JavaClassReader(apiList.get(0).getReq()); + JavaClassReader javaClass = new JavaClassReader(apiList.get(0).getRequestFile()); String[] split = javaClass.getDomainName().split("\\."); for (String s1 : split) { @@ -76,37 +65,26 @@ public class ApiCallable implements Callable { domain.mkdirs(); testdomain = new File(sbtest.toString()); testdomain.mkdirs(); - request = new File(domain, "request"); - request.mkdirs(); - response = new File(domain, "response"); - response.mkdirs(); - entity = new File(domain, "entity"); - entity.mkdirs(); - enumeration = new File(domain, "enums"); - enumeration.mkdirs(); - - { - System.out.println("生成模块:Pom"); - HashMap ctx = new HashMap(); - ctx.put("api", api.getName()); - File file = new File(api, "pom.xml"); - freeMarkerManager.outputTemp(file, "Java_api/pom.xml", ctx); - } - - - Set managerList = new HashSet<>(); - List methodList = new ArrayList<>(); for (Api api : apiList) { if (api.isCheck()) { + File module = new File(domain, api.getModule()); + module.mkdirs(); + File request = new File(module, "req"); + request.mkdirs(); + File response = new File(module, "rsp"); + response.mkdirs(); + File entity = new File(module, "ent"); + entity.mkdirs(); + try { //region 生成request - JavaClassReader javaClassReader = new JavaClassReader(api.getReq()); + JavaClassReader javaClassReader = new JavaClassReader(api.getRequestFile()); for (int i = 0; i < javaClassReader.getImportList().size(); i++) { String s = javaClassReader.getImportList().get(i); - if (s.contains(".api.entity.")) { - javaClassReader.getImportList().set(i, s.replaceAll("\\.api\\.entity\\.", ".entity.")); + if (s.contains(".module.entity.")) { + javaClassReader.getImportList().set(i, s); } } @@ -116,15 +94,14 @@ public class ApiCallable implements Callable { method.setTarget(Tool.getRequestTarget(javaClassReader.getClassName())); method.setMethod(Tool.getRequestAction(javaClassReader.getClassName())); method.setManager(Tool.getRequestTarget(javaClassReader.getClassName()) + "Manager"); - methodList.add(method); - - managerList.add(method.getManager()); { 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", "")); @@ -132,9 +109,9 @@ public class ApiCallable implements Callable { ctx.put("tool", tool); ctx.put("hasList", javaClassReader.isHasList()); ctx.put("findOrSearchflag", javaClassReader.getFindOrSearchflag()); - File file = new File(request.getAbsolutePath(), api.getReq().getName()); + File file = new File(request.getAbsolutePath(), api.getRequestFile().getName()); - freeMarkerManager.outputTemp(file, "Java_api/module/request/request.java", ctx); + freeMarkerManager.outputTemp(file, "Java_api/module/req/request.java", ctx); System.out.println("生成文件" + file.getName() + "成功"); } //endregion @@ -145,11 +122,11 @@ public class ApiCallable implements Callable { try { //region 生成response - JavaClassReader javaClassReader = new JavaClassReader(api.getRsp()); + JavaClassReader javaClassReader = new JavaClassReader(api.getResponseFile()); for (int i = 0; i < javaClassReader.getImportList().size(); i++) { String import_ = javaClassReader.getImportList().get(i); - if (import_.contains(".api.entity.")) { - javaClassReader.getImportList().set(i, import_.replaceAll("\\.api\\.entity\\.", ".entity.")); + if (import_.contains(".module.entity.")) { + javaClassReader.getImportList().set(i, import_); } } @@ -165,132 +142,141 @@ public class ApiCallable implements Callable { ctx.put("tool", tool); ctx.put("hasList", javaClassReader.isHasList()); ctx.put("Tclass", javaClassReader.getTclass()); - File file = new File(response.getAbsolutePath(), api.getRsp().getName().replaceAll("Request", "Response")); + File file = new File(response.getAbsolutePath(), api.getResponseFile().getName().replaceAll("Request", "Response")); - freeMarkerManager.outputTemp(file, "Java_api/module/response/response.java", ctx); - System.out.println("生成文件" + api.getRsp().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.getDepReq()) { - try { - File f = new File(req.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(".api.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/request/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(".api.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/response/response.java", ctx); - System.out.println("生成文件" + api.getRsp().getName() + "成功"); - } - //endregion - } catch (IOException e) { - e.printStackTrace(); - } - } - - for (String s : api.getDepEnt()) { +// 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(); +// } +// } + } + } - 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(".api.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/entity/entity.java", ctx); - System.out.println("生成文件" + file.getName() + "成功"); - } - //endregion - } catch (IOException e) { - e.printStackTrace(); - } - } - } + { + System.out.println("生成模块:Pom"); + HashMap ctx = new HashMap(); + ctx.put("module", apiFile.getName()); + File file = new File(apiFile, "pom.xml"); + freeMarkerManager.outputTemp(file, "Java_api/pom.xml", ctx); } { @@ -387,8 +373,6 @@ public class ApiCallable implements Callable { ctx.put("tool", tool); ctx.put("domain", javaClass.getDomainName()); ctx.put("module", javaClass.getModuleName()); - ctx.put("managerList", managerList); - ctx.put("methodList", methodList); 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/po/Api.java b/src/main/java/xyz/wbsite/dbtool/javafx/po/Api.java index 90ef906c..10091904 100644 --- a/src/main/java/xyz/wbsite/dbtool/javafx/po/Api.java +++ b/src/main/java/xyz/wbsite/dbtool/javafx/po/Api.java @@ -10,28 +10,20 @@ public class Api { private boolean check; //目标请求对象 - private String targetRequest; + private String request; //目标请求响应 - private String targetResponse; + private String response; private String module; private String target; private String method; - private File req; - private File rsp; + private File requestFile; + private File responseFile; private Set depReq = new HashSet<>(); private Set depEnt = new HashSet<>(); - private String error; - - public String getError() { - return error; - } - - public void setError(String error) { - this.error = error; - } + private String error = ""; public boolean isCheck() { return check; @@ -41,20 +33,36 @@ public class Api { this.check = check; } - public String getTargetRequest() { - return targetRequest; + public String getRequest() { + return request; + } + + public void setRequest(String request) { + this.request = request; } - public void setTargetRequest(String targetRequest) { - this.targetRequest = targetRequest; + public String getResponse() { + return response; + } + + public void setResponse(String response) { + this.response = response; + } + + public String getModule() { + return module; + } + + public void setModule(String module) { + this.module = module; } - public String getTargetResponse() { - return targetResponse; + public String getTarget() { + return target; } - public void setTargetResponse(String targetResponse) { - this.targetResponse = targetResponse; + public void setTarget(String target) { + this.target = target; } public String getMethod() { @@ -65,20 +73,20 @@ public class Api { this.method = method; } - public File getReq() { - return req; + public File getRequestFile() { + return requestFile; } - public void setReq(File req) { - this.req = req; + public void setRequestFile(File requestFile) { + this.requestFile = requestFile; } - public File getRsp() { - return rsp; + public File getResponseFile() { + return responseFile; } - public void setRsp(File rsp) { - this.rsp = rsp; + public void setResponseFile(File responseFile) { + this.responseFile = responseFile; } public Set getDepReq() { @@ -97,19 +105,11 @@ public class Api { this.depEnt = depEnt; } - public String getModule() { - return module; - } - - public void setModule(String module) { - this.module = module; - } - - public String getTarget() { - return target; + public String getError() { + return error; } - public void setTarget(String target) { - this.target = target; + public void setError(String error) { + this.error = error; } } diff --git a/src/main/java/xyz/wbsite/dbtool/javafx/po/ApiMethod.java b/src/main/java/xyz/wbsite/dbtool/javafx/po/ApiMethod.java new file mode 100644 index 00000000..25b2cbba --- /dev/null +++ b/src/main/java/xyz/wbsite/dbtool/javafx/po/ApiMethod.java @@ -0,0 +1,31 @@ +package xyz.wbsite.dbtool.javafx.po; + +public class ApiMethod { + private String request; + private String response; + private String method; + + public String getRequest() { + return request; + } + + public void setRequest(String request) { + this.request = request; + } + + public String getResponse() { + return response; + } + + public void setResponse(String response) { + this.response = response; + } + + public String getMethod() { + return method; + } + + public void setMethod(String method) { + this.method = method; + } +} diff --git a/src/main/java/xyz/wbsite/dbtool/javafx/tool/ApiClassReader.java b/src/main/java/xyz/wbsite/dbtool/javafx/tool/ApiClassReader.java new file mode 100644 index 00000000..6d440176 --- /dev/null +++ b/src/main/java/xyz/wbsite/dbtool/javafx/tool/ApiClassReader.java @@ -0,0 +1,63 @@ +package xyz.wbsite.dbtool.javafx.tool; + +import xyz.wbsite.dbtool.javafx.po.ApiMethod; + +import java.io.*; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class ApiClassReader { + private File javaClass; + + private List methodList; + + public ApiClassReader(File javaClass) throws IOException { + this.javaClass = javaClass; + methodList = new ArrayList<>(); + read(); + } + + private void read() throws IOException { + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(javaClass), "utf-8")); + + String line = null; + Pattern compile = Pattern.compile("\\s+public (.*Response)\\s+(.*)\\((.*Request) request\\) \\{"); + while ((line = bufferedReader.readLine()) != null) { + + Matcher matcher = compile.matcher(line); + + if (matcher.find()) { + ApiMethod apiMethod = new ApiMethod(); + apiMethod.setResponse(matcher.group(1)); + apiMethod.setMethod(matcher.group(2)); + apiMethod.setRequest(matcher.group(3)); + methodList.add(apiMethod); + } + } + bufferedReader.close(); + } + + public File getJavaClass() { + return javaClass; + } + + public void setJavaClass(File javaClass) { + this.javaClass = javaClass; + } + + public List getMethodList() { + return methodList; + } + + public void setMethodList(List methodList) { + this.methodList = methodList; + } + + public static void main(String[] args) throws IOException { + ApiClassReader javaClassReader = new ApiClassReader(new File("D:\\wangbing\\Project\\dbtool\\target\\project\\EXAMPLE-WEB\\src\\main\\java\\com\\example\\action\\api\\system\\DictApi.java")); + + System.out.println(); + } +} 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 f3931655..147beb1b 100644 --- a/src/main/java/xyz/wbsite/dbtool/javafx/tool/Dialog.java +++ b/src/main/java/xyz/wbsite/dbtool/javafx/tool/Dialog.java @@ -444,12 +444,6 @@ public class Dialog { check(controller, controller.getAll().isSelected(), ""); } }); - controller.getApi().setOnAction(new EventHandler() { - @Override - public void handle(ActionEvent event) { - check(controller, controller.getApi().isSelected(), ".api."); - } - }); controller.getCreate().setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { @@ -701,24 +695,11 @@ public class Dialog { controller.initData(); } - private static boolean hasChild(File file, String child) { - if (file == null || child == null || "".equals(child) || file.listFiles() == null) { - return false; - } - - for (File f : file.listFiles()) { - if (f.getName().equals(child)) { - return true; - } - } - - return false; - } private static File findAction(File file) { if (file == null) { return null; - } else if (hasChild(file, "action") && hasChild(file, "module")) { + } else if (Tool.hasChild(file, "action") && Tool.hasChild(file, "module")) { return file; } diff --git a/src/main/java/xyz/wbsite/dbtool/javafx/tool/JavaClassReader.java b/src/main/java/xyz/wbsite/dbtool/javafx/tool/JavaClassReader.java index 2fa410b5..563557a1 100644 --- a/src/main/java/xyz/wbsite/dbtool/javafx/tool/JavaClassReader.java +++ b/src/main/java/xyz/wbsite/dbtool/javafx/tool/JavaClassReader.java @@ -58,9 +58,6 @@ public class JavaClassReader { } if (line.startsWith("import")) { if (!line.contains("frame")) { - if (line.contains(".ent.")) { - line = line.replaceAll("\\.ent\\.", ".entity."); - } importList.add(line); } 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 d1755913..f05544e3 100644 --- a/src/main/java/xyz/wbsite/dbtool/javafx/tool/Tool.java +++ b/src/main/java/xyz/wbsite/dbtool/javafx/tool/Tool.java @@ -2,6 +2,7 @@ package xyz.wbsite.dbtool.javafx.tool; import java.io.*; import java.net.URL; +import java.util.ArrayList; import java.util.List; import java.util.UUID; import java.util.regex.Matcher; @@ -346,6 +347,43 @@ public class Tool { } } + public static List find(File file, String name) { + List fileList = new ArrayList<>(); + + if (file != null) { + if (file.getName().equals("target") || + file.getName().equals("classes") || + file.getName().equals(".idea")) { + return fileList; + } + + if (file.getName().equals(name)) { + fileList.add(file); + } + + if (file.listFiles() != null) { + for (File f : file.listFiles()) { + fileList.addAll(find(f, name)); + } + } + } + return fileList; + } + + public static boolean hasChild(File file, String child) { + if (file == null || child == null || "".equals(child) || file.listFiles() == null) { + return false; + } + + for (File f : file.listFiles()) { + if (f.getName().equals(child)) { + return true; + } + } + + return false; + } + 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/java/xyz/wbsite/dbtool/web/frame/base/FileUploadResponse.java b/src/main/java/xyz/wbsite/dbtool/web/frame/base/FileUploadResponse.java deleted file mode 100644 index 2b6a68e1..00000000 --- a/src/main/java/xyz/wbsite/dbtool/web/frame/base/FileUploadResponse.java +++ /dev/null @@ -1,34 +0,0 @@ -package xyz.wbsite.dbtool.web.frame.base; - -public class FileUploadResponse extends BaseResponse { - - private Long id; - - private String url; - - private String downloadUrl; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getUrl() { - return url; - } - - public void setUrl(String url) { - this.url = url; - } - - public String getDownloadUrl() { - return downloadUrl; - } - - public void setDownloadUrl(String downloadUrl) { - this.downloadUrl = downloadUrl; - } -} diff --git a/src/main/resources/fxml/apiOption.fxml b/src/main/resources/fxml/apiOption.fxml index 22961aae..ac6f2abf 100644 --- a/src/main/resources/fxml/apiOption.fxml +++ b/src/main/resources/fxml/apiOption.fxml @@ -24,9 +24,11 @@