com.stalary.easydoc.core.DocReader Maven / Gradle / Ivy
/*
* @(#)DocReader.java, 2018-09-27.
*
* Copyright 2018 Stalary.
*/
package com.stalary.easydoc.core;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.stalary.easydoc.config.EasyDocProperties;
import com.stalary.easydoc.config.SystemConfiguration;
import com.stalary.easydoc.data.*;
import com.stalary.easydoc.util.RegularExpressionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
/**
* DocReader
*
* @author lirongqian
* @since 2018/09/27
*/
@Slf4j
@Component
public class DocReader {
private View viewCache;
public View view;
@Autowired
private DocHandler docHandler;
@Autowired
private SystemConfiguration systemConfiguration;
private EasyDocProperties properties;
public DocReader(EasyDocProperties properties) {
this.properties = properties;
}
/**
* getFile 获取文件
*
* @param file 传入文件
* @param fileList 生成的文件列表
**/
private void getFile(File file, List fileList) {
if (file.exists()) {
if (file.isFile()) {
// 获取去掉后缀的文件名
String name = file.getName().split("\\.")[0];
// 排除掉不需要的文件
if (!properties.getExcludeFile().contains(name)) {
if (!properties.getIncludeFile().isEmpty()) {
if (properties.getIncludeFile().contains(name)) {
fileList.add(file);
}
} else {
fileList.add(file);
}
}
} else if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null) {
for (File single : files) {
getFile(single, fileList);
}
}
}
}
}
/**
* readFile 读取单个文件
*
* @param file 文件
* @return 内容
**/
private String readFile(File file) {
// 此处设置编码,解决乱码问题
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(new FileInputStream(file),
StandardCharsets.UTF_8))) {
StringBuilder sb = new StringBuilder();
String s = reader.readLine();
while (s != null) {
sb.append(s);
s = reader.readLine();
}
return sb.toString();
} catch (Exception e) {
log.warn("easy-doc readFile error!", e);
}
return "";
}
/**
* commonReader 公共读入方法
**/
private void commonReader() {
docHandler.addSuperAndNestModel(view);
docHandler.addURL(view);
// 缓存
viewCache = view;
}
/**
* multiReader 多文件读入方法
*
* @return 前端渲染对象
**/
public View multiReader() {
if (viewCache != null) {
return viewCache;
}
view = new View(properties);
if (properties.isGateway()) {
return view;
}
String fileName = Constant.CUR_PATH + Constant.JAVA + properties.getPath().replaceAll("\\.", "/");
File file = new File(fileName);
List fileList = new ArrayList<>();
getFile(file, fileList);
pathMapper(fileList);
for (File aFileList : fileList) {
singleReader(aFileList);
}
// 最后添加父类和接口映射
commonReader();
// 按照名词对接口和model排序
sortByName();
// 计算数量
calCount();
return view;
}
/**
* calCount 计算接口和对象数量
**/
private void calCount() {
int totalMethod = (int) view.getControllerList()
.stream()
.mapToLong(c -> c.getMethodList().size())
.sum();
int deprecatedMethod = (int) view.getControllerList()
.stream()
.mapToLong(c -> c.getMethodList().stream().filter(Method::isDeprecated).count())
.sum();
int totalModel = view.getModelList().size();
int deprecatedModel = (int) view.getModelList().stream().filter(Model::isDeprecated).count();
view.setTotalMethod(totalMethod);
view.setDeprecatedMethod(deprecatedMethod);
view.setTotalModel(totalModel);
view.setDeprecatedModel(deprecatedModel);
}
/**
* sortByName 根据名称排序
*
* @param view 前端渲染对象
**/
private void sortByName() {
view.getControllerList().sort(Comparator.comparing(Controller::getName));
view.getModelList().sort(Comparator.comparing(Model::getName));
}
/**
* multiReader 多匹配后字符串渲染方法
*
* @param str 匹配后的字符串
* @return 前端渲染对象
**/
public View multiReader(String str) {
if (viewCache != null) {
return viewCache;
}
view = new View(properties);
if (properties.isGateway()) {
return view;
}
String[] pathSplit = str.split(Constant.PATH_SPLIT);
Constant.PATH_MAP.putAll(JSONObject.parseObject(pathSplit[0], new TypeReference