KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > language > KeyRenamer


1 /*
2  * Created on Oct 5, 2003
3  *
4  * To change the template for this generated file go to
5  * Window>Preferences>Java>Code Generation>Code and Comments
6  */

7 package com.genimen.djeneric.language;
8
9 import java.io.BufferedReader JavaDoc;
10 import java.io.File JavaDoc;
11 import java.io.FileInputStream JavaDoc;
12 import java.io.FileNotFoundException JavaDoc;
13 import java.io.FileOutputStream JavaDoc;
14 import java.io.FileReader JavaDoc;
15 import java.io.FileWriter JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 import com.genimen.djeneric.util.DjLogger;
24 import com.genimen.djeneric.util.DjProperties;
25 import com.genimen.djeneric.util.DjStringReplacer;
26
27 /**
28  * @author Wido
29  *
30  * To change the template for this generated type comment go to
31  * Window>Preferences>Java>Code Generation>Code and Comments
32  */

33 public class KeyRenamer
34 {
35
36   public static void main(String JavaDoc[] args)
37   {
38     try
39     {
40
41       if (args.length < 3)
42       {
43         System.out.println("Usage: java " + KeyRenamer.class.getName() + " <makemap> <infile> <outfile>");
44         System.out.println(" java " + KeyRenamer.class.getName()
45                            + " <normalize> <infile> <mapfile> <msgdestfile> <mapdestfile>");
46         System.out.println(" java " + KeyRenamer.class.getName() + " <replace> <mapfile> <dir>");
47         System.out.println(" java " + KeyRenamer.class.getName() + " <check> <mapfile> <dir>");
48         System.exit(0);
49       }
50       if (args[0].equalsIgnoreCase("makemap")) makemap(args[1], args[2]);
51       else if (args[0].equalsIgnoreCase("normalize")) normalize(args[1], args[2], args[3], args[4]);
52       else if (args[0].equalsIgnoreCase("replace")) replace(args[1], args[2]);
53       else if (args[0].equalsIgnoreCase("check")) check(args[1], args[2]);
54       else System.out.println("Unknown command: " + args[0]);
55     }
56     catch (Exception JavaDoc e)
57     {
58       DjLogger.log(e);
59     }
60   }
61
62   private static void check(String JavaDoc mapfile, String JavaDoc dir) throws IOException JavaDoc
63   {
64     System.out.println("Checking...");
65     Properties JavaDoc inprops = new Properties JavaDoc();
66     inprops.load(new FileInputStream JavaDoc(mapfile));
67
68     checkDir(inprops, new File JavaDoc(dir));
69     System.out.println("Done");
70
71   }
72
73   private static void checkDir(Properties JavaDoc inprops, File JavaDoc dirspec) throws IOException JavaDoc
74   {
75     if (dirspec.isDirectory())
76     {
77       File JavaDoc[] files = dirspec.listFiles();
78       for (int i = 0; i < files.length; i++)
79       {
80         if (files[i].isDirectory()) checkDir(inprops, files[i]);
81         else checkFile(inprops, files[i]);
82       }
83     }
84     else
85     {
86       checkFile(inprops, dirspec);
87     }
88   }
89
90   private static void checkFile(Properties JavaDoc inprops, File JavaDoc inFile) throws IOException JavaDoc
91   {
92     if (!inFile.getName().endsWith("java") || inFile.getName().endsWith("KeyRenamer.java")) return;
93
94     String JavaDoc src = readFile(inFile);
95
96     int idx = src.indexOf("Messages.getString(");
97     while (idx != -1)
98     {
99       int start = src.indexOf("\"", idx) + 1;
100       int end = src.indexOf("\"", start);
101       String JavaDoc key = src.substring(start, end);
102       if (!inprops.containsKey(key))
103       {
104         System.err.println("Error: " + inFile.getAbsolutePath() + " has invalid key: " + key);
105       }
106
107       int endOfGetter = findEndingBracket(src, idx);
108       String JavaDoc stmt = src.substring(idx, endOfGetter);
109
110       int paramCount = countCommas(stmt);
111       int paramCount2 = countParams(inprops.getProperty(key));
112
113       if (paramCount != paramCount2)
114       {
115         System.err.println("Error: " + inFile.getAbsolutePath() + " has invalid number of parameters: ");
116         System.err.println(" : " + stmt);
117         System.err.println(" : " + inprops.getProperty(key));
118       }
119       idx = src.indexOf("Messages.getString(", end);
120     }
121   }
122
123   private static int countParams(String JavaDoc string)
124   {
125     int count = countOccurrences(string, "{1}");
126     count += countOccurrences(string, "{2}");
127     count += countOccurrences(string, "{3}");
128     count += countOccurrences(string, "{4}");
129     count += countOccurrences(string, "{5}");
130     return count;
131   }
132
133   private static int countOccurrences(String JavaDoc string, String JavaDoc match)
134   {
135     int idx = 0;
136     int count = 0;
137     do
138     {
139       idx = string.indexOf(match, idx);
140       if (idx != -1)
141       {
142         count++;
143         idx++;
144       }
145     }
146     while (idx != -1);
147     return count;
148   }
149
150   private static int countCommas(String JavaDoc stmt)
151   {
152     int count = 0;
153     for (int i = 0; i < stmt.length(); i++)
154     {
155       if (stmt.charAt(i) == ',') count++;
156     }
157     return count;
158   }
159
160   private static int findEndingBracket(String JavaDoc src, int fromIdx)
161   {
162     int idx = src.indexOf("(", fromIdx);
163     if (idx == -1) return -1;
164
165     int counter = 1;
166     idx++;
167     while (counter > 0)
168     {
169       if (src.charAt(idx) == '(') counter++;
170       if (src.charAt(idx) == ')') counter--;
171       idx++;
172     }
173     return idx;
174   }
175
176   private static void replace(String JavaDoc mapfile, String JavaDoc dir) throws FileNotFoundException JavaDoc, IOException JavaDoc
177   {
178     Properties JavaDoc inprops = new Properties JavaDoc();
179     inprops.load(new FileInputStream JavaDoc(mapfile));
180
181     replaceDir(inprops, new File JavaDoc(dir));
182
183   }
184
185   private static void replaceDir(Properties JavaDoc inprops, File JavaDoc dirspec) throws IOException JavaDoc
186   {
187     if (dirspec.isDirectory())
188     {
189       File JavaDoc[] files = dirspec.listFiles();
190       for (int i = 0; i < files.length; i++)
191       {
192         if (files[i].isDirectory()) replaceDir(inprops, files[i]);
193         else replaceFile(inprops, files[i]);
194       }
195     }
196     else
197     {
198       replaceFile(inprops, dirspec);
199     }
200   }
201
202   private static void replaceFile(Properties JavaDoc inprops, File JavaDoc inFile) throws IOException JavaDoc
203   {
204     if (!inFile.getName().endsWith("java") || inFile.getName().endsWith("KeyRenamer.java")) return;
205
206     String JavaDoc src = readFile(inFile);
207     DjStringReplacer sr = new DjStringReplacer(src);
208     Iterator JavaDoc it = inprops.keySet().iterator();
209     boolean doneOne = false;
210     while (it.hasNext())
211     {
212       String JavaDoc key = it.next().toString();
213       if (src.indexOf(key) != -1)
214       {
215         sr.replace("\"" + key + "\"", "\"" + inprops.getProperty(key) + "\"");
216         doneOne = true;
217       }
218     }
219
220     src = sr.toString();
221
222     if (src.indexOf("//$NON-NLS") != -1)
223     {
224       src = removeNls(src);
225       doneOne = true;
226     }
227
228     if (doneOne)
229     {
230       System.out.println("Replaced keys in " + inFile.getAbsolutePath());
231       FileWriter JavaDoc fw = new FileWriter JavaDoc(inFile);
232       fw.write(src);
233       fw.close();
234     }
235   }
236
237   private static String JavaDoc removeNls(String JavaDoc src)
238   {
239     int idx = src.indexOf("//$NON-NLS");
240     while (idx != -1)
241     {
242       int end = src.indexOf("\n", idx);
243       src = src.substring(0, idx).trim() + src.substring(end);
244       idx = src.indexOf("//$NON-NLS");
245     }
246     return src;
247   }
248
249   public static String JavaDoc readFile(File JavaDoc inFile) throws IOException JavaDoc
250   {
251     BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(inFile));
252     StringBuffer JavaDoc src = new StringBuffer JavaDoc(100);
253     String JavaDoc ln;
254     while ((ln = br.readLine()) != null)
255     {
256       src.append(ln);
257       src.append("\n");
258     }
259     br.close();
260     return src.toString();
261   }
262
263   private static void normalize(String JavaDoc infile, String JavaDoc mapfile, String JavaDoc outfile, String JavaDoc outmap)
264       throws FileNotFoundException JavaDoc, IOException JavaDoc
265   {
266     DjProperties outprops = new DjProperties();
267     DjProperties outmapprops = new DjProperties();
268
269     Properties JavaDoc inprops = new Properties JavaDoc();
270     inprops.load(new FileInputStream JavaDoc(infile));
271
272     Properties JavaDoc mapprops = new Properties JavaDoc();
273     mapprops.load(new FileInputStream JavaDoc(mapfile));
274
275     Iterator JavaDoc it = mapprops.keySet().iterator();
276     while (it.hasNext())
277     {
278       String JavaDoc key = it.next().toString();
279       String JavaDoc newKey = mapprops.getProperty(key);
280       String JavaDoc otherValue = inprops.getProperty(key);
281
282       // dup found?
283
if (outprops.containsKey(newKey))
284       {
285         String JavaDoc orgValue = outprops.getProperty(newKey);
286         if (!orgValue.equals(otherValue))
287         {
288           int counter = 1;
289           while (outprops.contains(newKey + counter))
290             counter++;
291           newKey = newKey + counter;
292         }
293       }
294       outprops.setProperty(newKey, otherValue);
295       outmapprops.setProperty(key, newKey);
296     }
297
298     while (createGlobals(outprops, outmapprops))
299     { // remove all dups
300
}
301
302     outprops.store(new FileOutputStream JavaDoc(outfile), "");
303     outmapprops.store(new FileOutputStream JavaDoc(outmap), "");
304   }
305
306   private static boolean createGlobals(DjProperties outprops, DjProperties outmapprops)
307   {
308     boolean doneOne = false;
309
310     HashMap JavaDoc newKeys = new HashMap JavaDoc();
311     HashMap JavaDoc tobeAdded = new HashMap JavaDoc();
312     ArrayList JavaDoc tobeRemoved = new ArrayList JavaDoc();
313
314     Iterator JavaDoc vals1 = outprops.entrySet().iterator();
315     while (vals1.hasNext())
316     {
317       Map.Entry JavaDoc entry1 = (Map.Entry JavaDoc) vals1.next();
318       if (tobeRemoved.contains(entry1.getKey())) continue;
319
320       Iterator JavaDoc vals2 = outprops.entrySet().iterator();
321       while (vals2.hasNext())
322       {
323         Map.Entry JavaDoc entry2 = (Map.Entry JavaDoc) vals2.next();
324         if (tobeRemoved.contains(entry2.getKey())) continue;
325
326         if (entry1.getValue().toString().equalsIgnoreCase(entry2.getValue().toString())
327             && !entry1.getKey().equals(entry2.getKey()))
328         {
329           // System.out.println("Duplicate value: " + entry1.getKey() + " and " + entry2.getKey());
330
String JavaDoc msg = entry1.getValue().toString();
331           String JavaDoc keyPart = entry1.getKey().toString();
332           int idx = keyPart.indexOf(".");
333           keyPart = keyPart.substring(idx + 1);
334
335           String JavaDoc newKey = (String JavaDoc) newKeys.get(msg);
336           if (newKey == null)
337           {
338             newKey = "global." + keyPart;
339             newKeys.put(msg, newKey);
340           }
341
342           tobeAdded.put(newKey, msg);
343           tobeRemoved.add(entry1.getKey());
344           tobeRemoved.add(entry2.getKey());
345           outmapprops.setProperty(entry1.getKey().toString(), newKey);
346           outmapprops.setProperty(entry2.getKey().toString(), newKey);
347           doneOne = true;
348           System.out.println("Remove " + entry1.getKey() + ", " + entry2.getKey());
349           System.out.println("Add " + newKey);
350         }
351       }
352     }
353
354     Iterator JavaDoc rem = tobeRemoved.iterator();
355     while (rem.hasNext())
356       outprops.remove(rem.next().toString());
357
358     outprops.putAll(tobeAdded);
359     return doneOne;
360   }
361
362   private static void makemap(String JavaDoc infile, String JavaDoc outfile) throws FileNotFoundException JavaDoc, IOException JavaDoc
363   {
364     if ((new File JavaDoc(outfile)).exists())
365     {
366       throw new IOException JavaDoc("Destination file " + outfile + " exists");
367     }
368
369     Properties JavaDoc props = new Properties JavaDoc();
370     props.load(new FileInputStream JavaDoc(infile));
371
372     DjProperties outprops = new DjProperties();
373     Iterator JavaDoc it = props.keySet().iterator();
374     while (it.hasNext())
375     {
376       String JavaDoc key = it.next().toString();
377       outprops.setProperty(key, key);
378     }
379     outprops.store(new FileOutputStream JavaDoc(outfile), "");
380   }
381 }
Popular Tags