package com.java110.code;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilterWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @ClassName DealHtml
* @Description TODO
* @Author wuxw
* @Date 2022/4/20 22:18
* @Version 1.0
* add by wuxw 2022/4/20
**/
public class DealHtml {
public static void main(String[] args) throws Exception {
File file = new File("C:\\Users\\Administrator\\Documents\\project\\hc\\MicroCommunityWeb\\public\\components");
JSONObject js = new JSONObject();
listFiles(file, js);
System.out.println("js = " + js.toJSONString());
}
// public static void main(String[] args) throws Exception {
// String js = "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
添加合同
\n" +
// "
\n" +
// "
\n" +addPrivilege
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// " \n" +
// "
\n" +
// "\n" +
// "
\n" +
// " \n" +
// " \n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
\n" +
// "
";
// Pattern p = Pattern.compile("=\"([\u4e00-\u9fa5]|,|,| )+");
//
// Matcher m = p.matcher(js);
//
// while (m.find()) {
// String chinese = m.group();//匹配出的中文
// chinese = chinese.replace("=\"", "");
// System.out.println(chinese);
// }
// }
public static void listFiles(File file, JSONObject js) throws Exception {
if (file.isFile()) {
if (file.getName().endsWith(".html")) {
doDealHtml(file, js);
}
return;
}
File[] files = file.listFiles();
for (File tmpFile : files) {
listFiles(tmpFile, js);
}
}
private static void doDealHtml(File tmpFile, JSONObject js) throws Exception {
String fileName = tmpFile.getName().replace(".html", "");
System.out.println("fileName=" + fileName + ",dir=" + tmpFile.getPath());
BufferedReader in = new BufferedReader(new FileReader(tmpFile));
String str;
String context = "";
JSONObject fileNameObj = new JSONObject();
while ((str = in.readLine()) != null) {
context += (doDealHtmlNode(str, fileName, fileNameObj) + "\n");
//doDealHtmlNode(str,fileName);
}
js.put(fileName, fileNameObj);
System.out.println(context);
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(tmpFile));
bufferedWriter.write(context);
bufferedWriter.close();
}
private static String doDealHtmlNode(String str, String fileName, JSONObject fileNameObj) {
// Pattern p = Pattern.compile("=\"([\u4e00-\u9fa5]|,)+");
//
// Matcher m = p.matcher(str);
Pattern p = Pattern.compile("=\"([\u4e00-\u9fa5]|,|,| )+");
Matcher m = p.matcher(str);
while (m.find()) {
String chinese = m.group();//匹配出的中文
chinese = chinese.replace("=\"", "");
fileNameObj.put(chinese, chinese);
//m.appendReplacement(buf, ">");
str = str.replace(chinese, "vc.i18n('" + chinese + "','" + fileName + "')");
str = str.replace("placeholder", ":placeholder");
}
p = Pattern.compile(">[\u4e00-\u9fa5]+");
m = p.matcher(str);
while (m.find()) {
String chinese = m.group();//匹配出的中文
chinese = chinese.replace(">", "");
fileNameObj.put(chinese, chinese);
//m.appendReplacement(buf, ">");
str = str.replace(chinese, "");
}
p = Pattern.compile("}}[\u4e00-\u9fa5]+");
m = p.matcher(str);
while (m.find()) {
String chinese = m.group();//匹配出的中文
chinese = chinese.replace("}}", "");
fileNameObj.put(chinese, chinese);
//m.appendReplacement(buf, ">");
str = str.replace(chinese, "");
}
p = Pattern.compile("> [\u4e00-\u9fa5]+");
m = p.matcher(str);
while (m.find()) {
String chinese = m.group();//匹配出的中文
chinese = chinese.replace("> ", "");
fileNameObj.put(chinese, chinese);
//m.appendReplacement(buf, ">");
str = str.replace(chinese, "");
}
return str;
}
}