1 7 package com.genimen.djeneric.language; 8 9 import java.io.BufferedReader ; 10 import java.io.File ; 11 import java.io.FileInputStream ; 12 import java.io.FileNotFoundException ; 13 import java.io.FileOutputStream ; 14 import java.io.FileReader ; 15 import java.io.FileWriter ; 16 import java.io.IOException ; 17 import java.util.ArrayList ; 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 import java.util.Properties ; 22 23 import com.genimen.djeneric.util.DjLogger; 24 import com.genimen.djeneric.util.DjProperties; 25 import com.genimen.djeneric.util.DjStringReplacer; 26 27 33 public class KeyRenamer 34 { 35 36 public static void main(String [] 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 e) 57 { 58 DjLogger.log(e); 59 } 60 } 61 62 private static void check(String mapfile, String dir) throws IOException 63 { 64 System.out.println("Checking..."); 65 Properties inprops = new Properties (); 66 inprops.load(new FileInputStream (mapfile)); 67 68 checkDir(inprops, new File (dir)); 69 System.out.println("Done"); 70 71 } 72 73 private static void checkDir(Properties inprops, File dirspec) throws IOException 74 { 75 if (dirspec.isDirectory()) 76 { 77 File [] 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 inprops, File inFile) throws IOException 91 { 92 if (!inFile.getName().endsWith("java") || inFile.getName().endsWith("KeyRenamer.java")) return; 93 94 String 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 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 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 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 string, String 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 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 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 mapfile, String dir) throws FileNotFoundException , IOException 177 { 178 Properties inprops = new Properties (); 179 inprops.load(new FileInputStream (mapfile)); 180 181 replaceDir(inprops, new File (dir)); 182 183 } 184 185 private static void replaceDir(Properties inprops, File dirspec) throws IOException 186 { 187 if (dirspec.isDirectory()) 188 { 189 File [] 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 inprops, File inFile) throws IOException 203 { 204 if (!inFile.getName().endsWith("java") || inFile.getName().endsWith("KeyRenamer.java")) return; 205 206 String src = readFile(inFile); 207 DjStringReplacer sr = new DjStringReplacer(src); 208 Iterator it = inprops.keySet().iterator(); 209 boolean doneOne = false; 210 while (it.hasNext()) 211 { 212 String 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 fw = new FileWriter (inFile); 232 fw.write(src); 233 fw.close(); 234 } 235 } 236 237 private static String removeNls(String 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 readFile(File inFile) throws IOException 250 { 251 BufferedReader br = new BufferedReader (new FileReader (inFile)); 252 StringBuffer src = new StringBuffer (100); 253 String 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 infile, String mapfile, String outfile, String outmap) 264 throws FileNotFoundException , IOException 265 { 266 DjProperties outprops = new DjProperties(); 267 DjProperties outmapprops = new DjProperties(); 268 269 Properties inprops = new Properties (); 270 inprops.load(new FileInputStream (infile)); 271 272 Properties mapprops = new Properties (); 273 mapprops.load(new FileInputStream (mapfile)); 274 275 Iterator it = mapprops.keySet().iterator(); 276 while (it.hasNext()) 277 { 278 String key = it.next().toString(); 279 String newKey = mapprops.getProperty(key); 280 String otherValue = inprops.getProperty(key); 281 282 if (outprops.containsKey(newKey)) 284 { 285 String 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 { } 301 302 outprops.store(new FileOutputStream (outfile), ""); 303 outmapprops.store(new FileOutputStream (outmap), ""); 304 } 305 306 private static boolean createGlobals(DjProperties outprops, DjProperties outmapprops) 307 { 308 boolean doneOne = false; 309 310 HashMap newKeys = new HashMap (); 311 HashMap tobeAdded = new HashMap (); 312 ArrayList tobeRemoved = new ArrayList (); 313 314 Iterator vals1 = outprops.entrySet().iterator(); 315 while (vals1.hasNext()) 316 { 317 Map.Entry entry1 = (Map.Entry ) vals1.next(); 318 if (tobeRemoved.contains(entry1.getKey())) continue; 319 320 Iterator vals2 = outprops.entrySet().iterator(); 321 while (vals2.hasNext()) 322 { 323 Map.Entry entry2 = (Map.Entry ) 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 String msg = entry1.getValue().toString(); 331 String keyPart = entry1.getKey().toString(); 332 int idx = keyPart.indexOf("."); 333 keyPart = keyPart.substring(idx + 1); 334 335 String newKey = (String ) 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 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 infile, String outfile) throws FileNotFoundException , IOException 363 { 364 if ((new File (outfile)).exists()) 365 { 366 throw new IOException ("Destination file " + outfile + " exists"); 367 } 368 369 Properties props = new Properties (); 370 props.load(new FileInputStream (infile)); 371 372 DjProperties outprops = new DjProperties(); 373 Iterator it = props.keySet().iterator(); 374 while (it.hasNext()) 375 { 376 String key = it.next().toString(); 377 outprops.setProperty(key, key); 378 } 379 outprops.store(new FileOutputStream (outfile), ""); 380 } 381 } | Popular Tags |