Statistics
| Revision:

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

History | View | Annotate | Download (8.9 kB)

1
// Copyright © 2010-2013 ENS de Lyon.
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
// 
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
// 
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
// 
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21

    
22
//
23
// This file is part of the TXM platform.
24
//
25
// The TXM platform is free software: you can redistribute it and/or modif y
26
// it under the terms of the GNU General Public License as published by
27
// the Free Software Foundation, either version 3 of the License, or
28
// (at your option) any later version.
29
//
30
// The TXM platform is distributed in the hope that it will be useful,
31
// but WITHOUT ANY WARRANTY; without even the implied warranty of
32
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33
// GNU General Public License for more details.
34
//
35
// You should have received a copy of the GNU General Public License
36
// along with the TXM platform.  If not, see <http://www.gnu.org/licenses/>.
37
// 
38
// 
39
// 
40
// $LastChangedDate: 2013-07-25 16:24:13 +0200 (jeu., 25 juil. 2013) $
41
// $LastChangedRevision: 2490 $
42
// $LastChangedBy: mdecorde $ 
43
//
44
package org.txm.scripts.i18n;
45

    
46
import java.io.BufferedReader;
47
import java.io.File;
48
import java.io.FileInputStream;
49
import java.io.InputStreamReader;
50
import java.util.LinkedList;
51

    
52
// TODO: Auto-generated Javadoc
53
/**
54
 * Tool to update the key of externalization files xxxx_fr.properties
55
 * adds _NA to missing keys
56
 * 
57
 * @author mdecorde
58
 * 
59
 */
60
class ExternalizationFilesUpdater {
61

    
62
        /** The dirfiles. */
63
        LinkedList<File> dirfiles = new LinkedList<File>();
64
        
65
        /** The propertyfiles. */
66
        HashMap<String, File> propertyfiles = new HashMap<String, File>();
67
        
68
        /** The fileentries. */
69
        HashMap<File, List<String>> fileentries = new HashMap<String, List<String>>();
70
        
71
        /** The fileentriesvalues. */
72
        HashMap<File, HashMap<String, String>> fileentriesvalues = new HashMap<String, HashMap<String, String>>();
73

    
74
        /**
75
         * Gets the entries.
76
         *
77
         * @param file the file
78
         * @return the entries
79
         */
80
        public List<String> getEntries(File file)
81
        {
82
                List<String> entries = [];
83
                BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file) , "iso-8859-1"));
84
                String line = reader.readLine();
85
                while (line != null) {
86
                        String[] split = line.split("=", 2);
87
                        if (split.length > 0) {
88
                                String key = split[0];
89
                                entries.add(key);
90
                        }
91
                        line = reader.readLine();
92
                }
93
                return entries;
94
        }
95

    
96
        /**
97
         * Gets the values.
98
         *
99
         * @param file the file
100
         * @return the values
101
         */
102
        public HashMap<String, String> getValues(File file) {
103
                HashMap<String, String> values = new HashMap<String, String>();
104
                BufferedReader reader = new BufferedReader(new InputStreamReader(
105
                                new FileInputStream(file), "iso-8859-1"));
106
                String line = reader.readLine();
107
                while (line != null) {
108
                        String[] split = line.split("=");
109
                        if (split.length > 0) {
110
                                String key = split[0];
111
                                String concat = "";
112
                                for (int i = 1; i < split.length; i++)
113
                                        concat += split[i];
114
                                values.put(key, concat);
115
                        }
116
                        line = reader.readLine();
117
                }
118
                return values;
119
        }
120

    
121
        /**
122
         * Process found directories.
123
         */
124
        public void processFoundDirectories()
125
        {
126
                for (String dirfile : dirfiles) {
127
                        println "DIRECTORY : "+dirfile
128
                        List<File> files = propertyfiles.get(dirfile)
129
                        for (File f : files) {
130
                                this.fileentries.put(f, getEntries(f));
131
                                this.fileentriesvalues.put(f, getValues(f));
132
                        }
133
                        File reference = null;
134
                        if (files.get(0).getName().startsWith("m")) {
135
                                reference = new File(dirfile,"messages.properties");
136
                        }
137
                        else if (files.get(0).getName().startsWith("b"))
138
                                reference = new File(dirfile,"bundle.properties");
139
                        
140
                        if (reference != null && reference.exists()) {
141
                                for (File f : files) {
142
                                        if (f != reference) {
143
                                                List<String> refentries = this.fileentries.get(reference).clone();
144
                                                List<String> tmp1 = this.fileentries.get(f).clone();
145
                                                tmp1.removeAll(refentries);
146
                                                
147
                                                refentries = this.fileentries.get(reference).clone();
148
                                                List<String> tmp = this.fileentries.get(f);
149
                                                refentries.removeAll(tmp);
150
                                                for (String missing : refentries) {
151
                                                        this.fileentriesvalues.get(f).put(missing,"N/A_"+this.fileentriesvalues.get(reference).get(missing)); // put entry's value
152
                                                }
153
                                                
154
                                                this.fileentries.put(f, this.fileentries.get(reference)); // update file entries
155
                                                
156
                                                if (tmp1.size() > 0 || refentries.size() > 0)
157
                                                        println " "+f.getName()
158
                                                if (tmp1.size() > 0)
159
                                                        println "  Removed keys : "+tmp1;
160
                                                if (refentries.size() > 0)
161
                                                        println "  Added keys : "+refentries;
162
                                        }
163
                                }
164
                        }
165
                }
166
        }
167

    
168
        /**
169
         * Update files.
170
         */
171
        public void updateFiles()
172
        {
173
                for(String dirfile : dirfiles)
174
                {
175
                        List<File> files = propertyfiles.get(dirfile);
176
                        for(File f : files)
177
                        {
178
                                List<String> entries =  this.fileentries.get(f);
179
                                HashMap<String, String> values = this.fileentriesvalues.get(f);
180
                                
181
                                Writer writer = new OutputStreamWriter(new FileOutputStream(f) , "iso-8859-1");
182
                                for(String entry : entries)
183
                                {
184
                                        writer.write(entry+"="+values.get(entry)+"\n");
185
                                }
186
                                writer.close()
187
                        }
188
                }
189
        }
190

    
191
        /**
192
         * Creates the missing files.
193
         *
194
         * @param suffix the suffix
195
         */
196
        public void createMissingFiles(String suffix)
197
        {
198
                println "Looking for missing messages files "+ suffix;
199
                for(String dirfile : dirfiles)
200
                {
201
                        //println "DIRECTORY : "+dirfile
202
                        File reference = null;
203
                        String lookingname = "";
204
                        List<File> files = propertyfiles.get(dirfile)
205
                        if(files.get(0).getName().startsWith("m"))
206
                        {
207
                                reference = new File(dirfile,"messages.properties");
208
                                lookingname = "messages_"+suffix+".properties"
209
                        }
210
                        else if (files.get(0).getName().startsWith("b"))
211
                        {
212
                                reference = new File(dirfile,"bundle.properties");
213
                                lookingname = "bundle_"+suffix+".properties"
214
                        }
215
                        
216
                        boolean create = true;
217
                        if(reference != null && reference.exists())
218
                        {
219
                                for(File f : files)
220
                                {
221
                                        if(f.getName() == lookingname)
222
                                                create = false;
223
                                }
224
                        }
225
                        if(create)
226
                        {
227
                                new File(dirfile,lookingname).createNewFile();
228
                                println "Create file " +new File(dirfile,lookingname)
229
                        }
230
                }
231
        }
232

    
233
        /* (non-Javadoc)
234
         * @see java.lang.Object#toString()
235
         */
236
        public String toString()
237
        {
238
                String rez = "";
239
                for(String dirfile : dirfiles)
240
                {
241
                        rez += dirfile+"\n";
242
                        for(File f : propertyfiles.get(dirfile))
243
                                rez += "  "+f.getName()+"\n"
244
                }
245
                return rez;
246
        }
247

    
248
        /**
249
         * Scan directory.
250
         *
251
         * @param directory the directory
252
         */
253
        public void scanDirectory(File directory)
254
        {
255
                if(!directory.exists())
256
                {
257
                        println "directory '$directory' does not exists"
258
                        return;
259
                }
260
                
261
                println "scan directory : "+directory.getAbsolutePath();
262
                LinkedList<File> files = new LinkedList<File>();
263
                files.add(directory);
264
                
265
                while(!files.isEmpty())
266
                {
267
                        File current = files.removeFirst();
268
                        if(current.isDirectory())
269
                        {
270
                                List<String> currentpfiles = [];
271
                                for(File sfile : current.listFiles())
272
                                {
273
                                        if(sfile.isDirectory())
274
                                                files.add(sfile);
275
                                        else if(sfile.getName().endsWith(".properties") && ( sfile.getName().startsWith("messages") || sfile.getName().startsWith("bundle")) )
276
                                                currentpfiles.add(sfile)
277
                                }
278
                                if(currentpfiles.size() > 0)
279
                                {
280
                                        dirfiles.add(current.getAbsolutePath());
281
                                        propertyfiles.put(current.getAbsolutePath(), currentpfiles);
282
                                }
283
                        }
284
                }
285
        }
286

    
287
        /**
288
         * The main method.
289
         *
290
         * @param args the arguments
291
         */
292
        public static void main(String[] args)
293
        {
294
                String userdir = System.getProperty("user.home");
295

    
296
                println "\nRCP\n";
297
                
298
                ExternalizationFilesUpdater scanner = new ExternalizationFilesUpdater();
299
//                scanner.scanDirectory(new File(userdir, "workspace37/org.txm.rcp")); // find directories with a messages.properties file
300
//                scanner.createMissingFiles("fr"); // create messages_fr.properties files when a messages.properties is found
301
//                scanner.processFoundDirectories(); // find missing and obsolets keys
302
//                scanner.updateFiles(); // update messages files content
303
                
304
                println "\nTOOLBOX\n";
305
                
306
                scanner = new ExternalizationFilesUpdater();
307
                scanner.scanDirectory(new File(userdir, "workspace43/org.txm.rcp/src/main/java"));
308
                scanner.createMissingFiles("ru");
309
                scanner.processFoundDirectories();
310
                scanner.updateFiles();
311
        }
312
}