Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.i18n / utils / java / src / org / gvsig / i18n / utils / UpdateTrans.java @ 40559

History | View | Annotate | Download (16.3 KB)

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

    
65

    
66
/**
67
 * 
68
 */
69
package org.gvsig.i18n.utils;
70

    
71
import java.io.BufferedWriter;
72
import java.io.File;
73
import java.io.FileNotFoundException;
74
import java.io.FileOutputStream;
75
import java.io.IOException;
76
import java.io.OutputStreamWriter;
77
import java.io.UnsupportedEncodingException;
78
import java.util.ArrayList;
79
import java.util.HashMap;
80
import java.util.Iterator;
81
import java.util.TreeMap;
82

    
83
/**
84
 * @author cesar
85
 *
86
 */
87
public class UpdateTrans {
88
        // The filename which stores the configuration (may be overriden by the command line parameter)
89
        private String configFileName = "config.xml";
90
        
91
        // Object to load and store the config options
92
        private ConfigOptions config;
93
        
94
        private TranslationDatabase database;
95

    
96
        /**
97
         * @param args
98
         */
99
        public static void main(String[] args) {
100
                UpdateTrans process = new UpdateTrans();
101
                
102
                // load command line parameters
103
                if (!process.readParameters(args)) {
104
                        usage();
105
                        System.exit(-1);
106
                }
107
                
108
                // transfer control to the program's main loop
109
                process.start();
110
        }
111
        
112
        private void start() {
113
                // load config options from the config file
114
                if (!loadConfig()) {
115
                        System.out.println("Error leyendo el fichero de configuraci?n.");
116
                        usage();
117
                        System.exit(-1);
118
                }
119
                
120
                loadKeys();
121
                loadDataBase();
122
                
123
                updateDB();
124
        }
125
        
126
        private void updateDB(){
127
                String lang, auxLang;
128
                String key, value, dbValue;
129
                
130
                HashMap newKeys = detectNewKeys();
131
                TreeMap newKeysDict;
132
                HashMap removedKeys = new HashMap();
133
                
134
                ArrayList removedKeysDict;
135

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

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

    
465
                for (int i=0; i<args.length; i++) {
466
                        configPair = args[i].split("=",2);
467
                        if ( (configPair[0].equals("-c") || configPair[0].equals("--config"))
468
                                        && configPair.length==2) {
469
                                configFileName = configPair[1];
470
                        }
471
                        else {
472
                                return false;
473
                        }
474
                }
475
                return true;
476
        }
477
        
478
        private boolean loadConfig() {
479
                config = new ConfigOptions(configFileName);
480
                config.load();
481
                return true;
482
        }
483
        
484
        /**
485
         * Reads the translation keys from the projects speficied in
486
         * the config file. The keys are stored for each project in
487
         * 'config.projects'. 
488
         * 
489
         * @return
490
         */
491
        private void loadKeys() {
492
                Keys keys = new Keys(config);
493
                keys.load();
494
        }
495
        
496
        private void saveKeys() {
497
                Project project;
498
                OrderedProperties dict;
499
                String lang;
500
                
501
                for (int currentProject=0; currentProject<config.projects.size(); currentProject++) {
502
                        project = ((Project)config.projects.get(currentProject));
503
                        
504
                        for (int currentLang=0; currentLang<config.languages.length; currentLang++) {
505
                                lang = (String) config.languages[currentLang];
506
                                dict = (OrderedProperties) project.dictionaries.get(lang);
507
                                String dbValue, key;
508

    
509
                                Iterator keysIterator = dict.keySet().iterator();
510
                                while (keysIterator.hasNext()) {
511
                                        key = (String) keysIterator.next();
512
                                        dbValue = database.getTranslation(lang, key);
513
                                        if (dbValue!=null) {
514
                                                dict.setProperty(key, dbValue);
515
                                        }
516
                                }
517
                        }
518
                }
519
                
520
                Keys keys = new Keys(config);
521
                keys.save();
522
        }
523
        
524
        private void loadDataBase() {
525
                database = new TranslationDatabase(config);
526
                database.load();
527
        }
528

    
529
}