java110
2022-04-22 307a77b525f261038dd526e07d8e16036ff61afc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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.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 DealSelectHtml {
 
    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 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 += (str + "\n");
            //doDealHtmlNode(str,fileName);
        }
        context = doDealHtmlNode(context, fileName, fileNameObj);
 
        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) {
        if(!str.contains("<option")){
            return str;
        }
 
        String[] options =  str.split("<option");
        String vcStr = "<vc:i18n name=\"";
        String endStr = "</vc:i18n>";
        String name = "";
        for(String optionStr: options){
            if(!optionStr.contains("</option>")){
                continue;
            }
            optionStr = optionStr.substring(0,optionStr.indexOf("</option>"));
            int vcPos = optionStr.indexOf(vcStr);
            if(vcPos < 0){
                continue;
            }
 
            optionStr = optionStr.substring(vcPos,optionStr.indexOf(endStr)+10);
 
            name = optionStr.substring(optionStr.indexOf("\"")+1);
            name = name.substring(0,name.indexOf("\""));
            str = str.replace(optionStr,"{{vc.i18n('"+name+"','"+fileName+"')}}");
        }
 
        return str;
    }
}