Statistics
| Revision:

root / trunk / libraries / libInternationalization / src-utils / org / gvsig / i18n / utils / UpdateTrans.java @ 11120

History | View | Annotate | Download (15.3 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
*
3
* Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
4
*
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
9
*
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
* GNU General Public License for more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
*
19
* For more information, contact:
20
*
21
*  Generalitat Valenciana
22
*   Conselleria d'Infraestructures i Transport
23
*   Av. Blasco Ib??ez, 50
24
*   46010 VALENCIA
25
*   SPAIN
26
*
27
*      +34 963862235
28
*   gvsig@gva.es
29
*      www.gvsig.gva.es
30
*
31
*    or
32
*
33
*   IVER T.I. S.A
34
*   Salamanca 50
35
*   46005 Valencia
36
*   Spain
37
*
38
*   +34 963163400
39
*   dac@iver.es
40
*/
41

    
42

    
43
/**
44
 * 
45
 */
46
package org.gvsig.i18n.utils;
47

    
48
import java.io.BufferedWriter;
49
import java.io.File;
50
import java.io.FileNotFoundException;
51
import java.io.FileOutputStream;
52
import java.io.IOException;
53
import java.io.OutputStreamWriter;
54
import java.io.UnsupportedEncodingException;
55
import java.util.ArrayList;
56
import java.util.HashMap;
57
import java.util.Iterator;
58
import java.util.TreeMap;
59

    
60
/**
61
 * @author cesar
62
 *
63
 */
64
public class UpdateTrans {
65
        // The filename which stores the configuration (may be overriden by the command line parameter)
66
        private String configFileName = "config.xml";
67
        
68
        // Object to load and store the config options
69
        private ConfigOptions config;
70
        
71
        private TranslationDatabase database;
72

    
73
        /**
74
         * @param args
75
         */
76
        public static void main(String[] args) {
77
                UpdateTrans process = new UpdateTrans();
78
                
79
                // load command line parameters
80
                if (!process.readParameters(args)) {
81
                        usage();
82
                        System.exit(-1);
83
                }
84
                
85
                // transfer control to the program's main loop
86
                process.start();
87
        }
88
        
89
        private void start() {
90
                // load config options from the config file
91
                if (!loadConfig()) {
92
                        System.out.println("Error leyendo el fichero de configuraci?n.");
93
                        usage();
94
                        System.exit(-1);
95
                }
96
                
97
                loadKeys();
98
                loadDataBase();
99
                
100
                updateDB();
101
        }
102
        
103
        private void updateDB(){
104
                String lang, auxLang;
105
                String key, value, dbValue;
106
                
107
                HashMap newKeys = detectNewKeys();
108
                TreeMap newKeysDict;
109
                HashMap removedKeys = new HashMap();
110
                
111
                ArrayList removedKeysDict;
112

    
113
                /**
114
                 * Process the new or changed keys
115
                 */
116
                for (int i=0; i<config.languages.length; i++) {
117
                        lang = config.languages[i];
118
                        File langDir = new File(config.outputDir+File.separator+lang);
119
                        langDir.mkdirs();
120
                        
121
                        // process the keys
122
                        newKeysDict = (TreeMap) newKeys.get(lang);
123
                        removedKeysDict = new ArrayList();
124
                        removedKeys.put(lang, removedKeysDict);
125
                        Iterator newKeysIterator = newKeysDict.keySet().iterator();
126
                        while (newKeysIterator.hasNext()) {
127
                                int numValues=0;
128
                                value = null;
129
                                key = (String) newKeysIterator.next();
130
                                
131
                                dbValue = database.getTranslation(lang, key);
132
                                ArrayList newKeyList = (ArrayList) newKeysDict.get(key);
133
                                String[] newKey;
134
                                boolean equal=true;
135
                                for (int j=0; j<newKeyList.size(); j++) {
136
                                        newKey = (String[]) newKeyList.get(j);
137
                                        if (!newKey[0].equals("")) {
138
                                                if (dbValue!=null && !dbValue.equals(newKey[0]))
139
                                                        equal = false;
140
                                                if (numValues==0) { //if there are several non-empty values, take the first one
141
                                                        value = newKey[0];
142
                                                }
143
                                                else if (!value.equals(newKey[0])) {
144
                                                        equal=false;
145
                                                }
146
                                                numValues++;        
147
                                        }
148
                                }
149
                                if (equal==false) {
150
                                        System.err.println("\nAtenci?n -- La clave '"+key+"' tiene diferentes valores para el idioma "  + lang + ".");
151
                                        System.err.println("Valor en base de datos: "+key+"="+dbValue);
152
                                        for (int j=0; j<newKeyList.size(); j++) {
153
                                                newKey = (String[]) newKeyList.get(j);
154
                                                System.err.println(newKey[1] + " -- " + key + "=" + newKey[0]);
155
                                        }
156
                                }
157
                                if (value!=null && !value.equals("")) { // the translation has a value
158
                                        if (dbValue==null) {
159
                                                // new translation
160
                                                database.setTranslation(lang, key, value);
161
                                                // it has been added to database, it isn't new anymore
162
                                                // we add the key to the list of keys to remove. We don't remove it now because then there is troubles with the iterator
163
                                                removedKeysDict.add(key);
164
                                        }
165
                                        else if (!dbValue.equals("")) {
166
                                                // if dbValue contains a translation, it isn't a new translation
167
                                                removedKeysDict.add(key);
168
                                        }
169
                                        /*
170
                                         * else { // if dbValue.equals(""), it means that the key has been changed, and it must be sent for translation
171
                                         *       //It should not be added to the database with the value from the property file, as it is not valid anymore.
172
                                         * 
173
                                         * }
174
                                         */
175
                                }
176
                        }
177
                }
178
                
179
                // remove 
180
                for (int i=0; i<config.languages.length; i++) {
181
                        lang = config.languages[i];
182
                        removedKeysDict = (ArrayList) removedKeys.get(lang);
183
                        newKeysDict = (TreeMap) newKeys.get(lang);
184
                        Iterator removeIterator = removedKeysDict.iterator();
185
                        while (removeIterator.hasNext()) {
186
                                key = (String) removeIterator.next();
187
                                newKeysDict.remove(key);
188
                        }
189
                }
190
                
191
                removedKeys = new HashMap();
192
                
193
                // we already added all the new keys with value to the database
194
                // now we try to find a translation for the keys without translation
195
                for (int i=0; i<config.languages.length; i++) {
196
                        lang = config.languages[i];
197
                        File langDir = new File(config.outputDir+File.separator+lang);
198
                        langDir.mkdirs();
199
                        
200
                        // process the keys
201
                        newKeysDict = (TreeMap) newKeys.get(lang);
202
                        removedKeysDict = new ArrayList();
203
                        removedKeys.put(lang, removedKeysDict);
204
                        Iterator newKeysIterator = newKeysDict.keySet().iterator();
205
                        while (newKeysIterator.hasNext()) {
206
                                key = (String) newKeysIterator.next();
207
                                value = "";
208
                                String auxValue;
209
                                for (int currentAuxLang=0; currentAuxLang<config.languages.length && (value==null || value.equals("")); currentAuxLang++) {
210
                                        auxLang = config.languages[currentAuxLang];
211
                                        auxValue = database.getTranslation(auxLang, key);
212
                                        if (auxValue!=null && !auxValue.equals("")) {
213
                                                ArrayList keyList = database.getAssociatedKeys(auxLang, value);
214
                                                if (keyList!=null) {
215
                                                        for (int j=0; j<keyList.size() && (value==null || !value.equals("")); j++) {
216
                                                                value = database.getTranslation(lang, (String)keyList.get(j));
217
                                                        }
218
                                                }
219
                                        }
220
                                }
221
                                if (value!=null && !value.equals("")) { // the translation has a value
222
                                        dbValue = database.getTranslation(lang, key);
223
                                        if (dbValue==null || !dbValue.equals("")) {
224
                                                /* if dbValue == "" means that the key has been changed and should be sent for translation.
225
                                                 * It should not be added to the database with the value from the property file, as it is not valid anymore.
226
                                                 */
227
                                                                                        
228
                                                database.setTranslation(lang, key, value);
229
                                                // it has been added to database, it isn't new anymore
230
                                                // we add the key to the list of keys to remove. We don't remove it now because then there is troubles with the iterator
231
                                                removedKeysDict.add(key);
232
                                        }
233
                                }
234
                        }
235
                }
236
                
237
                // remove 
238
                for (int i=0; i<config.languages.length; i++) {
239
                        lang = config.languages[i];
240
                        removedKeysDict = (ArrayList) removedKeys.get(lang);
241
                        newKeysDict = (TreeMap) newKeys.get(lang);
242
                        Iterator removeIterator = removedKeysDict.iterator();
243
                        while (removeIterator.hasNext()) {
244
                                key = (String) removeIterator.next();
245
                                newKeysDict.remove(key);
246
                        }
247
                }
248
                
249
                // output the keys to be translated
250
                outputNewKeys(newKeys);
251
                
252
                // update the values of each project's property files and store to disk
253
                saveKeys();
254
                
255
                // store datase to disk
256
                database.save();
257
        }
258
        
259
        private void outputNewKeys(HashMap newKeys) {
260
                String lang, auxLang;
261
                /**
262
                 * Process the new or changed keys
263
                 */
264
                for (int i=0; i<config.languages.length; i++) {
265
                        lang = config.languages[i];
266
                        File langDir = new File(config.outputDir+File.separator+lang);
267
                        langDir.mkdirs();
268
                        HashMap outputFiles = new HashMap();
269
                        HashMap outputPropertiesStream = new HashMap();
270
                        HashMap outputProperties = new HashMap();
271
                        
272
                        // open the output files, one for each defined language
273
                        for (int j=0; j<config.outputLanguages.length; j++) {
274
                                auxLang = config.outputLanguages[j];
275
                                FileOutputStream fos, fosProp;
276
                                OrderedProperties prop;
277
                                try {
278
                                        fos = new FileOutputStream(langDir.getPath()+File.separator+config.defaultBaseName+"_"+auxLang+".properties-"+config.outputEncoding);
279
                                        fosProp = new FileOutputStream(langDir.getPath()+File.separator+config.defaultBaseName+"_"+auxLang+".properties");
280
                                        prop = new OrderedProperties();
281
                                        outputPropertiesStream.put(auxLang, fosProp);
282
                                        outputProperties.put(auxLang, prop);
283
                                        try {
284
                                                outputFiles.put(auxLang, new BufferedWriter(new OutputStreamWriter(fos, config.outputEncoding)));
285
                                        } catch (UnsupportedEncodingException e) {
286
                                                // TODO Auto-generated catch block
287
                                                System.err.println(e.getLocalizedMessage());
288
                                                System.exit(-1);
289
                                        }
290
                                } catch (FileNotFoundException e) {
291
                                        // TODO Auto-generated catch block
292
                                        System.err.println(e.getLocalizedMessage());
293
                                        System.exit(-1);
294
                                }
295
                        }
296
                        
297
                        // also open the file for language we're processing currently
298
                        if (!outputFiles.containsKey(lang)) {
299
                                FileOutputStream fos, fosProp;
300
                                OrderedProperties prop;
301
                                try {
302
                                        fos = new FileOutputStream(langDir.getPath()+File.separator+config.defaultBaseName+"_"+lang+".properties-"+config.outputEncoding);
303
                                        fosProp = new FileOutputStream(langDir.getPath()+File.separator+config.defaultBaseName+"_"+lang+".properties");
304
                                        prop = new OrderedProperties();
305
                                        outputPropertiesStream.put(lang, fosProp);
306
                                        outputProperties.put(lang, prop);
307
                                        try {
308
                                                outputFiles.put(lang, new BufferedWriter(new OutputStreamWriter(fos, config.outputEncoding)));
309
                                        } catch (UnsupportedEncodingException e) {
310
                                                // TODO Auto-generated catch block
311
                                                System.err.println(e.getLocalizedMessage());
312
                                                System.exit(-1);
313
                                        }
314
                                } catch (FileNotFoundException e) {
315
                                        // TODO Auto-generated catch block
316
                                        System.err.println(e.getLocalizedMessage());
317
                                        System.exit(-1);
318
                                }
319
                        }
320
                        
321
                        TreeMap dict = (TreeMap) newKeys.get(lang);
322
                        Iterator keyIterator = dict.keySet().iterator();
323
                        String key, value;
324
                        while (keyIterator.hasNext()) {
325
                                key = (String) keyIterator.next();
326

    
327
                                Iterator files = outputFiles.keySet().iterator();
328
                                BufferedWriter writer;
329
                                while (files.hasNext()) {
330
                                        // we output the pair key-value for the defined output languages (they're used for reference by translators)                                                        
331
                                        auxLang = (String) files.next();
332
                                        writer = (BufferedWriter) outputFiles.get(auxLang);
333
                                        value = database.getTranslation(auxLang, key);
334
                                        try {
335
                                                if (value!=null)
336
                                                        writer.write(key+"="+value+"\n");
337
                                                else
338
                                                        writer.write(key+"=\n");
339
                                        } catch (IOException e) {
340
                                                // TODO Auto-generated catch block
341
                                                e.printStackTrace();
342
                                        }
343
                                }
344
                                Iterator props = outputProperties.keySet().iterator();
345
                                OrderedProperties prop;
346
                                while (props.hasNext()) {
347
                                        // we output the pair key-value for the defined output languages (they're used for reference by translators)                                                        
348
                                        auxLang = (String) props.next();
349
                                        prop = (OrderedProperties) outputProperties.get(auxLang);
350
                                        value = database.getTranslation(auxLang, key);
351
                                        if (value!=null)
352
                                                prop.put(key, value);
353
                                        else
354
                                                prop.put(key, "");
355
                                }
356
                        }
357
                        
358
                        Iterator props = outputProperties.keySet().iterator();
359
                        OrderedProperties prop;
360
                        FileOutputStream fos;
361
                        while (props.hasNext()) {
362
                                auxLang = (String) props.next();
363
                                fos = (FileOutputStream) outputPropertiesStream.get(auxLang);
364
                                prop = (OrderedProperties) outputProperties.get(auxLang);
365
                                try {
366
                                        prop.store(fos, "Translations for language ["+auxLang+"]");
367
                                } catch (IOException e) {
368
                                        // TODO Auto-generated catch block
369
                                        e.printStackTrace();
370
                                }
371
                        }
372
                        
373
                        // close the files now
374
                        Iterator files = outputFiles.keySet().iterator();
375
                        while (files.hasNext()) {                                                        
376
                                auxLang = (String) files.next();
377
                                BufferedWriter writer = (BufferedWriter) outputFiles.get(auxLang);
378
                                try {
379
                                        writer.close();
380
                                } catch (IOException e) {
381
                                        // do nothing here
382
                                }
383
                        }
384
                }                
385
        }
386
                
387
        private HashMap detectNewKeys() {
388
                Project currentProject;
389
                String lang;
390
                OrderedProperties dict;
391
                TreeMap auxDict;
392
                Iterator keys;
393
                String key, value, dbValue;
394
                HashMap newKeys = new HashMap();
395
                for (int i=0; i<config.languages.length; i++) {
396
                        lang = config.languages[i];
397
                        newKeys.put(lang, new TreeMap());
398
                }
399
        
400
                /**
401
                 * Detect the new or changed keys
402
                 * We just make a list, we will check the list later (to detect conflicting changes)
403
                 */
404
                for (int i=0; i<config.projects.size(); i++) {
405
                        currentProject = (Project) config.projects.get(i);
406
                        for (int j=0; j<config.languages.length; j++) {
407
                                lang = config.languages[j];
408
                                dict = (OrderedProperties) currentProject.dictionaries.get(lang);
409
                                keys = dict.keySet().iterator();
410
                                while (keys.hasNext()) {
411
                                        key = (String) keys.next();
412
                                        value = dict.getProperty(key);
413
                                        dbValue = database.getTranslation(lang, key);
414
                                        if (dbValue==null || dbValue.equals("") || (!value.equals("") && !dbValue.equals(value))) {
415
                                                String []newKey = new String[2];
416
                                                newKey[0]=value;
417
                                                newKey[1]= currentProject.dir;
418
                                                auxDict = (TreeMap) newKeys.get(lang);
419
                                                ArrayList keyList = (ArrayList) auxDict.get(key);
420
                                                if (keyList==null) {
421
                                                        keyList = new ArrayList();
422
                                                }
423
                                                keyList.add(newKey);
424
                                                auxDict.put(key, keyList);
425
                                        }
426
                                }
427
                        }
428
                }
429
                return newKeys;
430
        }
431
        
432
        private static void usage() {
433
                System.out.println("Uso: UpdateTrans [OPCION]");
434
                System.out.println("\t-c\t--config=configFile");
435
        }
436
        
437
        /**
438
         *  Reads the command line parameters */
439
        private boolean readParameters(String[] args) {
440
                String configPair[];
441

    
442
                for (int i=0; i<args.length; i++) {
443
                        configPair = args[i].split("=",2);
444
                        if ( (configPair[0].equals("-c") || configPair[0].equals("--config"))
445
                                        && configPair.length==2) {
446
                                configFileName = configPair[1];
447
                        }
448
                        else {
449
                                return false;
450
                        }
451
                }
452
                return true;
453
        }
454
        
455
        private boolean loadConfig() {
456
                config = new ConfigOptions(configFileName);
457
                config.load();
458
                return true;
459
        }
460
        
461
        /**
462
         * Reads the translation keys from the projects speficied in
463
         * the config file. The keys are stored for each project in
464
         * 'config.projects'. 
465
         * 
466
         * @return
467
         */
468
        private void loadKeys() {
469
                Keys keys = new Keys(config);
470
                keys.load();
471
        }
472
        
473
        private void saveKeys() {
474
                Project project;
475
                OrderedProperties dict;
476
                String lang;
477
                
478
                for (int currentProject=0; currentProject<config.projects.size(); currentProject++) {
479
                        project = ((Project)config.projects.get(currentProject));
480
                        
481
                        for (int currentLang=0; currentLang<config.languages.length; currentLang++) {
482
                                lang = (String) config.languages[currentLang];
483
                                dict = (OrderedProperties) project.dictionaries.get(lang);
484
                                String dbValue, key;
485

    
486
                                Iterator keysIterator = dict.keySet().iterator();
487
                                while (keysIterator.hasNext()) {
488
                                        key = (String) keysIterator.next();
489
                                        dbValue = database.getTranslation(lang, key);
490
                                        if (dbValue!=null) {
491
                                                dict.setProperty(key, dbValue);
492
                                        }
493
                                }
494
                        }
495
                }
496
                
497
                Keys keys = new Keys(config);
498
                keys.save();
499
        }
500
        
501
        private void loadDataBase() {
502
                database = new TranslationDatabase(config);
503
                database.load();
504
        }
505

    
506
}