diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/CompilationUnitHelper.java b/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/CompilationUnitHelper.java index 1fe7fd5e07268af4f53cf3a1b8828cfbfc0be70f..dbb34bf41c31ec73aaa8ac2ea14a18ddbde66857 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/CompilationUnitHelper.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/CompilationUnitHelper.java @@ -7,11 +7,11 @@ import java.util.Optional; public class CompilationUnitHelper { - public static Optional getCompilationUnit(Node node){ - if(node instanceof CompilationUnit){ + public static Optional getCompilationUnit(Node node) { + if (node instanceof CompilationUnit) { return Optional.of((CompilationUnit) node); } - if (node.getParentNode().isPresent()){ + if (node.getParentNode().isPresent()) { return getCompilationUnit(node.getParentNode().get()); } return Optional.empty(); diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/EnumHelper.java b/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/EnumHelper.java new file mode 100644 index 0000000000000000000000000000000000000000..a64657b7dc1f77930dd167bcf8bef18d1e8b433d --- /dev/null +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/EnumHelper.java @@ -0,0 +1,40 @@ +package com.kim.apidoc.core.common.helper; + +import com.github.javaparser.ast.body.EnumDeclaration; +import com.github.javaparser.resolution.declarations.ResolvedEnumConstantDeclaration; +import com.github.javaparser.resolution.declarations.ResolvedEnumDeclaration; +import com.kim.apidoc.core.schema.Section; + +public class EnumHelper { + + public static String getNames(ResolvedEnumDeclaration enumDeclaration) { + StringBuilder sb = new StringBuilder(); + for (ResolvedEnumConstantDeclaration resolvedEnumConstantDeclaration : enumDeclaration.getEnumConstants()) { + if (sb.length() > 0) { + sb.append(","); + } + sb.append(resolvedEnumConstantDeclaration.getName()); + } + return sb.toString(); + } + + public static Section toDetails(EnumDeclaration declaration) { + Section section = new Section(); + section.setId(declaration.getNameAsString()); + section.setName(declaration.getNameAsString()); + section.setNameAndDescription(declaration.getNameAsString()); + + //Map rows = Maps.newLinkedHashMap(); + //for (EnumConstantDeclaration constant : declaration.getEntries()) { + // Row row=new Row(); + // row.setKey(constant.getNameAsString()); + // for (Expression expression : constant.getArguments()) { + // Object value = Expressions.getValue(expression); + // cell.add(String.valueOf(value)); + // } + // cells.add(cell); + //} + return section; + } + +} diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/OptionalHelper.java b/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/OptionalHelper.java index a99b6ec73038d6bd0a0fb802f8413db60151dc20..8aab28beac65d8e1b613747f0a509f60ff10a4e7 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/OptionalHelper.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/OptionalHelper.java @@ -5,9 +5,9 @@ import java.util.Optional; public class OptionalHelper { @SafeVarargs - public static Optional any(Optional ... optionals){ + public static Optional any(Optional... optionals) { for (Optional optional : optionals) { - if(optional.isPresent()){ + if (optional.isPresent()) { return optional; } } diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/parser/VisitorParser.java b/apidoc-core/src/main/java/com/kim/apidoc/core/parser/VisitorParser.java index 044d98fd9c22eb8098c9fa0a15ca05bfc080cf7b..f498a2b0e339e217b189ce878d47fb666fd53539 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/parser/VisitorParser.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/parser/VisitorParser.java @@ -1,19 +1,23 @@ package com.kim.apidoc.core.parser; +import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; +import com.github.javaparser.ast.body.EnumDeclaration; +import com.github.javaparser.ast.body.MethodDeclaration; import com.github.javaparser.ast.comments.Comment; import com.github.javaparser.ast.comments.JavadocComment; +import com.github.javaparser.ast.visitor.VoidVisitorAdapter; import com.github.javaparser.javadoc.Javadoc; import com.github.javaparser.javadoc.JavadocBlockTag; -import com.kim.apidoc.core.common.helper.CommentHelper; -import com.kim.apidoc.core.schema.*; import com.kim.apidoc.core.common.helper.OptionalHelper; -import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; -import com.github.javaparser.ast.body.MethodDeclaration; -import com.github.javaparser.ast.visitor.VoidVisitorAdapter; -import jdk.nashorn.internal.runtime.options.Option; +import com.kim.apidoc.core.schema.Chapter; +import com.kim.apidoc.core.schema.Node; +import com.kim.apidoc.core.schema.Project; +import com.kim.apidoc.core.schema.Section; import java.util.Optional; +import static com.kim.apidoc.core.schema.Chapter.NODE_CHAPTER; + /** * The type Visitor parser. */ @@ -33,6 +37,11 @@ public class VisitorParser extends VoidVisitorAdapter { this.parserStrategy = parserStrategy; } + @Override + public void visit(final EnumDeclaration enumDeclaration, final Node arg) { + super.visit(enumDeclaration, arg); + } + /** * 类或者接口声明 * @@ -41,14 +50,7 @@ public class VisitorParser extends VoidVisitorAdapter { */ @Override public void visit(final ClassOrInterfaceDeclaration classOrInterfaceDeclaration, final Node arg) { - if (arg instanceof Project && isExistTag(classOrInterfaceDeclaration, "code")) { - - } - if (arg instanceof Project && isExistTag(classOrInterfaceDeclaration, "resultData")) { - - } - - if (arg instanceof Project && parserStrategy.accept(classOrInterfaceDeclaration)) { + if (arg instanceof Project) { Project project = (Project) arg; // 章节 Chapter chapter = new Chapter(); @@ -59,9 +61,19 @@ public class VisitorParser extends VoidVisitorAdapter { OptionalHelper.any(chapter.getTag("book"), chapter.getTag("group")) .ifPresent(tag -> chapter.setBookName(tag.getContent())); - parserStrategy.visit(classOrInterfaceDeclaration, chapter); - project.addChapter(chapter); - super.visit(classOrInterfaceDeclaration, chapter); + if (parserStrategy.accept(classOrInterfaceDeclaration)) { + chapter.setType(NODE_CHAPTER); + parserStrategy.visit(classOrInterfaceDeclaration, chapter); + project.addChapter(chapter); + super.visit(classOrInterfaceDeclaration, chapter); + } else if (isExistTag(classOrInterfaceDeclaration, "code")) { + //chapter.setType(NODE_APPENDIX); + //if (null==project.getAppendixChapter(chapter.getBookName())){ + // project.addChapter(chapter); + //} + super.visit(classOrInterfaceDeclaration, arg); + } else if (isExistTag(classOrInterfaceDeclaration, "resultData")) { + } } } diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Appendix.java b/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Appendix.java deleted file mode 100644 index b81afc3fad119cb7ffb85aed6487460d17661449..0000000000000000000000000000000000000000 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Appendix.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.kim.apidoc.core.schema; - -import com.github.fengyuchenglun.core.common.Cell; -import com.github.fengyuchenglun.core.resolver.ast.Enums; -import com.github.fengyuchenglun.core.resolver.ast.Fields; -import com.github.javaparser.ast.body.BodyDeclaration; -import com.github.javaparser.ast.comments.JavadocComment; -import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName; -import lombok.Getter; -import lombok.Setter; - -import javax.annotation.Nullable; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -/** - * 附录 - */ -@Setter -@Getter -public class Appendix extends Node { - - Map rows = new LinkedHashMap<>(); - - public boolean isEmpty() { - return cells.isEmpty(); - } - - @Nullable - public static Appendix parse(JavadocComment n) { - // 无注释 - if (!n.getCommentedNode().isPresent()) { - return null; - } - // 注释所在的类或者枚举 - final com.github.javaparser.ast.Node node = n.getCommentedNode().get(); - if(!(node instanceof BodyDeclaration)){ - return null; - } - final BodyDeclaration bodyDeclaration = (BodyDeclaration) node; - // 不是枚举与接口/类 - if(!bodyDeclaration.isEnumDeclaration() && !bodyDeclaration.isClassOrInterfaceDeclaration()){ - return null; - } - Appendix appendix = new Appendix(); - // 枚举 - if (bodyDeclaration.isEnumDeclaration()) { - appendix.getRows().addAll(Enums.toDetails(bodyDeclaration.asEnumDeclaration())); - }else if (bodyDeclaration.isClassOrInterfaceDeclaration()) { - appendix.getRows().addAll(Fields.getConstants(bodyDeclaration.asClassOrInterfaceDeclaration())); - } - if (node instanceof NodeWithSimpleName) { - appendix.setName(((NodeWithSimpleName) node).getNameAsString()); - } - appendix.accept(node.getComment()); - return appendix; - } - -} diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Chapter.java b/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Chapter.java index e65bda37313f7c3a3580a1165895262206ff3619..a411f6b68d18752584c674bcd0394a9c8b882e32 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Chapter.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Chapter.java @@ -15,6 +15,14 @@ import java.util.TreeSet; @Setter @Getter public class Chapter extends Node { + /** + * 章节节点. + */ + public static final String NODE_CHAPTER = "NODE_CHAPTER"; + /** + * 附录节点. + */ + public static final String NODE_APPENDIX = "NODE_APPENDIX"; /** * The Book name. diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Node.java b/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Node.java index c3eb0a0a9cf4881cea312be3791689f9b87c171b..63d628cef324fdfa5c1e82a7e67a870b57780914 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Node.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Node.java @@ -22,7 +22,10 @@ import java.util.Optional; @Setter @Getter public class Node implements Comparable { - + /** + * 节点类型 + */ + String type; /** * 节点编号 */ @@ -52,6 +55,9 @@ public class Node implements Comparable { @Override public int compareTo(@Nonnull Node other) { + if (this.type != null && other.type != null) { + return this.type.compareTo(other.type); + } if (this.index != other.index) { return this.index - other.index; } diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Project.java b/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Project.java index 656ce2bac78261abcde67ab76ad13bf421011697..4db12db59d404b30c4f4904756f62b23f7d96766 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Project.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Project.java @@ -2,9 +2,12 @@ package com.kim.apidoc.core.schema; import lombok.Getter; import lombok.Setter; +import org.apache.commons.lang3.StringUtils; import java.util.*; +import static com.kim.apidoc.core.schema.Chapter.NODE_APPENDIX; + /** * 项目 * @@ -39,4 +42,17 @@ public class Project extends Node { } books.get(chapter.getBookName()).getChapters().add(chapter); } + + public Chapter getAppendixChapter(String bookName) { + if (StringUtils.isBlank(bookName)) { + bookName = Book.DEFAULT; + } + if (books.containsKey(bookName)) { + Optional chapterOptional = books.get(bookName).getChapters().stream().filter(x -> NODE_APPENDIX.equals(x.getType())).findFirst(); + if (chapterOptional.isPresent()) { + return chapterOptional.get(); + } + } + return null; + } } diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Section.java b/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Section.java index cd039399e339a8374b8c921252083bf41b8c8b48..15634298257c76b45183c455c3d4191ff1dbf6ef 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Section.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Section.java @@ -67,6 +67,20 @@ public class Section extends Node { */ Object rawResponse; + /** + * 全局编码列表 + */ + Map codeRows = new LinkedHashMap<>(); + + /** + * Add code rows. + * + * @param row the row + */ + public void addCodeRows(Row row) { + codeRows.put(row.getKey(), row); + } + /** * Add request row. * diff --git a/apidoc-springmvc/src/test/java/com/kim/apidoc/example/common/Role.java b/apidoc-springmvc/src/test/java/com/kim/apidoc/example/common/Role.java index 25bdd5c8a3218029718d54da1674563459af1dbc..22b450ed7abac2b3191d8987e1a1c2da2044aa2a 100644 --- a/apidoc-springmvc/src/test/java/com/kim/apidoc/example/common/Role.java +++ b/apidoc-springmvc/src/test/java/com/kim/apidoc/example/common/Role.java @@ -2,14 +2,34 @@ package com.kim.apidoc.example.common; /** * 用户角色 + * * @code */ public enum Role { - ADMIN("管理员"),USER("用户"),VIP("会员"); + /** + * Admin role. + */ + ADMIN("管理员"), + /** + * User role. + */ + USER("用户"), + /** + * Vip role. + */ + VIP("会员"); + /** + * The Text. + */ String text; + /** + * Instantiates a new Role. + * + * @param text the text + */ Role(String text) { this.text = text; } diff --git a/build.gradle b/build.gradle index ece9c30264c61d53aa76070bb6bc229caf2b0568..f3d4228adc1783de05ddfed1140970269efc84e3 100644 --- a/build.gradle +++ b/build.gradle @@ -18,7 +18,7 @@ allprojects { dependencies { compile 'ch.qos.logback:logback-classic:1.2.3' - + compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.10' compileOnly 'org.projectlombok:lombok:1.18.4' annotationProcessor 'org.projectlombok:lombok:1.18.6'