Statistics
| Revision:

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

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
                                                for (int i = 0 ; i < strings.size() ; i++) {
127
                                                        if (nonnls.contains(i)) continue; // ignore
128

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

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

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

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

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

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

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

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

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