格式化前:

格式化后:

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格式化,解决多个形参空格暴多,页面溢出问题的更多相关文章

  1. php获取html纯文本,解决编辑器手动键入空格造成的无意义空白字符(空值问题)

    在项目中,我们常常需要用到一些验证,不管是前台还是后台的,上传的问题时,需要内容不为空,但可视化编辑器的介入让手动敲入空格跳出了常规的检测.空格是一种排版的手段,但毫无内容只有空格就显得没有意义了,今 ...

  2. ueditor的工具栏显示乱码解决方法 小问题.. 是你的页面编码与语言包js编码不符所导致的

    ueditor的工具栏显示乱码解决方法 小问题..  是你的页面编码与语言包js编码不符所导致的解决方法:用记事本将ueditor\..\lang\zh-cn\zh-cn.js打开,然后保存为ANSI ...

  3. 使用监听器解决路径问题,例如在jsp页面引入js,css的web应用路径

    使用监听器解决路径问题,例如在jsp页面引入js,css的web应用路径 经常地,我们要在jsp等页面引入像js,css这样的文件,但是在服务器来访问的时候,这时间就有关到相对路径与绝对路径了.像网页 ...

  4. event.preventDefault() 解决按钮多次点击 导致页面变大

    event.preventDefault() 解决按钮多次点击 导致页面变大

  5. 解决Vue编译和打包时频繁内存溢出情况CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

    解决Vue编译和打包时频繁内存溢出情况CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory 如上图所示:频繁出现此 ...

  6. 解决textarea 输出有空格问题

    我们在使用textarea标签输出的时候,经常会出现前后都有空格.使用trim()处理也不行. 这个原因是因为 我们在编写textarea标签对的时候使用了换行. 解决方法:就是<textare ...

  7. Java 解决采集UTF-8网页空格变成问号乱码

    http://blog.csdn.net/bob007/article/details/27098875 使用此方法转换后,在列表中看到的正常,但是在详情页的文本框中查看到的就是 了,只好过滤掉所有的 ...

  8. 解决for循环中空格的问题

    [root@node-01 ~]# cat 1 a b c ab cd 如果想按行循环遍历出文件中内容,直接使用for是有问题的,第一行按空格分隔的会有问题 [root@node-01 ~]# for ...

  9. 【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 ...

随机推荐

  1. Redis中单机数据库的实现

    1. 内存操作层 zmalloc 系接口 redis为了优化内存操作, 封装了一层内存操作接口. 默认情况下, 其底层实现就是最简朴的libc中的malloc系列接口. 如果有定制化需求, 可以通过配 ...

  2. 追源索骥:透过源码看懂Flink核心框架的执行流程

    li,ol.inline>li{display:inline-block;padding-right:5px;padding-left:5px}dl{margin-bottom:20px}dt, ...

  3. 【记一次pull request的惨痛教训】不可见的分隔符之Zero-with-space

    问题描述: 我在修改 ctf-wiki 目录后进行 mkdocs build 去生成索引目录的时候报错: 然后我尝试定位到第 2 行和第 288 行,这些行我似乎并没有修改过啊. 未果,开始去找师傅解 ...

  4. NoSQL是什么?

    导读 NoSQL(not only sql,不仅仅是SQL),是一项全新的数据库革命性运动,泛指非关系型数据库,对于NoSQL这个新兴的名词,每个人的理解都不同.其实NoSQL一词最早出现于1998年 ...

  5. sqlserver2008 传入的表格格式数据流(tds)协议流不正确。

    起因是在sql 2008 里使用 sql prompt 报了一个内部连接致命错误,原本应该想到是数据库连接问题的,奇怪的是能连接上数据库也能查询表仅仅是用不了工具没有智能提示. 几经查询无果,度娘上之 ...

  6. RabbitMQ 安装与使用

    RabbitMQ 安装与使用   前言 吃多了拉就是队列,吃饱了吐就是栈 使用场景 对操作的实时性要求不高,而需要执行的任务极为耗时:(发送短信,邮件提醒,更新文章阅读计数,记录用户操作日志) 存在异 ...

  7. jvisualvm远程监控 visualgc插件 不受此jvm支持问题

    https://yq.aliyun.com/ziliao/478212 1.修改远程服务器上java设置 vi $JAVA_HOME/jre/lib/security/java.policy    在 ...

  8. 配置nginx反向代理服务器,解决浏览器跨域调用接口的限制问题

    配置nginx反向代理服务器,解决浏览器跨域调用接口的限制问题 - 大venn的博客 - CSDN博客https://blog.csdn.net/u011135260/article/details/ ...

  9. python与C,在写程序时踩过的坑!

    1.  python与C有很多相似之处, 其一就是指针的大量应用,  因此在使用临时变量保存数据, 并将临时变量传递给其他变量时需要创建内存; 例如,在C中, char *temp 每次获取到不同的字 ...

  10. java设计模式:概述与GoF的23种设计模式

    软件设计模式的产生背景 设计模式这个术语最初并不是出现在软件设计中,而是被用于建筑领域的设计中. 1977 年,美国著名建筑大师.加利福尼亚大学伯克利分校环境结构中心主任克里斯托夫·亚历山大(Chri ...