Révision 1259
| tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/devtools/NormalizeKeys.java (revision 1259) | ||
|---|---|---|
| 1 | 1 |
package org.txm.rcp.translate.devtools; |
| 2 | 2 |
|
| 3 |
import java.io.File; |
|
| 4 | 3 |
import java.io.FileNotFoundException; |
| 5 | 4 |
import java.io.IOException; |
| 6 | 5 |
import java.io.UnsupportedEncodingException; |
| 6 |
import java.util.ArrayList; |
|
| 7 | 7 |
|
| 8 | 8 |
import org.txm.rcp.translate.i18n.PluginMessages; |
| 9 |
import org.txm.rcp.translate.i18n.WorkspaceMessagesManager; |
|
| 9 | 10 |
import org.txm.utils.BiHashMap; |
| 10 | 11 |
|
| 11 | 12 |
/** |
| 12 | 13 |
* Normalizes the keys using the words of the message sentence. |
| 13 |
* eg. : "Error while computing." => errorWhileComputing
|
|
| 14 |
* eg. : "Error while computing {0}." => errorWhileComputing0
|
|
| 14 | 15 |
* |
| 15 | 16 |
* |
| 16 | 17 |
* @author mdecorde |
| ... | ... | |
| 22 | 23 |
/** |
| 23 | 24 |
* Debug state. |
| 24 | 25 |
*/ |
| 25 |
protected boolean debug; |
|
| 26 |
protected boolean debug = false;
|
|
| 26 | 27 |
|
| 27 | 28 |
/** |
| 28 | 29 |
* Keys matching this REGEX will be preserved. |
| 29 | 30 |
*/ |
| 30 | 31 |
public static String preserve = "^(common_|error_|info_).*$"; |
| 31 |
|
|
| 32 | 32 |
|
| 33 |
|
|
| 34 | 33 |
/** |
| 35 | 34 |
* |
| 36 |
* @param debug |
|
| 35 |
* @param debug show lot of debug messages
|
|
| 37 | 36 |
*/ |
| 38 | 37 |
public NormalizeKeys(boolean debug) {
|
| 39 | 38 |
this.debug = debug; |
| 40 |
|
|
| 41 |
|
|
| 42 | 39 |
} |
| 43 | 40 |
|
| 41 |
/** |
|
| 42 |
* Normalize the messages keys in Messages.java and messages.properties files |
|
| 43 |
* |
|
| 44 |
* Warning: the sources files are not updated use WorkspaceMessagesManager.saveKeyModificationsInSources() when all work is done |
|
| 45 |
* |
|
| 46 |
* @param pmManager the PluginMessages to update |
|
| 47 |
* @return the number of modifications done |
|
| 48 |
*/ |
|
| 49 |
public int normalize(PluginMessages pmManager) {
|
|
| 44 | 50 |
|
| 45 |
|
|
| 46 |
public BiHashMap<String, String> normalize(PluginMessages pmManager) {
|
|
| 47 |
|
|
| 48 | 51 |
BiHashMap<String, String> messages = pmManager.getMessagesForMainLang(); |
| 49 | 52 |
|
| 50 |
for (String key : messages.getKeys()) {
|
|
| 53 |
ArrayList<String> keys = new ArrayList<String>(pmManager.getMessageKeys()); |
|
| 54 |
|
|
| 55 |
int n = 0; |
|
| 56 |
for (String key : keys) {
|
|
| 51 | 57 |
String value = messages.get(key); |
| 52 |
|
|
| 58 |
if (value == null) {
|
|
| 59 |
System.out.println("Warning: missing message for key="+key);
|
|
| 60 |
continue; |
|
| 61 |
} |
|
| 53 | 62 |
// already formatted message |
| 54 | 63 |
if(key.matches(preserve)) {
|
| 55 |
System.err.println("NormalizeKeys.normalize(): warning: skipped: " + key + "=" + value);
|
|
| 64 |
if (debug) System.err.println("NormalizeKeys.normalize(): warning: skipped: " + key + "=" + value);
|
|
| 56 | 65 |
continue; |
| 57 | 66 |
} |
| 58 | 67 |
// empty message |
| 59 | 68 |
else if(value.length() == 0) {
|
| 60 |
System.err.println("NormalizeKeys.normalize(): warning: empty string for key: " + key);
|
|
| 69 |
if (debug) System.err.println("NormalizeKeys.normalize(): warning: empty string for key: " + key);
|
|
| 61 | 70 |
continue; |
| 62 | 71 |
} |
| 72 |
|
|
| 63 | 73 |
String newKey = normalize(messages.get(key)); |
| 64 | 74 |
pmManager.renameKey(key, newKey); |
| 65 |
|
|
| 66 |
//messages.put(normalize(messages.get(key)), messages.get(key)); |
|
| 67 |
|
|
| 68 |
|
|
| 69 |
//messages.removeByKey(key); |
|
| 75 |
n++; |
|
| 70 | 76 |
} |
| 71 | 77 |
|
| 72 |
return messages; |
|
| 73 |
|
|
| 78 |
return n; |
|
| 74 | 79 |
} |
| 75 | 80 |
|
| 76 | 81 |
|
| ... | ... | |
| 166 | 171 |
/** |
| 167 | 172 |
* |
| 168 | 173 |
* @param args |
| 174 |
* @throws IOException |
|
| 175 |
* @throws FileNotFoundException |
|
| 176 |
* @throws UnsupportedEncodingException |
|
| 169 | 177 |
*/ |
| 170 |
public static void main(String[] args) {
|
|
| 178 |
public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException, IOException {
|
|
| 171 | 179 |
|
| 172 |
try {
|
|
| 173 |
System.out.println("NormalizeKeys.main(): starting process...");
|
|
| 174 |
|
|
| 175 |
File projectFile = new File(new File(System.getProperty("user.dir")).getParentFile().getAbsolutePath() + "/org.txm.core");
|
|
| 176 |
File messageFile = new File(projectFile, "src/java/org/txm/core/messages/TXMCoreMessages.java"); |
|
| 177 |
|
|
| 178 |
PluginMessages pmManager = new PluginMessages(projectFile, messageFile, false); |
|
| 179 |
NormalizeKeys keysNormalizer = new NormalizeKeys(true); |
|
| 180 |
|
|
| 181 |
// tests |
|
| 182 |
keysNormalizer.normalize(pmManager); |
|
| 183 |
|
|
| 184 |
//pmManager.dump(pmManager.getMessagesForMainLang()); |
|
| 185 |
pmManager.dumpKeysReplacements(); |
|
| 186 |
|
|
| 187 |
System.out.println("NormalizeKeys.main(): number of keys: " + pmManager.getMessageKeys().size() + ".");
|
|
| 188 |
System.out.println("NormalizeKeys.main(): number of replacements to do: " + pmManager.getKeyModifications().size() + ".");
|
|
| 189 |
|
|
| 190 |
System.out.println("NormalizeKeys.main(): terminated.");
|
|
| 191 |
|
|
| 180 |
// try {
|
|
| 181 |
// System.out.println("NormalizeKeys.main(): starting process...");
|
|
| 182 |
// |
|
| 183 |
// File projectFile = new File(new File(System.getProperty("user.dir")).getParentFile().getAbsolutePath() + "/org.txm.core");
|
|
| 184 |
// File messageFile = new File(projectFile, "src/java/org/txm/core/messages/TXMCoreMessages.java"); |
|
| 185 |
// |
|
| 186 |
// PluginMessages pmManager = new PluginMessages(projectFile, messageFile, false); |
|
| 187 |
// NormalizeKeys keysNormalizer = new NormalizeKeys(true); |
|
| 188 |
// |
|
| 189 |
// // tests |
|
| 190 |
// keysNormalizer.normalize(pmManager); |
|
| 191 |
// |
|
| 192 |
// //pmManager.dump(pmManager.getMessagesForMainLang()); |
|
| 193 |
// pmManager.dumpKeysReplacements(); |
|
| 194 |
// |
|
| 195 |
// System.out.println("NormalizeKeys.main(): number of keys: " + pmManager.getMessageKeys().size() + ".");
|
|
| 196 |
// System.out.println("NormalizeKeys.main(): number of replacements to do: " + pmManager.getKeyModifications().size() + ".");
|
|
| 197 |
// |
|
| 198 |
// System.out.println("NormalizeKeys.main(): terminated.");
|
|
| 199 |
// |
|
| 200 |
// } |
|
| 201 |
// catch (UnsupportedEncodingException e) {
|
|
| 202 |
// // TODO Auto-generated catch block |
|
| 203 |
// e.printStackTrace(); |
|
| 204 |
// } |
|
| 205 |
// catch (FileNotFoundException e) {
|
|
| 206 |
// // TODO Auto-generated catch block |
|
| 207 |
// e.printStackTrace(); |
|
| 208 |
// } |
|
| 209 |
// catch (IOException e) {
|
|
| 210 |
// // TODO Auto-generated catch block |
|
| 211 |
// e.printStackTrace(); |
|
| 212 |
// } |
|
| 213 |
|
|
| 214 |
NormalizeKeys keysNormalizer = new NormalizeKeys(false); |
|
| 215 |
WorkspaceMessagesManager wmm = new WorkspaceMessagesManager(); |
|
| 216 |
for (PluginMessages pm : wmm.getPluginMessages().values()) {
|
|
| 217 |
keysNormalizer.normalize(pm); |
|
| 192 | 218 |
} |
| 193 |
catch (UnsupportedEncodingException e) {
|
|
| 194 |
// TODO Auto-generated catch block |
|
| 195 |
e.printStackTrace(); |
|
| 196 |
} |
|
| 197 |
catch (FileNotFoundException e) {
|
|
| 198 |
// TODO Auto-generated catch block |
|
| 199 |
e.printStackTrace(); |
|
| 200 |
} |
|
| 201 |
catch (IOException e) {
|
|
| 202 |
// TODO Auto-generated catch block |
|
| 203 |
e.printStackTrace(); |
|
| 204 |
} |
|
| 205 |
|
|
| 219 |
wmm.saveKeyModificationsInSources(); |
|
| 206 | 220 |
} |
| 207 |
|
|
| 208 |
|
|
| 209 |
|
|
| 210 | 221 |
} |
| tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/i18n/WorkspaceMessagesManager.java (revision 1259) | ||
|---|---|---|
| 20 | 20 |
* |
| 21 | 21 |
*/ |
| 22 | 22 |
public class WorkspaceMessagesManager {
|
| 23 |
|
|
| 23 |
|
|
| 24 |
boolean debug = false; |
|
| 25 |
|
|
| 24 | 26 |
LinkedHashMap<File, PluginMessages> pluginsMessagesPerProject = new LinkedHashMap<File, PluginMessages>(); |
| 25 |
|
|
| 27 |
|
|
| 26 | 28 |
File workspaceLocation; |
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 29 | 31 |
/** |
| 30 | 32 |
* Index of the keys used and their associated files. |
| 31 | 33 |
*/ |
| 32 | 34 |
protected HashMap<String, TreeSet<File>> usedKeysFilesIndex; |
| 33 | 35 |
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 36 | 38 |
/** |
| 37 | 39 |
* Creates the manager using the "user.dir" parent directory -> works when run from Eclipse |
| 38 | 40 |
* @throws IOException |
| ... | ... | |
| 42 | 44 |
public WorkspaceMessagesManager() throws UnsupportedEncodingException, FileNotFoundException, IOException {
|
| 43 | 45 |
this(new File(System.getProperty("user.dir")).getParentFile());
|
| 44 | 46 |
} |
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 48 | 50 |
/** |
| 49 | 51 |
* |
| 50 | 52 |
* @param workspaceLocation |
| ... | ... | |
| 53 | 55 |
* @throws IOException |
| 54 | 56 |
*/ |
| 55 | 57 |
public WorkspaceMessagesManager(File workspaceLocation) throws UnsupportedEncodingException, FileNotFoundException, IOException {
|
| 56 |
|
|
| 58 |
|
|
| 57 | 59 |
this.workspaceLocation = workspaceLocation; |
| 58 |
|
|
| 59 |
System.out.println("WorkspaceMessagesManager.WorkspaceMessagesManager(): workspace location = " + this.workspaceLocation);
|
|
| 60 |
|
|
| 60 |
|
|
| 61 |
if (debug) {
|
|
| 62 |
System.out.println("WorkspaceMessagesManager.WorkspaceMessagesManager(): workspace location = " + this.workspaceLocation);
|
|
| 63 |
} |
|
| 64 |
|
|
| 61 | 65 |
if (!workspaceLocation.exists()) return; |
| 62 | 66 |
if (!workspaceLocation.isDirectory()) return; |
| 63 |
|
|
| 67 |
|
|
| 64 | 68 |
for (File project : workspaceLocation.listFiles()) {
|
| 65 | 69 |
if (!project.isDirectory()) continue; |
| 66 | 70 |
if (project.isHidden()) continue; |
| ... | ... | |
| 76 | 80 |
if (!messagesJavaFile.getName().endsWith("Messages.java")) continue;
|
| 77 | 81 |
|
| 78 | 82 |
PluginMessages messages = new PluginMessages(project, messagesJavaFile); |
| 79 |
|
|
| 83 |
|
|
| 80 | 84 |
pluginsMessagesPerProject.put(project, messages); |
| 81 |
|
|
| 82 |
System.out.println(project + "=" + messages.getLangs().size()+" langs."); |
|
| 83 | 85 |
|
| 86 |
if (debug) {
|
|
| 87 |
System.out.println(project + "=" + messages.getLangs().size()+" langs."); |
|
| 88 |
} |
|
| 84 | 89 |
} |
| 85 | 90 |
} |
| 86 | 91 |
} |
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 90 | 95 |
this.createUsedKeysFilesIndex(); |
| 91 |
this.dumpUsedKeysFilesIndex(); |
|
| 92 | 96 |
|
| 93 | 97 |
// Log summary |
| 94 |
System.out.println("WorkspaceMessagesManager.WorkspaceMessagesManager(): ------------------ process summary --------------------------------------------------------------");
|
|
| 95 |
System.out.println("WorkspaceMessagesManager.WorkspaceMessagesManager(): numbers of projects: " + this.pluginsMessagesPerProject.size() + ".");
|
|
| 96 |
System.out.println("WorkspaceMessagesManager.WorkspaceMessagesManager(): numbers of used keys: " + this.usedKeysFilesIndex.size() + ".");
|
|
| 97 |
System.out.println("WorkspaceMessagesManager.WorkspaceMessagesManager(): numbers of unused keys: " + PluginMessages.getUnusedKeys(this.usedKeysFilesIndex, false).size() + ".");
|
|
| 98 |
System.out.println("WorkspaceMessagesManager.WorkspaceMessagesManager(): done.");
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 98 |
if (debug) {
|
|
| 99 |
|
|
| 100 |
this.dumpUsedKeysFilesIndex(); |
|
| 101 |
|
|
| 102 |
System.out.println("WorkspaceMessagesManager.WorkspaceMessagesManager(): ------------------ process summary --------------------------------------------------------------");
|
|
| 103 |
System.out.println("WorkspaceMessagesManager.WorkspaceMessagesManager(): numbers of projects: " + this.pluginsMessagesPerProject.size() + ".");
|
|
| 104 |
System.out.println("WorkspaceMessagesManager.WorkspaceMessagesManager(): numbers of used keys: " + this.usedKeysFilesIndex.size() + ".");
|
|
| 105 |
System.out.println("WorkspaceMessagesManager.WorkspaceMessagesManager(): numbers of unused keys: " + PluginMessages.getUnusedKeys(this.usedKeysFilesIndex, false).size() + ".");
|
|
| 106 |
System.out.println("WorkspaceMessagesManager.WorkspaceMessagesManager(): done.");
|
|
| 107 |
} |
|
| 108 |
|
|
| 109 |
|
|
| 110 |
|
|
| 103 | 111 |
// FIXME normalizer test |
| 104 |
// File projectFile = new File(new File(System.getProperty("user.dir")).getParentFile().getAbsolutePath() + "/org.txm.core");
|
|
| 105 |
// File messageFile = new File(projectFile, "src/java/org/txm/core/messages/TXMCoreMessages.java"); |
|
| 106 |
// |
|
| 107 |
// PluginMessages pmManager = new PluginMessages(projectFile, messageFile); |
|
| 108 |
// NormalizeKeys keysNormalizer = new NormalizeKeys(true); |
|
| 109 |
// |
|
| 110 |
// // tests |
|
| 111 |
// keysNormalizer.normalize(pmManager); |
|
| 112 |
// |
|
| 113 |
// //pmManager.dump(pmManager.getMessagesForLang(""));
|
|
| 114 |
// pmManager.dumpKeysReplacements(); |
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 112 |
// File projectFile = new File(new File(System.getProperty("user.dir")).getParentFile().getAbsolutePath() + "/org.txm.core");
|
|
| 113 |
// File messageFile = new File(projectFile, "src/java/org/txm/core/messages/TXMCoreMessages.java");
|
|
| 114 |
//
|
|
| 115 |
// PluginMessages pmManager = new PluginMessages(projectFile, messageFile);
|
|
| 116 |
// NormalizeKeys keysNormalizer = new NormalizeKeys(true);
|
|
| 117 |
//
|
|
| 118 |
// // tests
|
|
| 119 |
// keysNormalizer.normalize(pmManager);
|
|
| 120 |
//
|
|
| 121 |
// //pmManager.dump(pmManager.getMessagesForLang(""));
|
|
| 122 |
// pmManager.dumpKeysReplacements();
|
|
| 123 |
|
|
| 124 |
|
|
| 125 |
|
|
| 118 | 126 |
} |
| 119 | 127 |
|
| 120 | 128 |
/** |
| ... | ... | |
| 124 | 132 |
System.out.println("WorkspaceMessagesManager.dumpUsedKeysIndex(): dumping used keys files index...");
|
| 125 | 133 |
|
| 126 | 134 |
for (Map.Entry<String, TreeSet<File>> entry : this.usedKeysFilesIndex.entrySet()) {
|
| 127 |
System.out.println("index key: " + entry.getKey());
|
|
| 128 |
if(entry.getValue().isEmpty()) {
|
|
| 129 |
System.out.println(" file(s): -not used in any file of the workspace-");
|
|
| 130 |
}
|
|
| 131 |
else {
|
|
| 132 |
for(File file : entry.getValue()) {
|
|
| 133 |
System.out.println(" file(s): " + file);
|
|
| 134 |
}
|
|
| 135 |
}
|
|
| 135 |
System.out.println("index key: " + entry.getKey());
|
|
| 136 |
if(entry.getValue().isEmpty()) {
|
|
| 137 |
System.out.println(" file(s): -not used in any file of the workspace-");
|
|
| 138 |
}
|
|
| 139 |
else {
|
|
| 140 |
for(File file : entry.getValue()) {
|
|
| 141 |
System.out.println(" file(s): " + file);
|
|
| 142 |
}
|
|
| 143 |
}
|
|
| 136 | 144 |
} |
| 137 | 145 |
} |
| 138 | 146 |
|
| ... | ... | |
| 141 | 149 |
*/ |
| 142 | 150 |
public void saveKeyModificationsInSources() {
|
| 143 | 151 |
HashMap<String, String> modifications = new HashMap<String, String>(); |
| 144 |
|
|
| 152 |
|
|
| 145 | 153 |
// stores all modifications |
| 146 | 154 |
for (PluginMessages pm : pluginsMessagesPerProject.values()) {
|
| 147 | 155 |
HashMap<String, String> modifs = pm.getKeyModifications(); |
| ... | ... | |
| 150 | 158 |
modifications.put(name+"."+old, name+"."+modifs.get(old)); // prefix the key with class name: Key -> XXXMessages.Key used in sources |
| 151 | 159 |
} |
| 152 | 160 |
} |
| 153 |
|
|
| 161 |
|
|
| 154 | 162 |
// update source files |
| 155 | 163 |
ArrayList<String> updated = new ArrayList<String>(); |
| 156 | 164 |
for (PluginMessages pm : pluginsMessagesPerProject.values()) {
|
| ... | ... | |
| 161 | 169 |
updated.add(oldKey); |
| 162 | 170 |
} |
| 163 | 171 |
} |
| 164 |
|
|
| 172 |
|
|
| 165 | 173 |
// remove saved modifications |
| 166 | 174 |
for (String oldKey : updated) {
|
| 167 | 175 |
modifications.remove(oldKey); |
| 168 | 176 |
} |
| 169 | 177 |
} |
| 170 | 178 |
} |
| 171 |
|
|
| 179 |
|
|
| 172 | 180 |
public LinkedHashMap<File, PluginMessages> getPluginMessages() {
|
| 173 | 181 |
return pluginsMessagesPerProject; |
| 174 | 182 |
} |
| 175 |
|
|
| 183 |
|
|
| 176 | 184 |
public File getWorkspaceLocation() {
|
| 177 | 185 |
return workspaceLocation; |
| 178 | 186 |
} |
| 179 |
|
|
| 180 |
// TODO |
|
| 181 |
// dedicated to get a manager that use the specified key in the project source files for example to move an unused message to another plugin (move dedicated string from core/rcp to dedicated plugin) |
|
| 182 |
public PluginMessages getPluginMessagesManagersUsingKey(String key) {
|
|
| 183 |
return null; |
|
| 187 |
|
|
| 188 |
/** |
|
| 189 |
* dedicated to get a manager that use the specified key in the project source files for example to move an unused message to another plugin (move dedicated string from core/rcp to dedicated plugin) |
|
| 190 |
* @param key local key (without the 'XXXMessages.' part) |
|
| 191 |
* @return the PluginMessages that uses the key in source files |
|
| 192 |
*/ |
|
| 193 |
public ArrayList<PluginMessages> getPluginMessagesManagersUsingKey(String key) {
|
|
| 194 |
ArrayList<PluginMessages> pms = new ArrayList<>(); |
|
| 195 |
|
|
| 196 |
for (PluginMessages pm : this.getPluginMessages().values()) {
|
|
| 197 |
if (pm.getUsedKeysFilesIndex().containsKey(key)) {
|
|
| 198 |
pms.add(pm); |
|
| 199 |
} |
|
| 200 |
} |
|
| 201 |
|
|
| 202 |
return pms; |
|
| 184 | 203 |
} |
| 185 |
|
|
| 186 |
|
|
| 204 |
|
|
| 205 |
|
|
| 187 | 206 |
/** |
| 188 | 207 |
* Creates the index of the keys used and their associated files. |
| 189 | 208 |
*/ |
| 190 | 209 |
public void createUsedKeysFilesIndex() {
|
| 191 | 210 |
this.usedKeysFilesIndex = new HashMap<String, TreeSet<File>>(); |
| 192 |
|
|
| 211 |
|
|
| 193 | 212 |
for (File p : this.pluginsMessagesPerProject.keySet()) {
|
| 194 | 213 |
PluginMessages pmManager = this.pluginsMessagesPerProject.get(p); |
| 195 |
|
|
| 214 |
|
|
| 196 | 215 |
HashMap<String, TreeSet<File>> index = pmManager.getUsedKeysFilesIndex(); |
| 197 |
|
|
| 216 |
|
|
| 198 | 217 |
for (Map.Entry<String, TreeSet<File>> entry :index.entrySet()) {
|
| 199 |
|
|
| 218 |
|
|
| 200 | 219 |
String fullKeyName = pmManager.getKeyFullName(entry.getKey()); |
| 201 |
|
|
| 202 |
System.out.println("WorkspaceMessagesManager.createUsedKeysFilesIndex(): key added: " + fullKeyName);
|
|
| 203 |
|
|
| 204 |
if(this.usedKeysFilesIndex.containsKey(fullKeyName)) {
|
|
| 205 |
this.usedKeysFilesIndex.get(fullKeyName).addAll(entry.getValue());
|
|
| 206 |
}
|
|
| 207 |
else {
|
|
| 208 |
this.usedKeysFilesIndex.put(fullKeyName, entry.getValue());
|
|
| 209 |
}
|
|
| 220 |
|
|
| 221 |
if (debug) System.out.println("WorkspaceMessagesManager.createUsedKeysFilesIndex(): key added: " + fullKeyName);
|
|
| 222 |
|
|
| 223 |
if(this.usedKeysFilesIndex.containsKey(fullKeyName)) {
|
|
| 224 |
this.usedKeysFilesIndex.get(fullKeyName).addAll(entry.getValue());
|
|
| 225 |
}
|
|
| 226 |
else {
|
|
| 227 |
this.usedKeysFilesIndex.put(fullKeyName, entry.getValue());
|
|
| 228 |
}
|
|
| 210 | 229 |
} |
| 211 | 230 |
} |
| 212 | 231 |
} |
| 213 | 232 |
|
| 214 |
|
|
| 233 |
|
|
| 215 | 234 |
/** |
| 216 | 235 |
* |
| 217 | 236 |
* @param args |
| tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/i18n/PluginMessages.java (revision 1259) | ||
|---|---|---|
| 19 | 19 |
|
| 20 | 20 |
import org.apache.commons.io.FilenameUtils; |
| 21 | 21 |
import org.apache.commons.lang.StringUtils; |
| 22 |
import org.txm.stat.utils.ConsoleProgressBar; |
|
| 22 | 23 |
import org.txm.utils.BiHashMap; |
| 23 | 24 |
import org.txm.utils.io.IOUtils; |
| 24 | 25 |
|
| ... | ... | |
| 35 | 36 |
* Files encoding. |
| 36 | 37 |
*/ |
| 37 | 38 |
public static String ENCODING = "UTF-8"; |
| 38 |
|
|
| 39 |
|
|
| 39 | 40 |
/** |
| 40 | 41 |
* Debug state. |
| 41 | 42 |
*/ |
| ... | ... | |
| 61 | 62 |
*/ |
| 62 | 63 |
protected String[] srcFilesExtensions = new String[] {"java", "groovy"};
|
| 63 | 64 |
|
| 64 |
|
|
| 65 |
|
|
| 65 | 66 |
/** |
| 66 | 67 |
* Global REGEX containing all keys to later use for search in files. |
| 67 | 68 |
*/ |
| 68 | 69 |
//protected Pattern keysGlobalREGEX; |
| 69 |
|
|
| 70 |
|
|
| 70 | 71 |
/** |
| 71 | 72 |
* Index of the keys used and their associated files. |
| 72 | 73 |
*/ |
| 73 | 74 |
protected HashMap<String, TreeSet<File>> usedKeysFilesIndex; |
| 74 |
|
|
| 75 |
/** |
|
| 76 |
* the XXXMessages.java messages keys |
|
| 77 |
*/ |
|
| 75 |
|
|
| 76 |
/**
|
|
| 77 |
* the XXXMessages.java messages keys
|
|
| 78 |
*/
|
|
| 78 | 79 |
LinkedHashSet<String> messageKeys = new LinkedHashSet<String>(); |
| 79 | 80 |
|
| 80 | 81 |
/** |
| 81 | 82 |
* Stores the key modifications for further saves in the source files |
| 82 | 83 |
*/ |
| 83 | 84 |
HashMap<String, String> keyModifications = new HashMap<String, String>(); |
| 84 |
|
|
| 85 |
|
|
| 85 | 86 |
/** |
| 86 | 87 |
* The messages stored by properties file |
| 87 | 88 |
*/ |
| ... | ... | |
| 91 | 92 |
* the properties files langs |
| 92 | 93 |
*/ |
| 93 | 94 |
BiHashMap<File, String> file2lang = new BiHashMap<File, String>(); |
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 98 | 99 |
/** |
| 99 | 100 |
* |
| 100 | 101 |
* @param projectDirectory |
| ... | ... | |
| 104 | 105 |
* @throws IOException |
| 105 | 106 |
*/ |
| 106 | 107 |
public PluginMessages(File projectDirectory, File javaMessageFile) throws UnsupportedEncodingException, FileNotFoundException, IOException {
|
| 107 |
this(projectDirectory, javaMessageFile, true);
|
|
| 108 |
this(projectDirectory, javaMessageFile, false);
|
|
| 108 | 109 |
} |
| 109 |
|
|
| 110 |
|
|
| 110 | 111 |
/** |
| 111 | 112 |
* |
| 112 | 113 |
* @param projectDirectory |
| ... | ... | |
| 121 | 122 |
this.projectDirectory = projectDirectory; |
| 122 | 123 |
this.srcFiles = new ArrayList<File>(); |
| 123 | 124 |
this.debug = debug; |
| 124 |
|
|
| 125 |
System.out.println("********************************************************************************************************************");
|
|
| 126 |
System.out.println("PluginMessages.PluginMessages(): project root directory = " + this.projectDirectory);
|
|
| 127 |
System.out.println("PluginMessages.PluginMessages(): java message file = " + this.javaMessageFile);
|
|
| 128 | 125 |
|
| 126 |
if (debug) {
|
|
| 127 |
System.out.println("********************************************************************************************************************");
|
|
| 128 |
System.out.println("PluginMessages.PluginMessages(): project root directory = " + this.projectDirectory);
|
|
| 129 |
System.out.println("PluginMessages.PluginMessages(): java message file = " + this.javaMessageFile);
|
|
| 130 |
} |
|
| 129 | 131 |
File[] propFiles = javaMessageFile.getParentFile().listFiles(); |
| 130 | 132 |
for (File propFile : propFiles) {
|
| 131 | 133 |
String name = propFile.getName(); |
| ... | ... | |
| 161 | 163 |
|
| 162 | 164 |
// create the project source files list |
| 163 | 165 |
this.createSourceFilesList(); |
| 164 |
|
|
| 166 |
|
|
| 165 | 167 |
//this.createKeysGlobalREGEX(); |
| 166 | 168 |
this.createUsedKeysFilesIndex(); |
| 167 |
|
|
| 169 |
|
|
| 168 | 170 |
if(debug) {
|
| 169 | 171 |
this.dumpUsedKeysFilesIndex(); |
| 170 | 172 |
} |
| 171 | 173 |
} |
| 172 |
|
|
| 173 |
// /** |
|
| 174 |
// * Creates a global REGEX containing all keys to later search in files. |
|
| 175 |
// */ |
|
| 176 |
// public void createKeysGlobalREGEX() {
|
|
| 177 |
// StringBuffer buffer = new StringBuffer(); |
|
| 178 |
// for (String key : messageKeys) {
|
|
| 179 |
// buffer.append("|" + key);
|
|
| 180 |
// } |
|
| 181 |
// |
|
| 182 |
// // remove first pipe |
|
| 183 |
// buffer.deleteCharAt(0); |
|
| 184 |
// |
|
| 185 |
// this.keysGlobalREGEX = Pattern.compile("(" + buffer + ")");
|
|
| 186 |
// |
|
| 187 |
// if(debug) {
|
|
| 188 |
// System.out.println("PluginMessages.createKeysGlobalREGEX(): REGEX created: " + this.keysGlobalREGEX + ".");
|
|
| 189 |
// } |
|
| 190 |
// |
|
| 191 |
// } |
|
| 192 |
|
|
| 174 |
|
|
| 175 |
// /**
|
|
| 176 |
// * Creates a global REGEX containing all keys to later search in files.
|
|
| 177 |
// */
|
|
| 178 |
// public void createKeysGlobalREGEX() {
|
|
| 179 |
// StringBuffer buffer = new StringBuffer();
|
|
| 180 |
// for (String key : messageKeys) {
|
|
| 181 |
// buffer.append("|" + key);
|
|
| 182 |
// }
|
|
| 183 |
//
|
|
| 184 |
// // remove first pipe
|
|
| 185 |
// buffer.deleteCharAt(0);
|
|
| 186 |
//
|
|
| 187 |
// this.keysGlobalREGEX = Pattern.compile("(" + buffer + ")");
|
|
| 188 |
//
|
|
| 189 |
// if(debug) {
|
|
| 190 |
// System.out.println("PluginMessages.createKeysGlobalREGEX(): REGEX created: " + this.keysGlobalREGEX + ".");
|
|
| 191 |
// }
|
|
| 192 |
//
|
|
| 193 |
// }
|
|
| 194 |
|
|
| 193 | 195 |
/** |
| 194 | 196 |
* Creates the index of the keys used and their associated files. |
| 195 | 197 |
*/ |
| 196 | 198 |
public void createUsedKeysFilesIndex() {
|
| 197 | 199 |
this.usedKeysFilesIndex = new HashMap<String, TreeSet<File>>(); |
| 198 |
|
|
| 200 |
|
|
| 199 | 201 |
for (String key : messageKeys) {
|
| 200 | 202 |
|
| 201 | 203 |
TreeSet<File> files = new TreeSet<File>(); |
| 202 | 204 |
this.usedKeysFilesIndex.put(key, files); |
| 203 |
|
|
| 205 |
|
|
| 204 | 206 |
if(debug) {
|
| 205 | 207 |
System.out.println("PluginMessages.createUsedKeysIndex(): looking for key " + this.getKeyFullName(key) + " in project source files...");
|
| 206 | 208 |
} |
| 207 |
else {
|
|
| 208 |
System.out.print(".");
|
|
| 209 |
} |
|
| 210 | 209 |
|
| 211 |
|
|
| 212 | 210 |
for (File file : this.srcFiles) {
|
| 213 |
ArrayList<String> lines = IOUtils.getLines(file, PluginMessages.ENCODING); |
|
| 214 |
|
|
| 215 |
for (String line : lines) {
|
|
| 216 |
if(line.contains(this.getKeyFullName(key))) {
|
|
| 217 |
files.add(file); |
|
| 218 |
}; |
|
| 219 |
|
|
| 220 |
} |
|
| 211 |
ArrayList<String> lines = IOUtils.getLines(file, PluginMessages.ENCODING); |
|
| 212 |
|
|
| 213 |
for (String line : lines) {
|
|
| 214 |
if(line.contains(this.getKeyFullName(key))) {
|
|
| 215 |
files.add(file); |
|
| 216 |
}; |
|
| 217 |
} |
|
| 221 | 218 |
} |
| 222 | 219 |
} |
| 223 | 220 |
} |
| 224 |
|
|
| 225 |
|
|
| 221 |
|
|
| 222 |
|
|
| 226 | 223 |
/** |
| 227 | 224 |
* Dumps the index of the keys used and their associated files. |
| 228 | 225 |
*/ |
| ... | ... | |
| 231 | 228 |
System.out.println("PluginMessages.dumpUsedKeysIndex(): dumping used keys files index...");
|
| 232 | 229 |
|
| 233 | 230 |
for (Map.Entry<String, TreeSet<File>> entry : this.usedKeysFilesIndex.entrySet()) {
|
| 234 |
System.out.println("key: " + this.getKeyFullName(entry.getKey()));
|
|
| 235 |
if(entry.getValue().isEmpty()) {
|
|
| 236 |
System.out.println(" file(s): -not used in any file of the project " + this.getProjectDirectory() + "-");
|
|
| 237 |
}
|
|
| 238 |
else {
|
|
| 239 |
for(File file : entry.getValue()) {
|
|
| 240 |
System.out.println(" file(s): " + file);
|
|
| 241 |
}
|
|
| 242 |
}
|
|
| 231 |
System.out.println("key: " + this.getKeyFullName(entry.getKey()));
|
|
| 232 |
if(entry.getValue().isEmpty()) {
|
|
| 233 |
System.out.println(" file(s): -not used in any file of the project " + this.getProjectDirectory() + "-");
|
|
| 234 |
}
|
|
| 235 |
else {
|
|
| 236 |
for(File file : entry.getValue()) {
|
|
| 237 |
System.out.println(" file(s): " + file);
|
|
| 238 |
}
|
|
| 239 |
}
|
|
| 243 | 240 |
} |
| 244 | 241 |
} |
| 245 |
|
|
| 242 |
|
|
| 246 | 243 |
/*** |
| 247 | 244 |
* Returns all source files using the specified message key (except the main Java message file). |
| 248 | 245 |
* @param key |
| 249 | 246 |
* @return |
| 250 | 247 |
*/ |
| 251 |
public TreeSet<File> getFilesUsingKey(String key) {
|
|
| 252 |
return this.usedKeysFilesIndex.get(key);
|
|
| 253 |
}
|
|
| 254 |
|
|
| 255 |
|
|
| 248 |
public TreeSet<File> getFilesUsingKey(String key) {
|
|
| 249 |
return this.usedKeysFilesIndex.get(key);
|
|
| 250 |
}
|
|
| 251 |
|
|
| 252 |
|
|
| 256 | 253 |
/** |
| 257 | 254 |
* Returns the keys of all messages that are not used in the project itself. |
| 258 | 255 |
* @return a sorted list containing all unused keys |
| ... | ... | |
| 269 | 266 |
public static ArrayList<String> getUnusedKeys(HashMap<String, TreeSet<File>> usedKeysFilesIndex, boolean debug) {
|
| 270 | 267 |
ArrayList<String> unusedKeys = new ArrayList<String>(); |
| 271 | 268 |
for (Map.Entry<String, TreeSet<File>> entry : usedKeysFilesIndex.entrySet()) {
|
| 272 |
if(entry.getValue().isEmpty()) {
|
|
| 273 |
|
|
| 274 |
if(debug) {
|
|
| 275 |
System.out.println("PluginMessages.getUnusedKeys(): unused key found: " + entry.getKey() + ".");
|
|
| 276 |
}
|
|
| 277 |
|
|
| 278 |
unusedKeys.add(entry.getKey());
|
|
| 279 |
}
|
|
| 269 |
if(entry.getValue().isEmpty()) {
|
|
| 270 |
|
|
| 271 |
if(debug) {
|
|
| 272 |
System.out.println("PluginMessages.getUnusedKeys(): unused key found: " + entry.getKey() + ".");
|
|
| 273 |
}
|
|
| 274 |
|
|
| 275 |
unusedKeys.add(entry.getKey());
|
|
| 276 |
}
|
|
| 280 | 277 |
} |
| 281 |
|
|
| 278 |
|
|
| 282 | 279 |
Collections.sort(unusedKeys); |
| 283 | 280 |
return unusedKeys; |
| 284 | 281 |
} |
| 285 |
|
|
| 282 |
|
|
| 286 | 283 |
/** |
| 287 | 284 |
* Gets the map of the messages keys and values for the specified language. |
| 288 | 285 |
* @param lang |
| ... | ... | |
| 300 | 297 |
public BiHashMap<String, String> getMessagesForMainLang() {
|
| 301 | 298 |
return this.getMessagesForLang("");
|
| 302 | 299 |
} |
| 303 |
|
|
| 304 |
|
|
| 300 |
|
|
| 301 |
|
|
| 305 | 302 |
public void put(String lang, String key, String message) {
|
| 306 | 303 |
messageKeys.add(key); |
| 307 | 304 |
|
| ... | ... | |
| 350 | 347 |
public String getMessageFullName() {
|
| 351 | 348 |
return javaMessageFile.getAbsolutePath().substring(javaMessageFile.getAbsolutePath().lastIndexOf("org/txm/")+8, javaMessageFile.getAbsolutePath().length()-5-8).replace("/", ".");
|
| 352 | 349 |
} |
| 353 |
|
|
| 350 |
|
|
| 354 | 351 |
/** |
| 355 | 352 |
* Returns the full name of the specified key including the Java message class name. |
| 356 | 353 |
* @param key |
| ... | ... | |
| 456 | 453 |
&& Arrays.asList(this.srcFilesExtensions).contains(FilenameUtils.getExtension(file.getName())) |
| 457 | 454 |
) {
|
| 458 | 455 |
|
| 459 |
if(debug) {
|
|
| 456 |
if (debug) {
|
|
| 460 | 457 |
System.out.println("PluginMessages.createSourceFilesList(): adding source files " + file + ".");
|
| 461 | 458 |
} |
| 462 | 459 |
else {
|
| 463 |
System.out.print(".");
|
|
| 460 |
//System.out.print(".");
|
|
| 464 | 461 |
} |
| 465 | 462 |
|
| 466 | 463 |
this.srcFiles.add(file); |
| ... | ... | |
| 473 | 470 |
*/ |
| 474 | 471 |
public void createSourceFilesList() {
|
| 475 | 472 |
|
| 476 |
System.out.println("PluginMessages.loadSourceFilesList(): creating source files list for extensions [" + StringUtils.join(this.srcFilesExtensions, ", ") + "]...");
|
|
| 473 |
if (debug) {
|
|
| 474 |
System.out.println("PluginMessages.loadSourceFilesList(): creating source files list for extensions [" + StringUtils.join(this.srcFilesExtensions, ", ") + "]...");
|
|
| 475 |
} |
|
| 477 | 476 |
|
| 478 | 477 |
this.createSourceFilesList(this.projectDirectory); |
| 479 | 478 |
|
| ... | ... | |
| 492 | 491 |
* @param newName |
| 493 | 492 |
*/ |
| 494 | 493 |
public void renameKey(String oldName, String newName) {
|
| 495 |
|
|
| 494 |
|
|
| 496 | 495 |
if (!messageKeys.contains(oldName)) {
|
| 497 |
System.err.println("Missing key=" + oldName + ", aborting renaming to " + newName + ".");
|
|
| 496 |
System.err.println("PluginMessages.renameKey(): key=" + oldName + " not found, aborting renaming to " + newName + ".");
|
|
| 498 | 497 |
return; |
| 499 | 498 |
} |
| 500 |
|
|
| 499 |
|
|
| 501 | 500 |
// continue only if key name has changed |
| 502 | 501 |
if(oldName.equals(newName)) {
|
| 503 | 502 |
if(debug) {
|
| ... | ... | |
| 506 | 505 |
return; |
| 507 | 506 |
} |
| 508 | 507 |
|
| 509 |
|
|
| 508 |
|
|
| 510 | 509 |
messageKeys.remove(oldName); |
| 511 | 510 |
messageKeys.add(newName); |
| 512 |
|
|
| 511 |
|
|
| 513 | 512 |
for (File p : langs.keySet()) {
|
| 514 | 513 |
BiHashMap<String, String> h = langs.get(p); |
| 515 | 514 |
if (h.containsKey(oldName)) {
|
| ... | ... | |
| 517 | 516 |
h.removeByKey(oldName); |
| 518 | 517 |
} |
| 519 | 518 |
} |
| 520 |
|
|
| 519 |
|
|
| 521 | 520 |
keyModifications.put(oldName, newName); |
| 522 | 521 |
} |
| 523 | 522 |
|
| ... | ... | |
| 528 | 527 |
public HashMap<String, String> getKeyModifications() {
|
| 529 | 528 |
return keyModifications; |
| 530 | 529 |
} |
| 531 |
|
|
| 530 |
|
|
| 532 | 531 |
public BiHashMap<File, String> getFile2lang() {
|
| 533 | 532 |
return file2lang; |
| 534 | 533 |
} |
| ... | ... | |
| 546 | 545 |
return projectDirectory; |
| 547 | 546 |
} |
| 548 | 547 |
|
| 549 |
|
|
| 548 |
|
|
| 550 | 549 |
/** |
| 551 |
* Dumps the specified BiHashMap<String, String> to standard output.
|
|
| 552 |
*/
|
|
| 553 |
public void dump(BiHashMap<String, String> messages) {
|
|
| 554 |
for (String key : messages.getKeys()) {
|
|
| 550 |
* Dumps the specified BiHashMap<String, String> to standard output.
|
|
| 551 |
*/
|
|
| 552 |
public void dump(BiHashMap<String, String> messages) {
|
|
| 553 |
for (String key : messages.getKeys()) {
|
|
| 555 | 554 |
System.out.println("PluginMessages.dump(): " + key + "=" + messages.get(key));
|
| 556 | 555 |
} |
| 557 |
}
|
|
| 558 |
|
|
| 559 |
|
|
| 560 |
/**
|
|
| 561 |
* Dumps the keys replacements table.
|
|
| 562 |
*/
|
|
| 563 |
public void dumpKeysReplacements() {
|
|
| 564 |
System.out.println("PluginMessages.dumpKeysReplacements(): keys replacements table");
|
|
| 565 |
for (String key : this.keyModifications.keySet()) {
|
|
| 556 |
}
|
|
| 557 |
|
|
| 558 |
|
|
| 559 |
/**
|
|
| 560 |
* Dumps the keys replacements table.
|
|
| 561 |
*/
|
|
| 562 |
public void dumpKeysReplacements() {
|
|
| 563 |
System.out.println("PluginMessages.dumpKeysReplacements(): keys replacements table");
|
|
| 564 |
for (String key : this.keyModifications.keySet()) {
|
|
| 566 | 565 |
System.out.println("PluginMessages.dumpKeysReplacements(): " + key + "=>" + this.keyModifications.get(key));
|
| 567 | 566 |
} |
| 568 |
}
|
|
| 567 |
}
|
|
| 569 | 568 |
|
| 570 | 569 |
/** |
| 571 | 570 |
* @return the usedKeysIndex |
| ... | ... | |
| 574 | 573 |
return usedKeysFilesIndex; |
| 575 | 574 |
} |
| 576 | 575 |
|
| 577 |
|
|
| 576 |
|
|
| 578 | 577 |
/** |
| 579 | 578 |
* |
| 580 | 579 |
* @param args |
| ... | ... | |
| 593 | 592 |
|
| 594 | 593 |
String key = "Base_0"; |
| 595 | 594 |
TreeSet<File> files = pmManager.getFilesUsingKey(key); |
| 596 |
System.out.println("getFilesUsingKey: files using key: " + key);
|
|
| 597 |
for(File file : files) {
|
|
| 598 |
System.out.println(" file: " + file);
|
|
| 599 |
}
|
|
| 595 |
System.out.println("getFilesUsingKey: files using key: " + key);
|
|
| 596 |
for(File file : files) {
|
|
| 597 |
System.out.println(" file: " + file);
|
|
| 598 |
}
|
|
| 600 | 599 |
key = "common_frequency"; |
| 601 | 600 |
files = pmManager.getFilesUsingKey(key); |
| 602 |
System.out.println("getFilesUsingKey: files using key: " + key);
|
|
| 603 |
for(File file : files) {
|
|
| 604 |
System.out.println(" file: " + file);
|
|
| 605 |
}
|
|
| 606 |
|
|
| 601 |
System.out.println("getFilesUsingKey: files using key: " + key);
|
|
| 602 |
for(File file : files) {
|
|
| 603 |
System.out.println(" file: " + file);
|
|
| 604 |
}
|
|
| 605 |
|
|
| 607 | 606 |
// test to find unused keys |
| 608 | 607 |
ArrayList<String> unusedKeys = pmManager.getUnusedKeys(); |
| 609 | 608 |
for (int i = 0; i < unusedKeys.size(); i++) {
|
Formats disponibles : Unified diff