package search.tools;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RePlaceJava{
// 哪些带有中文的字符串 不需要替换
public static String[] filter = { "info(", "error(",
"System.out.println(","indexOf(" };
// 表示以什么开头的不替换
public static String[] start = { "//", "/**" };
// 临时文件目录
public static String tempFile = "c://temp.java";
// 表示替换的类是以什么结尾
public static String[] replace = { "Action" };
// 替换的内容
public static String content = "getText(/":content:/")";
// 写入的资源文件的绝对路径
public static String proPath = "c://test//temp.properties";
public static String binHome="C://Java//bin";
public static void main(String[] args) throws Exception {
String str = "C://test";
listList(str);
Runtime.getRuntime().exec("cmd /c "+binHome+"//native2ascii "+proPath+" C://test//ascii.properties" ).waitFor();
}
public static void write(String key, String value) {
try {
File file = new File(proPath);
if (!file.exists()) {
file.createNewFile();
}
BufferedWriter writer = new BufferedWriter(new FileWriter(proPath,
true));
writer.write(key + "=" + value + "/n");
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 判断字符串中是否出现中文
public static boolean haveChinese(String line) {
Pattern p = Pattern.compile("[/u4e00-/u9fa5]");
Matcher match = p.matcher(line);
if (match.find()) {
match.end();
return true;
}
return false;
}
// 过滤出现中文的字符串
public static List<String> getStringChinese(String line) {
List<String> list = findString(line);
List<String> newlist = new ArrayList<String>();
for (String tg : list) {
if (haveChinese(tg)) {
newlist.add(tg);
}
}
return newlist;
}
// 找出一行中的字符串
public static List<String> findString(String line) {
List<String> list = new ArrayList<String>();
String temp = line;
temp = temp.trim();
for (String startstr : start)
if (temp.startsWith(startstr))
return list;
int index = temp.indexOf("/"");
int num = 1;
while (temp != null && !"".equals(temp) && index >= 0) {
boolean isfilter = false;
++num;
if (num % 2 == 0 && index > 0) {
for (int i = 0; i < filter.length; i++) {
String filterstr = filter[i];
if ((index - filterstr.length()) >= 0) {
String forward = temp.substring(index
- filterstr.length(), index);
if (forward != null) {
forward = forward.trim();
if (forward.equals(filterstr)) {
isfilter = true;
}
}
}
}
}
if (!isfilter) {
temp = temp.substring(index + 1);
index = temp.indexOf("/"");
if (num % 2 == 0 && index >= 0) {
list.add(temp.substring(0, index));
num = 0;
}
}
}
return list;
}
public static void listList(String dirName) throws Exception {
File file = new File(dirName);
if (file.exists() && file.isDirectory()) {
File[] files = file.listFiles();
for (File fe : files) {
if (fe.isDirectory()) {
listList(fe.getAbsolutePath());
} else {
// 处理文件中的中文
if (fe.getName().endsWith(".java") && fe.getName().split(".java")[0].endsWith("Action")) {
System.out.println("处理:" + fe.getName());
String cls = fe.getName().split(".java")[0];
String pack = null;
int id = 1;
BufferedReader read = new BufferedReader(
new InputStreamReader(new FileInputStream(fe),
"UTF-8"));
File ff = new File(tempFile);
if (!ff.exists())
ff.createNewFile();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(tempFile,true),"UTF-8"));
String line = null;
boolean isVal = false;
boolean readPackAndCls = false;
while ((line = read.readLine()) != null) {
if (pack == null && line.indexOf("package") >= 0) {
pack = line.split("package")[1].replaceAll(" ",
"").replaceAll(";", "");
}
if (cls == null && line.indexOf("class") >= 0) {
cls = line.split("class")[1];
cls = cls.trim();
cls = cls.split(" ")[0];
}
if (cls != null) {
for (String rp : replace)
if (cls.endsWith(rp)) {
isVal = true;
}
if (!isVal)
break;
}
if (cls == null || pack == null) {
continue;
}
if (!readPackAndCls && cls != null && pack != null) {
read.close();
read = new BufferedReader(
new InputStreamReader(
new FileInputStream(fe),
"UTF-8"));
readPackAndCls = true;
continue;
}
List<String> list = getStringChinese(line);
for (String ch : list)
if (ch != null && !"".equals(ch)) {
String key = pack + "." + cls + ".a" + id;
id = id + 1;
String replaceText = content.replace(
":content:", key);
line = line.replace("/"" + ch + "/"",
replaceText);
write(key, ch);
}
writer.write(line + "/n");
}
writer.close();
read.close();
if (!isVal)
Runtime.getRuntime().exec("cmd /c del " + tempFile)
.waitFor();
else {
Runtime.getRuntime().exec(
"cmd /c move " + tempFile + ","
+ fe.getAbsolutePath()).waitFor();
}
}
}
}
}
}
}