Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libInternationalization / src-utils / org / gvsig / i18n / utils / UpdateTrans.java @ 6205

History | View | Annotate | Download (9.73 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.Enumeration;
57
import java.util.HashMap;
58
import java.util.HashSet;
59
import java.util.Iterator;
60
import java.util.Properties;
61

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

    
75
        /**
76
         * @param args
77
         */
78
        public static void main(String[] args) {
79
                UpdateTrans process = new UpdateTrans();
80
                
81
                // load command line parameters
82
                if (!process.readParameters(args)) {
83
                        usage();
84
                        System.exit(-1);
85
                }
86
                
87
                // transfer control to the program's main loop
88
                process.start();
89
        }
90
        
91
        private void start() {
92
                // load config options from the config file
93
                if (!loadConfig()) {
94
                        System.out.println("Error leyendo el fichero de configuraci?n.");
95
                        usage();
96
                        System.exit(-1);
97
                }
98
                
99
                loadKeys();
100
                loadDataBase();
101
                
102
                updateDB();
103
        }
104
        
105
        private void updateDB(){
106
                Project currentProject;
107
                String lang, auxLang;
108
                Properties dict,auxDict;
109
                Enumeration keys;
110
                String key, value, dbValue;
111
                HashMap newKeys = new HashMap();
112
                for (int i=0; i<config.languages.length; i++) {
113
                        lang = config.languages[i];
114
                        newKeys.put(lang, new Properties());
115
                }
116
                
117
                /**
118
                 * Detect the new or changed keys
119
                 * We just make a list, we will check the list later (to detect conflicting changes)
120
                 */
121
                for (int i=0; i<config.projects.size(); i++) {
122
                        currentProject = (Project) config.projects.get(i);
123
                        for (int j=0; j<config.languages.length; j++) {
124
                                lang = config.languages[j];
125
                                dict = (Properties) currentProject.dictionaries.get(lang);
126
                                if (dict==null)
127
                                        System.out.println("aqui");
128
                                keys = dict.keys();
129
                                while (keys.hasMoreElements()) {
130
                                        key = (String) keys.nextElement();
131
                                        value = dict.getProperty(key);
132
                                        dbValue = database.getTranslation(lang, key);
133
                                        if (dbValue==null || !dbValue.equals(value)) {
134
                                                String []newKey = new String[2];
135
                                                newKey[0]=value;
136
                                                newKey[1]= currentProject.dir;
137
                                                auxDict = (Properties) newKeys.get(lang);
138
                                                ArrayList keyList = (ArrayList) auxDict.get(key);
139
                                                if (keyList==null) {
140
                                                        keyList = new ArrayList();
141
                                                }
142
                                                keyList.add(newKey);
143
                                                auxDict.put(key, keyList);
144
                                        }
145
                                }
146
                        }
147
                }
148
                
149
                /**
150
                 * Process the new or changed keys
151
                 */
152
                for (int i=0; i<config.languages.length; i++) {
153
                        lang = config.languages[i];
154
                        File langDir = new File(config.outputDir+File.separator+lang);
155
                        langDir.mkdirs();
156
                        HashMap outputFiles = new HashMap();
157
                        
158
                        // open the output files, one for each defined language
159
                        for (int j=0; j<config.outputLanguages.length; j++) {
160
                                auxLang = config.outputLanguages[j];
161
                                FileOutputStream fos;
162
                                try {
163
                                        fos = new FileOutputStream(langDir.getPath()+File.separator+config.defaultBaseName+"_"+auxLang+".properties-"+config.outputEncoding);
164
                                        try {
165
                                                outputFiles.put(auxLang, new BufferedWriter(new OutputStreamWriter(fos, config.outputEncoding)));
166
                                        } catch (UnsupportedEncodingException e) {
167
                                                // TODO Auto-generated catch block
168
                                                System.err.println(e.getLocalizedMessage());
169
                                                System.exit(-1);
170
                                        }
171
                                } catch (FileNotFoundException e) {
172
                                        // TODO Auto-generated catch block
173
                                        System.err.println(e.getLocalizedMessage());
174
                                        System.exit(-1);
175
                                }
176
                        }
177
                        
178
                        // also open the file for language we're processing currently
179
                        if (!outputFiles.containsKey(lang)) {
180
                                FileOutputStream fos;
181
                                try {
182
                                        fos = new FileOutputStream(langDir.getPath()+File.separator+config.defaultBaseName+"_"+lang+".properties-"+config.outputEncoding);
183
                                        try {
184
                                                outputFiles.put(lang, new BufferedWriter(new OutputStreamWriter(fos, config.outputEncoding)));
185
                                        } catch (UnsupportedEncodingException e) {
186
                                                // TODO Auto-generated catch block
187
                                                System.err.println(e.getLocalizedMessage());
188
                                                System.exit(-1);
189
                                        }
190
                                } catch (FileNotFoundException e) {
191
                                        // TODO Auto-generated catch block
192
                                        System.err.println(e.getLocalizedMessage());
193
                                        System.exit(-1);
194
                                }
195
                        }
196

    
197
                        // process the keys
198
                        Properties newKeysDict = (Properties) newKeys.get(lang);
199
                        Iterator newKeysIterator = newKeysDict.keySet().iterator();
200
                        while (newKeysIterator.hasNext()) {
201
                                int numValues=0;
202
                                value = null;
203
                                String project=null;
204
                                key = (String) newKeysIterator.next();
205
                                ArrayList newKeyList = (ArrayList) newKeysDict.get(key);
206
                                String[] newKey;
207
                                for (int j=0; j<newKeyList.size(); j++) {
208
                                        newKey = (String[]) newKeyList.get(j);
209
                                        if (!newKey[0].equals("")) {
210
                                                if (numValues==0) { //if there are several non-empty values, take the first one
211
                                                        value = newKey[0];
212
                                                        project = newKey[1];
213
                                                numValues++;
214
                                        }
215
                                        if (numValues==2) {
216
                                                System.err.println("Atenci?n -- La clave '"+key+"' tiene diferentes valores para el idioma "  + lang + ".");
217
                                                System.err.println("\tSe usar? la primera de las mostradas a continuaci?n:");
218
                                                System.err.println(project + " -- " + key + "=" + value);
219
                                                System.err.println(newKey[1] + " -- " + key + "=" + newKey[0]);
220
                                        }
221
                                        else if (numValues>2)
222
                                                System.err.println(newKey[1] + " -- " + key + "=" + newKey[0]);
223
                                        }
224
                                }
225
                                if (value!=null) { // the translation has a value
226
                                        database.setTranslation(lang, key, value);
227
                                }
228
                                else {
229
                                        String auxKey;
230
                                        value = "";
231
                                        // try to search a matching translation from other keys in the database
232
                                        for (int j=0; j<config.languages.length && value!=null && !value.equals(""); j++) {
233
                                                // is there a value for this key in any language?
234
                                                auxLang = (String) config.languages[j];
235
                                                auxDict = (Properties) newKeys.get(lang);
236
                                                value = (String) auxDict.get(key);
237
                                                if (value!=null && !value.equals("")) {
238
                                                        // if there was a value in some language, search for other keys with the same value
239
                                                        auxKey = database.getAssociatedKey(auxLang, value);
240
                                                        if (auxKey!=null) {
241
                                                                // ok, we found the key, now let's try to search for the translation in the target language
242
                                                                value = database.getTranslation(lang, auxKey);
243
                                                        }
244
                                                        else
245
                                                                value = null;
246
                                                }
247
                                        }
248
                                        // we found a matching translation, great
249
                                        if (value!=null && !value.equals(""))
250
                                                database.setTranslation(lang, key, value);
251
                                        else {
252
                                                // we didn't find a valid translation, so the key needs to be sent for translation
253
                                                Iterator files = outputFiles.keySet().iterator();
254
                                                BufferedWriter writer;
255
                                                while (files.hasNext()) {
256
                                                        // we output the pair key-value for the defined output languages (they're used for reference by translators)                                                        
257
                                                        auxLang = (String) files.next();
258
                                                        writer = (BufferedWriter) outputFiles.get(auxLang);
259
                                                        value = database.getTranslation(auxLang, key);
260
                                                        try {
261
                                                                if (value!=null)
262
                                                                        writer.write(key+"="+value+"\n");
263
                                                                else
264
                                                                        writer.write(key+"=\n");
265
                                                        } catch (IOException e) {
266
                                                                // TODO Auto-generated catch block
267
                                                                e.printStackTrace();
268
                                                        }
269
                                                }
270
                                        }
271
                                }
272
                        }
273
                        // close the files now
274
                        Iterator files = outputFiles.keySet().iterator();
275
                        while (files.hasNext()) {                                                        
276
                                auxLang = (String) files.next();
277
                                BufferedWriter writer = (BufferedWriter) outputFiles.get(auxLang);
278
                                try {
279
                                        writer.close();
280
                                } catch (IOException e) {
281
                                        // do nothing here
282
                                }
283
                        }
284
                }
285
                
286
                database.save();
287
        }
288
        
289
        private static void usage() {
290
                System.out.println("Uso: UpdateTrans [OPCION]");
291
                System.out.println("\t-c\t--config=configFile");
292
        }
293
        
294
        /**
295
         *  Reads the command line parameters */
296
        private boolean readParameters(String[] args) {
297
                String configPair[];
298

    
299
                for (int i=0; i<args.length; i++) {
300
                        configPair = args[i].split("=",2);
301
                        if ( (configPair[0].equals("-c") || configPair[0].equals("--config"))
302
                                        && configPair.length==2) {
303
                                configFileName = configPair[1];
304
                        }
305
                        else {
306
                                return false;
307
                        }
308
                }
309
                return true;
310
        }
311
        
312
        private boolean loadConfig() {
313
                config = new ConfigOptions(configFileName);
314
                config.load();
315
                return true;
316
        }
317
        
318
        /**
319
         * Reads the translation keys from the projects speficied in
320
         * the config file. The keys are stored for each project in
321
         * 'config.projects'. 
322
         * 
323
         * @return
324
         */
325
        private void loadKeys() {
326
                Keys keys = new Keys(config);
327
                keys.load();
328
        }
329
        
330
        private void loadDataBase() {
331
                database = new TranslationDatabase(config);
332
                database.load();
333
        }
334
        
335

    
336
}