18 |
18 |
*
|
19 |
19 |
*/
|
20 |
20 |
public class NormalizeKeys {
|
21 |
|
|
|
21 |
|
22 |
22 |
/**
|
23 |
23 |
* Debug state.
|
24 |
24 |
*/
|
25 |
25 |
protected boolean debug = false;
|
26 |
|
|
|
26 |
|
27 |
27 |
/**
|
28 |
28 |
* Keys matching this REGEX will be preserved.
|
29 |
29 |
*/
|
... | ... | |
48 |
48 |
public int normalize(PluginMessages pmManager) {
|
49 |
49 |
|
50 |
50 |
BiHashMap<String, String> messages = pmManager.getMessagesForDefaultLang();
|
51 |
|
|
|
51 |
|
52 |
52 |
ArrayList<String> keys = new ArrayList<String>(pmManager.getMessageKeys());
|
53 |
|
|
|
53 |
|
54 |
54 |
int n = 0;
|
55 |
55 |
for (String key : keys) {
|
56 |
56 |
String value = messages.get(key);
|
... | ... | |
68 |
68 |
if (debug) System.err.println("NormalizeKeys.normalize(): warning: empty string for key: " + key);
|
69 |
69 |
continue;
|
70 |
70 |
}
|
71 |
|
|
|
71 |
|
72 |
72 |
String newKeyOrig = normalize(messages.get(key));
|
73 |
|
String newKey = ""+newKeyOrig;
|
74 |
|
int c = 2;
|
75 |
|
while (pmManager.getMessageKeys().contains(newKey)) {
|
76 |
|
newKey += newKeyOrig+"_"+(c++);
|
|
73 |
|
|
74 |
if (value.equals(messages.get(newKeyOrig))) { // the newKey Value == the value
|
|
75 |
//A=hello world
|
|
76 |
//B=hello world
|
|
77 |
// rename A -> helloWorld
|
|
78 |
//B -> helloWorld SAUF QUE helloWorld existe ET meme valeur que valeur(B) valeur(helloWorld)
|
|
79 |
|
|
80 |
pmManager.removeKey(key); // remove the old key
|
|
81 |
} else {
|
|
82 |
// A=hello world
|
|
83 |
// B=hello world!
|
|
84 |
// A et B n'ont pas la meme clé
|
|
85 |
|
|
86 |
String newKey = ""+newKeyOrig;
|
|
87 |
int c = 2;
|
|
88 |
while (pmManager.getMessageKeys().contains(newKey)) {
|
|
89 |
newKey += newKeyOrig+"_"+(c++);
|
|
90 |
}
|
|
91 |
|
|
92 |
pmManager.renameKey(key, newKey);
|
77 |
93 |
}
|
78 |
|
|
79 |
|
pmManager.renameKey(key, newKey);
|
80 |
|
|
81 |
94 |
n++;
|
82 |
95 |
}
|
83 |
|
|
|
96 |
|
84 |
97 |
return n;
|
85 |
98 |
}
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
// /**
|
90 |
|
// * Normalizes the keys of the specified messages map.
|
91 |
|
// * @param messages
|
92 |
|
// * @return
|
93 |
|
// */
|
94 |
|
// public BiHashMap<String, String> normalize(BiHashMap<String, String> messages) {
|
95 |
|
//
|
96 |
|
// for (String key : messages.getKeys()) {
|
97 |
|
// String value = messages.get(key);
|
98 |
|
//
|
99 |
|
// // already formatted message
|
100 |
|
// if(key.matches(preserve)) {
|
101 |
|
// System.err.println("NormalizeKeys.normalize(): warning: skipped: " + key + "=" + value);
|
102 |
|
// continue;
|
103 |
|
// }
|
104 |
|
// // empty message
|
105 |
|
// else if(value.length() == 0) {
|
106 |
|
// System.err.println("NormalizeKeys.normalize(): warning: empty string for key: " + key);
|
107 |
|
// continue;
|
108 |
|
// }
|
109 |
|
//
|
110 |
|
// messages.put(normalize(messages.get(key)), messages.get(key));
|
111 |
|
// //messages.removeByKey(key);
|
112 |
|
// }
|
113 |
|
//
|
114 |
|
// return messages;
|
115 |
|
// }
|
116 |
|
//
|
117 |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
// /**
|
|
103 |
// * Normalizes the keys of the specified messages map.
|
|
104 |
// * @param messages
|
|
105 |
// * @return
|
|
106 |
// */
|
|
107 |
// public BiHashMap<String, String> normalize(BiHashMap<String, String> messages) {
|
|
108 |
//
|
|
109 |
// for (String key : messages.getKeys()) {
|
|
110 |
// String value = messages.get(key);
|
|
111 |
//
|
|
112 |
// // already formatted message
|
|
113 |
// if(key.matches(preserve)) {
|
|
114 |
// System.err.println("NormalizeKeys.normalize(): warning: skipped: " + key + "=" + value);
|
|
115 |
// continue;
|
|
116 |
// }
|
|
117 |
// // empty message
|
|
118 |
// else if(value.length() == 0) {
|
|
119 |
// System.err.println("NormalizeKeys.normalize(): warning: empty string for key: " + key);
|
|
120 |
// continue;
|
|
121 |
// }
|
|
122 |
//
|
|
123 |
// messages.put(normalize(messages.get(key)), messages.get(key));
|
|
124 |
// //messages.removeByKey(key);
|
|
125 |
// }
|
|
126 |
//
|
|
127 |
// return messages;
|
|
128 |
// }
|
|
129 |
//
|
|
130 |
|
118 |
131 |
/**
|
119 |
132 |
* Normalizes the specified string.
|
120 |
133 |
* @param str
|
... | ... | |
125 |
138 |
if(debug) {
|
126 |
139 |
System.out.println("NormalizeKeys.normalize(): normalizing: " + str);
|
127 |
140 |
}
|
128 |
|
|
|
141 |
|
129 |
142 |
str = str.replaceAll("[^a-zA-Z0-9 ]", "").trim();
|
130 |
|
|
|
143 |
|
131 |
144 |
// empty string
|
132 |
145 |
if(str.length() == 0) {
|
133 |
146 |
System.err.println("NormalizeKeys.normalize(): warning: empty string.");
|
134 |
147 |
}
|
135 |
148 |
else {
|
136 |
|
|
|
149 |
|
137 |
150 |
String[] words = str.split(" ");
|
138 |
151 |
str = "";
|
139 |
152 |
for (int i = 0; i < words.length; i++) {
|
... | ... | |
154 |
167 |
// nothing to do
|
155 |
168 |
}
|
156 |
169 |
}
|
157 |
|
|
|
170 |
|
158 |
171 |
// uncapitalize first letter
|
159 |
172 |
String tmpString = str.substring(0, 1).toLowerCase();
|
160 |
173 |
try {
|
... | ... | |
170 |
183 |
if(debug) {
|
171 |
184 |
System.out.println(" => " + str);
|
172 |
185 |
}
|
173 |
|
|
|
186 |
|
174 |
187 |
return str;
|
175 |
188 |
}
|
176 |
|
|
|
189 |
|
177 |
190 |
/**
|
178 |
191 |
*
|
179 |
192 |
* @param args
|
... | ... | |
183 |
196 |
*/
|
184 |
197 |
public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException, IOException {
|
185 |
198 |
|
186 |
|
// try {
|
187 |
|
// System.out.println("NormalizeKeys.main(): starting process...");
|
188 |
|
//
|
189 |
|
// File projectFile = new File(new File(System.getProperty("user.dir")).getParentFile().getAbsolutePath() + "/org.txm.core");
|
190 |
|
// File messageFile = new File(projectFile, "src/java/org/txm/core/messages/TXMCoreMessages.java");
|
191 |
|
//
|
192 |
|
// PluginMessages pmManager = new PluginMessages(projectFile, messageFile, false);
|
193 |
|
// NormalizeKeys keysNormalizer = new NormalizeKeys(true);
|
194 |
|
//
|
195 |
|
// // tests
|
196 |
|
// keysNormalizer.normalize(pmManager);
|
197 |
|
//
|
198 |
|
// //pmManager.dump(pmManager.getMessagesForMainLang());
|
199 |
|
// pmManager.dumpKeysReplacements();
|
200 |
|
//
|
201 |
|
// System.out.println("NormalizeKeys.main(): number of keys: " + pmManager.getMessageKeys().size() + ".");
|
202 |
|
// System.out.println("NormalizeKeys.main(): number of replacements to do: " + pmManager.getKeyModifications().size() + ".");
|
203 |
|
//
|
204 |
|
// System.out.println("NormalizeKeys.main(): terminated.");
|
205 |
|
//
|
206 |
|
// }
|
207 |
|
// catch (UnsupportedEncodingException e) {
|
208 |
|
// // TODO Auto-generated catch block
|
209 |
|
// e.printStackTrace();
|
210 |
|
// }
|
211 |
|
// catch (FileNotFoundException e) {
|
212 |
|
// // TODO Auto-generated catch block
|
213 |
|
// e.printStackTrace();
|
214 |
|
// }
|
215 |
|
// catch (IOException e) {
|
216 |
|
// // TODO Auto-generated catch block
|
217 |
|
// e.printStackTrace();
|
218 |
|
// }
|
219 |
|
|
|
199 |
// try {
|
|
200 |
// System.out.println("NormalizeKeys.main(): starting process...");
|
|
201 |
//
|
|
202 |
// File projectFile = new File(new File(System.getProperty("user.dir")).getParentFile().getAbsolutePath() + "/org.txm.core");
|
|
203 |
// File messageFile = new File(projectFile, "src/java/org/txm/core/messages/TXMCoreMessages.java");
|
|
204 |
//
|
|
205 |
// PluginMessages pmManager = new PluginMessages(projectFile, messageFile, false);
|
|
206 |
// NormalizeKeys keysNormalizer = new NormalizeKeys(true);
|
|
207 |
//
|
|
208 |
// // tests
|
|
209 |
// keysNormalizer.normalize(pmManager);
|
|
210 |
//
|
|
211 |
// //pmManager.dump(pmManager.getMessagesForMainLang());
|
|
212 |
// pmManager.dumpKeysReplacements();
|
|
213 |
//
|
|
214 |
// System.out.println("NormalizeKeys.main(): number of keys: " + pmManager.getMessageKeys().size() + ".");
|
|
215 |
// System.out.println("NormalizeKeys.main(): number of replacements to do: " + pmManager.getKeyModifications().size() + ".");
|
|
216 |
//
|
|
217 |
// System.out.println("NormalizeKeys.main(): terminated.");
|
|
218 |
//
|
|
219 |
// }
|
|
220 |
// catch (UnsupportedEncodingException e) {
|
|
221 |
// // TODO Auto-generated catch block
|
|
222 |
// e.printStackTrace();
|
|
223 |
// }
|
|
224 |
// catch (FileNotFoundException e) {
|
|
225 |
// // TODO Auto-generated catch block
|
|
226 |
// e.printStackTrace();
|
|
227 |
// }
|
|
228 |
// catch (IOException e) {
|
|
229 |
// // TODO Auto-generated catch block
|
|
230 |
// e.printStackTrace();
|
|
231 |
// }
|
|
232 |
|
220 |
233 |
NormalizeKeys keysNormalizer = new NormalizeKeys(false);
|
221 |
|
|
|
234 |
|
222 |
235 |
WorkspaceMessagesManager wmm = new WorkspaceMessagesManager();
|
223 |
236 |
for (PluginMessages pm : wmm.getPluginMessages().values()) {
|
224 |
237 |
keysNormalizer.normalize(pm);
|
225 |
238 |
}
|
226 |
|
|
|
239 |
|
227 |
240 |
//wmm.saveKeyModificationsInSources();
|
228 |
241 |
}
|
229 |
242 |
}
|