Révision 1234

tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/devtools/FindUnusedKeys.java (revision 1234)
1 1
package org.txm.rcp.translate.devtools;
2 2

  
3 3
/**
4
 * Checks that all key defined in Messages.java are used in the source files.
4
 * Checks that all key defined in Messages.java class of a bundle are used in the source files.
5 5
 * 
6 6
 * @author sjacquot
7 7
 *
tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/i18n/PluginMessages.java (revision 1234)
8 8
import java.io.InputStreamReader;
9 9
import java.io.PrintWriter;
10 10
import java.io.UnsupportedEncodingException;
11
import java.nio.file.Files;
12
import java.nio.file.Paths;
13 11
import java.util.ArrayList;
14 12
import java.util.HashMap;
15 13
import java.util.LinkedHashSet;
14
import java.util.Map;
16 15
import java.util.Properties;
17 16

  
17
import org.apache.tools.ant.types.resources.First;
18 18
import org.txm.utils.BiHashMap;
19 19
import org.txm.utils.io.IOUtils;
20 20

  
......
29 29
	
30 30
	File projectDirectory;
31 31
	File messageFile;
32
	String encoding = "UTF-8";
32
	public static String ENCODING = "UTF-8";
33 33
	
34
	public boolean debug = true;
35
	
34 36
	LinkedHashSet<String> messageKeys = new LinkedHashSet<String>();
35 37
	HashMap<File, BiHashMap<String, String>> langs = new HashMap<File, BiHashMap<String, String>>();
36 38
	BiHashMap<File, String> file2lang = new BiHashMap<File, String>();
......
57 59
			BiHashMap<String, String> hash = langs.get(propFile);
58 60
			
59 61
			Properties props1 = new Properties();
60
			props1.load(IOUtils.getReader(propFile, encoding));
62
			props1.load(IOUtils.getReader(propFile, ENCODING));
61 63
			for (Object k : props1.keySet()) {
62 64
				hash.put(k.toString(), props1.get(k).toString());
63 65
			}
64 66
		}
65 67
	
66 68
		if (messageFile != null) {
67
			BufferedReader reader2 = new BufferedReader(new InputStreamReader(new FileInputStream(messageFile), encoding));
69
			BufferedReader reader2 = new BufferedReader(new InputStreamReader(new FileInputStream(messageFile), ENCODING));
68 70
			String line2 = reader2.readLine();
69 71
			while (line2 != null) {
70 72
				line2 = line2.trim();
......
170 172
				propFile.renameTo(oldFile);
171 173
			}
172 174
			
173
			props.store(IOUtils.getWriter(propFile, encoding),  "TXM messages generated by the PluginMessages class");
175
			props.store(IOUtils.getWriter(propFile, ENCODING),  "TXM messages generated by the PluginMessages class");
174 176
		}
175 177
		
176 178
		// write message File
......
183 185
		String name = getMessageName();
184 186
		String classPackage = getMessageFullClassName().substring(0, getMessageFullClassName().length()- getMessageClassName().length());
185 187
		
186
		PrintWriter out = IOUtils.getWriter(messageFile, encoding);
188
		PrintWriter out = IOUtils.getWriter(messageFile, ENCODING);
187 189
		
188 190
			// write start
189 191
			out.println("package "+classPackage+";\n");
......
236 238
	}
237 239
	
238 240
	
239
    public void walk(File path, String fileExtension) {
241
    public void getFilesUsingKey(File path, String fileExtension, String key, HashMap<File, String> files) {
240 242

  
241 243
		File[] list = path.listFiles();
242 244

  
......
246 248

  
247 249
		for (File f : list) {
248 250
			if (f.isDirectory()) {
249
				this.walk(f, fileExtension);
251
				this.getFilesUsingKey(f, fileExtension, key, files);
250 252
				// System.out.println( "Dir:" + f.getAbsoluteFile() );
251 253
			}
252 254
			else if (f.getName().endsWith(fileExtension)
253
					//&& !f.equals(this.messageFile)
255
					&& !f.equals(this.messageFile)
254 256
					) {
257
				//System.out.println("File: " + f.getAbsoluteFile());
255 258
				
256
				System.out.println("File: " + f.getAbsoluteFile());
259
				ArrayList<String> lines = IOUtils.getLines(f, PluginMessages.ENCODING);
260
				
261
				for (String line : lines) {
262
					//println ""+line
263
					if(line.contains(key))	{
264
						files.put(f, key);
265
//						System.err.println("PluginMessages.walk(): file " + f + " contains key " + key);
266
					};
267
					
268
				}
269
				
257 270
			}
258 271
		}
259 272
    }
......
262 275
     * Returns all Java files using the specified message key.
263 276
     * @param key
264 277
     */
265
	public void getFilesUsingKey(String key)	{
266
		this.walk(this.projectDirectory, ".java");
278
	public HashMap<File, String> getFilesUsingKey(String key)	{
279
		HashMap<File, String> files = new HashMap<File, String>();
280
		this.getFilesUsingKey(this.projectDirectory, ".java", key, files);
281
		return files;
267 282
	}
268 283
	
284
	
285
	public ArrayList<String> findUnusedKeys()	{
286
		ArrayList<String> unusedKeys = new ArrayList<String>();
287
		for (String key : messageKeys) {
288

  
289
			if(debug) {
290
				System.out.println("PluginMessages.findUnsedKeys(): cheking key " + key + "...");
291
			}
292
			
293
			HashMap<File, String> filesUsingKey = this.getFilesUsingKey(key);
294
			if (filesUsingKey.isEmpty()) {
295
				unusedKeys.add(key);
296
			}
297
		}
298
		
299
		return unusedKeys;
300
	}
301
	
269 302
	/**
270 303
	 * 
271 304
	 * @param args
......
278 311
		File messageFile = new File(projectFile, "src/java/org/txm/core/messages/TXMCoreMessages.java");
279 312
		PluginMessages dict = new PluginMessages(projectFile, messageFile);
280 313
		
314
		// test to find files using the specified key
315
		HashMap<File, String> files = dict.getFilesUsingKey("Base_5");
316
		for (Map.Entry<File, String> entry : files.entrySet()) {
317
		    System.out.println("find files using keys: file " + entry.getKey() + " contains key " + entry.getValue());
318
		}
281 319
		
282
		//dict.getFilesUsingKey("test");
320
		// test to find unused keys
321
		ArrayList<String> unusedKeys = dict.findUnusedKeys();
322
		for (int i = 0; i < unusedKeys.size(); i++) {
323
			System.out.println("findUnusedKeys: key " + unusedKeys.get(i) + " is unused in project " + dict.getProjectDirectory() + " (main lang value = " + dict.getMessagesForLang("").get(unusedKeys.get(i)) + ")");	
324
		}
283 325
		
284
		dict.summary();
326
		
327
		
328
		
329
		//dict.summary();
285 330
		//dict.saveChanges();
331
		
332
		System.out.println("PluginMessages.main(): done.");
286 333
	}
287 334

  
288 335
	public BiHashMap<File, String> getFile2lang() {

Formats disponibles : Unified diff