格式化前:

格式化后:

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. 【C#复习总结】多线程编程

    1 基本概念 前一篇文章做了铺垫,详见:http://www.cnblogs.com/mhq-martin/p/9035640.html 2 多线程 多线程的优点:可以同时完成多个任务:可以使程序的响 ...

  2. vue webpack打包 -webkit-box-orient 失效

    一行省略 overflow: hidden; white-space: nowrap; text-overflow: ellipsis; 超出两行省略 overflow: hidden; text-o ...

  3. 了解可执行的NPM包

    NPM是Node.js的包管理工具,随着Node.js的出现,以及前端开发开始使用gulp.webpack.rollup以及其他各种优秀的编译打包工具(大多数采用Node.js来实现),大家都开始接触 ...

  4. 反射reflect

    JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性:这种动态获取信息以及动态调用对象方法的功能称为java语言的反射机制. ...

  5. 百度地图开发者API学习笔记二

    一,地图上多个覆盖物(Marker). 当有多个覆盖物时,我们需要获取每个点的信息.如下图,每个Marker的经度都不相同 二,代码: <!DOCTYPE html> <html&g ...

  6. Mysql drop function xxxx ERROR 1305 (42000): FUNCTION (UDF) xxxx does not exist

    mysql> drop function GetEmployeeInformationByID;ERROR 1305 (42000): FUNCTION (UDF) GetEmployeeInf ...

  7. centos ping www.baidu.com ping: unknown host www.baidu.com

    [root@zabbix ~]# cat /etc/resolv.conf ; generated by /sbin/dhclient-script nameserver 219.141.136.10

  8. jmeter压测参数设定(转)

    jmeter压测参数设定 一.基本公式 线程数 = QPS * time: 注:QPS--每秒完成请求的个数:time--每个请求响应完成平均需要时间: 故QPS * time就是所有请求完成响应所需 ...

  9. java注解和自定义注解的简单使用

    前言 在使用Spring Boot的时候,大量使用注解的语法去替代XML配置文件,十分好用. 然而,在使用注解的时候只知道使用,却不知道原理.直到需要用到自定义注解的时候,才发现对注解原理一无所知,所 ...

  10. [转帖]Runtime, Engine, VM 的区别是什么?

    这就是个WiFi和WLAN关系的问题嘛.Runtime是指用于支持程序运行时的组件,它可以是个Engine和/或VM.VM是一种系统抽象,它提供代码执行所需的API环境.Engine是一种处理抽象,它 ...