add ApiDataBuilder
This commit is contained in:
parent
68517fb32b
commit
552e52ccf5
|
@ -0,0 +1,39 @@
|
|||
package com.power.doc.builder;
|
||||
|
||||
import com.power.doc.model.ApiAllData;
|
||||
import com.power.doc.model.ApiConfig;
|
||||
import com.power.doc.model.ApiDoc;
|
||||
|
||||
/**
|
||||
* @author yu 2019/12/7.
|
||||
*/
|
||||
public class ApiDataBuilder {
|
||||
|
||||
/**
|
||||
* Get list of ApiDoc
|
||||
*
|
||||
* @param config ApiConfig
|
||||
* @return List of ApiDoc
|
||||
*/
|
||||
public static ApiAllData getApiData(ApiConfig config) {
|
||||
DocBuilderTemplate builderTemplate = new DocBuilderTemplate();
|
||||
builderTemplate.getApiData(config);
|
||||
builderTemplate.checkAndInitForGetApiData(config);
|
||||
return builderTemplate.getApiData(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get single api data
|
||||
*
|
||||
* @param config ApiConfig
|
||||
* @param controllerName controller name
|
||||
* @return ApiDoc
|
||||
*/
|
||||
public static ApiDoc getApiData(ApiConfig config, String controllerName) {
|
||||
DocBuilderTemplate builderTemplate = new DocBuilderTemplate();
|
||||
builderTemplate.checkAndInitForGetApiData(config);
|
||||
config.setMd5EncryptedHtmlName(true);
|
||||
SourceBuilder sourceBuilder = new SourceBuilder(config);
|
||||
return sourceBuilder.getSingleControllerApiData(controllerName);
|
||||
}
|
||||
}
|
|
@ -49,34 +49,4 @@ public class ApiDocBuilder {
|
|||
builderTemplate.checkAndInit(config);
|
||||
builderTemplate.buildSingleControllerApi(config.getOutPath(), controllerName, API_DOC_MD_TPL, API_EXTENSION);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of ApiDoc
|
||||
*
|
||||
* @param config ApiConfig
|
||||
* @return List of ApiDoc
|
||||
*/
|
||||
public static List<ApiDoc> listOfApiData(ApiConfig config) {
|
||||
DocBuilderTemplate builderTemplate = new DocBuilderTemplate();
|
||||
builderTemplate.checkAndInitForGetApiData(config);
|
||||
config.setMd5EncryptedHtmlName(true);
|
||||
SourceBuilder sourceBuilder = new SourceBuilder(config);
|
||||
return sourceBuilder.getControllerApiData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get single api data
|
||||
*
|
||||
* @param config ApiConfig
|
||||
* @param controllerName controller name
|
||||
* @return ApiDoc
|
||||
*/
|
||||
public static ApiDoc getApiData(ApiConfig config, String controllerName) {
|
||||
DocBuilderTemplate builderTemplate = new DocBuilderTemplate();
|
||||
builderTemplate.checkAndInitForGetApiData(config);
|
||||
config.setMd5EncryptedHtmlName(true);
|
||||
SourceBuilder sourceBuilder = new SourceBuilder(config);
|
||||
return sourceBuilder.getSingleControllerApiData(controllerName);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -9,6 +9,8 @@ import com.power.doc.constants.DocLanguage;
|
|||
import com.power.doc.constants.TemplateVariable;
|
||||
import com.power.doc.model.*;
|
||||
import com.power.doc.utils.BeetlTemplateUtil;
|
||||
import com.power.doc.utils.DocUtil;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.beetl.core.Template;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
@ -32,18 +34,10 @@ public class DocBuilderTemplate {
|
|||
* @param config Api config
|
||||
*/
|
||||
public void checkAndInit(ApiConfig config) {
|
||||
if (null == config) {
|
||||
throw new NullPointerException("ApiConfig can't be null");
|
||||
}
|
||||
this.checkAndInitForGetApiData(config);
|
||||
if (StringUtil.isEmpty(config.getOutPath())) {
|
||||
throw new RuntimeException("doc output path can't be null or empty");
|
||||
}
|
||||
if (null != config.getLanguage()) {
|
||||
System.setProperty(DocGlobalConstants.DOC_LANGUAGE, config.getLanguage().getCode());
|
||||
} else {
|
||||
//default is chinese
|
||||
System.setProperty(DocGlobalConstants.DOC_LANGUAGE, DocLanguage.CHINESE.getCode());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -63,6 +57,24 @@ public class DocBuilderTemplate {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* get all api data
|
||||
*
|
||||
* @param config
|
||||
* @return
|
||||
*/
|
||||
public ApiAllData getApiData(ApiConfig config) {
|
||||
ApiAllData apiAllData = new ApiAllData();
|
||||
apiAllData.setProjectName(config.getProjectName());
|
||||
apiAllData.setProjectId(DocUtil.handleId(config.getProjectName()));
|
||||
apiAllData.setLanguage(config.getLanguage().getCode());
|
||||
apiAllData.setApiDocList(listOfApiData(config));
|
||||
apiAllData.setErrorCodeList(errorCodeDictToList(config));
|
||||
apiAllData.setRevisionLogs(config.getRevisionLogs());
|
||||
apiAllData.setApiDocDictList(buildDictionary(config));
|
||||
return apiAllData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate api documentation for all controllers.
|
||||
*
|
||||
|
@ -100,10 +112,8 @@ public class DocBuilderTemplate {
|
|||
String outPath = config.getOutPath();
|
||||
String strTime = DateTimeUtil.long2Str(now, DateTimeUtil.DATE_FORMAT_SECOND);
|
||||
FileUtil.mkdirs(outPath);
|
||||
List<ApiErrorCode> errorCodeList = config.getErrorCodes();
|
||||
if (CollectionUtil.isEmpty(errorCodeList)) {
|
||||
errorCodeList = errorCodeDictToList(config);
|
||||
}
|
||||
List<ApiErrorCode> errorCodeList = errorCodeDictToList(config);
|
||||
|
||||
Template tpl = BeetlTemplateUtil.getByName(template);
|
||||
tpl.binding(TemplateVariable.API_DOC_LIST.getVariable(), apiDocList);
|
||||
tpl.binding(TemplateVariable.ERROR_CODE_LIST.getVariable(), errorCodeList);
|
||||
|
@ -142,10 +152,7 @@ public class DocBuilderTemplate {
|
|||
* @param outPutFileName output file
|
||||
*/
|
||||
public void buildErrorCodeDoc(ApiConfig config, String template, String outPutFileName) {
|
||||
List<ApiErrorCode> errorCodeList = config.getErrorCodes();
|
||||
if (CollectionUtil.isEmpty(errorCodeList)) {
|
||||
errorCodeList = errorCodeDictToList(config);
|
||||
}
|
||||
List<ApiErrorCode> errorCodeList = errorCodeDictToList(config);
|
||||
Template mapper = BeetlTemplateUtil.getByName(template);
|
||||
mapper.binding(TemplateVariable.LIST.getVariable(), errorCodeList);
|
||||
FileUtil.nioWriteFile(mapper.render(), config.getOutPath() + FILE_SEPARATOR + outPutFileName);
|
||||
|
@ -228,7 +235,11 @@ public class DocBuilderTemplate {
|
|||
}
|
||||
|
||||
private List<ApiErrorCode> errorCodeDictToList(ApiConfig config) {
|
||||
if (CollectionUtil.isNotEmpty(config.getErrorCodes())) {
|
||||
return config.getErrorCodes();
|
||||
}
|
||||
List<ApiErrorCodeDictionary> errorCodeDictionaries = config.getErrorCodeDictionaries();
|
||||
|
||||
if (CollectionUtil.isEmpty(errorCodeDictionaries)) {
|
||||
return new ArrayList<>(0);
|
||||
} else {
|
||||
|
@ -265,4 +276,11 @@ public class DocBuilderTemplate {
|
|||
return errorCodeList;
|
||||
}
|
||||
}
|
||||
|
||||
private List<ApiDoc> listOfApiData(ApiConfig config) {
|
||||
this.checkAndInitForGetApiData(config);
|
||||
config.setMd5EncryptedHtmlName(true);
|
||||
SourceBuilder sourceBuilder = new SourceBuilder(config);
|
||||
return sourceBuilder.getControllerApiData();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,6 @@ import com.power.doc.utils.DocUtil;
|
|||
import com.thoughtworks.qdox.JavaProjectBuilder;
|
||||
import com.thoughtworks.qdox.model.*;
|
||||
import com.thoughtworks.qdox.model.expression.AnnotationValue;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
|
@ -22,9 +21,10 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class SourceBuilder {
|
||||
import static com.power.doc.constants.DocGlobalConstants.NO_COMMENTS_FOUND;
|
||||
import static com.power.doc.constants.DocTags.IGNORE;
|
||||
|
||||
private static final String IGNORE_TAG = "ignore";
|
||||
public class SourceBuilder {
|
||||
|
||||
private static final String GET_MAPPING = "GetMapping";
|
||||
|
||||
|
@ -46,10 +46,6 @@ public class SourceBuilder {
|
|||
|
||||
private static final String MULTIPART_TYPE = "multipart/form-data";
|
||||
|
||||
private static final String MAP_CLASS = "java.util.Map";
|
||||
|
||||
private static final String NO_COMMENTS_FOUND = "No comments found.";
|
||||
|
||||
private static final String VALID = "Valid";
|
||||
|
||||
private Map<String, JavaClass> javaFilesMap = new ConcurrentHashMap<>();
|
||||
|
@ -266,7 +262,7 @@ public class SourceBuilder {
|
|||
// if ("void".equals(method.getReturnType().getFullyQualifiedName())) {
|
||||
// throw new RuntimeException(method.getName() + " method in " + cls.getCanonicalName() + " can't be return type 'void'");
|
||||
// }
|
||||
if (null != method.getTagByName(IGNORE_TAG)) {
|
||||
if (null != method.getTagByName(IGNORE)) {
|
||||
continue;
|
||||
}
|
||||
url = StringUtil.removeQuotes(url);
|
||||
|
@ -1303,24 +1299,14 @@ public class SourceBuilder {
|
|||
*/
|
||||
private void handControllerAlias(ApiDoc apiDoc) {
|
||||
if (isUseMD5) {
|
||||
String name = DigestUtils.md5Hex(apiDoc.getName());
|
||||
int length = name.length();
|
||||
if (name.length() < 32) {
|
||||
apiDoc.setAlias(name);
|
||||
} else {
|
||||
apiDoc.setAlias(name.substring(length - 32, length));
|
||||
}
|
||||
String name = DocUtil.handleId(apiDoc.getName());
|
||||
apiDoc.setAlias(name);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleMethodUid(ApiMethodDoc methodDoc, String methodName) {
|
||||
String name = DigestUtils.md5Hex(methodName);
|
||||
int length = name.length();
|
||||
if (name.length() < 32) {
|
||||
methodDoc.setMethodId(name);
|
||||
} else {
|
||||
methodDoc.setMethodId(name.substring(length - 32, length));
|
||||
}
|
||||
String name = DocUtil.handleId(methodName);
|
||||
methodDoc.setMethodId(name);
|
||||
}
|
||||
|
||||
private void commonHandleParam(List<ApiParam> paramList, ApiParam param, String isRequired, String comment, String since, boolean strRequired) {
|
||||
|
|
|
@ -24,4 +24,9 @@ public class DocTags {
|
|||
* java apiNote tag for method detail
|
||||
*/
|
||||
public static final String API_NOTE = "apiNote";
|
||||
|
||||
/**
|
||||
* custom ignore tag
|
||||
*/
|
||||
public static final String IGNORE = "ignore";
|
||||
}
|
||||
|
|
|
@ -0,0 +1,101 @@
|
|||
package com.power.doc.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author yu 2019/12/7.
|
||||
*/
|
||||
public class ApiAllData {
|
||||
|
||||
/**
|
||||
* project name
|
||||
*/
|
||||
private String projectName;
|
||||
|
||||
/**
|
||||
* project id
|
||||
*/
|
||||
private String projectId;
|
||||
|
||||
/**
|
||||
* docLanguage
|
||||
*/
|
||||
private String language;
|
||||
|
||||
/**
|
||||
* doc list
|
||||
*/
|
||||
private List<ApiDoc> apiDocList;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private List<ApiDocDict> apiDocDictList;
|
||||
|
||||
/**
|
||||
* error code list
|
||||
*/
|
||||
private List<ApiErrorCode> errorCodeList;
|
||||
|
||||
/**
|
||||
* List of change log
|
||||
*/
|
||||
private List<RevisionLog> revisionLogs;
|
||||
|
||||
|
||||
public String getProjectName() {
|
||||
return projectName;
|
||||
}
|
||||
|
||||
public void setProjectName(String projectName) {
|
||||
this.projectName = projectName;
|
||||
}
|
||||
|
||||
public String getProjectId() {
|
||||
return projectId;
|
||||
}
|
||||
|
||||
public void setProjectId(String projectId) {
|
||||
this.projectId = projectId;
|
||||
}
|
||||
|
||||
public String getLanguage() {
|
||||
return language;
|
||||
}
|
||||
|
||||
public void setLanguage(String language) {
|
||||
this.language = language;
|
||||
}
|
||||
|
||||
public List<ApiDoc> getApiDocList() {
|
||||
return apiDocList;
|
||||
}
|
||||
|
||||
public void setApiDocList(List<ApiDoc> apiDocList) {
|
||||
this.apiDocList = apiDocList;
|
||||
}
|
||||
|
||||
public List<ApiErrorCode> getErrorCodeList() {
|
||||
return errorCodeList;
|
||||
}
|
||||
|
||||
public void setErrorCodeList(List<ApiErrorCode> errorCodeList) {
|
||||
this.errorCodeList = errorCodeList;
|
||||
}
|
||||
|
||||
public List<RevisionLog> getRevisionLogs() {
|
||||
return revisionLogs;
|
||||
}
|
||||
|
||||
public void setRevisionLogs(List<RevisionLog> revisionLogs) {
|
||||
this.revisionLogs = revisionLogs;
|
||||
}
|
||||
|
||||
public List<ApiDocDict> getApiDocDictList() {
|
||||
return apiDocDictList;
|
||||
}
|
||||
|
||||
public void setApiDocDictList(List<ApiDocDict> apiDocDictList) {
|
||||
this.apiDocDictList = apiDocDictList;
|
||||
}
|
||||
}
|
|
@ -10,6 +10,7 @@ import com.power.doc.constants.DocGlobalConstants;
|
|||
import com.thoughtworks.qdox.model.DocletTag;
|
||||
import com.thoughtworks.qdox.model.JavaAnnotation;
|
||||
import com.thoughtworks.qdox.model.JavaMethod;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
@ -327,4 +328,19 @@ public class DocUtil {
|
|||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use md5 generate id number
|
||||
* @param value value
|
||||
* @return String
|
||||
*/
|
||||
public static String handleId(String value) {
|
||||
String valueId = DigestUtils.md5Hex(value);
|
||||
int length = valueId.length();
|
||||
if (valueId.length() < 32) {
|
||||
return valueId;
|
||||
} else {
|
||||
return valueId.substring(length - 32, length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue