FileFunction.java

来源:互联网 发布:java lru cache 编辑:程序博客网 时间:2024/05/17 05:09
package org.zhoulq.files;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
public class FileFunction {
// 宏定义判断依据
private static final String CONTENT_PATTERN_1 = ".*#define//s++.*";
// 宏实现判断依据
private static final String CONTENT_PATTERN_2 = ".*#ifdef//s++.*";
// 表达式1
private Pattern pattern = Pattern.compile(CONTENT_PATTERN_1);
// 表达式2
private Pattern pattern2 = Pattern.compile(CONTENT_PATTERN_2);
// 宏名列表
private List<String> lists = new ArrayList<String>();
public List<String> getLists() {
return lists;
}
public void setLists(List<String> lists) {
this.lists = lists;
}
/**
* 获取宏名列表
* @param file 文件名
* @param fileExtend 文件扩展名
* @return
* @throws Exception
*/
public boolean readFiles(File file, String fileExtend) throws Exception {
// 文件不存在
if (!file.exists()) {
System.out.println(file.getAbsolutePath() + "文件不存在!");
}
//文件为目录
if (file.isDirectory()) {
File[] files = file.listFiles();
//目录遍历
for (File temp : files) {
readFiles(temp, fileExtend);
}
} else if(file.getAbsolutePath().toLowerCase().endsWith(fileExtend)){
//如果文件为指定扩展名, 则解析
BufferedReader br = new BufferedReader(new FileReader(file));
//文件内容
String content = null;
// 注释代码标识
int flag = 0;
// 注释标示
boolean commentFlag = false;
// 逐行读取文件信息
while ((content = br.readLine()) != null) {
// 将注释标识设置为flase
commentFlag = false;
// 判断是否为注释的代码
if (flag > 0) {
continue;
} else {
// 判断该行是否为注释代码,如果有注释代码,判断宏定义是否在注释代码中
if (-1 != content.indexOf("/*")
&& content.indexOf("#define") > content
.indexOf("/*")) {
flag++;
commentFlag = true;
}
if (-1 != content.indexOf("*/")
&& content.indexOf("#define") < content
.indexOf("*/")) {
flag--;
commentFlag = true;
}
if(-1 != content.indexOf("//")&& content.indexOf("#define") < content.indexOf("//"))
{
commentFlag = true;
}
// 如果宏定义在注释中,则跳过当前的解析
if (commentFlag) {
continue;
}
// 判断是否为宏
if (varify("1", content)) {
int startIndex = content.indexOf("#define ") + 8;
int endIndex = content.length();
// 去掉#define前面部分的内容
content = content.substring(startIndex, endIndex);
// 找出一个非空的字符串作为宏名
for (String loopTemp : content.split(" ")) {
if (null != loopTemp && !loopTemp.equals("")) {
lists.add(loopTemp);
break;
}
}
}
}
}
br.close();
}
return true;
}
/**
* 如果宏没有宏定义,则注释这部分的宏功能
* @param file
*            文件
* @param lists
*            宏名列表
* @param bakName
*            备份文件名
* @return
* @throws Exception
*/
public boolean writeFile(File file, String bakName,String fileExtend) throws Exception {
// 文件不存在
if (!file.exists()) {
System.out.println(file.getAbsolutePath() + "文件不存在!");
}
// 判断是否文件夹
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File temp : files) {
// 如果是文件夹,则递归调用
writeFile(temp, bakName,fileExtend);
}
} else if(file.getAbsolutePath().toLowerCase().endsWith(fileExtend)){
BufferedReader br = new BufferedReader(new FileReader(file));
String content = null;
// 判断宏实现是否已经有宏定义
boolean isExist = true;
// 注释代码标识
int flag = 0;
// 注释标示
boolean commentFlag = false;
// 逐行读取文本
while (null != (content = br.readLine())) {
commentFlag = false;
// 判断是否为注释的代码
if (flag > 0) {
continue;
} else {
// 判断该行是否为注释代码,如果有注释代码,判断宏定义是否在注释代码中
if (-1 != content.indexOf("/*")
&& content.indexOf("#ifdef") > content
.indexOf("/*")) {
flag++;
commentFlag = true;
}
if (-1 != content.indexOf("*/")
&& content.indexOf("#ifdef") < content
.indexOf("*/")) {
flag--;
commentFlag = true;
}
// 如果宏定义在注释中,则跳过当前的解析
if (commentFlag) {
continue;
}
// 判断是否为宏
if (varify("2", content)) {
int startIndex = content.indexOf("#ifdef ") + 7;
int endIndex = content.length();
// 去掉#define前面部分的内容
content = content.substring(startIndex, endIndex);
// 找出一个非空的字符串作为宏名
for (String loopTemp : content.split(" ")) {
if (null != loopTemp && !loopTemp.equals("")) {
isExist = check(lists, loopTemp);
break;
}
}
}
}
if (!isExist) {
break;
}
}
br.close();
if (!isExist) {
// 将文件转化为备份文件
file.renameTo(new File(file.getAbsolutePath() + bakName));
File tmp = new File(file.getAbsolutePath());
// 创建新文件
tmp.createNewFile();
BufferedWriter bw = new BufferedWriter(new FileWriter(tmp));
BufferedReader br2 = new BufferedReader(new FileReader(
new File(file.getAbsolutePath() + bakName)));
boolean flag2 = true;
int count = 0;
while (null != (content = br2.readLine())) {
flag2 = true;
if(content.indexOf("//")<content.indexOf("#ifdef") && -1 != content.indexOf("//")){
continue;
}
if (varify("2", content)) {
int startIndex = content.indexOf("#ifdef ") + 7;
int endIndex = content.length();
String content2 = content.substring(startIndex,
endIndex);
// 找出一个非空的字符串作为宏名
for (String loopTemp : content2.split(" ")) {
if (null != loopTemp && !loopTemp.equals("")) {
flag2 = check(lists, loopTemp);
break;
}
}
}
if (!flag2) {
bw.write("// the function is no used, it will be hidden");
bw.newLine();
count++;
}
if (count >= 1 && -1 == content.indexOf("endif")) {
bw.write("//" + content);
bw.newLine();
} else if (count == 1 && -1 != content.indexOf("endif")) {
bw.write("//" + content);
bw.newLine();
bw.write("//end with the function");
bw.newLine();
count--;
} else {
bw.write(content);
bw.newLine();
}
}
bw.flush();
br2.close();
bw.close();
}
}
return true;
}
/**
* 判断行中是否为宏定义或宏实现
* @param type
*            宏类型 1: 宏定义 2: 宏实现
* @param content
*            内容
* @return
*/
public boolean varify(String type, String content) {
boolean result = false;
if (type.equals("1")) {
result = pattern.matcher(content).matches();
} else {
result = pattern2.matcher(content).matches();
}
return result;
}
// 检查字符串是否在列表中,在则返回trus,否则返回false
public boolean check(List<String> lists, String checkValue) {
for (String tmp : lists) {
if (tmp.equals(checkValue)) {
return true;
}
}
return false;
}
}
原创粉丝点击