格式化前:

格式化后:

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. A2D JS框架 - Web API CSRF保护实现

    这次自己实现了类似jQuery中ajax调用的方法,并且针对RESTFul进行了改造和集成,实现的A2D AJAX接口如下: $.ajax.RESTFulGetCollection("/ap ...

  2. iOS开发简记(8):数据持久化

    数据持久化,也就是把数据保存到磁盘,以后可以再读取出来使用(也可以再次更改或删除).很多场景需要数据持久化,比如为了减轻服务器的访问与存储压力,客户端需要在本地做一些数据持久化的工作. iOS的数据持 ...

  3. Can’t connect to local MySQL server through socket 原因解析

    在连接mysql的时,经常会出现以下错误提示: ERROR (HY000): Can't connect to local MySQL server through socket '/var/lib/ ...

  4. Python-正则复习-56

    # 正则表达式# 字符组 [字符]# 元字符 # \w \d \s # \W \D \S # . 除了换行符以外的任意字符 # \n \t # \b # ^ $ 匹配字符串的开始和结束 # () 分组 ...

  5. ICPC青岛站网络赛-C-高效模拟

    嗯这道辣鸡题,当时我队友写了错误的代码,我稍微改动了,思路基本上是对了,但是就是超时,我第一直觉是我这个算法思路是没有任何问题的,但是就是TLE,我感觉这个算法已经优化的不能再优化了啊...后面就怀疑 ...

  6. 将iso镜像转换为docker镜像

    为什么不用官方的镜像? 不是不想使用,而是官方镜像提供的功能实在是太少了,不信的话,你pull一个ubuntu:latest的进行,你看有没有wget.curl.ssh这些功能,就连最简单的ifcon ...

  7. VS2008引入头文件包含目录和lib库目录

    全局级别的引入 为VS所有项目设置包含目录和库目录,对所有项目都有效 如下图所示:工具-选项-项目和解决方案-VC++目录-包含文件:在此添加头文件目录即可 工具-选项-项目和解决方案-VC++目录- ...

  8. python安装与使用(Windows)

    日常使用PHP开发较多,但是有些地方PHP的语言的瓶颈就显露出来了,例如,同样是抓取一个网站的内容,使用PHP需要较为复杂的正则匹配,效率较为低下.python具有丰富的类库,拿过来直接可以使用,功能 ...

  9. Composer安装与使用

    Composer是PHP中用来管理依赖(dependency)关系的工具.你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer会帮你安装这些依赖的库文件. Windows ...

  10. [转帖]Linux 的静态库与动态库

    Linux下的静态库与动态库 2017年02月18日 09:17:13 LLZK_ 阅读数:10257 标签: linux动态库静态库区别使用 更多 个人分类: Linux学习笔记 所属专栏: Lin ...