javadoc格式化,解决多个形参空格暴多,页面溢出问题
格式化前:

格式化后:

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>geostack</groupId>
<artifactId>geostack-javadoc-fomart</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
</project>
JavaDocFormat.java
package com.nihaorz; import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set; /**
* @author Nihaorz
*/
public class JavaDocFormat { private static Set<String> excludeSet = new HashSet<String>(Arrays.asList(new String[]{"package-frame.html", "package-summary.html", "package-tree.html"}));
private static final String WRAP_STR = "\r\n";
private static int classCount = 0;
private static int methodCount = 0;
private static int responseBodyCount = 0;
private static int requestMappingCount = 0; public static void main(String[] args) throws Exception {
String parentPath = "C:\\Users\\Nihaorz\\Desktop\\OperationCenter_doc\\com";
File folder = new File(parentPath);
List<String> list = new ArrayList<String>();
getAllFile(folder, list);
classCount = list.size();
for (String s : list) {
File file = new File(s);
formatFile(file);
}
System.out.println("classCount:" + classCount);
System.out.println("methodCount:" + methodCount);
System.out.println("responseBodyCount:" + responseBodyCount);
System.out.println("requestMappingCount:" + requestMappingCount);
} /**
* 格式化文件
*
* @param file
*/
private static void formatFile(File file) throws IOException {
String html = FileUtils.readFileToString(file, "UTF-8");
Document doc = Jsoup.parse(html);
doc.outputSettings().prettyPrint(false);
Elements elements = doc.select("a[name=method.detail]");
if (elements.size() > 0) {
Element a = elements.get(0);
Elements pres = a.parent().select("li.blockList pre");
if (pres.size() > 0) {
for (Element pre : pres) {
String result;
int sum = 0;
methodCount += elements.size();
String text = pre.text();
if (text.indexOf("@ResponseBody") > -1) {
responseBodyCount++;
sum++;
}
if (text.indexOf("@RequestMapping") > -1) {
requestMappingCount++;
sum++;
}
text = text.replace(" @RequestMapping", "@RequestMapping");
text = text.replace(" @ResponseBody", "@ResponseBody");
if (sum == 2) {
int index = text.indexOf(WRAP_STR);
index = text.indexOf(WRAP_STR, index + 1);
String str1 = text.substring(0, index);
String str2 = text.substring(index + WRAP_STR.length(), text.length());
str2 = formatMain(str2);
result = str1 + WRAP_STR + str2;
} else if (sum == 1) {
int index = text.indexOf(WRAP_STR);
String str1 = text.substring(0, index);
String str2 = text.substring(index + WRAP_STR.length(), text.length());
str2 = formatMain(str2);
result = str1 + WRAP_STR + str2;
} else {
result = formatMain(text);
}
if (result != null) {
pre.text(result);
}
}
}
}
writeTxtFile(doc.html(), file);
} /**
* 写文件
* @param content
* @param file
* @return
* @throws IOException
*/
public static boolean writeTxtFile(String content, File file) throws IOException {
RandomAccessFile raf = null;
boolean flag = false;
FileOutputStream o;
try {
o = new FileOutputStream(file);
o.write(content.getBytes("UTF-8"));
o.close();
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (raf != null) {
raf.close();
}
}
return flag;
} /**
* 格式化方法签名
* @param str
* @return
*/
public static String formatMain(String str) {
String result;
StringBuilder nullStr = new StringBuilder();
str = str.replace(WRAP_STR, " ")
.replace(" ", "")
.replace("( @", "(@");
int index = str.indexOf(",");
String str3 = str.substring(0, str.indexOf("(") + 1);
for (int i = 0; i < str3.length() / 2; i++) {
nullStr.append(" ");
}
if (index > -1) {
String str4 = str.substring(str.indexOf("(") + 1, str.lastIndexOf(")"));
String str5 = str.substring(str.lastIndexOf(")"), str.length());
String[] arr = str4.split(", ");
StringBuilder sb = new StringBuilder();
sb.append(str3).append(WRAP_STR);
for (String s : arr) {
sb.append(nullStr).append(s).append(", ").append(WRAP_STR);
}
sb.delete(sb.lastIndexOf(","), sb.length());
sb.append(str5);
result = sb.toString();
} else {
result = str;
}
if (result.indexOf("throws") > -1) {
int throwsIndex = result.indexOf("throws");
if (throwsIndex > -1) {
result = result.substring(0, throwsIndex) + WRAP_STR + nullStr + result.substring(throwsIndex, result.length());
}
}
return result;
} /**
* 获取所有文件
*
* @param file
* @param resultFileName
* @return
*/
public static List<String> getAllFile(File file, List<String> resultFileName) {
File[] files = file.listFiles();
if (files == null) {
return resultFileName;
}
for (File f : files) {
if (!f.isDirectory() && !excludeSet.contains(f.getName())) {//如果不是文件夹
resultFileName.add(f.getPath());
} else {
getAllFile(f, resultFileName);//如果是文件夹进行递归
}
}
return resultFileName;//返回文件名的集合
} }
javadoc格式化,解决多个形参空格暴多,页面溢出问题的更多相关文章
- php获取html纯文本,解决编辑器手动键入空格造成的无意义空白字符(空值问题)
在项目中,我们常常需要用到一些验证,不管是前台还是后台的,上传的问题时,需要内容不为空,但可视化编辑器的介入让手动敲入空格跳出了常规的检测.空格是一种排版的手段,但毫无内容只有空格就显得没有意义了,今 ...
- ueditor的工具栏显示乱码解决方法 小问题.. 是你的页面编码与语言包js编码不符所导致的
ueditor的工具栏显示乱码解决方法 小问题.. 是你的页面编码与语言包js编码不符所导致的解决方法:用记事本将ueditor\..\lang\zh-cn\zh-cn.js打开,然后保存为ANSI ...
- 使用监听器解决路径问题,例如在jsp页面引入js,css的web应用路径
使用监听器解决路径问题,例如在jsp页面引入js,css的web应用路径 经常地,我们要在jsp等页面引入像js,css这样的文件,但是在服务器来访问的时候,这时间就有关到相对路径与绝对路径了.像网页 ...
- event.preventDefault() 解决按钮多次点击 导致页面变大
event.preventDefault() 解决按钮多次点击 导致页面变大
- 解决Vue编译和打包时频繁内存溢出情况CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
解决Vue编译和打包时频繁内存溢出情况CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory 如上图所示:频繁出现此 ...
- 解决textarea 输出有空格问题
我们在使用textarea标签输出的时候,经常会出现前后都有空格.使用trim()处理也不行. 这个原因是因为 我们在编写textarea标签对的时候使用了换行. 解决方法:就是<textare ...
- Java 解决采集UTF-8网页空格变成问号乱码
http://blog.csdn.net/bob007/article/details/27098875 使用此方法转换后,在列表中看到的正常,但是在详情页的文本框中查看到的就是 了,只好过滤掉所有的 ...
- 解决for循环中空格的问题
[root@node-01 ~]# cat 1 a b c ab cd 如果想按行循环遍历出文件中内容,直接使用for是有问题的,第一行按空格分隔的会有问题 [root@node-01 ~]# for ...
- 【JAVA】【Eclipse】出现This element neither has attached source nor attached Javadoc...的解决方法
This element neither has attached source nor attached Javadoc and hence no Javadoc could be found Ec ...
随机推荐
- C#调用迅雷下载,调用迅雷影音播放
方法很多种,这里介绍一种,通过命令行参数调用. try { ]; Process.Start(thunderPath, "http://www.baidu.com/abc.exe" ...
- 给扔物线 HenCoder Plus 学员的一次分享文字版
半个月前,和我的终极技术目标扔物线朱凯一拍即合,到了他所开展的 HenCoder Plus 课程给大家分享了 1 个多小时的「模拟面试」心得,也顺便听了几次凯哥的课程,感觉真的挺用心的.自己也希望能一 ...
- 小谈UAT(验收测试)
验收测试人员的测试任务: 1.验收人员是提出需求的人员,所以对需求最为熟悉,最主要测试功能的遗漏或者多余2.系统测试人员重点在测试功能的正确性和非功能的符合性,当然也希望验收人员测试功能的正确性3.因 ...
- 剑指Offer-- 之字形顺序打印二叉树
请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推 /* struct TreeNode { int val ...
- HDU 3478 Catch (连通性&&二分图判断)
链接 [https://vjudge.net/contest/281085#problem/C] 题意 一个n个点,m条边的图,开始的点是s 每次必须移动到相邻的位置,问你是否存在某个时刻所有点都可能 ...
- Daily Scrum 12.19
Member Task on 12.19 Task on 12.20 仇栋民 请假 完成Task972 : 完成活动评分基础功能 康家华 完成 Task1004 : 百度map UI优化 完成Task ...
- 第十二届湖南省赛 (B - 有向无环图 )(拓扑排序+思维)好题
Bobo 有一个 n 个点,m 条边的有向无环图(即对于任意点 v,不存在从点 v 开始.点 v 结束的路径). 为了方便,点用 1,2,…,n 编号. 设 count(x,y) 表示点 x 到点 y ...
- p9半幺群
如何不理解划红线的地方?第二个划红线地方,请举一个例子 1.0不是幺元 2.f(1)=2, f(2)=1, f(3)=3, g(1)=2, g(2)=3, g(3)=1 fg不等于gf
- Windows系统,文件和文件夹命名规则:
不能包含:< > / \ | : * ? windows中,文件名(包括扩展名)可高达 个字符.文件名可以包含除 ? “ ”/ \ < > * | : 之外的大多数字符:保留文 ...
- 【转】安装ambari的时候遇到的ambari和hadoop问题集
5.在安装的时候遇到的问题 5.1使用ambari-server start的时候出现ERROR: Exiting with exit code -1. 5.1.1REASON: Ambari Ser ...