Révision 2537

tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/devtools/FindMessagesToBind.java (revision 2537)
21 21

  
22 22
/**
23 23
 * 
24
 * Finds duplicated messages in all core and rcp plug-ins.
25
 * Merges them in a new key and move them to org.txm.core or org.txm.rcp.
24
 * Finds unbounded messages and saves them to a file.
25
 * E.g. Line of code containing Strings that are concatenated.
26 26
 *
27 27
 */
28 28
public class FindMessagesToBind {
29

  
29
	
30 30
	File report;
31
	
31 32
	PrintWriter writer;
33
	
32 34
	boolean searchOnlyExternalized = true;
33 35
	
34 36
	public FindMessagesToBind(File report) {
......
36 38
	}
37 39
	
38 40
	private int processSourceFile(File sourceFile, boolean searchOnlyExternalized) {
39

  
40 41
		
41
		//BufferedReader reader = IOUtils.getReader(sourceFile)
42
		//String line = reader.readLine();
42
		
43
		// BufferedReader reader = IOUtils.getReader(sourceFile)
44
		// String line = reader.readLine();
43 45
		this.searchOnlyExternalized = searchOnlyExternalized;
44 46
		boolean ignore = false;
45 47
		ArrayList<String> lines = IOUtils.getLines(sourceFile, "UTF-8");
46
		ArrayList<String> toBind = new ArrayList<String>();
48
		ArrayList<String> toBind = new ArrayList<>();
47 49
		
48 50
		int nLine = 0;
49 51
		for (String line : lines) {
50 52
			nLine++;
51
			//println ""+line
53
			// println ""+line
52 54
			String l = line.trim();
53

  
55
			
54 56
			if (ignore) {
55 57
				if (l.startsWith("*/") || l.endsWith("*/")) {
56 58
					ignore = false;
57 59
				}
58
			} else {
60
			}
61
			else {
59 62
				if (l.startsWith("//") || l.startsWith("@")) {
60 63
					// ignore
61
				} else if (l.startsWith("/*")) {
64
				}
65
				else if (l.startsWith("/*")) {
62 66
					ignore = true;
63
					if (l.endsWith("*/")) ignore=false;
64
				} else {
65
//					ArrayList<String> strings = getLineStrings(l);
66
//					if (strings.size() > 0) {
67
//						System.out.println(strings);
68
//					}
67
					if (l.endsWith("*/")) ignore = false;
68
				}
69
				else {
70
					// ArrayList<String> strings = getLineStrings(l);
71
					// if (strings.size() > 0) {
72
					// System.out.println(strings);
73
					// }
69 74
					
70 75
					Matcher m = p1.matcher(l);
71 76
					if (m.find()) {
72
						toBind.add(""+nLine+"\t"+l);
73
					//	System.out.println(sourceFile.getAbsolutePath()+ " " + l);
74
						//System.out.println("1 "+m.group(1));
75
					} else {
77
						toBind.add("" + nLine + "\t" + l);
78
						// System.out.println(sourceFile.getAbsolutePath()+ " " + l);
79
						// System.out.println("1 "+m.group(1));
80
					}
81
					else {
76 82
						m = p2.matcher(l);
77 83
						if (m.find()) {
78
							toBind.add(""+nLine+"\t"+l);
79
						//	System.out.println(sourceFile.getAbsolutePath()+ " " + l);
80
							//System.out.println("2 "+m.group(2));
81
						} else if (!l.contains("$NON-NLS-") && !searchOnlyExternalized) {
84
							toBind.add("" + nLine + "\t" + l);
85
							// System.out.println(sourceFile.getAbsolutePath()+ " " + l);
86
							// System.out.println("2 "+m.group(2));
87
						}
88
						else if (!l.contains("$NON-NLS-") && !searchOnlyExternalized) {
82 89
							m = p3.matcher(l);
83 90
							if (m.find()) {
84
								toBind.add(""+nLine+"\t"+l);
85
								//System.out.println(sourceFile.getAbsolutePath()+ " " + l);
86
								//System.out.println("2 "+m.group(2));
87
							} else {
91
								toBind.add("" + nLine + "\t" + l);
92
								// System.out.println(sourceFile.getAbsolutePath()+ " " + l);
93
								// System.out.println("2 "+m.group(2));
94
							}
95
							else {
88 96
								
89 97
							}
90 98
						}
......
96 104
		if (toBind.size() > 0) {
97 105
			writer.println(sourceFile.getAbsolutePath());
98 106
			for (String l : toBind) {
99
				writer.println("\t"+l);
107
				writer.println("\t" + l);
100 108
			}
101 109
		}
102 110
		
103 111
		return toBind.size();
104 112
	}
105
	Pattern p1 = Pattern.compile("("+WorkspaceMessagesManager.KEY_REGEX.pattern()+")( ?\\+)");
106
	Pattern p2 = Pattern.compile("( ?\\+)("+WorkspaceMessagesManager.KEY_REGEX.pattern()+")");
107
	Pattern p3 = Pattern.compile("("+WorkspaceMessagesManager.STRING_REGEX.pattern()+" ?\\+)");
108 113
	
114
	Pattern p1 = Pattern.compile("(" + WorkspaceMessagesManager.KEY_REGEX.pattern() + ")( ?\\+)");
115
	
116
	Pattern p2 = Pattern.compile("( ?\\+)(" + WorkspaceMessagesManager.KEY_REGEX.pattern() + ")");
117
	
118
	Pattern p3 = Pattern.compile("(" + WorkspaceMessagesManager.STRING_REGEX.pattern() + " ?\\+)");
119
	
109 120
	private ArrayList<String> getLineStrings(String l) {
110
		ArrayList<String> strings = new ArrayList<String>();
121
		ArrayList<String> strings = new ArrayList<>();
111 122
		boolean inS = false;
112 123
		int start = 0;
113 124
		int comment = 0;
114
		for(int i = 0 ; i < l.length() ; i++) {
115
			String c = ""+l.charAt(i);
125
		for (int i = 0; i < l.length(); i++) {
126
			String c = "" + l.charAt(i);
116 127
			if (inS) {
117 128
				comment = 0;
118 129
				if (c.equals("\\")) {
119 130
					i++;
120
				} else if (c.equals("\"")) {
131
				}
132
				else if (c.equals("\"")) {
121 133
					inS = false;
122
					strings.add(l.substring(start, i+1));
134
					strings.add(l.substring(start, i + 1));
123 135
				}
124
			} else {
136
			}
137
			else {
125 138
				if (c.equals("\"")) {
126 139
					inS = true;
127 140
					start = i;
128 141
					comment = 0;
129
				} else if (c.equals("/")) {
142
				}
143
				else if (c.equals("/")) {
130 144
					comment++;
131 145
					if (comment == 2) break;
132
				} else {
146
				}
147
				else {
133 148
					comment = 0;
134 149
				}
135 150
			}
136 151
		}
137 152
		return strings;
138 153
	}
139

  
154
	
140 155
	public void run() throws UnsupportedEncodingException, FileNotFoundException, IOException {
141 156
		WorkspaceMessagesManager wmm = new WorkspaceMessagesManager();
142 157
		LinkedHashMap<File, PluginMessagesManager> h = wmm.getPluginMessages();
143

  
158
		
144 159
		writer = IOUtils.getWriter(report);
145 160
		int total = 0;
146 161
		
147 162
		TreeSet<File> projects = new TreeSet(h.keySet());
148 163
		for (File project : projects) {
149

  
150
			//System.out.println("Project: "+project);
151

  
164
			
165
			// System.out.println("Project: "+project);
166
			
152 167
			File srcDir = new File(project, "src/java");
153
			if (!srcDir.exists())  srcDir = new File(project, "src");
168
			if (!srcDir.exists()) srcDir = new File(project, "src");
154 169
			if (!srcDir.exists()) continue; // no Java sources
155

  
170
			
156 171
			ArrayList<File> files = DeleteDir.scanDirectory(srcDir, true);
157 172
			Collections.sort(files, new Comparator<File>() {
173
				
158 174
				@Override
159 175
				public int compare(File arg0, File arg1) {
160 176
					return arg0.getAbsolutePath().compareTo(arg1.getAbsolutePath());
161 177
				}
162 178
			});
163 179
			for (File javaFile : files) {
164

  
180
				
165 181
				if (!javaFile.getName().endsWith(".java")) continue;
166 182
				if (javaFile.getName().endsWith("Messages.java")) continue;
167

  
168
				//System.out.println(" File: "+javaFile);
183
				
184
				// System.out.println(" File: "+javaFile);
169 185
				total += processSourceFile(javaFile, true);
170 186
			}
171 187
		}
......
173 189
		writer.close();
174 190
		
175 191
		if (total > 0) {
176
			System.out.println(""+total+" potential bindinds.");
177
		} else {
192
			System.out.println("" + total + " potential bindinds.");
193
		}
194
		else {
178 195
			System.out.println("You are the very best of the very best: nothing to do!");
179 196
		}
180 197
	}
181

  
198
	
182 199
	public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException, IOException {
183 200
		File report = new File("report.txt");
184 201
		new FindMessagesToBind(report).run();
185
		System.out.println("Report: "+report.getAbsolutePath());
202
		System.out.println("Report: " + report.getAbsolutePath());
186 203
	}
187 204
}
tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/devtools/MergeDuplicatedStringsInAPlugin.java (revision 2537)
12 12

  
13 13
/**
14 14
 * 
15
 * Finds duplicated strings in a plug-in, merge them in one key and remove duplicated.
15
 * Finds duplicated strings in a plug-in, merges them in one key and removes duplicated.
16 16
 *
17 17
 */
18 18
public class MergeDuplicatedStringsInAPlugin {
......
26 26
	public MergeDuplicatedStringsInAPlugin(boolean debug) {
27 27
		this.debug = debug;
28 28
	}
29

  
29
	
30 30
	public int merge(PluginMessagesManager pm) {
31 31
		HashMap<String, String> defaults = pm.getMessagesForDefaultLang();
32
		TreeMap<String, String> value2Key = new TreeMap<String, String>();
32
		TreeMap<String, String> value2Key = new TreeMap<>();
33 33
		
34
		TreeMap<String, TreeSet<String>> merges = new TreeMap<String, TreeSet<String>>();
35
		TreeSet<String> keys = new TreeSet<String>(defaults.keySet());
34
		TreeMap<String, TreeSet<String>> merges = new TreeMap<>();
35
		TreeSet<String> keys = new TreeSet<>(defaults.keySet());
36 36
		for (String key : keys) {
37 37
			String value = defaults.get(key);
38 38
			if (value2Key.containsKey(value)) { // duplicated message
39 39
				pm.renameKey(key, value2Key.get(value));
40 40
				merges.get(value2Key.get(value)).add(key);
41
			} else {
41
			}
42
			else {
42 43
				value2Key.put(value, key);
43 44
				merges.put(key, new TreeSet<String>());
44 45
			}
......
51 52
		}
52 53
		
53 54
		if (merges.size() > 0) {
54
			System.out.println(" Done: "+pm.getMessageFullName()+" "+merges.size()+" merges: "+merges);
55
		} else {
56
			//System.out.println(" Done: no merge");
55
			System.out.println(" Done: " + pm.getMessageFullName() + " " + merges.size() + " merges: " + merges);
57 56
		}
57
		else {
58
			// System.out.println(" Done: no merge");
59
		}
58 60
		
59 61
		return merges.size();
60 62
	}
61 63
	
62 64
	/**
63 65
	 * @param args
64
	 * @throws IOException 
65
	 * @throws FileNotFoundException 
66
	 * @throws UnsupportedEncodingException 
66
	 * @throws IOException
67
	 * @throws FileNotFoundException
68
	 * @throws UnsupportedEncodingException
67 69
	 */
68 70
	public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException, IOException {
69

  
71
		
70 72
		MergeDuplicatedStringsInAPlugin merger = new MergeDuplicatedStringsInAPlugin(false);
71

  
73
		
72 74
		WorkspaceMessagesManager wmm = new WorkspaceMessagesManager();
73 75
		int total = 0;
74 76
		for (PluginMessagesManager pm : wmm.getPluginMessages().values()) {
......
76 78
			
77 79
			pm.saveChanges(true);
78 80
		}
79

  
81
		
80 82
		wmm.saveKeyModificationsInSources();
81 83
		
82 84
		if (total > 0) {
83
			System.out.println("Done: "+total);
84
		} else {
85
			System.out.println("Done: " + total);
86
		}
87
		else {
85 88
			System.out.println("Done: no merge");
86 89
		}
87 90
	}

Formats disponibles : Unified diff