Commit f20179e3 authored by duanledexianxianxian's avatar duanledexianxianxian 😁

Refactoring code.

parent 8f321eae
......@@ -7,11 +7,11 @@ import java.util.Optional;
public class CompilationUnitHelper {
public static Optional<CompilationUnit> getCompilationUnit(Node node){
if(node instanceof CompilationUnit){
public static Optional<CompilationUnit> 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();
......
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<String, Row> 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;
}
}
......@@ -5,9 +5,9 @@ import java.util.Optional;
public class OptionalHelper {
@SafeVarargs
public static <T> Optional<T> any(Optional<T> ... optionals){
public static <T> Optional<T> any(Optional<T>... optionals) {
for (Optional<T> optional : optionals) {
if(optional.isPresent()){
if (optional.isPresent()) {
return optional;
}
}
......
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<Node> {
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<Node> {
*/
@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<Node> {
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")) {
}
}
}
......
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<String, Row> 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;
}
}
......@@ -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.
......
......@@ -22,7 +22,10 @@ import java.util.Optional;
@Setter
@Getter
public class Node implements Comparable<Node> {
/**
* 节点类型
*/
String type;
/**
* 节点编号
*/
......@@ -52,6 +55,9 @@ public class Node implements Comparable<Node> {
@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;
}
......
......@@ -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<Chapter> chapterOptional = books.get(bookName).getChapters().stream().filter(x -> NODE_APPENDIX.equals(x.getType())).findFirst();
if (chapterOptional.isPresent()) {
return chapterOptional.get();
}
}
return null;
}
}
......@@ -67,6 +67,20 @@ public class Section extends Node {
*/
Object rawResponse;
/**
* 全局编码列表
*/
Map<String, Row> codeRows = new LinkedHashMap<>();
/**
* Add code rows.
*
* @param row the row
*/
public void addCodeRows(Row row) {
codeRows.put(row.getKey(), row);
}
/**
* Add request row.
*
......
......@@ -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;
}
......
......@@ -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'
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment