Révision 1246
tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/i18n/PluginMessages.java (revision 1246) | ||
---|---|---|
31 | 31 |
* |
32 | 32 |
*/ |
33 | 33 |
public class PluginMessages { |
34 |
|
|
35 | 34 |
/** |
36 | 35 |
* Files encoding. |
37 | 36 |
*/ |
... | ... | |
46 | 45 |
* Project root directory. |
47 | 46 |
*/ |
48 | 47 |
protected File projectDirectory; |
49 |
|
|
48 |
|
|
50 | 49 |
/** |
51 | 50 |
* Java message file, eg.: TXMCoreMessages.java, ConcordanceUIMessages.java, etc. |
52 | 51 |
*/ |
53 | 52 |
protected File javaMessageFile; |
54 |
|
|
53 |
|
|
55 | 54 |
/** |
56 | 55 |
* Source files of the project. |
57 | 56 |
*/ |
58 | 57 |
protected ArrayList<File> srcFiles; |
59 |
|
|
58 |
|
|
60 | 59 |
/** |
61 | 60 |
* Extensions to use when creating the source files list. |
62 | 61 |
*/ |
63 | 62 |
protected String[] srcFilesExtensions = new String[] {"java", "groovy"}; |
63 |
|
|
64 | 64 |
|
65 | 65 |
/** |
66 | 66 |
* Global REGEX containing all keys to later use for search in files. |
... | ... | |
72 | 72 |
*/ |
73 | 73 |
protected HashMap<String, TreeSet<File>> usedKeysFilesIndex; |
74 | 74 |
|
75 |
/** |
|
76 |
* the XXXMessages.java messages keys |
|
77 |
*/ |
|
78 |
LinkedHashSet<String> messageKeys = new LinkedHashSet<String>(); |
|
79 |
|
|
80 |
/** |
|
81 |
* Stores the key modifications for further saves in the source files |
|
82 |
*/ |
|
83 |
HashMap<String, String> keyModifications = new HashMap<String, String>(); |
|
75 | 84 |
|
76 |
|
|
77 |
LinkedHashSet<String> messageKeys = new LinkedHashSet<String>(); |
|
85 |
/** |
|
86 |
* The messages stored by properties file |
|
87 |
*/ |
|
78 | 88 |
HashMap<File, BiHashMap<String, String>> langs = new HashMap<File, BiHashMap<String, String>>(); |
89 |
|
|
90 |
/** |
|
91 |
* the properties files langs |
|
92 |
*/ |
|
79 | 93 |
BiHashMap<File, String> file2lang = new BiHashMap<File, String>(); |
80 | 94 |
|
81 |
|
|
82 |
|
|
83 |
|
|
84 | 95 |
/** |
85 | 96 |
* |
86 | 97 |
* @param projectDirectory |
... | ... | |
90 | 101 |
* @throws IOException |
91 | 102 |
*/ |
92 | 103 |
public PluginMessages(File projectDirectory, File javaMessageFile) throws UnsupportedEncodingException, FileNotFoundException, IOException { |
93 |
|
|
104 |
|
|
94 | 105 |
this.javaMessageFile = javaMessageFile; |
95 | 106 |
this.projectDirectory = projectDirectory; |
96 | 107 |
this.srcFiles = new ArrayList<File>(); |
97 |
|
|
108 |
|
|
98 | 109 |
System.out.println("********************************************************************************************************************"); |
99 | 110 |
System.out.println("PluginMessages.PluginMessages(): project root directory = " + this.projectDirectory); |
100 | 111 |
System.out.println("PluginMessages.PluginMessages(): java message file = " + this.javaMessageFile); |
101 |
|
|
112 |
|
|
102 | 113 |
File[] propFiles = javaMessageFile.getParentFile().listFiles(); |
103 | 114 |
for (File propFile : propFiles) { |
104 | 115 |
String name = propFile.getName(); |
... | ... | |
108 | 119 |
String lang = name.substring(8, name.indexOf(".properties")); |
109 | 120 |
langs.put(propFile, new BiHashMap<String, String>()); |
110 | 121 |
file2lang.put(propFile, lang); |
111 |
|
|
122 |
|
|
112 | 123 |
BiHashMap<String, String> hash = langs.get(propFile); |
113 |
|
|
124 |
|
|
114 | 125 |
Properties props1 = new Properties(); |
115 | 126 |
props1.load(IOUtils.getReader(propFile, ENCODING)); |
116 | 127 |
for (Object k : props1.keySet()) { |
117 | 128 |
hash.put(k.toString(), props1.get(k).toString()); |
118 | 129 |
} |
119 | 130 |
} |
120 |
|
|
131 |
|
|
121 | 132 |
if (javaMessageFile != null) { |
122 | 133 |
BufferedReader reader2 = new BufferedReader(new InputStreamReader(new FileInputStream(javaMessageFile), ENCODING)); |
123 | 134 |
String line2 = reader2.readLine(); |
... | ... | |
131 | 142 |
} |
132 | 143 |
reader2.close(); |
133 | 144 |
} |
134 |
|
|
145 |
|
|
135 | 146 |
// create the project source files list |
136 | 147 |
this.createSourceFilesList(); |
137 | 148 |
|
... | ... | |
243 | 254 |
return unusedKeys; |
244 | 255 |
} |
245 | 256 |
|
246 |
|
|
247 | 257 |
/** |
248 | 258 |
* Gets the map of the messages keys and values for the specified language. |
249 | 259 |
* @param lang |
... | ... | |
253 | 263 |
File p = new File(javaMessageFile.getParentFile(), "messages" + lang + ".properties"); |
254 | 264 |
return langs.get(p); |
255 | 265 |
} |
266 |
|
|
256 | 267 |
|
257 |
|
|
258 | 268 |
public void put(String lang, String key, String message) { |
259 | 269 |
messageKeys.add(key); |
260 |
|
|
270 |
|
|
261 | 271 |
File p = new File(javaMessageFile.getParentFile(), "messages"+lang+".properties"); |
262 |
|
|
272 |
|
|
263 | 273 |
if (!langs.containsKey(p)) { |
264 | 274 |
langs.put(p, new BiHashMap<String, String>()); |
265 | 275 |
file2lang.put(p, lang); |
266 | 276 |
} |
267 |
|
|
277 |
|
|
268 | 278 |
if (message == null) { |
269 | 279 |
langs.get(p).removeByKey(key); |
270 | 280 |
} else { |
271 | 281 |
langs.get(p).put(key, message); |
272 | 282 |
} |
273 | 283 |
} |
274 |
|
|
284 |
|
|
275 | 285 |
public String get(String lang, String key) { |
276 | 286 |
File p = new File(javaMessageFile.getParentFile(), "messages"+lang+".properties"); |
277 |
|
|
287 |
|
|
278 | 288 |
if (!file2lang.containsKey(p)) return null; |
279 |
|
|
289 |
|
|
280 | 290 |
return langs.get(p).get(key); |
281 | 291 |
} |
282 |
|
|
292 |
|
|
283 | 293 |
public LinkedHashSet<String> getMessageKeys() { |
284 | 294 |
return messageKeys; |
285 | 295 |
} |
286 |
|
|
296 |
|
|
287 | 297 |
public File getMessageFile() { |
288 | 298 |
return javaMessageFile; |
289 | 299 |
} |
290 |
|
|
300 |
|
|
291 | 301 |
public String getMessageClassName() { |
292 | 302 |
return javaMessageFile.getName().substring(0, javaMessageFile.getName().length()-5); |
293 | 303 |
} |
294 |
|
|
304 |
|
|
295 | 305 |
public String getMessageFullClassName() { |
296 | 306 |
return javaMessageFile.getAbsolutePath().substring(javaMessageFile.getAbsolutePath().lastIndexOf("org/txm/"), javaMessageFile.getAbsolutePath().length()-5).replace("/", "."); |
297 | 307 |
} |
298 |
|
|
308 |
|
|
299 | 309 |
public String getMessageName() { |
300 | 310 |
return javaMessageFile.getName().substring(0, javaMessageFile.getName().length()-5-8); |
301 | 311 |
} |
302 |
|
|
312 |
|
|
303 | 313 |
public String getMessageFullName() { |
304 | 314 |
return javaMessageFile.getAbsolutePath().substring(javaMessageFile.getAbsolutePath().lastIndexOf("org/txm/")+8, javaMessageFile.getAbsolutePath().length()-5-8).replace("/", "."); |
305 | 315 |
} |
... | ... | |
312 | 322 |
public String getKeyFullName(String key) { |
313 | 323 |
return this.getMessageClassName() + "." + key; |
314 | 324 |
} |
315 |
|
|
316 |
// public def getMissingsMessageKeys(String lang) { |
|
317 |
// def missing = [] |
|
318 |
// for (String pKey : this.getKeys()) |
|
319 |
// if (!messageKeys.contains(pKey)) |
|
320 |
// missing << pKey |
|
321 |
// |
|
322 |
// return missing |
|
323 |
// } |
|
324 |
// |
|
325 |
// public getMissingPropKeys(String lang) { |
|
326 |
// def missing = [] |
|
327 |
// def pKeys = this.getKeys() |
|
328 |
// for (String mKey : messageKeys) |
|
329 |
// if (!pKeys.contains(mKey)) |
|
330 |
// missing << mKey |
|
331 |
// |
|
332 |
// return missing |
|
333 |
// } |
|
334 |
|
|
325 |
|
|
335 | 326 |
public void saveChanges() throws IOException { |
336 | 327 |
|
337 | 328 |
//Write prop File |
... | ... | |
342 | 333 |
for (String k : h.getKeys()) { |
343 | 334 |
props.setProperty(k, h.get(k)); |
344 | 335 |
} |
345 |
|
|
346 |
if (propFile.exists()) { |
|
347 |
File oldFile = new File(propFile.getParentFile(), propFile.getName()+".old"); |
|
348 |
if (oldFile.exists()) { |
|
349 |
oldFile.delete(); |
|
350 |
} |
|
351 |
propFile.renameTo(oldFile); |
|
352 |
} |
|
353 |
|
|
354 |
props.store(IOUtils.getWriter(propFile, ENCODING), "TXM messages generated by the PluginMessages class"); |
|
336 |
|
|
337 |
File newPropFile = new File(propFile.getParentFile(), propFile.getName()+".new"); |
|
338 |
|
|
339 |
props.store(IOUtils.getWriter(newPropFile, ENCODING), "TXM messages generated by the PluginMessages class"); |
|
355 | 340 |
} |
356 |
|
|
341 |
|
|
357 | 342 |
// write message File |
358 | 343 |
if (javaMessageFile == null) return; |
359 |
|
|
360 |
File oldFile2 = new File(javaMessageFile.getParentFile(), javaMessageFile.getName()+".old"); |
|
361 |
javaMessageFile.renameTo(oldFile2); // back up |
|
362 |
|
|
344 |
|
|
345 |
File newJavaMessageFile = new File(javaMessageFile.getParentFile(), javaMessageFile.getName()+".new"); |
|
346 |
|
|
363 | 347 |
String className = getMessageClassName(); |
364 | 348 |
String name = getMessageName(); |
365 | 349 |
String classPackage = getMessageFullClassName().substring(0, getMessageFullClassName().length()- getMessageClassName().length()); |
366 |
|
|
367 |
PrintWriter out = IOUtils.getWriter(javaMessageFile, ENCODING); |
|
368 |
|
|
369 |
// write start |
|
370 |
out.println("package "+classPackage+";\n"); |
|
371 | 350 |
|
372 |
out.println("import org.eclipse.osgi.util.NLS;"); |
|
373 |
out.println("import org.txm.utils.messages.Utf8NLS;\n"); |
|
351 |
PrintWriter out = IOUtils.getWriter(newJavaMessageFile, ENCODING); |
|
374 | 352 |
|
375 |
out.println("/** "+name+" messages. @author sjacquot, mdecorde **/"); |
|
353 |
// write start |
|
354 |
out.println("package "+classPackage+";\n"); |
|
376 | 355 |
|
377 |
out.println("public class "+className+" extends NLS {"); |
|
356 |
out.println("import org.eclipse.osgi.util.NLS;"); |
|
357 |
out.println("import org.txm.utils.messages.Utf8NLS;\n"); |
|
378 | 358 |
|
379 |
out.println(" private static final String BUNDLE_NAME = \""+classPackage+".messages\"; //$NON-NLS-1$"); |
|
380 |
|
|
381 |
//write keys |
|
382 |
for (String key : this.getMessageKeys()) |
|
383 |
out.println(" public static String "+key+";"); |
|
384 |
|
|
385 |
// write end |
|
386 |
out.println(" static {"); |
|
387 |
out.println(" // initialize resource bundle"); |
|
388 |
out.println(" Utf8NLS.initializeMessages(BUNDLE_NAME, "+className+".class);"); |
|
389 |
out.println(" }"); |
|
359 |
out.println("/**\n * "+name+" messages.\n * @author sjacquot, mdecorde \n**/"); |
|
390 | 360 |
|
391 |
out.println(" private "+className+"() { }"); |
|
392 |
out.println("}"); |
|
393 |
|
|
394 |
out.close(); |
|
361 |
out.println("public class "+className+" extends NLS {"); |
|
362 |
|
|
363 |
out.println(" private static final String BUNDLE_NAME = \""+classPackage+".messages\"; //$NON-NLS-1$"); |
|
364 |
|
|
365 |
//write keys |
|
366 |
for (String key : this.getMessageKeys()) { |
|
367 |
out.println(" public static String "+key+";"); |
|
368 |
} |
|
369 |
|
|
370 |
// write end |
|
371 |
out.println(" static {"); |
|
372 |
out.println(" // initialize resource bundle"); |
|
373 |
out.println(" Utf8NLS.initializeMessages(BUNDLE_NAME, "+className+".class);"); |
|
374 |
out.println(" }"); |
|
375 |
|
|
376 |
out.println(" private "+className+"() { }"); |
|
377 |
out.println("}"); |
|
378 |
|
|
379 |
out.close(); |
|
395 | 380 |
} |
396 |
|
|
381 |
|
|
397 | 382 |
public void summary() { |
398 |
|
|
383 |
|
|
399 | 384 |
System.out.println(getMessageClassName()); |
400 |
|
|
385 |
|
|
401 | 386 |
System.out.println("Messages keys"); |
402 | 387 |
for (String key : messageKeys) { |
403 | 388 |
System.out.println(key); |
404 | 389 |
} |
405 |
|
|
390 |
|
|
406 | 391 |
System.out.println("Langs"); |
407 | 392 |
for (File lang : langs.keySet()) { |
408 | 393 |
System.out.println(" ------------------------------------------------------------------------------------------------------------------------------"); |
... | ... | |
415 | 400 |
} |
416 | 401 |
} |
417 | 402 |
} |
418 |
|
|
419 | 403 |
|
420 | 404 |
/** |
421 | 405 |
* Parses the project and stores a list of the source files. |
... | ... | |
434 | 418 |
else if (!file.equals(this.javaMessageFile) |
435 | 419 |
&& Arrays.asList(this.srcFilesExtensions).contains(FilenameUtils.getExtension(file.getName())) |
436 | 420 |
) { |
437 |
|
|
421 |
|
|
438 | 422 |
if(debug) { |
439 | 423 |
System.out.println("PluginMessages.createSourceFilesList(): adding source files " + file + "."); |
440 | 424 |
} |
... | ... | |
443 | 427 |
} |
444 | 428 |
} |
445 | 429 |
} |
446 |
|
|
430 |
|
|
447 | 431 |
/** |
448 | 432 |
* Parses the project and stores a list of the source files. |
449 | 433 |
*/ |
450 | 434 |
public void createSourceFilesList() { |
451 | 435 |
|
452 | 436 |
System.out.println("PluginMessages.loadSourceFilesList(): creating source files list for extensions [" + StringUtils.join(this.srcFilesExtensions, ", ") + "]..."); |
453 |
|
|
437 |
|
|
454 | 438 |
this.createSourceFilesList(this.projectDirectory); |
439 |
|
|
440 |
|
|
441 |
if(debug) { |
|
442 |
System.out.println("PluginMessages.loadSourceFilesList(): done. Source files count = " + this.srcFiles.size() + "."); |
|
443 |
} |
|
444 |
} |
|
445 |
|
|
446 |
/** |
|
447 |
* Rename a key and update in the properties file the keys |
|
448 |
* |
|
449 |
* stores the modification to rename the keys later in the Workspace source files |
|
450 |
* |
|
451 |
* @param oldName |
|
452 |
* @param newName |
|
453 |
*/ |
|
454 |
public void renameKey(String oldName, String newName) { |
|
455 | 455 |
|
456 |
System.out.println("PluginMessages.loadSourceFilesList(): done. Source files count = " + this.srcFiles.size() + "."); |
|
456 |
if (!messageKeys.contains(oldName)) { |
|
457 |
System.out.println("Missing key="+oldName+" aborting renaming to "+newName+"."); |
|
458 |
return; |
|
459 |
} |
|
460 |
|
|
461 |
messageKeys.remove(oldName); |
|
462 |
messageKeys.add(newName); |
|
463 |
|
|
464 |
for (File p : langs.keySet()) { |
|
465 |
BiHashMap<String, String> h = langs.get(p); |
|
466 |
if (h.containsKey(oldName)) { |
|
467 |
h.put(newName, h.get(oldName)); |
|
468 |
h.removeByKey(oldName); |
|
469 |
} |
|
470 |
} |
|
471 |
|
|
472 |
keyModifications.put(oldName, newName); |
|
457 | 473 |
} |
458 | 474 |
|
475 |
/** |
|
476 |
* |
|
477 |
* @return the renamed keys |
|
478 |
*/ |
|
479 |
public HashMap<String, String> getKeyModifications() { |
|
480 |
return keyModifications; |
|
481 |
} |
|
459 | 482 |
|
460 | 483 |
public BiHashMap<File, String> getFile2lang() { |
461 | 484 |
return file2lang; |
... | ... | |
469 | 492 |
* Gets the project root directory. |
470 | 493 |
* @return the projectDirectory |
471 | 494 |
*/ |
495 |
|
|
472 | 496 |
public File getProjectDirectory() { |
473 | 497 |
return projectDirectory; |
474 | 498 |
} |
499 |
|
|
475 | 500 |
|
476 |
|
|
477 | 501 |
/** |
478 | 502 |
* Dumps the specified BiHashMap<String, String> to standard output. |
479 | 503 |
*/ |
... | ... | |
493 | 517 |
* @throws IOException |
494 | 518 |
*/ |
495 | 519 |
public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException, IOException { |
496 |
|
|
520 |
|
|
497 | 521 |
File projectFile = new File(new File(System.getProperty("user.dir")).getParentFile().getAbsolutePath() + "/org.txm.core"); |
498 | 522 |
File messageFile = new File(projectFile, "src/java/org/txm/core/messages/TXMCoreMessages.java"); |
499 |
|
|
523 |
|
|
500 | 524 |
PluginMessages pmManager = new PluginMessages(projectFile, messageFile); |
501 |
|
|
525 |
|
|
502 | 526 |
// test to find files using the specified key |
527 |
|
|
503 | 528 |
String key = "Base_0"; |
504 | 529 |
TreeSet<File> files = pmManager.getFilesUsingKey(key); |
505 | 530 |
System.out.println("getFilesUsingKey: files using key: " + key); |
... | ... | |
512 | 537 |
for(File file : files) { |
513 | 538 |
System.out.println(" file: " + file); |
514 | 539 |
} |
515 |
|
|
516 | 540 |
|
517 | 541 |
// test to find unused keys |
518 | 542 |
ArrayList<String> unusedKeys = pmManager.getUnusedKeys(); |
519 | 543 |
for (int i = 0; i < unusedKeys.size(); i++) { |
520 | 544 |
System.out.println("findUnusedKeys: key " + unusedKeys.get(i) + " is unused in project " + pmManager.getProjectDirectory() + " (main language value = " + pmManager.getMessagesForLang("").get(unusedKeys.get(i)) + ")"); |
521 | 545 |
} |
522 |
|
|
546 |
|
|
523 | 547 |
//dict.summary(); |
524 | 548 |
//dict.saveChanges(); |
525 |
|
|
549 |
|
|
526 | 550 |
System.out.println("PluginMessages.main(): done."); |
527 | 551 |
} |
528 | 552 |
|
... | ... | |
532 | 556 |
public HashMap<String, TreeSet<File>> getUsedKeysFilesIndex() { |
533 | 557 |
return usedKeysFilesIndex; |
534 | 558 |
} |
535 |
|
|
536 |
|
|
537 | 559 |
} |
Formats disponibles : Unified diff