格式化前:

格式化后:

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. odoo11 审批流中行总额与申请单总额的计算问题

    一. 问题的描述 在做审批流的过程中,涉及到这样一个问题,用户申请的行总额需要根据当前行的数量和单价相乘计算得出,这本来是一个很简单的功能需求,利用odoo的计算方法就可以轻松实现,但是在在view页 ...

  2. keras神经网络三个例子

    keras构造神经网络,非常之方便!以后就它了.本文给出了三个例子,都是普通的神经网络 例一.离散输出,单标签.多分类 例二.图像识别,单标签.多分类.没有用到卷积神经网络(CNN) 例三.时序预测, ...

  3. 一、Xadmin------安装

    翻译:http://xadmin.readthedocs.io/en/docs-chinese 1.安装方法: 1)pip install django-xadmin 2)通过源文件安装,我是通过这种 ...

  4. DelegatingFilterProxy作用浅析

    <filter> <filter-name>secondDomainFilter</filter-name> <filter-class>org.spr ...

  5. mysql 小数转换成百分数查出(保留两位小数百分数)

    SELECT id as 'ID',GROUP_CONCAT(concat(truncate(royalties *100,2),'%')) as '比例' FROM yser FROM id in( ...

  6. 未能加载文件或程序集&quot;Newtonsoft.Json, Version=4.5.0.0

    这问题遇到好几次了,重新更改了引用都不好使,有的时候版本改成一致就好了,但是有的地方你不知道在哪里用了就不好排查,所性在config里面加个配置让程序运行的时候去处理得了~ 很实用,放在configu ...

  7. 【转】Word之表格、图片的题注(抬头)自动编号

    问:word中的表格怎么自动插入题注(即表头的编号自动编号)? 答: 1首先搞清楚自动编号的意思.自动插入题注的意思是,在你在word中新建或者复制一个word表格的时候,表头的编号就自动生成了,而不 ...

  8. Tomcat connecttimeout sessiontimeout

    IIS中的会话超时和连接超时之间有什么区别? | Adept Technologies Inc.https://www.adepttech.com/blog/?p=825 IIS中的会话超时和连接超时 ...

  9. CentOS 7 安装配置带用户认证的squid代理服务器

    这里只简述搭建一个带用户认证的普通代理 一.安装 安装过程十分简便,只需要安装一下squid,一条命令搞定 yum install squid rpm -qa | grep squid squid-- ...

  10. Ubuntu16系统中安装htpasswd

    htpasswd是Apache附带的程序, htpasswd生成包含用户名和密码的文本文件, 每行内容格式为“用户名:密码”, 用于用户文件的基本身份认证. 当用户浏览某些网页的时候, 浏览器会提示输 ...