svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.i18n / utils / java / src / org / gvsig / i18n / utils / Keys.java @ 40769
History | View | Annotate | Download (14.7 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.BufferedReader; |
27 |
import java.io.File; |
28 |
import java.io.FileInputStream; |
29 |
import java.io.FileNotFoundException; |
30 |
import java.io.FileOutputStream; |
31 |
import java.io.IOException; |
32 |
import java.io.InputStreamReader; |
33 |
import java.io.UnsupportedEncodingException; |
34 |
import java.util.HashMap; |
35 |
import java.util.HashSet; |
36 |
import java.util.Iterator; |
37 |
import java.util.regex.Matcher; |
38 |
import java.util.regex.Pattern; |
39 |
|
40 |
import org.kxml2.io.KXmlParser; |
41 |
import org.xmlpull.v1.XmlPullParserException; |
42 |
|
43 |
/**
|
44 |
* @author cesar
|
45 |
*
|
46 |
*/
|
47 |
public class Keys { |
48 |
private ConfigOptions config;
|
49 |
|
50 |
public Keys(ConfigOptions config) {
|
51 |
this.config = config;
|
52 |
} |
53 |
|
54 |
public void load() { |
55 |
Project project; |
56 |
for (int currentProject=0; currentProject<config.projects.size(); currentProject++) { |
57 |
project = ((Project)config.projects.get(currentProject)); |
58 |
/**
|
59 |
* There is two options, "properties" and "sources". "sources" is the default, so
|
60 |
* if there was something different to "properties", we assume "sources".
|
61 |
*/
|
62 |
if (!project.sourceKeys.equals("properties")) { |
63 |
project.dictionaries = loadProjectFromSources(project); |
64 |
} |
65 |
else {
|
66 |
// load the keys for each language from the property file
|
67 |
project.dictionaries = loadProjectFromProperties(project); |
68 |
// add missing keys tp each language
|
69 |
completeKeys(project); |
70 |
} |
71 |
} |
72 |
} |
73 |
|
74 |
public void save() { |
75 |
Project project; |
76 |
OrderedProperties dict; |
77 |
FileOutputStream stream=null; |
78 |
String lang;
|
79 |
|
80 |
for (int currentProject=0; currentProject<config.projects.size(); currentProject++) { |
81 |
project = ((Project)config.projects.get(currentProject)); |
82 |
|
83 |
for (int currentLang=0; currentLang<config.languages.length; currentLang++) { |
84 |
lang = (String) config.languages[currentLang];
|
85 |
dict = (OrderedProperties) project.dictionaries.get(lang); |
86 |
|
87 |
if (dict.size()>0) { |
88 |
// ensure the directory exists
|
89 |
File propertyDir = new File(project.propertyDir); |
90 |
if (propertyDir.mkdirs())
|
91 |
System.out.println("Aviso -- directorio creado: "+project.propertyDir); |
92 |
|
93 |
try {
|
94 |
// different for spanish...
|
95 |
if (lang.equals("es")) { |
96 |
stream = new FileOutputStream(project.propertyDir+File.separator+project.basename+".properties"); |
97 |
} |
98 |
else {
|
99 |
stream = new FileOutputStream(project.propertyDir+File.separator+project.basename+"_"+lang+".properties"); |
100 |
} |
101 |
} catch (FileNotFoundException e) { |
102 |
// TODO Auto-generated catch block
|
103 |
e.printStackTrace(); |
104 |
} |
105 |
|
106 |
try {
|
107 |
dict.store(stream, "Translations for language ["+lang+"]"); |
108 |
} catch (IOException e) { |
109 |
// TODO Auto-generated catch block
|
110 |
e.printStackTrace(); |
111 |
} |
112 |
} |
113 |
} |
114 |
} |
115 |
} |
116 |
|
117 |
private HashMap loadProjectFromSources(Project project) { |
118 |
// always start with an empty HashMap when loading
|
119 |
HashMap dictionaries = new HashMap(); |
120 |
String lang;
|
121 |
|
122 |
/**
|
123 |
* The keys obtained from the sources and the config.xml files of the
|
124 |
* plugins.
|
125 |
*/
|
126 |
HashSet keys = new HashSet(); |
127 |
/**
|
128 |
* The translations loaded from the property files of the project.
|
129 |
*/
|
130 |
|
131 |
dictionaries = loadProjectFromProperties(project); |
132 |
|
133 |
for (int i=0; i<project.srcDirs.length; i++) { |
134 |
try {
|
135 |
keys = loadKeysFromSources(ConfigOptions.getAbsolutePath(File.separator, project.dir+File.separator+project.srcDirs[i]), keys); |
136 |
} |
137 |
catch (IOException ex) { |
138 |
// It there was an error reading the directory, just warn and skip the dir
|
139 |
System.err.println(project.dir +" -- Aviso: no se pudo leer el directorio "+project.dir+File.separator+project.srcDirs[i]); |
140 |
} |
141 |
} |
142 |
|
143 |
for (int currentLang=0; currentLang<config.languages.length; currentLang++) { |
144 |
lang = config.languages[currentLang]; |
145 |
|
146 |
OrderedProperties currentDict = (OrderedProperties) dictionaries.get(lang); |
147 |
Iterator keysIterator = keys.iterator();
|
148 |
String key;
|
149 |
// add missing keys
|
150 |
while (keysIterator.hasNext()) {
|
151 |
key = (String) keysIterator.next();
|
152 |
if (!currentDict.containsKey(key)) {
|
153 |
currentDict.put(key, "");
|
154 |
System.out.println(project.dir+" -- Aviso -- clave a?adida: "+key); |
155 |
} |
156 |
} |
157 |
|
158 |
// remove extra keys
|
159 |
// first make a list of keys to remove, because it's not possible to access the iterator and the TreeMap at the same time
|
160 |
/* HashSet removedKeys = new HashSet();
|
161 |
Iterator dictKey = currentDict.keySet().iterator();
|
162 |
while (dictKey.hasNext()) {
|
163 |
key = (String) dictKey.next();
|
164 |
if (!keys.contains(key)) {
|
165 |
removedKeys.add(key);
|
166 |
System.out.println(project.dir+" -- Aviso -- clave eliminada: "+key);
|
167 |
}
|
168 |
}
|
169 |
// now we really remove the keys
|
170 |
Iterator removedKeysIt = removedKeys.iterator();
|
171 |
while (removedKeysIt.hasNext()) {
|
172 |
key = (String) removedKeysIt.next();
|
173 |
currentDict.remove(key);
|
174 |
}*/
|
175 |
} |
176 |
|
177 |
return dictionaries;
|
178 |
} |
179 |
|
180 |
/**
|
181 |
* Reads the keys from all the languages, to make a unique list containing
|
182 |
* all the keys.
|
183 |
*/
|
184 |
public void completeKeys(Project project) { |
185 |
/* The list of all the keys */
|
186 |
HashSet keys = new HashSet(); |
187 |
// always start with an empty HashMap when loading
|
188 |
//HashMap dictionaries = new HashMap();
|
189 |
String lang;
|
190 |
|
191 |
// calculate all the keys
|
192 |
for (int currentLang=0; currentLang<config.languages.length; currentLang++) { |
193 |
lang = config.languages[currentLang]; |
194 |
|
195 |
OrderedProperties currentDict = (OrderedProperties) project.dictionaries.get(lang); |
196 |
if (currentDict==null) { |
197 |
currentDict = new OrderedProperties();
|
198 |
project.dictionaries.put(lang, currentDict); |
199 |
} |
200 |
else {
|
201 |
Iterator keysIterator = currentDict.keySet().iterator();
|
202 |
String key;
|
203 |
// add missing keys
|
204 |
while (keysIterator.hasNext()) {
|
205 |
key = (String) keysIterator.next();
|
206 |
keys.add(key); |
207 |
} |
208 |
} |
209 |
} |
210 |
|
211 |
// add missing keys to each language
|
212 |
for (int currentLang=0; currentLang<config.languages.length; currentLang++) { |
213 |
lang = config.languages[currentLang]; |
214 |
|
215 |
OrderedProperties currentDict = (OrderedProperties) project.dictionaries.get(lang); |
216 |
Iterator keysIterator = keys.iterator();
|
217 |
String key;
|
218 |
// add missing keys
|
219 |
while (keysIterator.hasNext()) {
|
220 |
key = (String) keysIterator.next();
|
221 |
if (!currentDict.containsKey(key)) {
|
222 |
currentDict.put(key, "");
|
223 |
System.out.println(project.dir+" -- Aviso -- clave a?adida: "+key); |
224 |
} |
225 |
} |
226 |
} |
227 |
} |
228 |
|
229 |
|
230 |
private HashSet loadKeysFromSources(String directory, HashSet keys) { |
231 |
String key;
|
232 |
File dir = new File(directory); |
233 |
File files[] = dir.listFiles(); |
234 |
final int BLOCKSIZE = 8192; |
235 |
char[] partialBuffer = new char[BLOCKSIZE+1]; |
236 |
String text;
|
237 |
StringBuffer buffer;
|
238 |
|
239 |
// stores the position of the newlines
|
240 |
//ArrayList newLines = new ArrayList();
|
241 |
// int lineNumber;
|
242 |
|
243 |
if (files!=null) { |
244 |
//Pattern keyPattern = Pattern.compile("(PluginServices|Messages)\\.(getText|getString|get)\\([^\"\\)]*\"([^\"]*)\"[^\\)]*\\)");
|
245 |
Pattern keyPattern = Pattern.compile( |
246 |
"(PluginServices|Messages)\\p{Space}*\\.\\p{Space}*(getText|getString|get)\\p{Space}*\\([^\"\\)]*\"([^\"]*)\"[^\\+\\)]*(\\+[^\\)]*)*\\)");
|
247 |
Matcher keyMatcher = keyPattern.matcher(""); |
248 |
//"(PluginServices|Messages)\\.(getText|getString|get)\\p{Space}*\\([^\"\\)]*\"([^\"]*)\"[^\\+\\)]*(\\+[^\"]*\"([^\"]*)\"[^\\+\\)]*)*\\)");
|
249 |
//Pattern newLinePattern = Pattern.compile("\n");
|
250 |
//Matcher newLineMatcher = newLinePattern.matcher("");
|
251 |
|
252 |
for (int i=0; i<files.length; i++) { |
253 |
if (files[i].isDirectory()) {
|
254 |
keys = loadKeysFromSources(files[i].toString(), keys); |
255 |
continue;
|
256 |
} |
257 |
else if (files[i].getName().toLowerCase().equals("PluginServices.java")) { |
258 |
|
259 |
//[Messages.]getText(...)
|
260 |
Pattern PsPattern = Pattern.compile("(Messages\\.)*getText\\([^\"\\)]*\"([^\"]*)\"[^\\)]*\\)"); |
261 |
Matcher PsMatcher = PsPattern.matcher(""); |
262 |
|
263 |
FileInputStream fis=null; |
264 |
try {
|
265 |
fis = new FileInputStream(files[i]); |
266 |
|
267 |
buffer = new StringBuffer(); |
268 |
BufferedReader currentFile=null; |
269 |
|
270 |
currentFile = new BufferedReader(new InputStreamReader(fis, config.sourcesEncoding)); |
271 |
while (currentFile.read(partialBuffer, 0, BLOCKSIZE)!=-1) { |
272 |
buffer.append(partialBuffer); |
273 |
} |
274 |
text = buffer.toString(); |
275 |
|
276 |
|
277 |
PsMatcher.reset(text); |
278 |
|
279 |
while (PsMatcher.find()) {
|
280 |
key = PsMatcher.group(2);
|
281 |
if (!key.equals("")) |
282 |
keys.add(key); |
283 |
} |
284 |
currentFile.close(); |
285 |
} catch (UnsupportedEncodingException e1) { |
286 |
System.err.println(e1.getLocalizedMessage());
|
287 |
continue;
|
288 |
} |
289 |
catch (IOException e1) { |
290 |
System.err.println(e1.getLocalizedMessage());
|
291 |
continue;
|
292 |
} |
293 |
} |
294 |
else if (files[i].getName().toLowerCase().endsWith(".java")) { |
295 |
FileInputStream fis=null; |
296 |
try {
|
297 |
fis = new FileInputStream(files[i]); |
298 |
BufferedReader currentFile=null; |
299 |
|
300 |
currentFile = new BufferedReader(new InputStreamReader(fis, config.sourcesEncoding)); |
301 |
buffer = new StringBuffer(); |
302 |
|
303 |
int readChars; // number of characters which were read |
304 |
while ( (readChars = currentFile.read(partialBuffer, 0, BLOCKSIZE)) != -1) { |
305 |
buffer.append(partialBuffer, 0, readChars);
|
306 |
} |
307 |
text = buffer.toString(); |
308 |
|
309 |
/*newLineMatcher.reset(text);
|
310 |
while (newLineMatcher.find()) {
|
311 |
newLines.add(new Integer(newLineMatcher.end()-1));
|
312 |
}*/
|
313 |
// lineNumber=1;
|
314 |
|
315 |
keyMatcher.reset(text); |
316 |
|
317 |
while (keyMatcher.find()) {
|
318 |
|
319 |
// find out in which line number we are
|
320 |
/*while (keyMatcher.start() > ((Integer)newLines.get(lineNumber)).intValue() ) lineNumber++;
|
321 |
System.out.println("FileName: "+files[i].getCanonicalPath()+"; lineNumber: "+lineNumber);*/
|
322 |
|
323 |
// StringBuffer keyBuffer = new StringBuffer();
|
324 |
|
325 |
// for (int ii=0; ii<=keyMatcher.groupCount(); ii++) {
|
326 |
// System.out.println("group: "+ ii+ "; " + keyMatcher.group(ii));
|
327 |
// }
|
328 |
// for (int groupNumb=3; groupNumb<=keyMatcher.groupCount(); groupNumb+=2) {
|
329 |
// if (keyMatcher.group(groupNumb)!=null)
|
330 |
// keyBuffer.append(keyMatcher.group(groupNumb));
|
331 |
// }
|
332 |
|
333 |
if (keyMatcher.group(4)!=null) { |
334 |
System.err.println("\nError: clave repartida en varias l?neas"); |
335 |
System.err.println("Fichero: "+files[i].getCanonicalPath()); |
336 |
System.err.println("C?digo: "); |
337 |
System.err.println(keyMatcher.group());
|
338 |
} |
339 |
else {
|
340 |
key = keyMatcher.group(3);
|
341 |
if (!key.equals("")) |
342 |
keys.add(key); |
343 |
} |
344 |
} |
345 |
currentFile.close(); |
346 |
} catch (UnsupportedEncodingException e1) { |
347 |
System.err.println(e1.getLocalizedMessage());
|
348 |
continue;
|
349 |
} |
350 |
catch (IOException e1) { |
351 |
System.err.println(e1.getLocalizedMessage());
|
352 |
continue;
|
353 |
} |
354 |
} |
355 |
else if (files[i].getName().equalsIgnoreCase("config.xml")) { |
356 |
keys = loadKeysFromXml(files[i], keys); |
357 |
} |
358 |
} |
359 |
} |
360 |
|
361 |
return keys;
|
362 |
} |
363 |
|
364 |
|
365 |
private HashSet loadKeysFromXml(File fileName, HashSet keys) { |
366 |
KXmlParser parser = new KXmlParser();
|
367 |
String tagname, attribute;
|
368 |
|
369 |
// we use null encoding, in this way kxml2 tries to detect the encoding
|
370 |
try {
|
371 |
parser.setInput(new FileInputStream(fileName), null); |
372 |
} catch (FileNotFoundException e1) { |
373 |
System.err.println(e1.getLocalizedMessage());
|
374 |
return keys;
|
375 |
} catch (XmlPullParserException e1) {
|
376 |
// No podemos leer el fichero de configuraci?n. Usamos valores por defecto
|
377 |
System.err.println("Aviso: error al cargar el fichero "+fileName); |
378 |
return keys;
|
379 |
} |
380 |
|
381 |
try {
|
382 |
for (parser.next(); parser.getEventType()!=KXmlParser.END_DOCUMENT; parser.next()) {
|
383 |
// este bucle externo recorre las etiquetas de primer y segundo nivel
|
384 |
if (parser.getEventType()==KXmlParser.START_TAG) {
|
385 |
tagname = parser.getName(); |
386 |
if (tagname.equals("menu") || tagname.equals("action-tool") || tagname.equals("selectable-tool") || tagname.equals("entry")) { |
387 |
attribute = parser.getAttributeValue(null, "text"); |
388 |
if (attribute!=null) { |
389 |
String menuParts[] = attribute.split("/"); |
390 |
for (int i=0; i<menuParts.length; i++) { |
391 |
if (!menuParts[i].equals("")) |
392 |
keys.add(menuParts[i]); |
393 |
} |
394 |
} |
395 |
|
396 |
attribute = parser.getAttributeValue(null, "tooltip"); |
397 |
if (attribute!=null && !attribute.equals("")) { |
398 |
keys.add(attribute); |
399 |
} |
400 |
|
401 |
|
402 |
} |
403 |
else if (tagname.equals("combo-scale")) { |
404 |
attribute = parser.getAttributeValue(null, "label"); |
405 |
if (attribute!=null && !attribute.equals("")) { |
406 |
keys.add(attribute); |
407 |
} |
408 |
} |
409 |
else if (tagname.equals("tool-bar")) { |
410 |
attribute = parser.getAttributeValue(null, "name"); |
411 |
if (attribute!=null && !attribute.equals("")) { |
412 |
keys.add(attribute); |
413 |
} |
414 |
} |
415 |
|
416 |
} |
417 |
} |
418 |
} catch (XmlPullParserException e1) {
|
419 |
e1.getLocalizedMessage(); |
420 |
} catch (IOException e1) { |
421 |
e1.getLocalizedMessage(); |
422 |
} |
423 |
return keys;
|
424 |
} |
425 |
|
426 |
private HashMap loadProjectFromProperties(Project project) { |
427 |
// always start with an empty HashMap when loading
|
428 |
HashMap dictionaries = new HashMap(); |
429 |
String lang;
|
430 |
OrderedProperties dictionary; |
431 |
|
432 |
FileInputStream stream=null; |
433 |
|
434 |
for (int currentLang=0; currentLang<config.languages.length; currentLang++) { |
435 |
lang = config.languages[currentLang]; |
436 |
dictionary = new OrderedProperties();
|
437 |
try {
|
438 |
// different for spanish...
|
439 |
if (lang.equals("es")) { |
440 |
stream = new FileInputStream(project.propertyDir+File.separator+project.basename+".properties"); |
441 |
} |
442 |
else {
|
443 |
stream = new FileInputStream(project.propertyDir+File.separator+project.basename+"_"+lang+".properties"); |
444 |
} |
445 |
try {
|
446 |
dictionary.load(stream); |
447 |
} catch (IOException e) { |
448 |
System.err.println("Error cargando la base de datos para el idioma: ["+lang+"]. "+e.getLocalizedMessage()); |
449 |
} |
450 |
} catch (FileNotFoundException e) { |
451 |
System.err.println(project.dir + " -- Error cargando la base de datos para el idioma: ["+lang+"]. "+e.getLocalizedMessage()); |
452 |
} |
453 |
dictionaries.put(lang, dictionary); |
454 |
} |
455 |
return dictionaries;
|
456 |
} |
457 |
} |