Revision 1238

tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/i18n/PluginMessages.java (revision 1238)
9 9
import java.io.PrintWriter;
10 10
import java.io.UnsupportedEncodingException;
11 11
import java.util.ArrayList;
12
import java.util.Arrays;
12 13
import java.util.HashMap;
13 14
import java.util.LinkedHashSet;
14 15
import java.util.Map;
15 16
import java.util.Properties;
16 17

  
17
import org.apache.tools.ant.types.resources.First;
18
import org.apache.commons.io.FilenameUtils;
19
import org.apache.commons.lang.StringUtils;
18 20
import org.txm.utils.BiHashMap;
19 21
import org.txm.utils.io.IOUtils;
20 22

  
......
27 29
 */
28 30
public class PluginMessages {
29 31
	
30
	File projectDirectory;
31
	File messageFile;
32

  
32 33
	public static String ENCODING = "UTF-8";
33
	
34 34
	public boolean debug = true;
35

  
36
	/**
37
	 * Project root directory.
38
	 */
39
	protected File projectDirectory;
35 40
	
41
	/**
42
	 * Java message file, eg.: TXMCoreMessages.java, ConcordanceUIMessages.java, etc.
43
	 */
44
	protected File javaMessageFile;
45
	
46
	/**
47
	 * Source files of the project.
48
	 */
49
	protected ArrayList<File> srcFiles;
50
	
51
	/**
52
	 * Extensions to use when creating the source files list.
53
	 */
54
	protected String[] srcFilesExtensions = new String[] {"java", "groovy"};
55
	
56
	
36 57
	LinkedHashSet<String> messageKeys = new LinkedHashSet<String>();
37 58
	HashMap<File, BiHashMap<String, String>> langs = new HashMap<File, BiHashMap<String, String>>();
38 59
	BiHashMap<File, String> file2lang = new BiHashMap<File, String>();
60
	
61
	
62
	
63

  
64
	/**
65
	 * 
66
	 * @param projectDirectory
67
	 * @param javaMessageFile
68
	 * @throws UnsupportedEncodingException
69
	 * @throws FileNotFoundException
70
	 * @throws IOException
71
	 */
72
	public PluginMessages(File projectDirectory, File javaMessageFile) throws UnsupportedEncodingException, FileNotFoundException, IOException {
39 73
		
40
	public PluginMessages(File projectDirectory, File messageFile) throws UnsupportedEncodingException, FileNotFoundException, IOException {
74
		this.javaMessageFile = javaMessageFile;
75
		this.projectDirectory = projectDirectory;
76
		this.srcFiles = new ArrayList<File>();
41 77
		
42
		this.messageFile = messageFile;
43
		this.projectDirectory = projectDirectory;
44

  
45 78
		System.out.println("********************************************************************************************************************");
46
		System.out.println("PluginMessages.PluginMessages(): project directory = " + this.projectDirectory);
47
		System.out.println("PluginMessages.PluginMessages(): message file = " + this.messageFile);
79
		System.out.println("PluginMessages.PluginMessages(): project root directory = " + this.projectDirectory);
80
		System.out.println("PluginMessages.PluginMessages(): java message file = " + this.javaMessageFile);
48 81
		
49
		File[] propFiles = messageFile.getParentFile().listFiles();
82
		File[] propFiles = javaMessageFile.getParentFile().listFiles();
50 83
		for (File propFile : propFiles) {
51 84
			String name = propFile.getName();
52 85
			if (!name.endsWith(".properties")) continue;
......
65 98
			}
66 99
		}
67 100
	
68
		if (messageFile != null) {
69
			BufferedReader reader2 = new BufferedReader(new InputStreamReader(new FileInputStream(messageFile), ENCODING));
101
		if (javaMessageFile != null) {
102
			BufferedReader reader2 = new BufferedReader(new InputStreamReader(new FileInputStream(javaMessageFile), ENCODING));
70 103
			String line2 = reader2.readLine();
71 104
			while (line2 != null) {
72 105
				line2 = line2.trim();
......
78 111
			}
79 112
			reader2.close();
80 113
		}
114
		
115
		// create the project source files list
116
		this.loadSourceFilesList();
81 117
	}
82 118
	
83 119
	public BiHashMap<String, String> getMessagesForLang(String lang) {
84
		File p = new File(messageFile.getParentFile(), "messages"+lang+".properties");
120
		File p = new File(javaMessageFile.getParentFile(), "messages" + lang + ".properties");
85 121
		return langs.get(p);
86 122
	}
87 123
	
88 124
	public void put(String lang, String key, String message) {
89 125
		messageKeys.add(key);
90 126
		
91
		File p = new File(messageFile.getParentFile(), "messages"+lang+".properties");
127
		File p = new File(javaMessageFile.getParentFile(), "messages"+lang+".properties");
92 128
		
93 129
		if (!langs.containsKey(p)) {
94 130
			langs.put(p, new BiHashMap<String, String>());
......
103 139
	}
104 140
	
105 141
	public String get(String lang, String key) {
106
		File p = new File(messageFile.getParentFile(), "messages"+lang+".properties");
142
		File p = new File(javaMessageFile.getParentFile(), "messages"+lang+".properties");
107 143
		
108 144
		if (!file2lang.containsKey(p)) return null;
109 145
		
......
115 151
	}
116 152
	
117 153
	public File getMessageFile() {
118
		return messageFile;
154
		return javaMessageFile;
119 155
	}
120 156
	
121 157
	public String getMessageClassName() {
122
		return messageFile.getName().substring(0, messageFile.getName().length()-5);
158
		return javaMessageFile.getName().substring(0, javaMessageFile.getName().length()-5);
123 159
	}
124 160
	
125 161
	public String getMessageFullClassName() {
126
		return messageFile.getAbsolutePath().substring(messageFile.getAbsolutePath().lastIndexOf("org/txm/"), messageFile.getAbsolutePath().length()-5).replace("/", ".");
162
		return javaMessageFile.getAbsolutePath().substring(javaMessageFile.getAbsolutePath().lastIndexOf("org/txm/"), javaMessageFile.getAbsolutePath().length()-5).replace("/", ".");
127 163
	}
128 164
	
129 165
	public String getMessageName() {
130
		return messageFile.getName().substring(0, messageFile.getName().length()-5-8);
166
		return javaMessageFile.getName().substring(0, javaMessageFile.getName().length()-5-8);
131 167
	}
132 168
	
133 169
	public String getMessageFullName() {
134
		return messageFile.getAbsolutePath().substring(messageFile.getAbsolutePath().lastIndexOf("org/txm/")+8, messageFile.getAbsolutePath().length()-5-8).replace("/", ".");
170
		return javaMessageFile.getAbsolutePath().substring(javaMessageFile.getAbsolutePath().lastIndexOf("org/txm/")+8, javaMessageFile.getAbsolutePath().length()-5-8).replace("/", ".");
135 171
	}
136 172
	
137 173
//	public def getMissingsMessageKeys(String lang) {
......
176 212
		}
177 213
		
178 214
		// write message File
179
		if (messageFile == null) return;
215
		if (javaMessageFile == null) return;
180 216
		
181
		File oldFile2 = new File(messageFile.getParentFile(), messageFile.getName()+".old");
182
		messageFile.renameTo(oldFile2); // back up
217
		File oldFile2 = new File(javaMessageFile.getParentFile(), javaMessageFile.getName()+".old");
218
		javaMessageFile.renameTo(oldFile2); // back up
183 219
		
184 220
		String className = getMessageClassName();
185 221
		String name = getMessageName();
186 222
		String classPackage = getMessageFullClassName().substring(0, getMessageFullClassName().length()- getMessageClassName().length());
187 223
		
188
		PrintWriter out = IOUtils.getWriter(messageFile, ENCODING);
224
		PrintWriter out = IOUtils.getWriter(javaMessageFile, ENCODING);
189 225
		
190 226
			// write start
191 227
			out.println("package "+classPackage+";\n");
......
238 274
	}
239 275
	
240 276
	
241
    public void getFilesUsingKey(File path, String fileExtension, String key, HashMap<File, String> files) {
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>();
242 285

  
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
	/**
302
	 * Parses the project and stores a list of the source files. 
303
	 */
304
	private void loadSourceFilesList(File path)	{
243 305
		File[] list = path.listFiles();
244 306

  
245 307
		if (list == null) {
246 308
			return;
247 309
		}
248 310

  
249
		for (File f : list) {
250
			if (f.isDirectory()) {
251
				this.getFilesUsingKey(f, fileExtension, key, files);
252
				// System.out.println( "Dir:" + f.getAbsoluteFile() );
311
		for (File file : list) {
312
			if (file.isDirectory()) {
313
				this.loadSourceFilesList(file);
253 314
			}
254
			else if (f.getName().endsWith(fileExtension)
255
					&& !f.equals(this.messageFile)
315
			else if (!file.equals(this.javaMessageFile)
316
					&& Arrays.asList(this.srcFilesExtensions).contains(FilenameUtils.getExtension(file.getName())) 
256 317
					) {
257
				//System.out.println("File: " + f.getAbsoluteFile());
258 318
				
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
					
319
				if(debug) {
320
					System.out.println("PluginMessages.loadSourceFilesList(): adding source files " + file + ".");
268 321
				}
269
				
322

  
323
				this.srcFiles.add(file);
270 324
			}
271 325
		}
272
    }
326
	}
273 327
	
274
    /**
275
     * Returns all Java files using the specified message key.
276
     * @param key
277
     */
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;
328
	/**
329
	 * Parses the project and stores a list of the source files.
330
	 */
331
	public void loadSourceFilesList()	{
332

  
333
		if(debug) {
334
			System.out.println("PluginMessages.loadSourceFilesList(): creating source files list for extensions [" + StringUtils.join(this.srcFilesExtensions, ", ") + "]...");
335
		}
336
		
337
		this.loadSourceFilesList(this.projectDirectory);
338
		
339
		if(debug) {
340
			System.out.println("PluginMessages.loadSourceFilesList(): done. Source files count = " + this.srcFiles.size() + ".");
341
		}
342
		
282 343
	}
344

  
283 345
	
284
	
346
	/**
347
	 * Returns the keys of all messages that are not used in the project.
348
	 * @return
349
	 */
285 350
	public ArrayList<String> findUnusedKeys()	{
286 351
		ArrayList<String> unusedKeys = new ArrayList<String>();
287 352
		for (String key : messageKeys) {

Also available in: Unified diff