8 |
8 |
import java.io.InputStreamReader;
|
9 |
9 |
import java.io.PrintWriter;
|
10 |
10 |
import java.io.UnsupportedEncodingException;
|
|
11 |
import java.text.Collator;
|
11 |
12 |
import java.util.ArrayList;
|
12 |
13 |
import java.util.Arrays;
|
13 |
14 |
import java.util.Collections;
|
|
15 |
import java.util.Comparator;
|
|
16 |
import java.util.Enumeration;
|
14 |
17 |
import java.util.HashMap;
|
15 |
18 |
import java.util.LinkedHashSet;
|
|
19 |
import java.util.Locale;
|
16 |
20 |
import java.util.Map;
|
17 |
21 |
import java.util.Properties;
|
18 |
22 |
import java.util.TreeMap;
|
... | ... | |
22 |
26 |
|
23 |
27 |
import org.apache.commons.io.FilenameUtils;
|
24 |
28 |
import org.apache.commons.lang.StringUtils;
|
|
29 |
import org.eclipse.osgi.util.NLS;
|
25 |
30 |
import org.txm.utils.BiHashMap;
|
26 |
31 |
import org.txm.utils.io.IOUtils;
|
27 |
32 |
|
... | ... | |
78 |
83 |
/**
|
79 |
84 |
* the XXXMessages.java messages keys
|
80 |
85 |
*/
|
81 |
|
LinkedHashSet<String> messageKeys = new LinkedHashSet<String>();
|
|
86 |
TreeSet<String> messageKeys = new TreeSet<String>();
|
82 |
87 |
|
83 |
88 |
/**
|
84 |
89 |
* Stores the key modifications for further saves in the source files
|
... | ... | |
231 |
236 |
|
232 |
237 |
//for (String key : messageKeys) {
|
233 |
238 |
|
234 |
|
// if(debug) {
|
235 |
|
// System.out.println("PluginMessages.createUsedKeysIndex(): looking for key " + this.getKeyFullName(key) + " in project source files...");
|
236 |
|
// }
|
|
239 |
// if(debug) {
|
|
240 |
// System.out.println("PluginMessages.createUsedKeysIndex(): looking for key " + this.getKeyFullName(key) + " in project source files...");
|
|
241 |
// }
|
237 |
242 |
|
238 |
243 |
for (File file : this.srcFiles) {
|
239 |
244 |
if (!file.exists()) continue;
|
240 |
245 |
String lines = IOUtils.getText(file, PluginMessages.ENCODING);
|
241 |
246 |
|
242 |
|
// for (String line : lines) {
|
243 |
|
// // if(line.contains(this.getKeyFullName(key))) {
|
244 |
|
// // files.add(file);
|
245 |
|
// // };
|
|
247 |
// for (String line : lines) {
|
|
248 |
// // if(line.contains(this.getKeyFullName(key))) {
|
|
249 |
// // files.add(file);
|
|
250 |
// // };
|
246 |
251 |
|
247 |
|
Matcher m = WorkspaceMessagesManager.KEY_REGEX.matcher(lines);
|
248 |
|
while (m.find()) {
|
249 |
|
// Get the matching string
|
250 |
|
String key = m.group();
|
251 |
|
if (!this.usedKeysFilesIndex.containsKey(key)) {
|
252 |
|
this.usedKeysFilesIndex.put(key, new TreeSet<File>());
|
253 |
|
}
|
254 |
|
this.usedKeysFilesIndex.get(key).add(file);
|
|
252 |
Matcher m = WorkspaceMessagesManager.KEY_REGEX.matcher(lines);
|
|
253 |
while (m.find()) {
|
|
254 |
// Get the matching string
|
|
255 |
String key = m.group();
|
|
256 |
if (!this.usedKeysFilesIndex.containsKey(key)) {
|
|
257 |
this.usedKeysFilesIndex.put(key, new TreeSet<File>());
|
255 |
258 |
}
|
256 |
|
// }
|
|
259 |
this.usedKeysFilesIndex.get(key).add(file);
|
|
260 |
}
|
|
261 |
// }
|
257 |
262 |
}
|
258 |
263 |
//}
|
259 |
264 |
}
|
... | ... | |
290 |
295 |
return new TreeSet<File>();
|
291 |
296 |
}
|
292 |
297 |
}
|
293 |
|
|
|
298 |
|
294 |
299 |
/***
|
295 |
300 |
* @param key local key name
|
296 |
301 |
* @return all source files using the specified local message key (except the main Java message file).
|
... | ... | |
372 |
377 |
return langs.get(p).get(key);
|
373 |
378 |
}
|
374 |
379 |
|
375 |
|
public LinkedHashSet<String> getMessageKeys() {
|
|
380 |
public TreeSet<String> getMessageKeys() {
|
376 |
381 |
return messageKeys;
|
377 |
382 |
}
|
378 |
383 |
|
... | ... | |
408 |
413 |
return this.getMessageClassName() + "." + key;
|
409 |
414 |
}
|
410 |
415 |
|
|
416 |
public static Collator col = Collator.getInstance(Locale.FRANCE);
|
|
417 |
public static Comparator comp = new Comparator<String>() {
|
|
418 |
@Override
|
|
419 |
public int compare(String arg0, String arg1) {
|
|
420 |
return col.compare(arg0, arg1);
|
|
421 |
}
|
|
422 |
};
|
|
423 |
static {
|
|
424 |
col.setStrength(Collator.TERTIARY);
|
|
425 |
}
|
411 |
426 |
public void saveChanges() throws IOException {
|
412 |
427 |
|
413 |
428 |
//Write prop File
|
414 |
429 |
for (File propFile : langs.keySet()) {
|
415 |
430 |
|
416 |
|
Properties props = new Properties();
|
|
431 |
Properties props = new Properties() {
|
|
432 |
@Override
|
|
433 |
public synchronized Enumeration<Object> keys() {
|
|
434 |
TreeSet<Object> set = new TreeSet<Object>(comp);
|
|
435 |
set.addAll(super.keySet());
|
|
436 |
return Collections.enumeration(set);
|
|
437 |
}
|
|
438 |
};
|
417 |
439 |
BiHashMap<String, String> h = langs.get(propFile);
|
418 |
440 |
for (String k : h.getKeys()) {
|
419 |
441 |
props.setProperty(k, h.get(k));
|
420 |
442 |
}
|
421 |
443 |
|
422 |
444 |
File newPropFile = new File(propFile.getParentFile(), propFile.getName()+".new");
|
423 |
|
|
424 |
445 |
props.store(IOUtils.getWriter(newPropFile, ENCODING), "TXM messages generated by the PluginMessages class");
|
425 |
446 |
}
|
426 |
447 |
|
... | ... | |
431 |
452 |
|
432 |
453 |
String className = getMessageClassName();
|
433 |
454 |
String name = getMessageName();
|
434 |
|
String classPackage = getMessageFullClassName().substring(0, getMessageFullClassName().length()- getMessageClassName().length());
|
|
455 |
String classPackage = getMessageFullClassName().substring(0, getMessageFullClassName().length() - getMessageClassName().length() - 1);
|
435 |
456 |
|
436 |
457 |
PrintWriter out = IOUtils.getWriter(newJavaMessageFile, ENCODING);
|
437 |
458 |
|
... | ... | |
441 |
462 |
out.println("import org.eclipse.osgi.util.NLS;");
|
442 |
463 |
out.println("import org.txm.utils.messages.Utf8NLS;\n");
|
443 |
464 |
|
444 |
|
out.println("/**\n * "+name+" messages.\n * @author sjacquot, mdecorde \n**/");
|
|
465 |
out.println("/**\n * "+name+" messages.\n * @author sjacquot, mdecorde \n */");
|
445 |
466 |
|
446 |
|
out.println("public class "+className+" extends NLS {");
|
|
467 |
out.println("public class "+className+" extends NLS {\n");
|
447 |
468 |
|
448 |
|
out.println(" private static final String BUNDLE_NAME = \""+classPackage+".messages\"; //$NON-NLS-1$");
|
|
469 |
out.println(" private static final String BUNDLE_NAME = \""+classPackage+".messages\"; //$NON-NLS-1$\n");
|
449 |
470 |
|
450 |
471 |
//write keys
|
451 |
472 |
for (String key : this.getMessageKeys()) {
|
... | ... | |
453 |
474 |
}
|
454 |
475 |
|
455 |
476 |
// write end
|
456 |
|
out.println(" static {");
|
|
477 |
out.println("\n /**");
|
|
478 |
out.println(" * @param message the message containing parameters {0}...{N}");
|
|
479 |
out.println(" * @param bindings the parameters ");
|
|
480 |
out.println(" * @return the given message's substitution locations with the given string values.");
|
|
481 |
out.println(" */");
|
|
482 |
out.println(" public static String bind(String message, Object... bindings) {");
|
|
483 |
out.println(" return NLS.bind(message, bindings);");
|
|
484 |
out.println(" }");
|
|
485 |
|
|
486 |
out.println("\n static {");
|
457 |
487 |
out.println(" // initialize resource bundle");
|
458 |
488 |
out.println(" Utf8NLS.initializeMessages(BUNDLE_NAME, "+className+".class);");
|
459 |
489 |
out.println(" }");
|
... | ... | |
661 |
691 |
PluginMessages pmManager = new PluginMessages(projectFile);
|
662 |
692 |
|
663 |
693 |
// test to find files using the specified key
|
664 |
|
/*
|
|
694 |
/*
|
665 |
695 |
String key = "TXMCoreMessages.Base_0";
|
666 |
696 |
TreeSet<File> files = pmManager.getFilesUsingKey(key);
|
667 |
697 |
System.out.println("getFilesUsingKey: files using key: " + key);
|
... | ... | |
680 |
710 |
for (int i = 0; i < unusedKeys.size(); i++) {
|
681 |
711 |
System.out.println("findUnusedKeys: key " + unusedKeys.get(i) + " is unused in project " + pmManager.getProjectDirectory() + " (main language value = " + pmManager.getMessagesForLang("").get(unusedKeys.get(i)) + ")");
|
682 |
712 |
}
|
683 |
|
*/
|
|
713 |
*/
|
684 |
714 |
//dict.summary();
|
685 |
|
|
686 |
|
|
|
715 |
|
|
716 |
|
687 |
717 |
pmManager.saveChanges();
|
688 |
718 |
|
689 |
719 |
System.out.println("PluginMessages.main(): done.");
|