svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.i18n / utils / java / src / org / gvsig / i18n / utils / UpdateTrans.java @ 40596
History | View | Annotate | Download (15.1 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 |
package org.gvsig.i18n.utils; |
25 |
|
26 |
import java.io.BufferedWriter; |
27 |
import java.io.File; |
28 |
import java.io.FileNotFoundException; |
29 |
import java.io.FileOutputStream; |
30 |
import java.io.IOException; |
31 |
import java.io.OutputStreamWriter; |
32 |
import java.io.UnsupportedEncodingException; |
33 |
import java.util.ArrayList; |
34 |
import java.util.HashMap; |
35 |
import java.util.Iterator; |
36 |
import java.util.TreeMap; |
37 |
|
38 |
/**
|
39 |
* @author cesar
|
40 |
*
|
41 |
*/
|
42 |
public class UpdateTrans { |
43 |
// The filename which stores the configuration (may be overriden by the command line parameter)
|
44 |
private String configFileName = "config.xml"; |
45 |
|
46 |
// Object to load and store the config options
|
47 |
private ConfigOptions config;
|
48 |
|
49 |
private TranslationDatabase database;
|
50 |
|
51 |
/**
|
52 |
* @param args
|
53 |
*/
|
54 |
public static void main(String[] args) { |
55 |
UpdateTrans process = new UpdateTrans();
|
56 |
|
57 |
// load command line parameters
|
58 |
if (!process.readParameters(args)) {
|
59 |
usage(); |
60 |
System.exit(-1); |
61 |
} |
62 |
|
63 |
// transfer control to the program's main loop
|
64 |
process.start(); |
65 |
} |
66 |
|
67 |
private void start() { |
68 |
// load config options from the config file
|
69 |
if (!loadConfig()) {
|
70 |
System.out.println("Error leyendo el fichero de configuraci?n."); |
71 |
usage(); |
72 |
System.exit(-1); |
73 |
} |
74 |
|
75 |
loadKeys(); |
76 |
loadDataBase(); |
77 |
|
78 |
updateDB(); |
79 |
} |
80 |
|
81 |
private void updateDB(){ |
82 |
String lang, auxLang;
|
83 |
String key, value, dbValue;
|
84 |
|
85 |
HashMap newKeys = detectNewKeys();
|
86 |
TreeMap newKeysDict;
|
87 |
HashMap removedKeys = new HashMap(); |
88 |
|
89 |
ArrayList removedKeysDict;
|
90 |
|
91 |
/**
|
92 |
* Process the new or changed keys
|
93 |
*/
|
94 |
for (int i=0; i<config.languages.length; i++) { |
95 |
lang = config.languages[i]; |
96 |
File langDir = new File(config.outputDir+File.separator+lang); |
97 |
langDir.mkdirs(); |
98 |
|
99 |
// process the keys
|
100 |
newKeysDict = (TreeMap) newKeys.get(lang);
|
101 |
removedKeysDict = new ArrayList(); |
102 |
removedKeys.put(lang, removedKeysDict); |
103 |
Iterator newKeysIterator = newKeysDict.keySet().iterator();
|
104 |
while (newKeysIterator.hasNext()) {
|
105 |
int numValues=0; |
106 |
value = null;
|
107 |
key = (String) newKeysIterator.next();
|
108 |
|
109 |
dbValue = database.getTranslation(lang, key); |
110 |
ArrayList newKeyList = (ArrayList) newKeysDict.get(key); |
111 |
String[] newKey; |
112 |
boolean equal=true; |
113 |
for (int j=0; j<newKeyList.size(); j++) { |
114 |
newKey = (String[]) newKeyList.get(j); |
115 |
if (!newKey[0].equals("")) { |
116 |
if (dbValue!=null && !dbValue.equals(newKey[0])) |
117 |
equal = false;
|
118 |
if (numValues==0) { //if there are several non-empty values, take the first one |
119 |
value = newKey[0];
|
120 |
} |
121 |
else if (!value.equals(newKey[0])) { |
122 |
equal=false;
|
123 |
} |
124 |
numValues++; |
125 |
} |
126 |
} |
127 |
if (equal==false) { |
128 |
System.err.println("\nAtenci?n -- La clave '"+key+"' tiene diferentes valores para el idioma " + lang + "."); |
129 |
System.err.println("Valor en base de datos: "+key+"="+dbValue); |
130 |
for (int j=0; j<newKeyList.size(); j++) { |
131 |
newKey = (String[]) newKeyList.get(j); |
132 |
System.err.println(newKey[1] + " -- " + key + "=" + newKey[0]); |
133 |
} |
134 |
} |
135 |
if (value!=null && !value.equals("")) { // the translation has a value |
136 |
if (dbValue==null) { |
137 |
// new translation
|
138 |
database.setTranslation(lang, key, value); |
139 |
// it has been added to database, it isn't new anymore
|
140 |
// 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
|
141 |
removedKeysDict.add(key); |
142 |
} |
143 |
else if (!dbValue.equals("")) { |
144 |
// if dbValue contains a translation, it isn't a new translation
|
145 |
removedKeysDict.add(key); |
146 |
} |
147 |
/*
|
148 |
* else { // if dbValue.equals(""), it means that the key has been changed, and it must be sent for translation
|
149 |
* //It should not be added to the database with the value from the property file, as it is not valid anymore.
|
150 |
*
|
151 |
* }
|
152 |
*/
|
153 |
} |
154 |
} |
155 |
} |
156 |
|
157 |
// remove
|
158 |
for (int i=0; i<config.languages.length; i++) { |
159 |
lang = config.languages[i]; |
160 |
removedKeysDict = (ArrayList) removedKeys.get(lang);
|
161 |
newKeysDict = (TreeMap) newKeys.get(lang);
|
162 |
Iterator removeIterator = removedKeysDict.iterator();
|
163 |
while (removeIterator.hasNext()) {
|
164 |
key = (String) removeIterator.next();
|
165 |
newKeysDict.remove(key); |
166 |
} |
167 |
} |
168 |
|
169 |
removedKeys = new HashMap(); |
170 |
|
171 |
// we already added all the new keys with value to the database
|
172 |
// now we try to find a translation for the keys without translation
|
173 |
for (int i=0; i<config.languages.length; i++) { |
174 |
lang = config.languages[i]; |
175 |
File langDir = new File(config.outputDir+File.separator+lang); |
176 |
langDir.mkdirs(); |
177 |
|
178 |
// process the keys
|
179 |
newKeysDict = (TreeMap) newKeys.get(lang);
|
180 |
removedKeysDict = new ArrayList(); |
181 |
removedKeys.put(lang, removedKeysDict); |
182 |
Iterator newKeysIterator = newKeysDict.keySet().iterator();
|
183 |
while (newKeysIterator.hasNext()) {
|
184 |
key = (String) newKeysIterator.next();
|
185 |
value = "";
|
186 |
String auxValue;
|
187 |
for (int currentAuxLang=0; currentAuxLang<config.languages.length && (value==null || value.equals("")); currentAuxLang++) { |
188 |
auxLang = config.languages[currentAuxLang]; |
189 |
auxValue = database.getTranslation(auxLang, key); |
190 |
if (auxValue!=null && !auxValue.equals("")) { |
191 |
ArrayList keyList = database.getAssociatedKeys(auxLang, value);
|
192 |
if (keyList!=null) { |
193 |
for (int j=0; j<keyList.size() && (value==null || !value.equals("")); j++) { |
194 |
value = database.getTranslation(lang, (String)keyList.get(j));
|
195 |
} |
196 |
} |
197 |
} |
198 |
} |
199 |
if (value!=null && !value.equals("")) { // the translation has a value |
200 |
dbValue = database.getTranslation(lang, key); |
201 |
if (dbValue==null || !dbValue.equals("")) { |
202 |
/* if dbValue == "" means that the key has been changed and should be sent for translation.
|
203 |
* It should not be added to the database with the value from the property file, as it is not valid anymore.
|
204 |
*/
|
205 |
|
206 |
database.setTranslation(lang, key, value); |
207 |
// it has been added to database, it isn't new anymore
|
208 |
// 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
|
209 |
removedKeysDict.add(key); |
210 |
} |
211 |
} |
212 |
} |
213 |
} |
214 |
|
215 |
// remove
|
216 |
for (int i=0; i<config.languages.length; i++) { |
217 |
lang = config.languages[i]; |
218 |
removedKeysDict = (ArrayList) removedKeys.get(lang);
|
219 |
newKeysDict = (TreeMap) newKeys.get(lang);
|
220 |
Iterator removeIterator = removedKeysDict.iterator();
|
221 |
while (removeIterator.hasNext()) {
|
222 |
key = (String) removeIterator.next();
|
223 |
newKeysDict.remove(key); |
224 |
} |
225 |
} |
226 |
|
227 |
// output the keys to be translated
|
228 |
outputNewKeys(newKeys); |
229 |
|
230 |
// update the values of each project's property files and store to disk
|
231 |
saveKeys(); |
232 |
|
233 |
// store datase to disk
|
234 |
database.save(); |
235 |
} |
236 |
|
237 |
private void outputNewKeys(HashMap newKeys) { |
238 |
String lang, auxLang;
|
239 |
/**
|
240 |
* Process the new or changed keys
|
241 |
*/
|
242 |
for (int i=0; i<config.languages.length; i++) { |
243 |
lang = config.languages[i]; |
244 |
File langDir = new File(config.outputDir+File.separator+lang); |
245 |
langDir.mkdirs(); |
246 |
HashMap outputFiles = new HashMap(); |
247 |
HashMap outputPropertiesStream = new HashMap(); |
248 |
HashMap outputProperties = new HashMap(); |
249 |
|
250 |
// open the output files, one for each defined language
|
251 |
for (int j=0; j<config.outputLanguages.length; j++) { |
252 |
auxLang = config.outputLanguages[j]; |
253 |
FileOutputStream fos, fosProp;
|
254 |
OrderedProperties prop; |
255 |
try {
|
256 |
fos = new FileOutputStream(langDir.getPath()+File.separator+config.defaultBaseName+"_"+auxLang+".properties-"+config.outputEncoding); |
257 |
fosProp = new FileOutputStream(langDir.getPath()+File.separator+config.defaultBaseName+"_"+auxLang+".properties"); |
258 |
prop = new OrderedProperties();
|
259 |
outputPropertiesStream.put(auxLang, fosProp); |
260 |
outputProperties.put(auxLang, prop); |
261 |
try {
|
262 |
outputFiles.put(auxLang, new BufferedWriter(new OutputStreamWriter(fos, config.outputEncoding))); |
263 |
} catch (UnsupportedEncodingException e) { |
264 |
// TODO Auto-generated catch block
|
265 |
System.err.println(e.getLocalizedMessage());
|
266 |
System.exit(-1); |
267 |
} |
268 |
} catch (FileNotFoundException e) { |
269 |
// TODO Auto-generated catch block
|
270 |
System.err.println(e.getLocalizedMessage());
|
271 |
System.exit(-1); |
272 |
} |
273 |
} |
274 |
|
275 |
// also open the file for language we're processing currently
|
276 |
if (!outputFiles.containsKey(lang)) {
|
277 |
FileOutputStream fos, fosProp;
|
278 |
OrderedProperties prop; |
279 |
try {
|
280 |
fos = new FileOutputStream(langDir.getPath()+File.separator+config.defaultBaseName+"_"+lang+".properties-"+config.outputEncoding); |
281 |
fosProp = new FileOutputStream(langDir.getPath()+File.separator+config.defaultBaseName+"_"+lang+".properties"); |
282 |
prop = new OrderedProperties();
|
283 |
outputPropertiesStream.put(lang, fosProp); |
284 |
outputProperties.put(lang, prop); |
285 |
try {
|
286 |
outputFiles.put(lang, new BufferedWriter(new OutputStreamWriter(fos, config.outputEncoding))); |
287 |
} catch (UnsupportedEncodingException e) { |
288 |
// TODO Auto-generated catch block
|
289 |
System.err.println(e.getLocalizedMessage());
|
290 |
System.exit(-1); |
291 |
} |
292 |
} catch (FileNotFoundException e) { |
293 |
// TODO Auto-generated catch block
|
294 |
System.err.println(e.getLocalizedMessage());
|
295 |
System.exit(-1); |
296 |
} |
297 |
} |
298 |
|
299 |
TreeMap dict = (TreeMap) newKeys.get(lang); |
300 |
Iterator keyIterator = dict.keySet().iterator();
|
301 |
String key, value;
|
302 |
while (keyIterator.hasNext()) {
|
303 |
key = (String) keyIterator.next();
|
304 |
|
305 |
Iterator files = outputFiles.keySet().iterator();
|
306 |
BufferedWriter writer;
|
307 |
while (files.hasNext()) {
|
308 |
// we output the pair key-value for the defined output languages (they're used for reference by translators)
|
309 |
auxLang = (String) files.next();
|
310 |
writer = (BufferedWriter) outputFiles.get(auxLang);
|
311 |
value = database.getTranslation(auxLang, key); |
312 |
try {
|
313 |
if (value!=null) |
314 |
writer.write(key+"="+value+"\n"); |
315 |
else
|
316 |
writer.write(key+"=\n");
|
317 |
} catch (IOException e) { |
318 |
// TODO Auto-generated catch block
|
319 |
e.printStackTrace(); |
320 |
} |
321 |
} |
322 |
Iterator props = outputProperties.keySet().iterator();
|
323 |
OrderedProperties prop; |
324 |
while (props.hasNext()) {
|
325 |
// we output the pair key-value for the defined output languages (they're used for reference by translators)
|
326 |
auxLang = (String) props.next();
|
327 |
prop = (OrderedProperties) outputProperties.get(auxLang); |
328 |
value = database.getTranslation(auxLang, key); |
329 |
if (value!=null) |
330 |
prop.put(key, value); |
331 |
else
|
332 |
prop.put(key, "");
|
333 |
} |
334 |
} |
335 |
|
336 |
Iterator props = outputProperties.keySet().iterator();
|
337 |
OrderedProperties prop; |
338 |
FileOutputStream fos;
|
339 |
while (props.hasNext()) {
|
340 |
auxLang = (String) props.next();
|
341 |
fos = (FileOutputStream) outputPropertiesStream.get(auxLang);
|
342 |
prop = (OrderedProperties) outputProperties.get(auxLang); |
343 |
try {
|
344 |
prop.store(fos, "Translations for language ["+auxLang+"]"); |
345 |
} catch (IOException e) { |
346 |
// TODO Auto-generated catch block
|
347 |
e.printStackTrace(); |
348 |
} |
349 |
} |
350 |
|
351 |
// close the files now
|
352 |
Iterator files = outputFiles.keySet().iterator();
|
353 |
while (files.hasNext()) {
|
354 |
auxLang = (String) files.next();
|
355 |
BufferedWriter writer = (BufferedWriter) outputFiles.get(auxLang); |
356 |
try {
|
357 |
writer.close(); |
358 |
} catch (IOException e) { |
359 |
// do nothing here
|
360 |
} |
361 |
} |
362 |
} |
363 |
} |
364 |
|
365 |
private HashMap detectNewKeys() { |
366 |
Project currentProject; |
367 |
String lang;
|
368 |
OrderedProperties dict; |
369 |
TreeMap auxDict;
|
370 |
Iterator keys;
|
371 |
String key, value, dbValue;
|
372 |
HashMap newKeys = new HashMap(); |
373 |
for (int i=0; i<config.languages.length; i++) { |
374 |
lang = config.languages[i]; |
375 |
newKeys.put(lang, new TreeMap()); |
376 |
} |
377 |
|
378 |
/**
|
379 |
* Detect the new or changed keys
|
380 |
* We just make a list, we will check the list later (to detect conflicting changes)
|
381 |
*/
|
382 |
for (int i=0; i<config.projects.size(); i++) { |
383 |
currentProject = (Project) config.projects.get(i); |
384 |
for (int j=0; j<config.languages.length; j++) { |
385 |
lang = config.languages[j]; |
386 |
dict = (OrderedProperties) currentProject.dictionaries.get(lang); |
387 |
keys = dict.keySet().iterator(); |
388 |
while (keys.hasNext()) {
|
389 |
key = (String) keys.next();
|
390 |
value = dict.getProperty(key); |
391 |
dbValue = database.getTranslation(lang, key); |
392 |
if (dbValue==null || dbValue.equals("") || (!value.equals("") && !dbValue.equals(value))) { |
393 |
String []newKey = new String[2]; |
394 |
newKey[0]=value;
|
395 |
newKey[1]= currentProject.dir;
|
396 |
auxDict = (TreeMap) newKeys.get(lang);
|
397 |
ArrayList keyList = (ArrayList) auxDict.get(key); |
398 |
if (keyList==null) { |
399 |
keyList = new ArrayList(); |
400 |
} |
401 |
keyList.add(newKey); |
402 |
auxDict.put(key, keyList); |
403 |
} |
404 |
} |
405 |
} |
406 |
} |
407 |
return newKeys;
|
408 |
} |
409 |
|
410 |
private static void usage() { |
411 |
System.out.println("Uso: UpdateTrans [OPCION]"); |
412 |
System.out.println("\t-c\t--config=configFile"); |
413 |
} |
414 |
|
415 |
/**
|
416 |
* Reads the command line parameters */
|
417 |
private boolean readParameters(String[] args) { |
418 |
String configPair[]; |
419 |
|
420 |
for (int i=0; i<args.length; i++) { |
421 |
configPair = args[i].split("=",2); |
422 |
if ( (configPair[0].equals("-c") || configPair[0].equals("--config")) |
423 |
&& configPair.length==2) {
|
424 |
configFileName = configPair[1];
|
425 |
} |
426 |
else {
|
427 |
return false; |
428 |
} |
429 |
} |
430 |
return true; |
431 |
} |
432 |
|
433 |
private boolean loadConfig() { |
434 |
config = new ConfigOptions(configFileName);
|
435 |
config.load(); |
436 |
return true; |
437 |
} |
438 |
|
439 |
/**
|
440 |
* Reads the translation keys from the projects speficied in
|
441 |
* the config file. The keys are stored for each project in
|
442 |
* 'config.projects'.
|
443 |
*
|
444 |
* @return
|
445 |
*/
|
446 |
private void loadKeys() { |
447 |
Keys keys = new Keys(config);
|
448 |
keys.load(); |
449 |
} |
450 |
|
451 |
private void saveKeys() { |
452 |
Project project; |
453 |
OrderedProperties dict; |
454 |
String lang;
|
455 |
|
456 |
for (int currentProject=0; currentProject<config.projects.size(); currentProject++) { |
457 |
project = ((Project)config.projects.get(currentProject)); |
458 |
|
459 |
for (int currentLang=0; currentLang<config.languages.length; currentLang++) { |
460 |
lang = (String) config.languages[currentLang];
|
461 |
dict = (OrderedProperties) project.dictionaries.get(lang); |
462 |
String dbValue, key;
|
463 |
|
464 |
Iterator keysIterator = dict.keySet().iterator();
|
465 |
while (keysIterator.hasNext()) {
|
466 |
key = (String) keysIterator.next();
|
467 |
dbValue = database.getTranslation(lang, key); |
468 |
if (dbValue!=null) { |
469 |
dict.setProperty(key, dbValue); |
470 |
} |
471 |
} |
472 |
} |
473 |
} |
474 |
|
475 |
Keys keys = new Keys(config);
|
476 |
keys.save(); |
477 |
} |
478 |
|
479 |
private void loadDataBase() { |
480 |
database = new TranslationDatabase(config);
|
481 |
database.load(); |
482 |
} |
483 |
|
484 |
} |