Commit 678ef8b4 authored by duanledexianxianxian's avatar duanledexianxianxian 😁

代码重构

parent 8e6f9c27
package com.github.fengyuchenglun.apidoc.core; package com.github.fengyuchenglun.apidoc.core;
import com.github.fengyuchenglun.apidoc.core.common.Constants; import com.github.fengyuchenglun.apidoc.core.common.Constants;
import com.github.fengyuchenglun.apidoc.core.common.enums.FieldShowWay;
import com.github.fengyuchenglun.apidoc.core.render.AsciiDocRender; import com.github.fengyuchenglun.apidoc.core.render.AsciiDocRender;
import com.github.fengyuchenglun.apidoc.core.render.PostmanRender; import com.github.fengyuchenglun.apidoc.core.render.PostmanRender;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
...@@ -44,7 +45,6 @@ public class Context { ...@@ -44,7 +45,6 @@ public class Context {
public static final String DEFAULT_CODE_STRUCTURE = "src/main/java"; public static final String DEFAULT_CODE_STRUCTURE = "src/main/java";
/** /**
* 设置当前解析框架 * 设置当前解析框架
*/ */
...@@ -115,6 +115,14 @@ public class Context { ...@@ -115,6 +115,14 @@ public class Context {
@Setter @Setter
private String markdownTemplate = Constants.MARKDOWN_TEMPLATE; private String markdownTemplate = Constants.MARKDOWN_TEMPLATE;
/**
* 字段显示方式.
* 1. 平级(默认)
* 2. tree
*/
@Setter
private FieldShowWay fileShowWay= FieldShowWay.FLAT;
/** /**
* 自定义扩展参数 * 自定义扩展参数
*/ */
......
...@@ -2,18 +2,43 @@ package com.github.fengyuchenglun.apidoc.core.common; ...@@ -2,18 +2,43 @@ package com.github.fengyuchenglun.apidoc.core.common;
import com.google.common.base.Strings; import com.google.common.base.Strings;
/**
* The type Assert.
*
* @author duanledexianxianxian
*/
public class Assert { public class Assert {
/**
* Is blank boolean.
*
* @param text the text
* @return the boolean
*/
public static boolean isBlank(String text) { public static boolean isBlank(String text) {
return Strings.isNullOrEmpty(text) || Strings.isNullOrEmpty(text.trim()); return Strings.isNullOrEmpty(text) || Strings.isNullOrEmpty(text.trim());
} }
/**
* Not blank.
*
* @param text the text
* @param message the message
*/
public static void notBlank(String text, String message) { public static void notBlank(String text, String message) {
if (Strings.isNullOrEmpty(text) || Strings.isNullOrEmpty(text.trim())) { if (Strings.isNullOrEmpty(text) || Strings.isNullOrEmpty(text.trim())) {
throw new IllegalArgumentException(message); throw new IllegalArgumentException(message);
} }
} }
/**
* Between.
*
* @param num the num
* @param min the min
* @param max the max
* @param message the message
*/
public static void between(int num, int min, int max, String message) { public static void between(int num, int min, int max, String message) {
if (num < min || num > max) { if (num < min || num > max) {
throw new IllegalArgumentException(message); throw new IllegalArgumentException(message);
......
...@@ -31,5 +31,18 @@ public class Constants { ...@@ -31,5 +31,18 @@ public class Constants {
* 自定义tag-mock * 自定义tag-mock
*/ */
public static final String TAG_CUSTOM_JAVA_DOC_MOCK = "mock"; public static final String TAG_CUSTOM_JAVA_DOC_MOCK = "mock";
/**
* 自定义tag-data
*/
public static final String TAG_CUSTOM_JAVA_DOC_DATA = "data";
/**
* javadoc-tag-return
*/
public static final String TAG_JAVA_DOC_RETURN = "return";
/**
* 空格符
*/
public static final String FIELD_SPACE = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
} }
...@@ -7,8 +7,16 @@ import com.fasterxml.jackson.databind.node.ObjectNode; ...@@ -7,8 +7,16 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.Iterator; import java.util.Iterator;
/**
* The type Object mappers.
*
* @author duanledexianxianxian
*/
public class ObjectMappers { public class ObjectMappers {
/**
* The constant instance.
*/
public static final ObjectMapper instance; public static final ObjectMapper instance;
static { static {
...@@ -16,6 +24,12 @@ public class ObjectMappers { ...@@ -16,6 +24,12 @@ public class ObjectMappers {
instance.setSerializationInclusion(JsonInclude.Include.NON_NULL); instance.setSerializationInclusion(JsonInclude.Include.NON_NULL);
} }
/**
* Pretty string.
*
* @param node the node
* @return the string
*/
public static String pretty(Object node) { public static String pretty(Object node) {
try { try {
return instance.writerWithDefaultPrettyPrinter().writeValueAsString(node); return instance.writerWithDefaultPrettyPrinter().writeValueAsString(node);
...@@ -24,6 +38,13 @@ public class ObjectMappers { ...@@ -24,6 +38,13 @@ public class ObjectMappers {
} }
} }
/**
* Merge object node.
*
* @param result the result
* @param nodes the nodes
* @return the object node
*/
public static ObjectNode merge(ObjectNode result, ObjectNode... nodes) { public static ObjectNode merge(ObjectNode result, ObjectNode... nodes) {
if (result != null) { if (result != null) {
for (ObjectNode node : nodes) { for (ObjectNode node : nodes) {
......
...@@ -7,10 +7,25 @@ import org.apache.commons.lang3.ObjectUtils; ...@@ -7,10 +7,25 @@ import org.apache.commons.lang3.ObjectUtils;
import java.util.Iterator; import java.util.Iterator;
/**
* The type Query string builder.
*
* @author duanledexianxianxian
*/
public class QueryStringBuilder { public class QueryStringBuilder {
/**
* The Builder.
*/
private StringBuilder builder = new StringBuilder(); private StringBuilder builder = new StringBuilder();
/**
* Append query string builder.
*
* @param key the key
* @param value the value
* @return the query string builder
*/
public QueryStringBuilder append(Object key, Object value) { public QueryStringBuilder append(Object key, Object value) {
if (builder.length() > 0) { if (builder.length() > 0) {
builder.append("&"); builder.append("&");
...@@ -21,6 +36,12 @@ public class QueryStringBuilder { ...@@ -21,6 +36,12 @@ public class QueryStringBuilder {
return this; return this;
} }
/**
* Append query string builder.
*
* @param objectNode the object node
* @return the query string builder
*/
public QueryStringBuilder append(ObjectNode objectNode) { public QueryStringBuilder append(ObjectNode objectNode) {
Iterator<String> iterator = objectNode.fieldNames(); Iterator<String> iterator = objectNode.fieldNames();
while (iterator.hasNext()) { while (iterator.hasNext()) {
......
...@@ -9,25 +9,56 @@ import java.util.ArrayList; ...@@ -9,25 +9,56 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
/**
* The type Uri.
*
* @author duanledexianxianxian
*/
@Getter @Getter
@EqualsAndHashCode @EqualsAndHashCode
public class URI { public class URI {
/**
* The Text.
*/
private String text; private String text;
/**
* The Next.
*/
private URI next; private URI next;
/**
* Instantiates a new Uri.
*/
public URI() { public URI() {
} }
/**
* Instantiates a new Uri.
*
* @param text the text
*/
public URI(String text) { public URI(String text) {
this.text = text; this.text = text;
} }
/**
* Add uri.
*
* @param text the text
* @return the uri
*/
public URI add(String text) { public URI add(String text) {
return add(new URI(text)); return add(new URI(text));
} }
/**
* Add uri.
*
* @param uri the uri
* @return the uri
*/
public URI add(URI uri) { public URI add(URI uri) {
if (next != null) { if (next != null) {
next.add(uri); next.add(uri);
...@@ -37,6 +68,11 @@ public class URI { ...@@ -37,6 +68,11 @@ public class URI {
return this; return this;
} }
/**
* Remove.
*
* @param uri the uri
*/
public void remove(URI uri) { public void remove(URI uri) {
if (Objects.equals(next, uri)) { if (Objects.equals(next, uri)) {
next = null; next = null;
...@@ -59,6 +95,11 @@ public class URI { ...@@ -59,6 +95,11 @@ public class URI {
return builder.toString(); return builder.toString();
} }
/**
* Append to.
*
* @param list the list
*/
private void appendTo(List<String> list) { private void appendTo(List<String> list) {
if (Objects.nonNull(text)) { if (Objects.nonNull(text)) {
list.addAll(Lists.newArrayList(text.split("/"))); list.addAll(Lists.newArrayList(text.split("/")));
......
...@@ -2,71 +2,65 @@ package com.github.fengyuchenglun.apidoc.core.common.convert; ...@@ -2,71 +2,65 @@ package com.github.fengyuchenglun.apidoc.core.common.convert;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import java.sql.JDBCType;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
/** /**
* The enum Field type convert. * 字段转换.
* *
* @author duanledexianxianxian * @author duanledexianxianxian
*/ */
public enum FieldTypeConvert implements IFieldTypeConvert { public enum FieldTypeConvert implements IFieldTypeConvert {
/** /**
* String field type convert. * String.
*/ */
STRING("String", "String"), STRING("String", "String"),
/** /**
* Byte field type convert. * Byte.
*/ */
BYTE("Byte", "Byte"), BYTE("Byte", "Byte"),
/** /**
* Short field type convert. * Short.
*/ */
SHORT("Integer", "Short"), SHORT("Integer", "Short"),
/** /**
* Char field type convert. * Char.
*/ */
CHAR("String", "Char"), CHAR("String", "Char"),
/** /**
* Int field type convert. * Int.
*/ */
INT("Integer", "int"), INT("Integer", "int"),
/** /**
* Long field type convert. * Long.
*/ */
LONG("Long", "Long"), LONG("Long", "Long"),
/** /**
* Boolean field type convert. * Boolean.
*/ */
BOOLEAN("Boolean", "Boolean"), BOOLEAN("Boolean", "Boolean"),
/** /**
* Float field type convert. * Float.
*/ */
FLOAT("Float", "Float"), FLOAT("Float", "Float"),
/** /**
* Double field type convert. * Double.
*/ */
DOUBLE("Double", "Double"), DOUBLE("Double", "Double"),
/** /**
* Local date field type convert. * Local date.
*/ */
LOCAL_DATE("Date", "LocalDate"), LOCAL_DATE("Date", "LocalDate"),
/** /**
* Local date time field type convert. * Local date time.
*/ */
LOCAL_DATE_TIME("DateTime", "LocalDateTime"), LOCAL_DATE_TIME("DateTime", "LocalDateTime"),
/** /**
* Local time field type convert. * Local time.
*/ */
LOCAL_TIME("Time", "LocalTime"), LOCAL_TIME("Time", "LocalTime"),
/** /**
* Other field type convert. * Other.
*/ */
OTHER("object", "Object"), OTHER("object", "Object"),
; ;
......
...@@ -105,7 +105,7 @@ public class ArrayTypeDescription extends TypeDescription { ...@@ -105,7 +105,7 @@ public class ArrayTypeDescription extends TypeDescription {
@Override @Override
public String fullKey() { public String fullKey() {
return StringHelper.join("[].", prefix, key); return StringHelper.join(".", prefix, key);
} }
@Override @Override
......
package com.github.fengyuchenglun.apidoc.core.common.description; package com.github.fengyuchenglun.apidoc.core.common.description;
import com.github.fengyuchenglun.apidoc.core.common.ObjectMappers; import com.github.fengyuchenglun.apidoc.core.common.ObjectMappers;
import com.github.fengyuchenglun.apidoc.core.common.helper.StringHelper;
import com.github.fengyuchenglun.apidoc.core.schema.Row; import com.github.fengyuchenglun.apidoc.core.schema.Row;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
...@@ -121,6 +122,11 @@ public class ObjectTypeDescription extends TypeDescription { ...@@ -121,6 +122,11 @@ public class ObjectTypeDescription extends TypeDescription {
} }
} }
@Override
public String fullKey() {
return StringHelper.join(".", prefix, key);
}
@Override @Override
public ObjectNode getValue() { public ObjectNode getValue() {
return value; return value;
......
...@@ -5,6 +5,7 @@ import lombok.EqualsAndHashCode; ...@@ -5,6 +5,7 @@ import lombok.EqualsAndHashCode;
/** /**
* 字符串类型 * 字符串类型
* @author duanledexianxianxian
*/ */
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@Data @Data
......
...@@ -20,6 +20,7 @@ import java.util.HashMap; ...@@ -20,6 +20,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import static com.github.fengyuchenglun.apidoc.core.common.Constants.TAG_CUSTOM_JAVA_DOC_DATA;
import static com.github.fengyuchenglun.apidoc.core.common.Constants.TAG_CUSTOM_JAVA_DOC_MOCK; import static com.github.fengyuchenglun.apidoc.core.common.Constants.TAG_CUSTOM_JAVA_DOC_MOCK;
/** /**
...@@ -78,6 +79,12 @@ public abstract class TypeDescription { ...@@ -78,6 +79,12 @@ public abstract class TypeDescription {
*/ */
protected Boolean required = false; protected Boolean required = false;
/**
* The Level.
*/
protected Integer level = 0;
/** /**
* Is available boolean. * Is available boolean.
* *
...@@ -180,7 +187,7 @@ public abstract class TypeDescription { ...@@ -180,7 +187,7 @@ public abstract class TypeDescription {
if (remark == null) { if (remark == null) {
remark = value; remark = value;
} else { } else {
remark += " " + value; remark = StringUtils.isNotBlank(remark) ? remark : value;
} }
} }
...@@ -206,9 +213,9 @@ public abstract class TypeDescription { ...@@ -206,9 +213,9 @@ public abstract class TypeDescription {
return Lists.newArrayList(); return Lists.newArrayList();
} }
String def; String def;
if (null != tags.get(TAG_CUSTOM_JAVA_DOC_MOCK)) {
def = tags.get(TAG_CUSTOM_JAVA_DOC_MOCK).getContent();
} else if (defaultValue != null) { if (defaultValue != null) {
def = String.valueOf(defaultValue); def = String.valueOf(defaultValue);
} else if (value != null) { } else if (value != null) {
def = String.valueOf(value); def = String.valueOf(value);
...@@ -216,11 +223,20 @@ public abstract class TypeDescription { ...@@ -216,11 +223,20 @@ public abstract class TypeDescription {
def = ""; def = "";
} }
String mock = def;
if (null != tags.get(TAG_CUSTOM_JAVA_DOC_MOCK)) {
mock = tags.get(TAG_CUSTOM_JAVA_DOC_MOCK).getContent();
}
Boolean isData = false;
if (null != tags.get(TAG_CUSTOM_JAVA_DOC_DATA)) {
isData = true;
}
// if (required != null) { // if (required != null) {
// condition.append("required=").append(required); // condition.append("required=").append(required);
// } // }
return Lists.newArrayList(new Row(fullKey, type, required, condition.toString(), def, remark, parameterType)); return Lists.newArrayList(new Row(fullKey, type, required, condition.toString(), def, remark, mock, parameterType, isData));
} }
......
...@@ -15,6 +15,7 @@ import java.util.List; ...@@ -15,6 +15,7 @@ import java.util.List;
/** /**
* 文件对比工具 * 文件对比工具
* @author duanledexianxianxian
*/ */
@Getter @Getter
public class FileMatcher { public class FileMatcher {
......
...@@ -6,6 +6,9 @@ import java.io.IOException; ...@@ -6,6 +6,9 @@ import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
/**
* @author duanledexianxianxian
*/
public class FileSystem { public class FileSystem {
public static boolean open(Path path) { public static boolean open(Path path) {
......
package com.github.fengyuchenglun.apidoc.core.common.enums;
/**
* 字段显示方式
*
* @author duanledexianxianxian
*/
public enum FieldShowWay {
/**
* 平级方式.
*/
FLAT,
/**
* 树级目录.
*/
TREE
}
...@@ -7,6 +7,9 @@ import java.util.List; ...@@ -7,6 +7,9 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
/**
* @author duanledexianxianxian
*/
public class AnnotationHelper { public class AnnotationHelper {
public static boolean isAnnotationPresent(NodeWithAnnotations node, List<String> annotationNames) { public static boolean isAnnotationPresent(NodeWithAnnotations node, List<String> annotationNames) {
......
...@@ -9,6 +9,9 @@ import lombok.extern.slf4j.Slf4j; ...@@ -9,6 +9,9 @@ import lombok.extern.slf4j.Slf4j;
import java.util.Optional; import java.util.Optional;
/**
* @author duanledexianxianxian
*/
@Slf4j @Slf4j
public class ClassDeclarationHelper { public class ClassDeclarationHelper {
......
...@@ -17,6 +17,7 @@ import java.util.stream.Collectors; ...@@ -17,6 +17,7 @@ import java.util.stream.Collectors;
/** /**
* The type Comment helper. * The type Comment helper.
* @author duanledexianxianxian
*/ */
public class CommentHelper { public class CommentHelper {
......
...@@ -5,8 +5,18 @@ import com.github.javaparser.ast.Node; ...@@ -5,8 +5,18 @@ import com.github.javaparser.ast.Node;
import java.util.Optional; import java.util.Optional;
/**
* The type Compilation unit helper.
* @author duanledexianxianxian
*/
public class CompilationUnitHelper { public class CompilationUnitHelper {
/**
* Gets compilation unit.
*
* @param node the node
* @return the compilation unit
*/
public static Optional<CompilationUnit> getCompilationUnit(Node node) { public static Optional<CompilationUnit> getCompilationUnit(Node node) {
if (node instanceof CompilationUnit) { if (node instanceof CompilationUnit) {
return Optional.of((CompilationUnit) node); return Optional.of((CompilationUnit) node);
......
...@@ -14,6 +14,7 @@ import java.util.Optional; ...@@ -14,6 +14,7 @@ import java.util.Optional;
/** /**
* The type Field helper. * The type Field helper.
* @author duanledexianxianxian
*/ */
public class FieldHelper { public class FieldHelper {
......
...@@ -11,9 +11,19 @@ import java.nio.file.Path; ...@@ -11,9 +11,19 @@ import java.nio.file.Path;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/**
* The type File helper.
* @author duanledexianxianxian
*/
@Slf4j @Slf4j
public class FileHelper { public class FileHelper {
/**
* Write.
*
* @param file the file
* @param content the content
*/
public static void write(Path file, String content) { public static void write(Path file, String content) {
if (file.getParent() != null) { if (file.getParent() != null) {
...@@ -32,6 +42,13 @@ public class FileHelper { ...@@ -32,6 +42,13 @@ public class FileHelper {
} }
} }
/**
* Find list.
*
* @param start the start
* @param structure the structure
* @return the list
*/
public static List<Path> find(Path start, String structure) { public static List<Path> find(Path start, String structure) {
try { try {
return Files.walk(start) return Files.walk(start)
......
...@@ -8,12 +8,32 @@ import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParse ...@@ -8,12 +8,32 @@ import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParse
import java.util.Optional; import java.util.Optional;
/**
* The type Json property helper.
*
* @author duanledexianxianxian
*/
public class JsonPropertyHelper { public class JsonPropertyHelper {
/**
* The constant ANNOTAION_JSON_PROPERTY.
*/
public static final String ANNOTAION_JSON_PROPERTY = "JsonProperty"; public static final String ANNOTAION_JSON_PROPERTY = "JsonProperty";
/**
* The constant ANNOTAION_JSON_FIELD.
*/
public static final String ANNOTAION_JSON_FIELD = "JSONField"; public static final String ANNOTAION_JSON_FIELD = "JSONField";
/**
* The constant ANNOTAION_SERIALIZED_NAME.
*/
public static final String ANNOTAION_SERIALIZED_NAME = "SerializedName"; public static final String ANNOTAION_SERIALIZED_NAME = "SerializedName";
/**
* Get json name optional.
*
* @param declaredField the declared field
* @return the optional
*/
public static Optional<String> getJsonName(ResolvedFieldDeclaration declaredField){ public static Optional<String> getJsonName(ResolvedFieldDeclaration declaredField){
if(declaredField instanceof JavaParserFieldDeclaration){ if(declaredField instanceof JavaParserFieldDeclaration){
FieldDeclaration fieldDeclaration = ((JavaParserFieldDeclaration) declaredField).getWrappedNode(); FieldDeclaration fieldDeclaration = ((JavaParserFieldDeclaration) declaredField).getWrappedNode();
...@@ -26,6 +46,14 @@ public class JsonPropertyHelper { ...@@ -26,6 +46,14 @@ public class JsonPropertyHelper {
return Optional.empty(); return Optional.empty();
} }
/**
* Get string value optional.
*
* @param fieldDeclaration the field declaration
* @param anno the anno
* @param attr the attr
* @return the optional
*/
public static Optional<String> getStringValue(FieldDeclaration fieldDeclaration, String anno, String attr){ public static Optional<String> getStringValue(FieldDeclaration fieldDeclaration, String anno, String attr){
Optional<AnnotationExpr> optional = fieldDeclaration.getAnnotationByName(anno); Optional<AnnotationExpr> optional = fieldDeclaration.getAnnotationByName(anno);
if (optional.isPresent()) { if (optional.isPresent()) {
......
...@@ -6,6 +6,7 @@ import java.util.Set; ...@@ -6,6 +6,7 @@ import java.util.Set;
/** /**
* 解决循环依赖问题 * 解决循环依赖问题
* @author duanledexianxianxian
*/ */
public class ReferenceContext { public class ReferenceContext {
......
...@@ -2,6 +2,9 @@ package com.github.fengyuchenglun.apidoc.core.common.helper; ...@@ -2,6 +2,9 @@ package com.github.fengyuchenglun.apidoc.core.common.helper;
import com.google.common.base.Strings; import com.google.common.base.Strings;
/**
* @author duanledexianxianxian
*/
public class StringHelper { public class StringHelper {
public static boolean isBlank(String text) { public static boolean isBlank(String text) {
......
...@@ -6,8 +6,18 @@ import com.github.javaparser.ast.type.Type; ...@@ -6,8 +6,18 @@ import com.github.javaparser.ast.type.Type;
import java.util.Optional; import java.util.Optional;
/**
* The type Type name helper.
* @author duanledexianxianxian
*/
public class TypeNameHelper { public class TypeNameHelper {
/**
* Get name string.
*
* @param type the type
* @return the string
*/
public static String getName(Type type){ public static String getName(Type type){
String name = type.toString(); String name = type.toString();
if(type.isClassOrInterfaceType()){ if(type.isClassOrInterfaceType()){
...@@ -21,6 +31,13 @@ public class TypeNameHelper { ...@@ -21,6 +31,13 @@ public class TypeNameHelper {
return name; return name;
} }
/**
* Get name from import string.
*
* @param name the name
* @param compilationUnit the compilation unit
* @return the string
*/
private static String getNameFromImport(String name, CompilationUnit compilationUnit){ private static String getNameFromImport(String name, CompilationUnit compilationUnit){
int dotPos = name.indexOf('.'); int dotPos = name.indexOf('.');
String prefix = null; String prefix = null;
......
...@@ -9,6 +9,9 @@ import java.util.List; ...@@ -9,6 +9,9 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
/**
* @author duanledexianxianxian
*/
public class TypeParameterHelper { public class TypeParameterHelper {
/** /**
......
package com.github.fengyuchenglun.apidoc.core.common.markup.asciidoc; package com.github.fengyuchenglun.apidoc.core.common.markup.asciidoc;
/**
* The enum Ascii doc.
* @author duanledexianxianxian
*/
public enum AsciiDoc implements CharSequence { public enum AsciiDoc implements CharSequence {
/**
* Extension ascii doc.
*/
EXTENSION(".adoc"), EXTENSION(".adoc"),
/** /**
* 各种关键字 * 各种关键字
*/ */
HEADER("= "), HEADER("= "),
/**
* Table ascii doc.
*/
TABLE("|==="), TABLE("|==="),
/**
* Table cell ascii doc.
*/
TABLE_CELL("|"), TABLE_CELL("|"),
/**
* Title ascii doc.
*/
TITLE("="), TITLE("="),
/**
* Emphasized ascii doc.
*/
EMPHASIZED("_"), EMPHASIZED("_"),
/**
* Strong ascii doc.
*/
STRONG("*"), STRONG("*"),
/**
* Monospaced ascii doc.
*/
MONOSPACED("+"), MONOSPACED("+"),
/**
* Quoted ascii doc.
*/
QUOTED("`"), QUOTED("`"),
/**
* Double quoted ascii doc.
*/
DOUBLE_QUOTED("``"), DOUBLE_QUOTED("``"),
/**
* Unquoted ascii doc.
*/
UNQUOTED("#"), UNQUOTED("#"),
/**
* List flag ascii doc.
*/
LIST_FLAG("1. "), LIST_FLAG("1. "),
/**
* List flag letter ascii doc.
*/
LIST_FLAG_LETTER("a. "), LIST_FLAG_LETTER("a. "),
/**
* List flag letter upper ascii doc.
*/
LIST_FLAG_LETTER_UPPER("A. "), LIST_FLAG_LETTER_UPPER("A. "),
/**
* Listing ascii doc.
*/
LISTING("----"), LISTING("----"),
/**
* Literal ascii doc.
*/
LITERAL("...."), LITERAL("...."),
/**
* Sidebar ascii doc.
*/
SIDEBAR("****"), SIDEBAR("****"),
/**
* Comment ascii doc.
*/
COMMENT("////"), COMMENT("////"),
/**
* Passthrough ascii doc.
*/
PASSTHROUGH("++++"), PASSTHROUGH("++++"),
/**
* Quote ascii doc.
*/
QUOTE("____"), QUOTE("____"),
/**
* Example ascii doc.
*/
EXAMPLE("===="), EXAMPLE("===="),
/**
* Note ascii doc.
*/
NOTE("NOTE"), NOTE("NOTE"),
/**
* Tip ascii doc.
*/
TIP("TIP"), TIP("TIP"),
/**
* Important ascii doc.
*/
IMPORTANT("IMPORTANT"), IMPORTANT("IMPORTANT"),
/**
* Warning ascii doc.
*/
WARNING("WARNING"), WARNING("WARNING"),
/**
* Caution ascii doc.
*/
CAUTION("CAUTION"), CAUTION("CAUTION"),
/**
* Pagebreaks ascii doc.
*/
PAGEBREAKS("<<<"), PAGEBREAKS("<<<"),
/**
* Hardbreaks ascii doc.
*/
HARDBREAKS("[%hardbreaks]"), HARDBREAKS("[%hardbreaks]"),
/**
* Whitespace ascii doc.
*/
WHITESPACE(" "), WHITESPACE(" "),
/**
* Br ascii doc.
*/
BR("\r\n"), BR("\r\n"),
/**
* New line ascii doc.
*/
NEW_LINE("\r\n\r\n"), NEW_LINE("\r\n\r\n"),
/**
* Hbr ascii doc.
*/
HBR(" +"), HBR(" +"),
/** /**
* 文档属性 * 文档属性
*/ */
TOC(":toc:"), TOC(":toc:"),
/**
* Left ascii doc.
*/
LEFT("left"), LEFT("left"),
/**
* Toc level ascii doc.
*/
TOC_LEVEL(":toclevels:"), TOC_LEVEL(":toclevels:"),
/**
* Toc title ascii doc.
*/
TOC_TITLE(":toc-title:"), TOC_TITLE(":toc-title:"),
/**
* Doctype ascii doc.
*/
DOCTYPE(":doctype:"), DOCTYPE(":doctype:"),
/**
* Book ascii doc.
*/
BOOK("book"), BOOK("book"),
/**
* Source highlighter ascii doc.
*/
SOURCE_HIGHLIGHTER(":source-highlighter:"), SOURCE_HIGHLIGHTER(":source-highlighter:"),
/**
* Prettify ascii doc.
*/
PRETTIFY("prettify"), PRETTIFY("prettify"),
/**
* Highlightjs ascii doc.
*/
HIGHLIGHTJS("highlightjs"), HIGHLIGHTJS("highlightjs"),
/**
* Coderay ascii doc.
*/
CODERAY("coderay"), CODERAY("coderay"),
/** /**
* 文字样式 * 文字样式
*/ */
STYLE_BIG("big"), STYLE_BIG("big"),
/**
* Style small ascii doc.
*/
STYLE_SMALL("small"), STYLE_SMALL("small"),
/**
* Style underline ascii doc.
*/
STYLE_UNDERLINE("underline"), STYLE_UNDERLINE("underline"),
/**
* Style overline ascii doc.
*/
STYLE_OVERLINE("overline"), STYLE_OVERLINE("overline"),
/**
* Style line through ascii doc.
*/
STYLE_LINE_THROUGH("line-through"), STYLE_LINE_THROUGH("line-through"),
; ;
/**
* The Markup.
*/
private final String markup; private final String markup;
/**
* Instantiates a new Ascii doc.
*
* @param markup the markup
*/
AsciiDoc(final String markup) { AsciiDoc(final String markup) {
this.markup = markup; this.markup = markup;
} }
...@@ -85,6 +229,13 @@ public enum AsciiDoc implements CharSequence { ...@@ -85,6 +229,13 @@ public enum AsciiDoc implements CharSequence {
return markup; return markup;
} }
/**
* Attr char sequence.
*
* @param key the key
* @param value the value
* @return the char sequence
*/
public static CharSequence attr(AsciiDoc key, Object value){ public static CharSequence attr(AsciiDoc key, Object value){
return key.toString() + " " + String.valueOf(value); return key.toString() + " " + String.valueOf(value);
} }
......
...@@ -9,10 +9,21 @@ import java.util.Objects; ...@@ -9,10 +9,21 @@ import java.util.Objects;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.regex.Matcher; import java.util.regex.Matcher;
/**
* The type Ascii doc builder.
*
* @author duanledexianxianxian
*/
public class AsciiDocBuilder implements MarkupBuilder { public class AsciiDocBuilder implements MarkupBuilder {
/**
* The constant MAX_TITLE.
*/
public static final int MAX_TITLE = 6; public static final int MAX_TITLE = 6;
/**
* The Content.
*/
private StringBuilder content = new StringBuilder(); private StringBuilder content = new StringBuilder();
@Override @Override
...@@ -241,6 +252,14 @@ public class AsciiDocBuilder implements MarkupBuilder { ...@@ -241,6 +252,14 @@ public class AsciiDocBuilder implements MarkupBuilder {
} }
/**
* Style markup builder.
*
* @param flag the flag
* @param text the text
* @param textStyle the text style
* @return the markup builder
*/
public MarkupBuilder style(CharSequence flag, String text, CharSequence... textStyle) { public MarkupBuilder style(CharSequence flag, String text, CharSequence... textStyle) {
if (Assert.isBlank(text)) { if (Assert.isBlank(text)) {
return this; return this;
...@@ -324,6 +343,12 @@ public class AsciiDocBuilder implements MarkupBuilder { ...@@ -324,6 +343,12 @@ public class AsciiDocBuilder implements MarkupBuilder {
content = new StringBuilder(); content = new StringBuilder();
} }
/**
* Nobr string.
*
* @param content the content
* @return the string
*/
String nobr(String content) { String nobr(String content) {
if (Assert.isBlank(content)) { if (Assert.isBlank(content)) {
return content; return content;
......
...@@ -2,6 +2,7 @@ package com.github.fengyuchenglun.apidoc.core.common.markup.asciidoc; ...@@ -2,6 +2,7 @@ package com.github.fengyuchenglun.apidoc.core.common.markup.asciidoc;
/** /**
* https://en.wikipedia.org/wiki/Web_colors#HTML_color_names * https://en.wikipedia.org/wiki/Web_colors#HTML_color_names
* @author duanledexianxianxian
*/ */
public enum Color implements CharSequence{ public enum Color implements CharSequence{
WHITE("white"), WHITE("white"),
......
package com.github.fengyuchenglun.apidoc.core.common.markup.markdown;
public enum Markdown implements CharSequence {
EXTENSION(".md"),
/**
* 各种关键字
*/
HEADER("# "),
TABLE_CELL("|"),
TABLE_ROW("-"),
TABLE_Header("---|"),
TITLE("#"),
EMPHASIZED("**"),
STRONG("**"),
MONOSPACED("`"),
QUOTED("**"),
DOUBLE_QUOTED("**"),
UNQUOTED("**"),
LIST_FLAG("* "),
LISTING("```"),
LITERAL("```"),
SIDEBAR("```"),
COMMENT("```"),
PASSTHROUGH("```"),
QUOTE("> "),
EXAMPLE("===="),
NOTE("NOTE"),
TIP("TIP"),
IMPORTANT("IMPORTANT"),
WARNING("WARNING"),
CAUTION("CAUTION"),
PAGEBREAKS(" "),
WHITESPACE(" "),
BR("\r\n"),
NEW_LINE("\r\n\r\n"),
HBR(" +"),
;
private final String markup;
Markdown(final String markup) {
this.markup = markup;
}
@Override
public int length() {
return markup.length();
}
@Override
public char charAt(int index) {
return markup.charAt(index);
}
@Override
public CharSequence subSequence(int start, int end) {
return markup.subSequence(start, end);
}
@Override
public String toString() {
return markup;
}
public static CharSequence attr(Markdown key, Object value){
return key.toString() + " " + String.valueOf(value);
}
}
package com.github.fengyuchenglun.apidoc.core.common.markup.markdown;
import com.github.fengyuchenglun.apidoc.core.common.Assert;
import com.github.fengyuchenglun.apidoc.core.common.markup.MarkupBuilder;
import com.google.common.base.Strings;
import java.util.List;
import java.util.function.Consumer;
import java.util.regex.Matcher;
public class MarkdownBuilder implements MarkupBuilder {
public static final int MAX_TITLE = 6;
private StringBuilder content = new StringBuilder();
@Override
public MarkupBuilder header(String text, CharSequence... attrs) {
Assert.notBlank(text, "header must not be blank");
content.append(Markdown.HEADER);
content.append(nobr(text.trim()));
br();
return this;
}
@Override
public MarkupBuilder title(int level, String text) {
Assert.notBlank(text, "header must not be blank");
Assert.between(level, 1, MAX_TITLE, "title level can not be " + level);
br();
content.append(Strings.repeat(Markdown.TITLE.toString(), level + 1)).append(Markdown.WHITESPACE)
.append(nobr(text.trim()));
br();
return this;
}
@Override
public MarkupBuilder text(String text) {
if (Assert.isBlank(text)) {
return this;
}
content.append(text.trim());
return this;
}
@Override
public MarkupBuilder textLine(String text) {
if (Assert.isBlank(text)) {
return this;
}
text(nobr(text));
br();
return this;
}
@Override
public MarkupBuilder paragraph(String text, CharSequence... attrs) {
if (Assert.isBlank(text)) {
return this;
}
text(text);
newLine();
return this;
}
@Override
public MarkupBuilder note(String text) {
content.append(Markdown.QUOTE);
paragraph(text);
return this;
}
@Override
public MarkupBuilder tip(String text) {
content.append(Markdown.QUOTE);
paragraph(text);
return this;
}
@Override
public MarkupBuilder important(String text) {
content.append(Markdown.QUOTE);
paragraph(text);
return this;
}
@Override
public MarkupBuilder warning(String text) {
content.append(Markdown.QUOTE);
paragraph(text);
return this;
}
@Override
public MarkupBuilder caution(String text) {
content.append(Markdown.QUOTE);
paragraph(text);
return this;
}
@Override
public MarkupBuilder block(Consumer<MarkupBuilder> consumer, CharSequence flag, CharSequence... attrs) {
content.append(flag);
br();
consumer.accept(this);
br();
content.append(flag);
newLine();
return this;
}
@Override
public MarkupBuilder listing(Consumer<MarkupBuilder> consumer, CharSequence... attrs) {
return block(consumer, Markdown.LISTING, attrs);
}
@Override
public MarkupBuilder literal(Consumer<MarkupBuilder> consumer, CharSequence... attrs) {
return block(consumer, Markdown.LISTING, attrs);
}
@Override
public MarkupBuilder sidebar(Consumer<MarkupBuilder> consumer, CharSequence... attrs) {
return block(consumer, Markdown.LISTING, attrs);
}
@Override
public MarkupBuilder comment(Consumer<MarkupBuilder> consumer, CharSequence... attrs) {
return block(consumer, Markdown.LISTING, attrs);
}
@Override
public MarkupBuilder passthrough(Consumer<MarkupBuilder> consumer, CharSequence... attrs) {
return block(consumer, Markdown.LISTING, attrs);
}
@Override
public MarkupBuilder quote(Consumer<MarkupBuilder> consumer, CharSequence... attrs) {
return block(consumer, Markdown.LISTING, attrs);
}
@Override
public MarkupBuilder example(Consumer<MarkupBuilder> consumer, CharSequence... attrs) {
return block(consumer, Markdown.LISTING, attrs);
}
@Override
public MarkupBuilder list(String text) {
return list(text, Markdown.LIST_FLAG);
}
@Override
public MarkupBuilder list(String text, CharSequence flag) {
if (!Assert.isBlank(text)) {
content.append(flag).append(nobr(text));
}
return this;
}
@Override
public MarkupBuilder url(String text, String url) {
if (!Assert.isBlank(text) && !Assert.isBlank(url)) {
content.append("[").append(nobr(text)).append("](").append(url).append(")");
br();
}
return this;
}
@Override
public MarkupBuilder image(String text, String url) {
if (!Assert.isBlank(text) && !Assert.isBlank(url)) {
text("!");
url(text, url);
}
return this;
}
@Override
public MarkupBuilder table(List<List<String>> data) {
return table(data, true, false);
}
@Override
public MarkupBuilder table(List<List<String>> data, boolean header, boolean footer) {
for (int i = 0; i < data.size(); i++) {
content.append(Markdown.TABLE_CELL);
for (int j = 0; j < data.get(i).size(); j++) {
String value = data.get(i).get(j);
if(value!=null){
content.append(data.get(i).get(j).replace(Markdown.TABLE_CELL, "\\" + Markdown.TABLE_CELL));
}else{
content.append(" ");
}
content.append(Markdown.TABLE_CELL);
}
br();
if(i==0 && header){
content.append(Markdown.TABLE_CELL);
for (int j = 0; j < data.get(i).size(); j++) {
content.append(Markdown.TABLE_Header);
}
br();
}
if(i==data.size()-2 && footer){
content.append(Markdown.TABLE_CELL);
for (int j = 0; j < data.get(i).size(); j++) {
content.append(Markdown.TABLE_Header);
}
br();
}
}
newLine();
return this;
}
public MarkupBuilder style(CharSequence flag, String text, CharSequence... textStyle) {
if (Assert.isBlank(text)) {
return this;
}
content.append(flag);
text(text);
content.append(flag);
return this;
}
@Override
public MarkupBuilder emphasized(String text, CharSequence... textStyle) {
return style(Markdown.EMPHASIZED, text, textStyle);
}
@Override
public MarkupBuilder strong(String text, CharSequence... textStyle) {
return style(Markdown.STRONG, text, textStyle);
}
@Override
public MarkupBuilder monospaced(String text, CharSequence... textStyle) {
return style(Markdown.MONOSPACED, text, textStyle);
}
@Override
public MarkupBuilder quoted(String text, CharSequence... textStyle) {
return style(Markdown.QUOTE, text, textStyle);
}
@Override
public MarkupBuilder doubleQuoted(String text, CharSequence... textStyle) {
return style(Markdown.DOUBLE_QUOTED, text, textStyle);
}
@Override
public MarkupBuilder unquoted(String text, CharSequence... textStyle) {
return style(Markdown.UNQUOTED, text, textStyle);
}
@Override
public MarkupBuilder br() {
content.append(Markdown.BR);
return this;
}
@Override
public MarkupBuilder hbr() {
content.append(Markdown.HBR);
return this;
}
@Override
public MarkupBuilder newLine() {
content.append(Markdown.NEW_LINE);
return this;
}
@Override
public MarkupBuilder pageBreak() {
content.append(Markdown.PAGEBREAKS);
br();
return this;
}
@Override
public String getContent() {
return content.toString();
}
@Override
public void clean() {
content = new StringBuilder();
}
String nobr(String content) {
if (Assert.isBlank(content)) {
return content;
}
return content.replaceAll(Markdown.BR.toString(),
Matcher.quoteReplacement(Markdown.WHITESPACE.toString()));
}
}
...@@ -10,6 +10,11 @@ import com.google.common.collect.ImmutableList; ...@@ -10,6 +10,11 @@ import com.google.common.collect.ImmutableList;
import java.util.Optional; import java.util.Optional;
/**
* The type Collection type resolver.
*
* @author duanledexianxianxian
*/
public class CollectionTypeResolver implements TypeResolver { public class CollectionTypeResolver implements TypeResolver {
@Override @Override
public boolean accept(ResolvedType type) { public boolean accept(ResolvedType type) {
...@@ -24,6 +29,12 @@ public class CollectionTypeResolver implements TypeResolver { ...@@ -24,6 +29,12 @@ public class CollectionTypeResolver implements TypeResolver {
.orElseGet(UnAvailableTypeDescription::new)); .orElseGet(UnAvailableTypeDescription::new));
} }
/**
* Is collection boolean.
*
* @param type the type
* @return the boolean
*/
private static boolean isCollection(ResolvedType type){ private static boolean isCollection(ResolvedType type){
if(!type.isReferenceType()){ if(!type.isReferenceType()){
return false; return false;
...@@ -31,6 +42,12 @@ public class CollectionTypeResolver implements TypeResolver { ...@@ -31,6 +42,12 @@ public class CollectionTypeResolver implements TypeResolver {
return isCollection(type.asReferenceType().getId()); return isCollection(type.asReferenceType().getId());
} }
/**
* Is collection boolean.
*
* @param id the id
* @return the boolean
*/
private static boolean isCollection(String id){ private static boolean isCollection(String id){
return ImmutableList.of("java.util.List", return ImmutableList.of("java.util.List",
"java.util.Collection", "java.util.Collection",
......
...@@ -5,6 +5,11 @@ import com.github.fengyuchenglun.apidoc.core.common.description.StringTypeDescri ...@@ -5,6 +5,11 @@ import com.github.fengyuchenglun.apidoc.core.common.description.StringTypeDescri
import com.github.javaparser.resolution.types.ResolvedType; import com.github.javaparser.resolution.types.ResolvedType;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
/**
* The type Date type resolver.
*
* @author duanledexianxianxian
*/
public class DateTypeResolver implements TypeResolver { public class DateTypeResolver implements TypeResolver {
@Override @Override
public boolean accept(ResolvedType type) { public boolean accept(ResolvedType type) {
...@@ -16,6 +21,12 @@ public class DateTypeResolver implements TypeResolver { ...@@ -16,6 +21,12 @@ public class DateTypeResolver implements TypeResolver {
return new StringTypeDescription(type.asReferenceType().getTypeDeclaration().getName(),""); return new StringTypeDescription(type.asReferenceType().getTypeDeclaration().getName(),"");
} }
/**
* Is date boolean.
*
* @param type the type
* @return the boolean
*/
private static boolean isDate(ResolvedType type){ private static boolean isDate(ResolvedType type){
if(!type.isReferenceType()){ if(!type.isReferenceType()){
return false; return false;
...@@ -23,6 +34,12 @@ public class DateTypeResolver implements TypeResolver { ...@@ -23,6 +34,12 @@ public class DateTypeResolver implements TypeResolver {
return isDate(type.asReferenceType().getId()); return isDate(type.asReferenceType().getId());
} }
/**
* Is date boolean.
*
* @param id the id
* @return the boolean
*/
private static boolean isDate(String id){ private static boolean isDate(String id){
return ImmutableList.of("java.util.Date", return ImmutableList.of("java.util.Date",
"java.time.LocalDateTime", "java.time.LocalDateTime",
......
...@@ -6,6 +6,11 @@ import com.github.javaparser.resolution.declarations.ResolvedEnumConstantDeclara ...@@ -6,6 +6,11 @@ import com.github.javaparser.resolution.declarations.ResolvedEnumConstantDeclara
import com.github.javaparser.resolution.declarations.ResolvedEnumDeclaration; import com.github.javaparser.resolution.declarations.ResolvedEnumDeclaration;
import com.github.javaparser.resolution.types.ResolvedType; import com.github.javaparser.resolution.types.ResolvedType;
/**
* The type Enum type resolver.
*
* @author duanledexianxianxian
*/
public class EnumTypeResolver implements TypeResolver { public class EnumTypeResolver implements TypeResolver {
@Override @Override
public boolean accept(ResolvedType type) { public boolean accept(ResolvedType type) {
......
...@@ -7,8 +7,16 @@ import com.google.common.collect.ImmutableList; ...@@ -7,8 +7,16 @@ import com.google.common.collect.ImmutableList;
/** /**
* 不支持直接使用Map,建议使用DTO * 不支持直接使用Map,建议使用DTO
*
* @author duanledexianxianxian
*/ */
public class MapTypeResolver implements TypeResolver { public class MapTypeResolver implements TypeResolver {
/**
* Is map boolean.
*
* @param type the type
* @return the boolean
*/
private static boolean isMap(ResolvedType type) { private static boolean isMap(ResolvedType type) {
if (!type.isReferenceType()) { if (!type.isReferenceType()) {
return false; return false;
...@@ -16,6 +24,12 @@ public class MapTypeResolver implements TypeResolver { ...@@ -16,6 +24,12 @@ public class MapTypeResolver implements TypeResolver {
return isMap(type.asReferenceType().getId()); return isMap(type.asReferenceType().getId());
} }
/**
* Is map boolean.
*
* @param id the id
* @return the boolean
*/
private static boolean isMap(String id) { private static boolean isMap(String id) {
return ImmutableList.of("java.util.Map", return ImmutableList.of("java.util.Map",
"java.util.HashMap", "java.util.HashMap",
......
...@@ -17,6 +17,8 @@ import java.util.Optional; ...@@ -17,6 +17,8 @@ import java.util.Optional;
/** /**
* java bean解析 * java bean解析
*
* @author duanledexianxianxian
*/ */
public class ObjectTypeResolver implements TypeResolver { public class ObjectTypeResolver implements TypeResolver {
...@@ -88,6 +90,12 @@ public class ObjectTypeResolver implements TypeResolver { ...@@ -88,6 +90,12 @@ public class ObjectTypeResolver implements TypeResolver {
} }
/**
* Sets default.
*
* @param declaredField the declared field
* @param fieldDescription the field description
*/
private void setDefault(ResolvedFieldDeclaration declaredField, TypeDescription fieldDescription) { private void setDefault(ResolvedFieldDeclaration declaredField, TypeDescription fieldDescription) {
Optional<Comment> optional = CommentHelper.getOptionalComment(declaredField); Optional<Comment> optional = CommentHelper.getOptionalComment(declaredField);
if (optional.isPresent()) { if (optional.isPresent()) {
......
...@@ -5,6 +5,11 @@ import com.github.fengyuchenglun.apidoc.core.common.description.StringTypeDescri ...@@ -5,6 +5,11 @@ import com.github.fengyuchenglun.apidoc.core.common.description.StringTypeDescri
import com.github.javaparser.resolution.types.ResolvedType; import com.github.javaparser.resolution.types.ResolvedType;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
/**
* The type String type resolver.
*
* @author duanledexianxianxian
*/
public class StringTypeResolver implements TypeResolver { public class StringTypeResolver implements TypeResolver {
@Override @Override
public boolean accept(ResolvedType type) { public boolean accept(ResolvedType type) {
...@@ -16,6 +21,12 @@ public class StringTypeResolver implements TypeResolver { ...@@ -16,6 +21,12 @@ public class StringTypeResolver implements TypeResolver {
return new StringTypeDescription("String",""); return new StringTypeDescription("String","");
} }
/**
* Is string boolean.
*
* @param type the type
* @return the boolean
*/
private static boolean isString(ResolvedType type){ private static boolean isString(ResolvedType type){
if(!type.isReferenceType()){ if(!type.isReferenceType()){
return false; return false;
...@@ -23,6 +34,12 @@ public class StringTypeResolver implements TypeResolver { ...@@ -23,6 +34,12 @@ public class StringTypeResolver implements TypeResolver {
return isString(type.asReferenceType().getId()); return isString(type.asReferenceType().getId());
} }
/**
* Is string boolean.
*
* @param id the id
* @return the boolean
*/
private static boolean isString(String id){ private static boolean isString(String id){
return ImmutableList.of("java.lang.String", return ImmutableList.of("java.lang.String",
"java.lang.CharSequence" "java.lang.CharSequence"
......
...@@ -4,6 +4,11 @@ import com.github.fengyuchenglun.apidoc.core.common.description.TypeDescription; ...@@ -4,6 +4,11 @@ import com.github.fengyuchenglun.apidoc.core.common.description.TypeDescription;
import com.github.fengyuchenglun.apidoc.core.common.description.UnAvailableTypeDescription; import com.github.fengyuchenglun.apidoc.core.common.description.UnAvailableTypeDescription;
import com.github.javaparser.resolution.types.ResolvedType; import com.github.javaparser.resolution.types.ResolvedType;
/**
* The type System object type resolver.
*
* @author duanledexianxianxian
*/
public class SystemObjectTypeResolver implements TypeResolver { public class SystemObjectTypeResolver implements TypeResolver {
@Override @Override
public boolean accept(ResolvedType type) { public boolean accept(ResolvedType type) {
...@@ -15,6 +20,12 @@ public class SystemObjectTypeResolver implements TypeResolver { ...@@ -15,6 +20,12 @@ public class SystemObjectTypeResolver implements TypeResolver {
return new UnAvailableTypeDescription(); return new UnAvailableTypeDescription();
} }
/**
* Is system boolean.
*
* @param type the type
* @return the boolean
*/
private static boolean isSystem(ResolvedType type){ private static boolean isSystem(ResolvedType type){
if(!type.isReferenceType()){ if(!type.isReferenceType()){
return false; return false;
...@@ -22,6 +33,12 @@ public class SystemObjectTypeResolver implements TypeResolver { ...@@ -22,6 +33,12 @@ public class SystemObjectTypeResolver implements TypeResolver {
return isSystem(type.asReferenceType().getId()); return isSystem(type.asReferenceType().getId());
} }
/**
* Is system boolean.
*
* @param id the id
* @return the boolean
*/
private static boolean isSystem(String id){ private static boolean isSystem(String id){
return id!=null && (id.startsWith("java") ||id.startsWith("sun")); return id!=null && (id.startsWith("java") ||id.startsWith("sun"));
} }
......
package com.github.fengyuchenglun.apidoc.core.schema; package com.github.fengyuchenglun.apidoc.core.schema;
import com.github.fengyuchenglun.apidoc.core.ApiDoc;
import com.github.fengyuchenglun.apidoc.core.common.convert.FieldTypeConvert; import com.github.fengyuchenglun.apidoc.core.common.convert.FieldTypeConvert;
import com.github.fengyuchenglun.apidoc.core.common.convert.IFieldTypeConvert; import com.github.fengyuchenglun.apidoc.core.common.convert.IFieldTypeConvert;
import com.github.fengyuchenglun.apidoc.core.common.enums.FieldShowWay;
import lombok.*; import lombok.*;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import static com.github.fengyuchenglun.apidoc.core.common.Constants.FIELD_SPACE;
/** /**
* The type Row. * The type Row.
* *
...@@ -41,12 +45,21 @@ public class Row { ...@@ -41,12 +45,21 @@ public class Row {
* 说明. * 说明.
*/ */
String remark; String remark;
/**
* mock值
*/
String mock;
/** /**
* 请求参数类型 * 请求参数类型
*/ */
String parameterType; String parameterType;
/**
* 是否为统一结果
*/
Boolean isData;
/** /**
* Instantiates a new Row. * Instantiates a new Row.
...@@ -75,17 +88,41 @@ public class Row { ...@@ -75,17 +88,41 @@ public class Row {
* @return the label type * @return the label type
*/ */
public String getLabelType() { public String getLabelType() {
String javaType=this.type; String javaType = this.type;
Boolean isArray=false; Boolean isArray = false;
if (StringUtils.endsWith(javaType,"[]")){ if (StringUtils.endsWith(javaType, "[]")) {
javaType=StringUtils.substringBefore(this.type,"[]"); javaType = StringUtils.substringBefore(this.type, "[]");
isArray=true; isArray = true;
}
IFieldTypeConvert fieldTypeConvert = FieldTypeConvert.javaTypeOf(javaType);
if (null == fieldTypeConvert) {
return this.type;
} }
IFieldTypeConvert fieldTypeConvert=FieldTypeConvert.javaTypeOf(javaType); return isArray ? fieldTypeConvert.getType() + "[]" : fieldTypeConvert.getType();
if (null==fieldTypeConvert){ }
return this.type;
/**
* Gets markdown key.
*
* @return the markdown key
*/
public String getMarkdownKey() {
if (StringUtils.isNotBlank(this.key)) {
if (FieldShowWay.TREE.equals(ApiDoc.getInstance().getContext().getFileShowWay())) {
String[] keys = StringUtils.split(this.key, ".");
if (keys.length > 1) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < keys.length - 2; i++) {
sb.append(FIELD_SPACE);
}
sb.append("└─");
sb.append(keys[keys.length - 1]);
return sb.toString();
}
}
} }
return isArray?fieldTypeConvert.getType()+"[]":fieldTypeConvert.getType(); return this.key;
} }
} }
...@@ -13,6 +13,8 @@ import org.apache.commons.lang3.StringUtils; ...@@ -13,6 +13,8 @@ import org.apache.commons.lang3.StringUtils;
import java.util.*; import java.util.*;
import static com.github.fengyuchenglun.apidoc.core.common.Constants.TAG_JAVA_DOC_RETURN;
/** /**
* 小节,一个请求,封装为一个小节 * 小节,一个请求,封装为一个小节
* *
...@@ -77,6 +79,10 @@ public class Section extends Node { ...@@ -77,6 +79,10 @@ public class Section extends Node {
* The Raw response. * The Raw response.
*/ */
Object rawResponse; Object rawResponse;
/**
* 是否为统一结果返回
*/
Boolean isResultData=false;
/** /**
* Sets parameter. * Sets parameter.
...@@ -180,7 +186,14 @@ public class Section extends Node { ...@@ -180,7 +186,14 @@ public class Section extends Node {
* @return the query parameter string * @return the query parameter string
*/ */
public String getQueryParameterString() { public String getQueryParameterString() {
return new QueryStringBuilder().append((ObjectNode) queryParameters).toString(); String qs = new QueryStringBuilder().append((ObjectNode) queryParameters).toString();
qs = StringUtils.replace(qs, "[\"\"]", "1,2,3");
qs = StringUtils.replace(qs, "[0]", "1,2,3");
qs = StringUtils.replace(qs, "[0.0]", "1.0,2.0,3.0");
qs = StringUtils.replace(qs, "[false]", "false,true,false");
return qs;
} }
/** /**
...@@ -222,11 +235,15 @@ public class Section extends Node { ...@@ -222,11 +235,15 @@ public class Section extends Node {
public void addResponseRows(Collection<Row> rows) { public void addResponseRows(Collection<Row> rows) {
for (Row row : rows) { for (Row row : rows) {
if (row.getKey() != null && !responseRows.containsKey(row.getKey())) { if (row.getKey() != null && !responseRows.containsKey(row.getKey())) {
if (row.getIsData()) {
Optional.ofNullable(this.tags.get(TAG_JAVA_DOC_RETURN)).ifPresent(x -> row.setRemark(x.content));
}
responseRows.put(row.getKey(), row); responseRows.put(row.getKey(), row);
} }
} }
} }
/** /**
* Has response body boolean. * Has response body boolean.
* *
......
...@@ -40,18 +40,23 @@ ${section.description} ...@@ -40,18 +40,23 @@ ${section.description}
<#-- 请求示例--> <#-- 请求示例-->
```HTTP ```HTTP
${section.method} ${section.uri!''} HTTP/1.1 HTTP/1.1
<#if section.inHeaders??> <#if section.inHeaders??>
<#list section.inHeaders as inHeaderKey,inHeaderValue> <#list section.inHeaders as inHeaderKey,inHeaderValue>
${inHeaderValue!''} ${inHeaderValue!''}
</#list> </#list>
</#if> </#if>
<#if (section.getQueryParameterString()?length gt 1)>
Url:
${section.method} ${section.uri!''}
${section.getQueryParameterString()} <#if (section.getQueryParameterString()?length gt 1)>
Query param:
?${section.getQueryParameterString()}
</#if> </#if>
<#if section.hasRequestBody()> <#if section.hasRequestBody()>
Request body:
${section.getRequestBodyParameterString()} ${section.getRequestBodyParameterString()}
</#if> </#if>
``` ```
...@@ -61,7 +66,7 @@ ${section.getRequestBodyParameterString()} ...@@ -61,7 +66,7 @@ ${section.getRequestBodyParameterString()}
| 字段 | 类型 | 参数类型 | 必填 | 验证 | 默认值 | 描述 | | 字段 | 类型 | 参数类型 | 必填 | 验证 | 默认值 | 描述 |
| :------- | :----- | :----- |:-------- |:-------- | :------ | :---------- | | :------- | :----- | :----- |:-------- |:-------- | :------ | :---------- |
<#list section.requestRows as rowKey,rowValue> <#list section.requestRows as rowKey,rowValue>
| ${rowValue.key!''} | ${rowValue.getLabelType()!''} | **${rowValue.parameterType!''}** |${rowValue.required?string('true','false')} | ${rowValue.condition!''} | ${rowValue.def!''} | ${rowValue.getHtmlRemark()!''} | | ${rowValue.getMarkdownKey()!''} | ${rowValue.getLabelType()!''} | **${rowValue.parameterType!''}** |${rowValue.required?string('true','false')} | ${rowValue.condition!''} | ${rowValue.def!''} | ${rowValue.getHtmlRemark()!''} |
</#list> </#list>
</#if> </#if>
<#-- 响应--> <#-- 响应-->
...@@ -74,10 +79,10 @@ ${section.getResponseString()} ...@@ -74,10 +79,10 @@ ${section.getResponseString()}
<#-- 响应参数table列表--> <#-- 响应参数table列表-->
<#if section.responseRows?? && (section.responseRows?size>0)> <#if section.responseRows?? && (section.responseRows?size>0)>
| 字段 | 类型 | 默认值 | 描述 | | 字段 | 类型 | 默认值 | 示例 | 描述 |
| :------- | :----- |:----- | :---------- | | :------- | :----- |:----- |:----- | :---------- |
<#list section.responseRows as rowKey,rowValue> <#list section.responseRows as rowKey,rowValue>
| ${rowValue.key!''} | ${rowValue.getLabelType()!''} | ${rowValue.def!''} | ${rowValue.getHtmlRemark()!''} | | ${rowValue.getMarkdownKey()!''} | ${rowValue.getLabelType()!''} | ${rowValue.def!''} | ${rowValue.mock!''} | ${rowValue.getHtmlRemark()!''} |
</#list> </#list>
</#if> </#if>
</#if> </#if>
......
...@@ -5,6 +5,7 @@ import com.github.javaparser.ast.body.Parameter; ...@@ -5,6 +5,7 @@ import com.github.javaparser.ast.body.Parameter;
/** /**
* The type Parameter helper. * The type Parameter helper.
* @author duanledexianxianxian
*/ */
public class ParameterHelper { public class ParameterHelper {
...@@ -60,7 +61,7 @@ public class ParameterHelper { ...@@ -60,7 +61,7 @@ public class ParameterHelper {
* 是否是请求参数 * 是否是请求参数
* *
* @param parameter the parameter * @param parameter the parameter
* @return boolean * @return boolean boolean
*/ */
public static boolean isRequestParam(Parameter parameter) { public static boolean isRequestParam(Parameter parameter) {
if (!parameter.isAnnotationPresent(ANNOTATION_PATH_VARIABLE) && if (!parameter.isAnnotationPresent(ANNOTATION_PATH_VARIABLE) &&
......
package com.github.fengyuchenglun.apidoc.springmvc; package com.github.fengyuchenglun.apidoc.springmvc;
import com.github.fengyuchenglun.apidoc.core.common.description.ArrayTypeDescription;
import com.github.fengyuchenglun.apidoc.core.common.helper.AnnotationHelper; import com.github.fengyuchenglun.apidoc.core.common.helper.AnnotationHelper;
import com.github.fengyuchenglun.apidoc.core.common.helper.ClassDeclarationHelper; import com.github.fengyuchenglun.apidoc.core.common.helper.ClassDeclarationHelper;
import com.github.fengyuchenglun.apidoc.core.common.helper.ExpressionHelper; import com.github.fengyuchenglun.apidoc.core.common.helper.ExpressionHelper;
...@@ -24,6 +25,7 @@ import com.github.fengyuchenglun.apidoc.springmvc.resovler.SpringComponentTypeRe ...@@ -24,6 +25,7 @@ import com.github.fengyuchenglun.apidoc.springmvc.resovler.SpringComponentTypeRe
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import static com.github.fengyuchenglun.apidoc.core.common.Constants.TAG_JAVA_DOC_RETURN;
import static com.github.fengyuchenglun.apidoc.core.schema.ParameterType.*; import static com.github.fengyuchenglun.apidoc.core.schema.ParameterType.*;
...@@ -95,6 +97,7 @@ public class SpringParser implements ParserStrategy { ...@@ -95,6 +97,7 @@ public class SpringParser implements ParserStrategy {
/** /**
* 解析方法定义 * 解析方法定义
*
* @param n 方法声明 * @param n 方法声明
* @param chapter 章 * @param chapter 章
* @param section 节 * @param section 节
...@@ -326,20 +329,36 @@ public class SpringParser implements ParserStrategy { ...@@ -326,20 +329,36 @@ public class SpringParser implements ParserStrategy {
returnType.setTypeArguments(n.getType()); returnType.setTypeArguments(n.getType());
n.findCompilationUnit().get().addImport(ClassDeclarationHelper.getClassOrInterfacePackageName(resultDataClassOrInterfaceDeclaration)); n.findCompilationUnit().get().addImport(ClassDeclarationHelper.getClassOrInterfacePackageName(resultDataClassOrInterfaceDeclaration));
n.setType(returnType); n.setType(returnType);
section.setIsResultData(true);
} }
TypeDescription description = ApiDoc.getInstance().getTypeResolvers().resolve(n.getType()); TypeDescription description = ApiDoc.getInstance().getTypeResolvers().resolve(n.getType());
if (description.isAvailable()) { if (description.isAvailable()) {
if (description.isPrimitive()) { if (description.isPrimitive()) {
section.setRawResponse(description.getValue()); section.setRawResponse(description.getValue());
handleResultData(section,description);
} else if (description.isString()) { } else if (description.isString()) {
section.setRawResponse(description.getValue()); section.setRawResponse(description.getValue());
handleResultData(section,description);
} else if (description.isArray()) { } else if (description.isArray()) {
ArrayTypeDescription asArray = description.asArray();
if (asArray.getComponent().isPrimitive() || asArray.getComponent().isString()) {
handleResultData(section,description);
}
section.setResponse(description.asArray().getValue()); section.setResponse(description.asArray().getValue());
} else if (description.isObject()) { } else if (description.isObject()) {
section.setResponse(description.asObject().getValue()); section.setResponse(description.asObject().getValue());
} }
section.addResponseRows(description.rows()); section.addResponseRows(description.rows());
} }
} }
private void handleResultData(Section section, TypeDescription description) {
if (!section.getIsResultData()) {
description.setKey("result");
section.getTag(TAG_JAVA_DOC_RETURN).ifPresent(x -> description.setRemark(x.getContent()));
}
}
} }
...@@ -51,9 +51,11 @@ public class Query { ...@@ -51,9 +51,11 @@ public class Query {
// /** // /**
// * 结束时间 // * 结束时间
// * 2020-07-25 01:00:00 // * 2020-07-25 01:00:00
// */ //
// private LocalDateTime endTime; //
private LocalDateTime endTime;
private List<Menu> menuList; private List<Query> queryList;
} }
...@@ -26,6 +26,8 @@ public class ResultData<T> { ...@@ -26,6 +26,8 @@ public class ResultData<T> {
String msg; String msg;
/** /**
* 数据对象. * 数据对象.
*
* @data
*/ */
T data; T data;
......
...@@ -20,40 +20,32 @@ import java.util.List; ...@@ -20,40 +20,32 @@ import java.util.List;
@Getter @Getter
public class User { public class User {
// /**
// * 用户编号.
// */
// int id;
// /**
// * 用户名称.
// */
// @NotBlank
// String name;
// /**
// * 用户年龄.
// */
// @Min(1)
// @NotNull
// Integer age;
// /**
// * 创建时间.
// */
// Date createAt;
// /**
// * 性别.
// */
// @NotBlank
// @JsonProperty("Sex")
// String sex;
/** /**
* 用户. * 用户编号.
*/ */
private UserQuery query; int id;
/**
* 用户名称.
*/
@NotBlank
String name;
/**
* 用户年龄.
* @mock 20
*/
@Min(1)
@NotNull
Integer age;
/**
* 创建时间.
* @mock 2020-07-28
*/
Date createAt;
/** /**
* 用户. * 性别.
*/ */
private List<Menu> menus; @NotBlank
@JsonProperty("Sex")
String sex;
} }
...@@ -27,133 +27,170 @@ public class UserController { ...@@ -27,133 +27,170 @@ public class UserController {
* 查看用户详情 * 查看用户详情
* *
* @param userId 用户编号 * @param userId 用户编号
* @param age 年龄 * @return the user
* @param query 过滤条件
* @return 用户对象 user
*/ */
@PostMapping(value = "/{userId}") @PostMapping(value = "/{userId}")
public User detail(@PathVariable String userId, String age, @RequestBody Query query) { public User detail(@PathVariable("userId")Long userId) {
User user = new User();
return user;
}
/**
* 查看用户详情
* 测试get的query对象
*
* @param query 过滤条件
* @return 用户对象 user
*/
@GetMapping(value = "/detail1")
public User detail1(Query query) {
User user = new User();
return user;
}
/**
*
* 测试返回List
*
* @param query 过滤条件
* @return 用户对象 user
*/
@GetMapping(value = "/getList")
public List<User> getList(Query query) {
return null;
}
/**
*
* 测试返回List
*
* @param query 过滤条件
* @return 用户对象 user
*/
@GetMapping(value = "/getStringList")
public List<String> getStringList(Query query) {
return null;
}
/**
*
* 测试返回List
*
* @param query 过滤条件
* @return 用户对象 user
*/
@GetMapping(value = "/getStringArrayList")
public List<List<String>> getStringArrayList(Query query) {
return null;
}
/**
* 测试get的query对象,带RequestParam注解
*
* @param query the query
* @return 用户对象 user
*/
@GetMapping(value = "/detail3")
public User detail3(Query query) {
User user = new User(); User user = new User();
return user; return user;
} }
//
// /** /**
// * 查看用户详情 * 测试get的query对象,带RequestParam注解,required=false
// * 测试get的query对象 *
// * * @param query 过滤条件
// * @param query 过滤条件 * @return 用户对象 user
// * @return 用户对象 user */
// */ @GetMapping(value = "/detail4")
// @GetMapping(value = "/detail1") public User detail4(@RequestParam(required = false) String query) {
// public User detail1(Query query) { User user = new User();
// User user = new User(); return user;
// return user; }
// }
// /**
// * get-无查询参数
// *
// /** * @return 用户对象 user
// * 测试get的query对象,带RequestParam注解 */
// * @GetMapping(value = "/detail5")
// * @param query the query public List<User> detail5() {
// * @return 用户对象 user return null;
// */ }
// @GetMapping(value = "/detail3")
// public User detail3(Query query) { /**
// User user = new User(); * get-原始对象+查询对象
// return user; *
// } * @param userName the user name
// * @param age the age
// /** * @param query the query
// * 测试get的query对象,带RequestParam注解,required=false * @return 用户对象 user
// * */
// * @param query 过滤条件 @GetMapping(value = "/detail6")
// * @return 用户对象 user public User detail6(String userName, Integer age, UserQuery query) {
// */ User user = new User();
// @GetMapping(value = "/detail4") return user;
// public User detail4(@RequestParam(required = false) String query) { }
// User user = new User();
// return user; /**
// } * post-
// *
// /** * @return 用户对象 user
// * get-无查询参数 */
// * @GetMapping(value = "/post1")
// * @return 用户对象 user public UserQuery post1() {
// */ return null;
// @GetMapping(value = "/detail5") }
// public List<User> detail5() {
// return null;
// } /**
// * 删除用户
// /** *
// * get-原始对象+查询对象 * @param userId the user id
// * * @return boolean 是否成功
// * @param userName the user name */
// * @param age the age @DeleteMapping(value = "/{userId}")
// * @param query the query public Boolean deleteUser(@PathVariable("userId") Long userId) {
// * @return 用户对象 user return true;
// */ }
// @GetMapping(value = "/detail6")
// public User detail6(String userName, Integer age, UserQuery query) {
// User user = new User(); /**
// return user; * 用户表单对象.
// } *
// * @author duanledexianxianxian
// /** */
// * post- @Data
// * public static class UserForm implements Serializable {
// * @return 用户对象 user
// */ /**
// @GetMapping(value = "/post1") * The constant serialVersionUID.
// public UserQuery post1() { */
// return null; private static final long serialVersionUID = 5681371348688016281L;
// } /**
// * 用户名
// */
// /** private String userName;
// * 删除用户 /**
// * * 地址
// * @param userId the user id */
// * @return boolean 是否成功 private String address;
// */ /**
// @DeleteMapping(value = "/{userId}") * 年龄
// public Boolean deleteUser(@PathVariable("userId") Long userId) { */
// return true; private Integer age;
// } }
//
// /**
// /** * 添加用户
// * 用户表单对象. *
// * * @param form 用户表单对象
// * @author duanledexianxianxian * @return 返回记录 boolean
// */ */
// @Data @PostMapping
// public static class UserForm implements Serializable { public Boolean add(@RequestBody UserForm form) {
// return null;
// private static final long serialVersionUID = 5681371348688016281L; }
// /**
// * 用户名
// */
// private String userName;
// /**
// * 地址
// */
// private String address;
// /**
// * 年龄
// */
// private Integer age;
// }
//
// /**
// * 添加用户
// *
// * @param form 用户表单对象
// * @return integer 返回记录
// */
// @PostMapping
// public Integer add(@RequestBody UserForm form) {
// return null;
// }
} }
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