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 ca7691ef32a0b9eff2f5c098259a9660fbe2de55..044d98fd9c22eb8098c9fa0a15ca05bfc080cf7b 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,14 +1,18 @@ package com.kim.apidoc.core.parser; +import com.github.javaparser.ast.comments.Comment; import com.github.javaparser.ast.comments.JavadocComment; -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 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 java.util.Optional; /** * The type Visitor parser. @@ -37,8 +41,14 @@ 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) { + if (arg instanceof Project && parserStrategy.accept(classOrInterfaceDeclaration)) { Project project = (Project) arg; // 章节 Chapter chapter = new Chapter(); @@ -49,14 +59,40 @@ public class VisitorParser extends VoidVisitorAdapter { OptionalHelper.any(chapter.getTag("book"), chapter.getTag("group")) .ifPresent(tag -> chapter.setBookName(tag.getContent())); - if (parserStrategy.accept(classOrInterfaceDeclaration)) { - parserStrategy.visit(classOrInterfaceDeclaration, chapter); - } + parserStrategy.visit(classOrInterfaceDeclaration, chapter); project.addChapter(chapter); super.visit(classOrInterfaceDeclaration, chapter); } } + + /** + * 处理特殊的tag + * + * @param classOrInterfaceDeclaration the class or interface declaration + * @param tag the tag + * @return boolean boolean + */ + private Boolean isExistTag(ClassOrInterfaceDeclaration classOrInterfaceDeclaration, String tag) { + if (classOrInterfaceDeclaration.getComment().isPresent()) { + Comment comment = classOrInterfaceDeclaration.getComment().get(); + if (!comment.isJavadocComment()) { + return false; + } + Javadoc javadoc = comment.asJavadocComment().parse(); + + Optional blockTagOptional = + javadoc.getBlockTags().stream().filter(blockTag -> { + return tag.equalsIgnoreCase(blockTag.getTagName()); + }).findFirst(); + if (blockTagOptional.isPresent()) { + return true; + } + + } + return false; + } + @Override public void visit(JavadocComment n, Node arg) { super.visit(n, arg); 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 new file mode 100644 index 0000000000000000000000000000000000000000..b81afc3fad119cb7ffb85aed6487460d17661449 --- /dev/null +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Appendix.java @@ -0,0 +1,61 @@ +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/Node.java b/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Node.java index d887a0b738d136ead1ea7717ca9b4e2d8fe6669d..c3eb0a0a9cf4881cea312be3791689f9b87c171b 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 @@ -1,10 +1,10 @@ package com.kim.apidoc.core.schema; +import com.github.javaparser.ast.comments.Comment; +import com.github.javaparser.javadoc.Javadoc; import com.kim.apidoc.core.Context; import com.kim.apidoc.core.common.helper.CommentHelper; import com.kim.apidoc.core.common.helper.StringHelper; -import com.github.javaparser.ast.comments.Comment; -import com.github.javaparser.javadoc.Javadoc; import lombok.Getter; import lombok.Setter; @@ -120,7 +120,7 @@ public class Node implements Comparable { * @return the param tag */ public Optional getParamTag(String id) { - return Optional.ofNullable(tags.get("param:"+id)); + return Optional.ofNullable(tags.get("param:" + id)); } /** @@ -130,7 +130,7 @@ public class Node implements Comparable { */ public void putTag(Tag tag) { String id = tag.id; - if(StringHelper.nonBlank(tag.getKey())){ + if (StringHelper.nonBlank(tag.getKey())) { id += ":" + tag.getKey(); } tags.put(id, tag);