import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.xpup.bear.code.EnumDescription;
import org.xpup.bear.code.gb.RadiationSickness;
/**
* :打印java文件中的所有注释
*/
public class PrintComments {
public static final String filepath="E:/workspace/bear-code/src/org/xpup/bear/code/ws/";
public static void main(String[] args) throws FileNotFoundException {
List<String> list=new ArrayList<String>();
File file = new File(filepath);
String [] fileName = file.list();
int i=0;
for (String name : fileName) {
list.add(readFile(filepath, name));
i++;
}
// list.add(readFile(filepath, "BornDefectType.java"));
write2TxtFile(list);
System.out.println("已经写入文件的JAVA类个数:"+i);
}
/*
* 获取JAVA类文件中的类注释
* @ filePath 文件路径
* @ fileName 文件名称
* @ return 注释内容
*/
public static String readFile(String filePath,String fileName) {
String rtn="";
try {
FileReader freader = new FileReader(filePath+fileName);
BufferedReader breader = new BufferedReader(freader);
StringBuilder sb = new StringBuilder();
try {
String temp = "";
/**
* 读取文件内容,并将读取的每一行后都不加\n
* 其目的是为了在解析双反斜杠(//)注释时做注释中止符
*/
while((temp=breader.readLine())!= null)
{
sb.append(temp);
sb.append('\n');
}
String src = sb.toString();
/**
* 1、做/* 注释的正则匹配
*
* 通过渐进法做注释的正则匹配,因为/*注释总是成对出现
* 当匹配到一个/*时总会在接下来的内容中会首先匹配到"*\\/",
* 因此在获取对应的"*\\/"注释时只需要从当前匹配的/*开始即可,
* 下一次匹配时只需要从上一次匹配的结尾开始即可
* (这样对于大文本可以节省匹配效率)——
* 这就是渐进匹配法
*
* */
Pattern leftpattern = Pattern.compile("/\\*");
Matcher leftmatcher = leftpattern.matcher(src);
Pattern rightpattern = Pattern.compile("\\*/");
Matcher rightmatcher = rightpattern.matcher(src);
sb = new StringBuilder();
/**
* begin 变量用来做渐进匹配的游标 {@value}
* 初始值为文件开头
* **/
int begin = 0;
while(leftmatcher.find(begin))
{
rightmatcher.find(leftmatcher.start());
sb.append(src.substring(leftmatcher.start(), rightmatcher.end()));
/** 为输出时格式的美观 **/
sb.append('\n');
begin = rightmatcher.end();
}
// System.out.println(sb.toString());
rtn= getDescrpition(sb.toString(),fileName);
// list.add(strList);
return rtn;
// /**
// * 2、对//注释进行匹配(渐进匹配法)
// * 匹配方法是 // 总是与 \n 成对出现
// * */
// begin = 0;
// Pattern leftpattern1 = Pattern.compile("//");
// Matcher leftmatcher1 = leftpattern1.matcher(src);
// Pattern rightpattern1 = Pattern.compile("\n");
// Matcher rightmatcher1 = rightpattern1.matcher(src);
// sb = new StringBuilder();
// while(leftmatcher1.find(begin))
// {
// rightmatcher1.find(leftmatcher1.start());
// sb.append(src.substring(leftmatcher1.start(), rightmatcher1.end()));
// begin = rightmatcher1.end();
// }
// System.out.println(sb.toString());
} catch (IOException e) {
System.out.println("文件读取失败");
}finally
{
breader.close();
freader.close();
}
} catch (FileNotFoundException e) {
System.out.println("文件不存在");
}catch(IOException e)
{
System.out.println("文件读取失败");
}
return rtn;
}
/**
* 获取相关信息
* @param strTxt
*/
public static String getDescrpition(String strTxt,String fileName) {
// List<String> list=new ArrayList<String>();
String rtn="";
String preStr=strTxt.substring(strTxt.indexOf("<pre>")+5, strTxt.indexOf("</pre>")-2);
// System.out.println(preStr);
String temp=preStr.substring(preStr.indexOf("标准依据:")+5, preStr.indexOf("* 发布单位:"));
// System.out.println("temp:"+temp);
//名称
String name=temp.substring(temp.lastIndexOf(".")+4, temp.length());
// System.out.println("名称:"+name);
//标准依据
String code=temp.substring(temp.indexOf("WS"), temp.lastIndexOf(".")+4);
// System.out.println("标准依据:"+code);
//发布单位
String maker=preStr.substring(preStr.indexOf("发布单位:")+5, preStr.indexOf("* 发布时间:"));
// System.out.println("发布单位:"+maker);
//发布时间
String releaseTime=preStr.substring(preStr.indexOf("发布时间:")+5, preStr.length());
// System.out.println("发布时间:"+releaseTime);
String className=fileName.substring(0, fileName.indexOf(".java"));
String effectiveTime="2012-02-01";
rtn= "list.add(new EnumDescription('"+name.trim()+"', '"+code.trim()+"', '"+maker.trim()+"', '"+releaseTime.trim()+"', '"+effectiveTime.trim()+"', "+"\r\n"+" "+className.trim()+".class.getName()));";
System.out.println(rtn);
return rtn;
// list.add(new EnumDescription("放射性疾病名单代码", "GB/T 18201-2000", "中国国家质量技术监督局", "2000-09-30", "2001-03-01",
// RadiationSickness.class.getName()));
}
/**
* 将拼接好的字符串写进文本
* @param List<String> 要写入文件的字符串集合
*
*/
public static void write2TxtFile(List<String> strList) throws FileNotFoundException {
// String lan+="\r\n";
File file=new File("E:\\result.txt");
if (file.exists()) {
file.delete();
}
PrintWriter writer=null;
if(!file.exists()){
try {
file.createNewFile();
writer=new PrintWriter(file);
for (String str : strList) {
writer.write(str+"\r\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
writer.flush();
writer.close();
}
}
}
}