Revision 1240

tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/i18n/PluginMessages.java (revision 1240)
14 14
import java.util.LinkedHashSet;
15 15
import java.util.Map;
16 16
import java.util.Properties;
17
import java.util.TreeSet;
18
import java.util.regex.Pattern;
17 19

  
18 20
import org.apache.commons.io.FilenameUtils;
19 21
import org.apache.commons.lang.StringUtils;
......
53 55
	 */
54 56
	protected String[] srcFilesExtensions = new String[] {"java", "groovy"};
55 57
	
58
	/**
59
	 * Global REGEX containing all keys to later use for search in files.
60
	 */
61
	protected Pattern keysGlobalREGEX;
56 62
	
63
	/**
64
	 * Index of the keys used and their associated files.
65
	 */
66
	protected HashMap<String, TreeSet<File>> usedKeysIndex;
67
	
68
	
69
	
57 70
	LinkedHashSet<String> messageKeys = new LinkedHashSet<String>();
58 71
	HashMap<File, BiHashMap<String, String>> langs = new HashMap<File, BiHashMap<String, String>>();
59 72
	BiHashMap<File, String> file2lang = new BiHashMap<File, String>();
......
113 126
		}
114 127
		
115 128
		// create the project source files list
116
		this.loadSourceFilesList();
129
		this.createSourceFilesList();
130
		
131
		//this.createKeysGlobalREGEX();
132
		this.createUsedKeysIndex();
117 133
	}
118 134
	
135
	/**
136
	 * Creates a global REGEX containing all keys to later search in files.
137
	 */
138
	public void createKeysGlobalREGEX()	{
139
		StringBuffer buffer = new StringBuffer();
140
		for (String key : messageKeys) {
141
			buffer.append("|" + key);
142
		}
143
		
144
		// remove first pipe
145
		buffer.deleteCharAt(0);
146
		
147
		this.keysGlobalREGEX = Pattern.compile("(" + buffer + ")");
148
		
149
		if(debug)	{
150
			System.out.println("PluginMessages.createKeysGlobalREGEX(): REGEX created: " + this.keysGlobalREGEX + ".");
151
		}
152
		
153
	}
154
	
155
	/**
156
	 * Creates the index of the keys used and their associated files.
157
	 */
158
	public void createUsedKeysIndex()	{
159
		this.usedKeysIndex = new HashMap<String, TreeSet<File>>();
160
		
161
		//ArrayList<String> unusedKeys = new ArrayList<String>();
162
		for (String key : messageKeys) {
163

  
164
			TreeSet<File> files = new TreeSet<File>();
165
			this.usedKeysIndex.put(key, files);
166
			
167
			if(debug) {
168
				System.out.println("PluginMessages.createUsedKeysIndex(): cheking key " + key + "...");
169
			}
170
			
171
			//HashMap<File, String> files = new HashMap<File, String>();
172

  
173
			for (File file : this.srcFiles) {
174
					ArrayList<String> lines = IOUtils.getLines(file, PluginMessages.ENCODING);
175
					
176
					for (String line : lines) {
177
						if(line.contains(this.getKeyFullName(key)))	{
178
							files.add(file);
179
						};
180
						
181
					}
182
			}
183
			
184
//			if (filesUsingKey.isEmpty()) {
185
//				unusedKeys.add(key);
186
//			}
187
		}
188
		
189
//		return unusedKeys;
190
		this.dumpUsedKeysIndex();
191
		
192
	}
193
	
194
	
195
	/**
196
	 * Dumps the index of the keys used and their associated files.
197
	 */
198
	public void dumpUsedKeysIndex()	{
199

  
200
		System.out.println("PluginMessages.dumpUsedKeysIndex(): dumping used keys files index...");
201

  
202
		for (Map.Entry<String, TreeSet<File>> entry : this.usedKeysIndex.entrySet()) {
203
		    System.out.println("key: " + this.getKeyFullName(entry.getKey()));
204
		    if(entry.getValue().isEmpty())	{
205
		    	System.out.println("   file(s): -not used in any file of the project " + this.getProjectDirectory() + "-");
206
		    }
207
		    else	{
208
			    for(File file : entry.getValue())	{
209
			    	System.out.println("   file(s): " + file);
210
			    }
211
		    }
212
		}
213
	}
214
	
215
//	/***
216
//	 * Returns all source files using the specified message key (except the main Java message file).
217
//	 * @param key
218
//	 * @return
219
//	 */
220
//    public HashMap<File, String> getFilesUsingKey(String key) {
221
//    	
222
//    	HashMap<File, String> files = new HashMap<File, String>();
223
//
224
//		for (File file : this.srcFiles) {
225
//				ArrayList<String> lines = IOUtils.getLines(file, PluginMessages.ENCODING);
226
//				
227
//				for (String line : lines) {
228
//					if(line.contains(key))	{
229
//						files.put(file, key);
230
//					};
231
//					
232
//				}
233
//		}
234
//		
235
//		return files;
236
//    }
237
//	
238
//    
239
//	/**
240
//	 * Returns the keys of all messages that are not used in the project itself.
241
//	 * @return
242
//	 */
243
//	public ArrayList<String> findUnusedKeys()	{
244
//		ArrayList<String> unusedKeys = new ArrayList<String>();
245
//		for (String key : messageKeys) {
246
//
247
//			if(debug) {
248
//				System.out.println("PluginMessages.findUnsedKeys(): cheking key " + key + "...");
249
//			}
250
//			
251
//			HashMap<File, String> filesUsingKey = this.getFilesUsingKey(key);
252
//			if (filesUsingKey.isEmpty()) {
253
//				unusedKeys.add(key);
254
//			}
255
//		}
256
//		
257
//		return unusedKeys;
258
//	}
259

  
260
	
261
	/**
262
	 * Gets the map of the messages keys and values for the specified language.
263
	 * @param lang
264
	 * @return
265
	 */
119 266
	public BiHashMap<String, String> getMessagesForLang(String lang) {
120 267
		File p = new File(javaMessageFile.getParentFile(), "messages" + lang + ".properties");
121 268
		return langs.get(p);
122 269
	}
123 270
	
271
	
124 272
	public void put(String lang, String key, String message) {
125 273
		messageKeys.add(key);
126 274
		
......
170 318
		return javaMessageFile.getAbsolutePath().substring(javaMessageFile.getAbsolutePath().lastIndexOf("org/txm/")+8, javaMessageFile.getAbsolutePath().length()-5-8).replace("/", ".");
171 319
	}
172 320
	
321
	/**
322
	 * Returns the full name of the specified key including the Java message class name.
323
	 * @param key
324
	 * @return
325
	 */
326
	public String getKeyFullName(String key)	{
327
		return this.getMessageClassName() + "." + key;
328
	}
329
	
173 330
//	public def getMissingsMessageKeys(String lang) {
174 331
//		def missing = []
175 332
//		for (String pKey : this.getKeys())
......
273 430
		}
274 431
	}
275 432
	
276
	
277
	/***
278
	 * Returns all source files using the specified message key (except the main Java message file).
279
	 * @param key
280
	 * @return
281
	 */
282
    public HashMap<File, String> getFilesUsingKey(String key) {
283
    	
284
    	HashMap<File, String> files = new HashMap<File, String>();
285 433

  
286
		for (File file : this.srcFiles) {
287
				ArrayList<String> lines = IOUtils.getLines(file, PluginMessages.ENCODING);
288
				
289
				for (String line : lines) {
290
					if(line.contains(key))	{
291
						files.put(file, key);
292
					};
293
					
294
				}
295
		}
296
		
297
		return files;
298
    }
299
	
300
    
301 434
	/**
302 435
	 * Parses the project and stores a list of the source files. 
303 436
	 */
......
328 461
	/**
329 462
	 * Parses the project and stores a list of the source files.
330 463
	 */
331
	public void loadSourceFilesList()	{
464
	public void createSourceFilesList()	{
332 465

  
333 466
		if(debug) {
334 467
			System.out.println("PluginMessages.loadSourceFilesList(): creating source files list for extensions [" + StringUtils.join(this.srcFilesExtensions, ", ") + "]...");
......
343 476
	}
344 477

  
345 478
	
479
	public BiHashMap<File, String> getFile2lang() {
480
		return file2lang;
481
	}
482

  
483
	public HashMap<File, BiHashMap<String, String>> getLangs() {
484
		return langs;
485
	}
486

  
346 487
	/**
347
	 * Returns the keys of all messages that are not used in the project itself.
348
	 * @return
488
	 * Gets the project root directory.
489
	 * @return the projectDirectory
349 490
	 */
350
	public ArrayList<String> findUnusedKeys()	{
351
		ArrayList<String> unusedKeys = new ArrayList<String>();
352
		for (String key : messageKeys) {
353

  
354
			if(debug) {
355
				System.out.println("PluginMessages.findUnsedKeys(): cheking key " + key + "...");
356
			}
357
			
358
			HashMap<File, String> filesUsingKey = this.getFilesUsingKey(key);
359
			if (filesUsingKey.isEmpty()) {
360
				unusedKeys.add(key);
361
			}
362
		}
363
		
364
		return unusedKeys;
491
	public File getProjectDirectory() {
492
		return projectDirectory;
365 493
	}
366 494
	
495
	
367 496
	/**
497
     * Dumps the specified BiHashMap<String, String> to standard output.
498
     */
499
    public void dump(BiHashMap<String, String> messages)	{
500
    	for (String key : messages.getKeys()) {
501
			System.out.println("PluginMessages.dump(): " + key + "=" + messages.get(key));
502
		}
503
    }
504
    
505
    
506
    
507
	/**
368 508
	 * 
369 509
	 * @param args
370 510
	 * @throws UnsupportedEncodingException
......
379 519
		PluginMessages pmManager = new PluginMessages(projectFile, messageFile);
380 520
		
381 521
		// test to find files using the specified key
382
		HashMap<File, String> files = pmManager.getFilesUsingKey("Base_5");
383
		for (Map.Entry<File, String> entry : files.entrySet()) {
384
		    System.out.println("find files using key: file " + entry.getKey() + " contains key " + entry.getValue());
385
		}
522
//		HashMap<File, String> files = pmManager.getFilesUsingKey("Base_5");
523
//		for (Map.Entry<File, String> entry : files.entrySet()) {
524
//		    System.out.println("find files using key: file " + entry.getKey() + " contains key " + entry.getValue());
525
//		}
526
//		
527
//		// test to find unused keys
528
//		ArrayList<String> unusedKeys = pmManager.findUnusedKeys();
529
//		for (int i = 0; i < unusedKeys.size(); i++) {
530
//			System.out.println("findUnusedKeys: key " + unusedKeys.get(i) + " is unused in project " + pmManager.getProjectDirectory() + " (main language value = " + pmManager.getMessagesForLang("").get(unusedKeys.get(i)) + ")");	
531
//		}
386 532
		
387
		// test to find unused keys
388
		ArrayList<String> unusedKeys = pmManager.findUnusedKeys();
389
		for (int i = 0; i < unusedKeys.size(); i++) {
390
			System.out.println("findUnusedKeys: key " + unusedKeys.get(i) + " is unused in project " + pmManager.getProjectDirectory() + " (main language value = " + pmManager.getMessagesForLang("").get(unusedKeys.get(i)) + ")");	
391
		}
392
		
393 533
		//dict.summary();
394 534
		//dict.saveChanges();
395 535
		
396 536
		System.out.println("PluginMessages.main(): done.");
397 537
	}
398 538

  
399
	public BiHashMap<File, String> getFile2lang() {
400
		return file2lang;
401
	}
402

  
403
	public HashMap<File, BiHashMap<String, String>> getLangs() {
404
		return langs;
405
	}
406

  
407 539
	/**
408
	 * Gets the project root directory.
409
	 * @return the projectDirectory
540
	 * @return the usedKeysIndex
410 541
	 */
411
	public File getProjectDirectory() {
412
		return projectDirectory;
542
	public HashMap<String, TreeSet<File>> getUsedKeysIndex() {
543
		return usedKeysIndex;
413 544
	}
414
	
415
	
416
	/**
417
     * Dumps the specified BiHashMap<String, String> to standard output.
418
     */
419
    public void dump(BiHashMap<String, String> messages)	{
420
    	for (String key : messages.getKeys()) {
421
			System.out.println("PluginMessages.dump(): " + key + "=" + messages.get(key));
422
		}
423
    }
545

  
546
    
424 547
}

Also available in: Unified diff