Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / scripts / i18n / MergeExternalizedStringFiles.groovy @ 1000

History | View | Annotate | Download (8 kB)

1
package org.txm.scripts.scripts.i18n
2

    
3
import java.io.File;
4
import org.eclipse.osgi.util.NLS;
5
import org.txm.utils.DeleteDir;
6

    
7
/**
8
 * Scan for *.properties files and merge them into one
9
 * @author mdecorde
10
 *
11
 */
12
class MergeExternalizedStringFiles {
13
        
14
        ArrayList<File> alldirs;
15
        LinkedHashMap<File, ArrayList> data = [:];
16
        
17
        public MergeExternalizedStringFiles(File projectDir)
18
        {
19
                alldirs = scanDirectory(projectDir);
20
                alldirs.sort{it.getAbsolutePath();}
21
                //System.out.println("All dirs size: "+alldirs.size);
22
                findMessageClasses();
23
                mergeAll()
24
        }
25
        
26
        public findMessageClasses()
27
        {
28
                File messageClass;
29
                ArrayList<File> externalizeMessages;
30
                for(int i = 0 ; i < alldirs.size() ; i++) {
31
                        File dir = alldirs.get(i)
32
                        if (!dir.isDirectory())
33
                                continue;
34
                        //System.out.println("Dir: "+dir);
35
                        externalizeMessages = new ArrayList<File>();
36
                        for (File f : dir.listFiles()) {
37
                                if (f.getName() == "Messages.java")
38
                                        messageClass = f
39
                                if (f.getName() =~ "messages(_..)?\\.properties")
40
                                        externalizeMessages << f
41
                        }
42
                        
43
                        if (messageClass != null) {
44
                                String bundle = findBundle(messageClass);
45
                                data.put(dir, [messageClass, bundle, externalizeMessages])
46
                        } else {
47
                                alldirs.remove(i);
48
                                i--;
49
                        }
50
                }
51
                println "all dirs size: "+alldirs.size()
52
                //                for(def f : alldirs)
53
                //                        println f
54
        }
55
        
56
        public String findBundle(File message)
57
        {
58
                String ret;
59
                message.eachLine("iso-8859-1") {
60
                        if (it.contains("private static final String BUNDLE_NAME = ")) {
61
                                int start = it.indexOf("\"")
62
                                int end = it.lastIndexOf("\"")
63
                                ret = it.substring(start+1, end)
64
                        }
65
                }
66
                return ret;
67
        }
68
        
69
        /**
70
         * Scan directory.
71
         *
72
         * @param directory the directory
73
         */
74
        public ArrayList<File> scanDirectory(File directory)
75
        {
76
                ArrayList<File> dirfiles = [];
77
                
78
                if (!directory.exists()) {
79
                        println "directory '$directory' does not exists"
80
                        return;
81
                }
82
                
83
                println "scan directory : "+directory.getAbsolutePath();
84
                LinkedList<File> files = new LinkedList<File>();
85
                files.add(directory);
86
                
87
                while (!files.isEmpty()) {
88
                        File current = files.removeFirst();
89
                        if (current.isDirectory() && !current.getName().startsWith(".")) {
90
                                //println "add dir: "+current
91
                                List<String> currentpfiles = [];
92
                                for (File sfile : current.listFiles()) {
93
                                        if (sfile.isDirectory())
94
                                                files.add(sfile);
95
                                }
96
                                
97
                                dirfiles.add(current);
98
                        }
99
                }
100
                return dirfiles;
101
        }
102
        
103
        public void mergeAll()
104
        {
105
                def files = new ArrayList<File>(data.keySet());
106
                println files
107
                if (files.size() < 1)
108
                        return;
109
                
110
                File refDir = files.get(0);
111
                File refClass = data.get(refDir)[0];
112
                String refBundle = data.get(refDir)[1];
113
                def externalized = data.get(refDir)[2];
114
                
115
                System.out.println("ref dir: "+refDir);
116
                System.out.println("ref class: "+refClass);
117
                System.out.println("ref bundle: "+refBundle);
118
                System.out.println("ref files: "+externalized);
119
                
120
                // get externalized ref strings
121
                //      Lang            Key     externalized string 
122
                HashMap<String, HashMap<String, String>> externalizedLines = [:];
123
                for (File source : externalized) {
124
                        def lines = new HashMap<String, String>();
125
                        externalizedLines.put(source.getName(), lines);
126
                        source.eachLine("iso-8859-1"){
127
                                if (it =~ ".+=.+") {
128
                                        int idx = it.indexOf("=");
129
                                        lines.put(it.substring(0, idx), it.substring(idx+1))
130
                                } else if(it =~ ".+=\$") {
131
                                        int idx = it.indexOf("=")
132
                                        String key = it.substring(0, idx)
133
                                        String value = ""
134
                                        lines.put(key, value)
135
                                }
136
                        }
137
                }
138
                
139
                // get references keys
140
                HashSet<String> refKeys = new HashSet<String>();
141
                refClass.eachLine("iso-8859-1") {
142
                        if (it =~ ".+public static String .+")
143
                                refKeys << it.trim()
144
                }
145
                
146
                println "BEFORE"
147
                println "refKeys size: "+refKeys.size();
148
                println "fr externalizedLines size: "+externalizedLines.get("messages_fr.properties").size();
149
                println "en externalizedLines size: "+externalizedLines.get("messages.properties").size();
150
                
151
                // for all sub dir
152
                for (int i = 1 ; i < files.size() ; i++) {
153
                        File srcDir = files.get(i);
154
                        File srcClass = data.get(srcDir)[0];
155
                        String srcBundle = data.get(srcDir)[1];
156
                        
157
                        def srcExternalized = data.get(srcDir)[2];
158
                        // merge externalized strings
159
                        for (File source : srcExternalized) {
160
                                def lines = externalizedLines.get(source.getName());
161
                                source.eachLine("iso-8859-1") {
162
                                        if (it =~ ".+=.+") {
163
                                                int idx = it.indexOf("=")
164
                                                String key = it.substring(0, idx)
165
                                                String value = it.substring(idx+1)
166
                                                if (lines.containsKey(key) && lines.get(key) != value)
167
                                                        println "merged message of "+source.getName()+": "+it+ " >> "+lines.get(key)
168
                                                lines.put(key, value)
169
                                        } else if(it =~ ".+=\$") {
170
                                                int idx = it.indexOf("=")
171
                                                String key = it.substring(0, idx)
172
                                                String value = ""
173
                                                lines.put(key, value)
174
                                        }
175
                                }
176
                                source.delete(); // bye bye *.properties files
177
                        }
178
                                                
179
                        // merge static String
180
                        if(!srcClass.exists())
181
                        {
182
                                println "file removed: "+srcClass
183
                                println  srcDir
184
                                println  srcClass
185
                                println  srcBundle
186
                                println  srcExternalized
187
                                continue;
188
                        }
189
                        srcClass.eachLine("iso-8859-1"){
190
                                if(it =~ ".+public static String .+")
191
                                {
192
//                                        if(refKeys.contains(it.trim()))
193
//                                                println "Externalized key merged: "+it
194
                                        refKeys << it.trim()
195
                                }
196
                        }
197
                        srcClass.delete(); // bye bye Messages.java
198
                }
199
                
200
                println "AFTER"
201
                println "refKeys size: "+refKeys.size();
202
                println "fr externalizedLines size: "+externalizedLines.get("messages_fr.properties").size();
203
                println "en externalizedLines size: "+externalizedLines.get("messages.properties").size();
204
                
205
                //check unused externalized strings
206
                def keysDeclared = refKeys.collect{it.substring(21, it.length() -1) };
207
                System.out.println("number of keys declared in Message.java: "+keysDeclared.size());
208
                def missingDeclarations = new HashMap<String, HashSet<String>>();
209
                def missingExternalizations = new HashMap<String, HashSet<String>>();
210
                for(def lang : externalizedLines.keySet())
211
                {
212
                        def keys = externalizedLines.get(lang).keySet(); // keys  in propeties files
213
                        missingExternalizations.put(lang, new HashSet<String>());
214
                        missingDeclarations.put(lang, new HashSet<String>());
215
                        for(String key : keys)
216
                                if(!keysDeclared.contains(key))
217
                                        missingDeclarations.get(lang) << key
218
                                        
219
                        for(String key : keysDeclared)
220
                                if(!keys.contains(key))
221
                                        missingExternalizations.get(lang) << key
222
                }
223
                
224
                println "fix missing key declaration and externalization"
225
                for(String lang: missingExternalizations.keySet())
226
                {
227
                        println "Lang: "+lang
228
                        println " missingDeclarations: "+missingDeclarations.get(lang)
229
                        for(String key : missingDeclarations.get(lang))
230
                                refKeys << "public static String "+key+";"
231
                        println " missingExternalizations: "+missingExternalizations.get(lang)
232
                        for(String key : missingExternalizations.get(lang))
233
                                externalizedLines.get(lang).put(key, "N/A")
234
                }
235
                
236
                println "AFTER FIX"
237
                println "refKeys size: "+refKeys.size();
238
                println "fr externalizedLines size: "+externalizedLines.get("messages_fr.properties").size();
239
                println "en externalizedLines size: "+externalizedLines.get("messages.properties").size();
240
                
241
        
242
                // write externalized lines
243
                for(File source : externalized)
244
                {
245
                        source.withWriter("iso-8859-1") { out ->
246
                                def hash = externalizedLines.get(source.getName())
247
                                def keys = hash.keySet();
248
                                keys = keys.sort()
249
                                keys.each() { 
250
                                        out.writeLine(it+"="+hash.get(it))
251
                                }
252
                        }
253
                }
254

    
255
                // write classRef File
256
                refClass.withWriter("iso-8859-1") { out ->                        
257
                        // write start
258
                        out.writeLine('''
259
package org.txm.scripts.;
260

261
import org.eclipse.osgi.util.NLS;
262

263
public class Messages extends NLS {
264

265
        // The Constant BUNDLE_NAME. 
266
        private static final String BUNDLE_NAME = "org.txm.messages"; //$NON-NLS-1$
267
        
268
                                ''')
269
                        
270
                        //write keys
271
                        for(String key : refKeys.sort())
272
                                out.writeLine("\t"+key);
273
                        
274
                        // write end
275
                        out.writeLine('''
276
        static {
277
                // initialize resource bundle
278
                NLS.initializeMessages(BUNDLE_NAME, Messages.class);
279
        }
280

281
        private Messages() {
282
        }
283
}
284
                                ''')
285
                }
286
        }
287
        
288
        public static void main(String[] args)
289
        {
290
                File projectdir = new File("/home/mdecorde/workspace37/org.txm.core/src/java")
291
                def rez = new MergeExternalizedStringFiles(projectdir);
292
        }
293
}