java110
2022-04-26 fd7eafe5b15a59728edd5d31c018590bd626ea83
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.java110.code;
 
import com.alibaba.fastjson.JSONObject;
import com.java110.utils.util.StringUtil;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
 
/**
 * @ClassName DealHtml
 * @Description TODO
 * @Author wuxw
 * @Date 2022/4/20 22:18
 * @Version 1.0
 * add by wuxw 2022/4/20
 **/
public class DealPrintVc18nNameHtml {
 
    public static void main(String[] args) throws Exception {
        File file = new File("C:\\project\\vip\\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(str, 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) {
        String vcStr = "<vc:i18n name=\"";
        String vcStrFunc = "vc.i18n";
        if (!str.contains(vcStr) && !str.contains(vcStrFunc)) {
            return str;
        }
        String[] options ;
        if(str.contains(vcStr)) {
           options = str.split(vcStr);
 
            String endStr = "</vc:i18n>";
            String name = "";
            for (String optionStr : options) {
                if (!optionStr.contains(endStr)) {
                    continue;
                }
                optionStr = optionStr.substring(0, optionStr.indexOf("\""));
 
                fileNameObj.put(optionStr, optionStr);
                //str = str.replace(optionStr,"{{vc.i18n('"+name+"','"+fileName+"')}}");
            }
        }else {
 
            options = str.split(vcStrFunc);
            String optionStr = "";
            for (int optionIndex = 0; optionIndex < options.length; optionIndex++) {
                if (optionIndex % 2 == 0) {
                    continue;
                }
                optionStr = options[optionIndex];
                if (!optionStr.contains("'")) {
                    continue;
                }
                optionStr = optionStr.substring(2);
                optionStr = optionStr.substring(0, optionStr.indexOf("'"));
 
                if(StringUtil.isEmpty(optionStr)){
                    continue;
                }
 
                fileNameObj.put(optionStr, optionStr);
                //str = str.replace(optionStr,"{{vc.i18n('"+name+"','"+fileName+"')}}");
            }
 
        }
        return str;
    }
}