Statistics
| Revision:

root / tmp / org.txm.core / src / groovy / org / txm / scripts / i18n / ExternalizeStringFile.groovy @ 187

History | View | Annotate | Download (6.4 kB)

1
package org.txm.scripts.i18n
2

    
3
import javax.swing.*;
4

    
5
/**
6
 * Scan a java file to find unexternalized Strings
7
 * create the Messages.java entries and the *.properties entries too
8
 * replace externalized Strings in the java file
9
 * 
10
 * uses a ReverseI18nDict
11
 * 
12
 * @author mdecorde
13
 */
14
class ExternalizeStringFile {
15

    
16
        File propFile;
17
        File messageFile
18

    
19
        ReverseI18nDict dict
20

    
21
        String prefix
22
        int iprefix = 0;
23
        
24
        boolean mustSaveChanges = false;
25

    
26
        public ExternalizeStringFile(File propFile, File messageFile) {
27
                dict = new ReverseI18nDict(propFile, messageFile);                
28
        }
29
        
30
        private ArrayList<String> getLineStrings(String l) {
31
                def strings = []
32
                boolean inS = false;
33
                int start = 0;
34
                int comment = 0;
35
                for(int i = 0 ; i < l.length() ; i++) {
36
                        def c = l[i]
37
                        if (inS) {
38
                                comment = 0
39
                                if (c == "\\") {
40
                                        i++;
41
                                } else if (c == "\"") {
42
                                        inS = false;
43
                                        strings << l.substring(start, i+1);
44
                                }
45
                        } else {
46
                                if (c == "\"") {
47
                                        inS = true;
48
                                        start = i;
49
                                        comment = 0
50
                                } else if (c == "/") {
51
                                        comment++;
52
                                        if (comment == 2) break;
53
                                } else {
54
                                        comment = 0
55
                                }
56
                        }
57
                }
58
                return strings;
59
        }
60
        
61
        BufferedReader reader;
62
        String line;
63
        String previousLine;
64
        boolean ignore = false;
65
        int nline = 1;
66

    
67
        def nlserrors = []
68
        def linesToWrite = []
69

    
70
        int nTotalChanges = 0;
71
        
72
        def strings;
73
        def nonnls;
74
        def toExternalize = []
75
        def toNLSize = []
76
        
77
        private int processSourceFile(File sourceFile, boolean processMode) {
78
                
79
                prefix = sourceFile.getName();
80
                prefix = prefix.substring(0, prefix.indexOf("."))+"_" ;
81
                iprefix = 0;
82
                
83
                reader = new BufferedReader(new InputStreamReader(new FileInputStream(sourceFile), "UTF-8"));
84
                line = reader.readLine();
85
                previousLine;
86
                ignore = false;
87
                nline = 1;
88

    
89
                nlserrors = []
90
                linesToWrite = []
91

    
92
                nTotalChanges = 0;
93
                
94
                while (line != null) {
95
                        String l = line.trim()
96
                        if (ignore) {
97
                                if (l.startsWith("*/") || l.endsWith("*/")) {
98
                                        ignore = false
99
                                } else if (l.startsWith("*")) {
100

    
101
                                } else {
102
//                                        println "ERROR LINE $nline : not a comment ? "+l
103
//                                        return -1;
104
                                }
105
                        } else {
106
                                if (l.startsWith("//")) {
107
                                        // ignore
108
                                } else if (l.startsWith("@")) {
109
                                        // ignore
110
                                } else if (l.startsWith("/*")) {
111
                                        ignore = true;
112
                                        if (l.endsWith("*/")) ignore=false
113
                                } else {
114
                                        strings = getLineStrings(l);
115
                                        nonnls = getNONNLS(l);
116
                                        toExternalize = []
117
                                        toNLSize = []
118

    
119
                                        // fix NON-NLS
120
                                        if (nonnls.size() > 0 && nonnls.size() != strings.size()) {
121
                                                println "NLS error line $nline: "+l
122
                                                nlserrors << nline
123
                                        }
124

    
125
                                        if (nlserrors.size() == 0) {
126
                                                // quick pre process
127
                                                for (int i = 0 ; i < strings.size() ; i++) {
128
                                                        if (nonnls.contains(i)) continue; // ignore
129

    
130
                                                        if (strings[i] == "\"\"" || strings[i] == "\" \"" || strings[i] == "\"\\n\"") {
131
                                                                toNLSize << i;
132
                                                        } else {
133
                                                                toExternalize << i
134
                                                        }
135
                                                }
136

    
137
                                                if (processMode) {
138
                                                        nTotalChanges += processLine();
139
                                                } else {
140
                                                        nTotalChanges += (toExternalize.size() + toNLSize.size());
141
                                                }
142
                                        }
143
                                }
144
                        }
145

    
146
                        linesToWrite << line
147
                        previousLine = line;
148
                        line = reader.readLine();
149
                        nline++;
150
                }
151

    
152
                if (nlserrors.size() > 0) {
153
                        println "NLS ERROR "+sourceFile.getName()+" lines $nlserrors"
154
                        return -1;
155
                } else if (processMode && nTotalChanges > 0) {
156
                        mustSaveChanges = true;
157
                        File oldFile = new File(sourceFile.getParentFile(), sourceFile.getName()+".cpy");
158
                        if (!sourceFile.renameTo(oldFile)) {
159
                                println "Failed to backup old source file $sourceFile to $oldFile"
160
                                return 0;
161
                        } else {
162
                                sourceFile.withWriter("UTF-8") { writer ->
163
                                        for(String l : linesToWrite) {
164
                                                writer.println l
165
                                        }
166
                                }
167
                        }
168
                }
169
                return nTotalChanges
170
        }
171
        
172
        private int processLine() {
173
                int nChanges = 0;
174
                int ichar = 0;
175
                for (int i = 0 ; i < strings.size() ; i++) {
176
                        ichar = line.indexOf(strings[i], ichar);
177
                        if (toExternalize.contains(i)) {
178
                                println "Line $nline : $strings $nonnls $toNLSize $toExternalize"
179
                                nChanges++;
180
                                String value = strings[i].substring(1,strings[i].length() -1)
181
                                if (dict.containsValue(value)) {
182
                                        String str = "Messages."+dict.getKey(value)
183
                                        println " automatic replace "+strings[i]+" with Messages."+dict.getKey(value);
184
                                        String p1 = line.substring(0, ichar);
185
                                        String p2 = line.substring(ichar+strings[i].length(), line.length())
186
                                        println " old: $line"
187
                                        line = p1+str+p2
188
                                        println " new: $line"
189
                                } else {
190
                                        String key = getNextKey();
191
                                        String ok = JOptionPane.showInputDialog(null, "Key for value=$value\n\n$previousLine\n$line\n", key);
192
                                        if(ok == null) {
193
                                                println " add new NON-NLS "+(i+1)
194
                                                println " old: $line"
195
                                                line += " //\$NON-NLS-"+(i+1)+"\$"
196
                                                println " new: $line"
197
                                        } else {
198
                                                println " add new key: $key"
199
                                                dict.put(key, value);
200

    
201
                                                String p1 = line.substring(0, ichar);
202
                                                String p2 = line.substring(ichar+strings[i].length(), line.length())
203
                                                String str = "Messages."+key
204
                                                println " old: $line"
205
                                                line = p1+str+p2
206
                                                println " new: $line"
207
                                        }
208
                                }
209
                        } else if (toNLSize.contains(i)) {
210
                                println "Line $nline : $strings $nonnls $toNLSize $toExternalize"
211
                                nChanges++;
212
                                println " add automatic new NON-NLS "+(i+1)
213
                                println " old: $line"
214
                                line += " //\$NON-NLS-"+(i+1)+"\$"
215
                                println " new: $line"
216
                        }
217
                }
218
                return nChanges;
219
        }
220

    
221
        private def getNONNLS(String l) {
222
                def idx = []
223
                def m = l =~ /\/\/.NON-NLS-[0-9]\$/
224
                m.each { idx << Integer.parseInt(it[11])-1 }
225
                return idx.sort()
226
        }
227

    
228
        private def getNextKey() {
229
                String tmp = prefix+(iprefix++);
230
                while (dict.containsKey(tmp)) {
231
                        tmp = prefix+(iprefix++);
232
                }
233
                return tmp;
234
        }
235
        
236
        int getNbOfChanges(File sourceFile) {
237
                return processSourceFile(sourceFile, false);
238
        }
239
        
240
        int process(File sourceFile) {
241
                return processSourceFile(sourceFile, true);
242
        }
243
        
244
        public boolean saveChanges() {
245
                if (mustSaveChanges ) {
246
                        mustSaveChanges = false;
247
                        dict.saveChanges();
248
                        return true;
249
                }
250
                return false;
251
        }
252

    
253
        public static void main(String[] args) {
254
                File sourceFile = new File("/home/mdecorde/workspace37/org.txm.toolbox/src/java/org/txm/functions/cooccurrences/Cooccurrence.java");
255
                File propFile = new File("/home/mdecorde/workspace37/org.txm.toolbox/src/java/org/txm/messages.properties")
256
                File messageFile = new File("/home/mdecorde/workspace37/org.txm.toolbox/src/java/org/txm/Messages.java")
257

    
258
                ExternalizeStringFile externalizer = new ExternalizeStringFile(propFile, messageFile)
259
                println externalizer.getNbOfChanges(sourceFile);
260
                println externalizer.process(sourceFile);
261
                externalizer.saveChanges();
262
        }
263
}