Révision 1320

tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/i18n/PluginMessagesManager.java (revision 1320)
44 44
	public static String ENCODING = "UTF-8";
45 45

  
46 46
	public final static String FILE_SEPARATOR = System.getProperty("file.separator");
47
	
47

  
48 48
	/**
49 49
	 * Debug state.
50 50
	 */
......
153 153
	}
154 154

  
155 155

  
156
	
156

  
157 157
	/**
158 158
	 * 
159 159
	 * @param projectDirectory
......
166 166
		this(projectDirectory, javaMessageFile, false);
167 167
	}
168 168

  
169
	
170
	
169

  
170

  
171 171
	/**
172 172
	 * 
173 173
	 * @param projectDirectory
......
190 190
			System.out.println("PluginMessagesManager.PluginMessages(): java message file = " + this.javaMessageFile);
191 191
		}
192 192

  
193
		// create the project source files list
194
		this.createSourceFilesList();
193 195

  
196
		//this.createKeysGlobalREGEX();
197
		this.createUsedKeysFilesIndex();
198

  
199

  
194 200
		if (this.javaMessageFile != null) {
195 201

  
196 202
			File[] propFiles = javaMessageFile.getParentFile().listFiles();
......
216 222
			String line2 = reader2.readLine();
217 223
			while (line2 != null) {
218 224
				line2 = line2.trim();
219
				
225

  
220 226
				if (line2.startsWith("public static String")) {
221 227
					int idx = line2.indexOf("//");
222 228
					if (idx > 0 && line2.contains("\"")) { // remove comment from line
......
225 231
					}
226 232
					if (line2.endsWith(";") && !line2.contains("=") && !line2.contains("\"")) {
227 233
						line2 = line2.substring(21, line2.length() -1);
228
						
234

  
229 235
						if(debug)	{
230 236
							System.out.println("PluginMessagesManager.PluginMessagesManager(): messages key: " + line2 + " added.");
231 237
						}
232 238
						messageKeys.add(line2);
233 239
					}
234 240
				}
235
				
241

  
236 242
				line2 = reader2.readLine();
237 243
			}
238 244
			reader2.close();
239
			
240
			
245

  
246

  
241 247
			if(debug)	{
242 248
				System.out.println("PluginMessagesManager.PluginMessagesManager(): number of keys: " + this.messageKeys.size() + ".");
243 249
			}
244
			
245 250

  
246
			// create the project source files list
247
			this.createSourceFilesList();
248

  
249
			//this.createKeysGlobalREGEX();
250
			this.createUsedKeysFilesIndex();
251

  
252 251
			// try loading OSGI messages 
253 252
			if (pluginXMLFile.exists()) {
254 253
				ArrayList<String> result = IOUtils.findWithGroup(pluginXMLFile, "\"%([^\"]+)\"");
......
281 280
		}
282 281
	}
283 282

  
284
//		/**
285
//		 * Creates a global REGEX containing all keys to later search in files.
286
//		 */
287
//		public void createKeysGlobalREGEX()	{
288
//			
289
//			if(messageKeys.size() == 0) {
290
//				return;
291
//			}
292
//			
293
//			StringBuffer buffer = new StringBuffer();
294
//			buffer.append(this.getMessageClassName() + "\\\\.(");
295
//			
296
//			int i = 0;
297
//			for (String key : messageKeys) {
298
//				if(i > 0)	{
299
//					buffer.append("|" + key);	
300
//				}
301
//				else {
302
//					buffer.append(key);
303
//				}
304
//				i++;
305
//			}
306
//			
307
//			// remove first pipe
308
//			//buffer.deleteCharAt(0);
309
//			
310
//			this.keysGlobalREGEX = Pattern.compile("(" + buffer + "))");
311
//			
312
//			if(debug)	{
313
//				System.out.println("PluginMessagesManager.createKeysGlobalREGEX(): REGEX created: " + this.keysGlobalREGEX + ".");
314
//			}
315
//			
316
//		}
283
	//		/**
284
	//		 * Creates a global REGEX containing all keys to later search in files.
285
	//		 */
286
	//		public void createKeysGlobalREGEX()	{
287
	//			
288
	//			if(messageKeys.size() == 0) {
289
	//				return;
290
	//			}
291
	//			
292
	//			StringBuffer buffer = new StringBuffer();
293
	//			buffer.append(this.getMessageClassName() + "\\\\.(");
294
	//			
295
	//			int i = 0;
296
	//			for (String key : messageKeys) {
297
	//				if(i > 0)	{
298
	//					buffer.append("|" + key);	
299
	//				}
300
	//				else {
301
	//					buffer.append(key);
302
	//				}
303
	//				i++;
304
	//			}
305
	//			
306
	//			// remove first pipe
307
	//			//buffer.deleteCharAt(0);
308
	//			
309
	//			this.keysGlobalREGEX = Pattern.compile("(" + buffer + "))");
310
	//			
311
	//			if(debug)	{
312
	//				System.out.println("PluginMessagesManager.createKeysGlobalREGEX(): REGEX created: " + this.keysGlobalREGEX + ".");
313
	//			}
314
	//			
315
	//		}
317 316

  
318
		
319
		/**
320
		 * Creates a global REGEX containing all keys to later search in files.
321
		 */
322
		public void createKeysGlobalREGEX()	{
323
			StringBuffer buffer = new StringBuffer();
324
			for (String key : messageKeys) {
325
				buffer.append("|" + this.getKeyFullName(key).replaceAll("\\.", "\\\\."));
326
			}
327
			
328
			// remove first pipe
329
			buffer.deleteCharAt(0);
330
			
331
			this.keysGlobalREGEX = Pattern.compile("(" + buffer + ")");
332
			
333
			if(debug)	{
334
				System.out.println("PluginMessagesManager.createKeysGlobalREGEX(): REGEX created: " + this.keysGlobalREGEX + ".");
335
			}
336
			
317

  
318
	/**
319
	 * Creates a global REGEX containing all keys to later search in files.
320
	 */
321
	public void createKeysGlobalREGEX()	{
322
		StringBuffer buffer = new StringBuffer();
323
		for (String key : messageKeys) {
324
			buffer.append("|" + this.getKeyFullName(key).replaceAll("\\.", "\\\\."));
337 325
		}
338
		
339
		
340
		
341
		public static File findMessageFile(File projectDirectory2) {
342
			File messagesPackageDir = new File(projectDirectory2, "src/"+projectDirectory2.getName().replaceAll("\\.", "/")+"/messages");
343 326

  
344
			if (!messagesPackageDir.exists()) {
345
				messagesPackageDir = new File(projectDirectory2, "src/java/"+projectDirectory2.getName().replaceAll("\\.", "/")+"/messages");
346
			}
347
			
348
			if (!messagesPackageDir.exists()) {
349
				messagesPackageDir = new File(projectDirectory2, "src/main/java/"+projectDirectory2.getName().replaceAll("\\.", "/")+"/messages");
350
			}
351
			
352
			if (!messagesPackageDir.exists()) {
353
				return null;
354
			}
355
			
356
			for (File messagesJavaFile : messagesPackageDir.listFiles()) {
357
				if (messagesJavaFile.isDirectory()) continue;
358
				if (!messagesJavaFile.getName().endsWith("Messages.java")) continue;
359
				return messagesJavaFile;
360
			}
327
		// remove first pipe
328
		buffer.deleteCharAt(0);
329

  
330
		this.keysGlobalREGEX = Pattern.compile("(" + buffer + ")");
331

  
332
		if(debug)	{
333
			System.out.println("PluginMessagesManager.createKeysGlobalREGEX(): REGEX created: " + this.keysGlobalREGEX + ".");
334
		}
335

  
336
	}
337

  
338

  
339

  
340
	public static File findMessageFile(File projectDirectory2) {
341
		File messagesPackageDir = new File(projectDirectory2, "src/"+projectDirectory2.getName().replaceAll("\\.", "/")+"/messages");
342

  
343
		if (!messagesPackageDir.exists()) {
344
			messagesPackageDir = new File(projectDirectory2, "src/java/"+projectDirectory2.getName().replaceAll("\\.", "/")+"/messages");
345
		}
346

  
347
		if (!messagesPackageDir.exists()) {
348
			messagesPackageDir = new File(projectDirectory2, "src/main/java/"+projectDirectory2.getName().replaceAll("\\.", "/")+"/messages");
349
		}
350

  
351
		if (!messagesPackageDir.exists()) {
361 352
			return null;
362 353
		}
363
	
364
//	public static File findMessageFile(File projectDirectory2) {
365
////		System.out.println("PluginMessagesManager.findMessageFile() " + FILE_SEPARATOR);
366
//		File messagesPackageDir = new File(projectDirectory2, "src" + FILE_SEPARATOR + projectDirectory2.getName().replaceAll("\\.", "\\" + FILE_SEPARATOR) + FILE_SEPARATOR + "messages");
367
//
368
//		if (!messagesPackageDir.exists()) {
369
//			messagesPackageDir = new File(projectDirectory2, "src" + FILE_SEPARATOR + "java" + FILE_SEPARATOR + projectDirectory2.getName().replaceAll("\\.", "\\" + FILE_SEPARATOR) + FILE_SEPARATOR + "messages");
370
//		}
371
//
372
//		if (!messagesPackageDir.exists()) {
373
//			messagesPackageDir = new File(projectDirectory2, "src" + FILE_SEPARATOR + "main" + FILE_SEPARATOR + "java" + FILE_SEPARATOR + projectDirectory2.getName().replaceAll("\\.", "\\" + FILE_SEPARATOR) + FILE_SEPARATOR + "messages");
374
//		}
375
//
376
//		if (!messagesPackageDir.exists()) {
377
//			return null;
378
//		}
379
//
380
//		for (File messagesJavaFile : messagesPackageDir.listFiles()) {
381
//			if (messagesJavaFile.isDirectory()) continue;
382
//			if (!messagesJavaFile.getName().endsWith("Messages.java")) continue;
383
//			return messagesJavaFile;
384
//		}
385
//		return null;
386
//	}
387 354

  
355
		for (File messagesJavaFile : messagesPackageDir.listFiles()) {
356
			if (messagesJavaFile.isDirectory()) continue;
357
			if (!messagesJavaFile.getName().endsWith("Messages.java")) continue;
358
			return messagesJavaFile;
359
		}
360
		return null;
361
	}
388 362

  
389
	
363
	//	public static File findMessageFile(File projectDirectory2) {
364
	////		System.out.println("PluginMessagesManager.findMessageFile() " + FILE_SEPARATOR);
365
	//		File messagesPackageDir = new File(projectDirectory2, "src" + FILE_SEPARATOR + projectDirectory2.getName().replaceAll("\\.", "\\" + FILE_SEPARATOR) + FILE_SEPARATOR + "messages");
366
	//
367
	//		if (!messagesPackageDir.exists()) {
368
	//			messagesPackageDir = new File(projectDirectory2, "src" + FILE_SEPARATOR + "java" + FILE_SEPARATOR + projectDirectory2.getName().replaceAll("\\.", "\\" + FILE_SEPARATOR) + FILE_SEPARATOR + "messages");
369
	//		}
370
	//
371
	//		if (!messagesPackageDir.exists()) {
372
	//			messagesPackageDir = new File(projectDirectory2, "src" + FILE_SEPARATOR + "main" + FILE_SEPARATOR + "java" + FILE_SEPARATOR + projectDirectory2.getName().replaceAll("\\.", "\\" + FILE_SEPARATOR) + FILE_SEPARATOR + "messages");
373
	//		}
374
	//
375
	//		if (!messagesPackageDir.exists()) {
376
	//			return null;
377
	//		}
378
	//
379
	//		for (File messagesJavaFile : messagesPackageDir.listFiles()) {
380
	//			if (messagesJavaFile.isDirectory()) continue;
381
	//			if (!messagesJavaFile.getName().endsWith("Messages.java")) continue;
382
	//			return messagesJavaFile;
383
	//		}
384
	//		return null;
385
	//	}
386

  
387

  
388

  
390 389
	/**
391 390
	 * Creates/updates the index of the keys used and their associated files.
392 391
	 * @throws IOException 
......
395 394
		this.usedKeysFilesIndex = new TreeMap<String, TreeSet<File>>();
396 395

  
397 396
		// Old version, based on real key name
398
//		for (File file : this.srcFiles) {
399
//			for (String key : messageKeys) {
400
//				if(debug) {
401
//					System.out.println("PluginMessagesManager.createUsedKeysIndex(): looking for key " + this.getKeyFullName(key) + " in project source files...");
402
//				}
403
//				String lines = IOUtils.getText(file, PluginMessagesManager.ENCODING);
404
//				if(lines.contains(this.getKeyFullName(key)))	{
405
//					if (!this.usedKeysFilesIndex.containsKey(key)) {
406
//						this.usedKeysFilesIndex.put(key, new TreeSet<File>());
407
//					}
408
//					this.usedKeysFilesIndex.get(key).add(file);
409
//				};
410
//			}
411
//		}
397
		//		for (File file : this.srcFiles) {
398
		//			for (String key : messageKeys) {
399
		//				if(debug) {
400
		//					System.out.println("PluginMessagesManager.createUsedKeysIndex(): looking for key " + this.getKeyFullName(key) + " in project source files...");
401
		//				}
402
		//				String lines = IOUtils.getText(file, PluginMessagesManager.ENCODING);
403
		//				if(lines.contains(this.getKeyFullName(key)))	{
404
		//					if (!this.usedKeysFilesIndex.containsKey(key)) {
405
		//						this.usedKeysFilesIndex.put(key, new TreeSet<File>());
406
		//					}
407
		//					this.usedKeysFilesIndex.get(key).add(file);
408
		//				};
409
		//			}
410
		//		}
412 411

  
413 412
		// New version using REGEX
414 413
		for (File file : this.srcFiles) {
......
420 419
			while (m.find()) {
421 420
				// Get the matching string
422 421
				String key = m.group();
423
				
422

  
424 423
				if (!this.usedKeysFilesIndex.containsKey(key)) {
425 424
					this.usedKeysFilesIndex.put(key, new TreeSet<File>());
426 425
				}
427 426
				this.usedKeysFilesIndex.get(key).add(file);
428
				
427

  
429 428
				if(debug) {
430 429
					System.out.println("PluginMessagesManager.createUsedKeysFilesIndex(): messages key: " + key + " added for file " + file +  ".");
431 430
				}
......
447 446
			System.out.println("PluginMessagesManager.dumpUsedKeysIndex(): project have no used keys.");
448 447
			return;
449 448
		}
450
		
449

  
451 450
		for (Map.Entry<String, TreeSet<File>> entry : this.usedKeysFilesIndex.entrySet()) {
452 451
			System.out.println("key: " + entry.getKey());
453 452
			if(entry.getValue().isEmpty())	{
......
459 458
				}
460 459
			}
461 460
		}
462
		
461

  
463 462
		System.out.println("PluginMessagesManager.dumpUsedKeysIndex(): number of used keys: " + this.usedKeysFilesIndex.size() + ".");
464 463
	}
465 464

  
......
641 640

  
642 641
		// write message File
643 642
		File newJavaMessageFile = new File(javaMessageFile.getParentFile(), javaMessageFile.getName()+".new");
644
		
643

  
645 644
		ArrayList<String> lines = IOUtils.getLines(javaMessageFile, "UFT-8");
646 645
		TreeSet<String> oldKeys = new TreeSet<String>(IOUtils.findWithGroup(javaMessageFile, "public static String ([^\"=;]+);"));
647 646
		TreeSet<String> newKeys = new TreeSet<String>(messageKeys);
648 647
		newKeys.removeAll(oldKeys);
649 648
		newKeys.removeAll(keyModifications.values()); // now contains only the very newly created keys
650
		
649

  
651 650
		if (replaceFiles) {
652 651
			newJavaMessageFile = javaMessageFile;
653 652
		}
654 653

  
655 654
		PrintWriter out = IOUtils.getWriter(newJavaMessageFile, ENCODING);
656
		
655

  
657 656
		// update lines
658 657
		for (String line : lines) {
659 658
			String l = line.trim();// remove tabs
......
663 662
				comments = l.substring(idx).trim();
664 663
				l = l.substring(0, idx).trim();
665 664
			}
666
			
665

  
667 666
			if (l.startsWith("private static final String BUNDLE_NAME")) {
668 667
				out.println(line);
669
				
668

  
670 669
				// write the totally new keys
671 670
				if (newKeys.size() > 0) {
672 671
					out.println("\t");
......
675 674
					out.println("\tpublic static String "+key+";");
676 675
				}
677 676
			} else if (l.startsWith("public static String") && l.endsWith(";") && !l.contains("=") && !l.contains("\"")) {
678
				
677

  
679 678
				String key = l.substring(21, l.length() -1);
680 679
				if (messageKeys.contains(key)) {
681 680
					out.println(line); // keep the line
......
689 688
				out.println(line);
690 689
			}
691 690
		}
692
		
691

  
693 692
		out.close();
694 693
	}
695 694

  
......
835 834
		while (messageKeys.contains(newKey)) {
836 835
			newKey = key+"_"+(c++);
837 836
		}
838
		
837

  
839 838
		messageKeys.add(newKey);
840 839
		getMessagesForDefaultLang().put(newKey, defaultMessage);
841 840
	}
......
903 902
	public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException, IOException {
904 903

  
905 904
		long startTime = System.currentTimeMillis();
906
		
905

  
907 906
		//File projectFile = new File(new File(System.getProperty("user.dir")).getParentFile().getAbsolutePath() + "/org.txm.rcp");
908 907
		File projectFile = new File(new File(System.getProperty("user.dir")).getParentFile().getAbsolutePath() + "/org.txm.core");
909 908

  
......
936 935

  
937 936
		System.out.println("dir="+pmManager.getProjectDirectory());
938 937
		System.out.println("Elapsed time:"+ ((double)(System.currentTimeMillis()-startTime)/1000));
939
		
938

  
940 939
		System.out.println("PluginMessagesManager.main(): done.");
941 940
	}
942 941
}
tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/i18n/WorkspaceMessagesManager.java (revision 1320)
63 63
	public WorkspaceMessagesManager() throws UnsupportedEncodingException, FileNotFoundException, IOException {
64 64
		this(false);
65 65
	}
66
	
66

  
67 67
	/**
68 68
	 * Creates the manager using the "user.dir" parent directory -> works when run from Eclipse
69 69
	 * 
......
91 91

  
92 92
		this.workspaceLocation = workspaceLocation;
93 93
		this.debug = debug;
94
		
94

  
95 95
		if (debug) {
96 96
			System.out.println("WorkspaceMessagesManager.WorkspaceMessagesManager(): workspace location = " + this.workspaceLocation);
97 97
		}
......
105 105
			if (!project.getName().startsWith("org.txm")) continue;
106 106
			if (project.getName().endsWith(".feature")) continue;
107 107

  
108
//			File messagesPackageDir = new File(project, "src/"+project.getName().replaceAll("\\.", "/")+"/messages");
109
//			if (!messagesPackageDir.exists()) {
110
//				messagesPackageDir = new File(project, "src/java/"+project.getName().replaceAll("\\.", "/")+"/messages");
111
//			}
112
//			//System.out.println(messagesPackageDir.getAbsolutePath());
113
//
114
//			File messagesJavaFile = PluginMessages.findMessageFile(project);
108
			//			File messagesPackageDir = new File(project, "src/"+project.getName().replaceAll("\\.", "/")+"/messages");
109
			//			if (!messagesPackageDir.exists()) {
110
			//				messagesPackageDir = new File(project, "src/java/"+project.getName().replaceAll("\\.", "/")+"/messages");
111
			//			}
112
			//			//System.out.println(messagesPackageDir.getAbsolutePath());
113
			//
114
			//			File messagesJavaFile = PluginMessages.findMessageFile(project);
115 115

  
116
//			if (messagesJavaFile != null && messagesJavaFile.exists()) {
116
			//			if (messagesJavaFile != null && messagesJavaFile.exists()) {
117 117

  
118
				PluginMessagesManager messages = new PluginMessagesManager(project, debug);
118
			PluginMessagesManager messages = new PluginMessagesManager(project, debug);
119 119

  
120
				pluginsMessagesPerProject.put(project, messages);
120
			pluginsMessagesPerProject.put(project, messages);
121 121

  
122
				if (debug) {
123
					System.out.println(project + "=" + messages.getLangs().size() + " langs.");
124
				}
125
//			}
122
			if (debug) {
123
				System.out.println(project + "=" + messages.getLangs().size() + " langs.");
124
			}
125
			//			}
126 126
		}
127 127

  
128 128
		this.createUsedKeysFilesIndex();
......
161 161
				}
162 162
			}
163 163
		}
164
		
164

  
165 165
		System.out.println("WorkspaceMessagesManager.dumpUsedKeysFilesIndex(): number of used keys: " + this.usedKeysFilesIndex.size());
166 166
	}
167 167

  
......
207 207

  
208 208
		System.out.println("Keys to update: "+keys);
209 209
		ArrayList<String> updated = new ArrayList<String>();
210
//		for (PluginMessagesManager pm : pluginsMessagesPerProject.values()) {
211
			TreeMap<String, TreeSet<File>> index = getUsedKeysFilesIndex();
212
			
213
			for (String oldKey : keys) {
214
				if (index.containsKey(oldKey)) {
215
					TreeSet<File> files = index.get(oldKey);
216
					if (files.size() > 0) {
217
						System.out.println("Replace KEY="+oldKey+" BY KEY="+modifications.get(oldKey));
218
						for (File srcFile : files) {
219
							System.out.println(" "+srcFile+" "+oldKey+" -> "+modifications.get(oldKey));
220
							IOUtils.replaceAll(srcFile, oldKey, modifications.get(oldKey));
221
						}
222
						updated.add(oldKey);
210
		//		for (PluginMessagesManager pm : pluginsMessagesPerProject.values()) {
211
		TreeMap<String, TreeSet<File>> index = getUsedKeysFilesIndex();
212

  
213
		for (String oldKey : keys) {
214
			if (index.containsKey(oldKey)) {
215
				TreeSet<File> files = index.get(oldKey);
216
				if (files.size() > 0) {
217
					System.out.println("Replace KEY="+oldKey+" BY KEY="+modifications.get(oldKey));
218
					System.out.println("Files: "+files);
219
					for (File srcFile : files) {
220
						IOUtils.replaceAll(srcFile, oldKey, modifications.get(oldKey));
223 221
					}
222
					updated.add(oldKey);
224 223
				}
225 224
			}
226
//		}
225
		}
226
		//		}
227 227
	}
228 228

  
229 229
	/**
......
270 270
				System.err.println("WorkspaceMessagesManager.createUsedKeysFilesIndex(): project " + pmManager.getProjectDirectory() + " have no used keys.");
271 271
				continue;
272 272
			}
273
			
273

  
274 274
			for (Map.Entry<String, TreeSet<File>> entry :index.entrySet()) {
275 275

  
276 276
				//String fullKeyName = pmManager.getKeyFullName(entry.getKey());

Formats disponibles : Unified diff