1 24 25 package org.objectweb.cjdbc.scenario.tools.util; 26 27 import java.io.BufferedReader ; 28 import java.io.File ; 29 import java.io.FileReader ; 30 import java.io.FilenameFilter ; 31 import java.util.ArrayList ; 32 import java.util.Enumeration ; 33 import java.util.ResourceBundle ; 34 35 import org.objectweb.cjdbc.common.i18n.Translate; 36 37 43 public class TranslationCheck 44 { 45 static final String TRANSLATE_TAG = "Translate.get("; 46 static final String LOGGER_TAG = "logger."; 47 static final String LOGGER_TEST = "logger.is"; 48 static final String EXCEPTION_TAG = "Exception("; 49 static final int TRANSLATE_TAG_LENGTH = TRANSLATE_TAG.length(); 50 private String translationFile = Translate.CJDBC_LANGUAGE_FILE; 51 private ArrayList ignore; 52 private final String userDir = System.getProperty("cjdbc.dir"); 53 54 int analysedXslFile = 0; 55 int analyseJavaFile = 0; 56 57 private ArrayList xslKeys; 58 private ArrayList missingXslKeys; 59 private ArrayList usedXslKeys; 60 private ArrayList configurationKeys; 61 private ArrayList javaKeys; 62 private ArrayList invalidKeys; 63 private ArrayList missingTranslations; 65 private ArrayList missingLogger; 66 private ArrayList javaFiles; 67 68 71 class InvalidTag 72 { 73 String file; 74 String tmp; 75 String description; 76 int line; 77 78 InvalidTag(String description, String file, String tmp, int line) 79 { 80 this.description = description; 81 this.file = file; 82 this.tmp = tmp; 83 this.line = line; 84 } 85 } 86 87 91 public TranslationCheck() 92 { 93 94 ignore = new ArrayList (); 95 configurationKeys = new ArrayList (); 96 javaKeys = new ArrayList (); 97 invalidKeys = new ArrayList (); 98 missingTranslations = new ArrayList (); 99 missingLogger = new ArrayList (); 100 xslKeys = new ArrayList (); 101 missingXslKeys = new ArrayList (); 102 usedXslKeys = new ArrayList (); 103 javaFiles = new ArrayList (); 104 105 ignore.add("Translate.java"); 106 getMissingTranslations(); 107 analyseXsl(); 108 analyseJava(); 109 } 110 111 117 private void getMissingTranslations() 118 { 119 ResourceBundle rb = ResourceBundle.getBundle(translationFile); 120 Enumeration e = rb.getKeys(); 121 while (e.hasMoreElements()) 122 { 123 String translation = (String ) e.nextElement(); 124 if (translation.trim().startsWith("##")) 125 continue; 126 configurationKeys.add(translation); 127 if (rb.getString(translation).equals("")) 128 missingTranslations.add(translation); 129 } 130 } 131 132 private void analyseXsl() 133 { 134 ResourceBundle keys = ResourceBundle.getBundle("c-jdbc-xsl"); 135 for (Enumeration e = keys.getKeys(); e.hasMoreElements(); xslKeys.add(e 136 .nextElement())) 137 { 138 } 140 File f = new File (userDir + "/xml"); 141 File [] xsls = f.listFiles(new FilenameFilter () 142 { 143 public boolean accept(File dir, String name) 144 { 145 if (name.endsWith(".xsl")) 146 return true; 147 else 148 return false; 149 } 150 }); 151 for (int i = 0; i < xsls.length; i++) 152 analyseXslFile(xsls[i]); 153 } 154 155 private void analyseXslFile(File f) 156 { 157 analysedXslFile++; 159 try 160 { 161 BufferedReader br = new BufferedReader (new FileReader (f)); 162 String read = ""; 163 String oi18n = "<i18n>"; 164 String ci18n = "</i18n>"; 165 int oi18nl = oi18n.length(); 166 int ci18nl = oi18nl + 1; 167 String i18n = ""; 168 while ((read = br.readLine()) != null) 169 { 170 int indexOpen = 0, indexClose = 0; 171 while ((indexOpen = read.indexOf(oi18n)) != -1) 172 { 173 indexClose = read.indexOf(ci18n); 174 i18n = read.substring(indexOpen + oi18nl, indexClose).trim(); 175 if (xslKeys.contains(i18n) == false) 176 { 177 missingXslKeys.add(i18n); 178 } 179 else 180 usedXslKeys.add(i18n); 181 read = read.substring(0, indexOpen) + i18n 182 + read.substring(indexClose + ci18nl); 183 } 184 } 185 } 186 catch (Exception e) 187 { 188 e.printStackTrace(); 189 } 190 191 } 192 193 196 private void analyseJava() 197 { 198 File f = new File (userDir + "/src/org"); 199 searchForKeys(f); 201 } 202 203 208 private void searchForKeys(File target) 209 { 210 if (target.isDirectory()) 211 { 212 File [] list = target.listFiles(); 213 for (int i = 0; i < list.length; i++) 214 searchForKeys(list[i]); 215 } 216 else 217 { 218 String path = target.getAbsolutePath(); 219 if (path.indexOf("CVS") == -1 220 && (path.indexOf("controller") != -1 || path.indexOf("console") != -1)) 221 { 222 if (target.getName().endsWith(".java")) 223 searchJavaFile(target); 224 } 225 } 226 } 227 228 235 private boolean isValidTranslateTag(String tmp) 236 { 237 return (tmp.indexOf('+') == -1); 238 } 239 240 247 private boolean processValidTag(String tmp) 248 { 249 if ((tmp.charAt(0) == '\"') && (tmp.charAt(tmp.length() - 1) == '\"')) 250 { 251 javaKeys.add(tmp.substring(1, tmp.length() - 1)); 252 return true; 253 } 254 else 255 return false; 256 } 257 258 265 private void processUnValidTag(String description, File target, String tmp, 266 int line) 267 { 268 invalidKeys.add(new InvalidTag(description, target.getName(), tmp, line)); 269 } 270 271 277 private void searchJavaFile(File target) 278 { 279 if (ignore.contains(target.getName())) 280 return; 281 analyseJavaFile++; 282 javaFiles.add(target.getName()); 283 284 String twoLinesBefore = ""; 285 String previousLine = ""; 286 String read = ""; 287 String tmp = ""; 288 try 289 { 290 BufferedReader br = new BufferedReader (new FileReader (target)); 291 int index = -1; 292 int line = 0; 293 while ((read = br.readLine()) != null) 294 { 295 line++; 296 if (read.indexOf(EXCEPTION_TAG) != -1) 298 { 299 while (read.indexOf(';') == -1) 300 { 301 read = read.trim() + br.readLine().trim(); 302 line++; 303 } 304 if (read.indexOf(TRANSLATE_TAG) == -1) 305 { 306 if (read.indexOf(EXCEPTION_TAG + "\"") != -1) 307 { 308 if (previousLine.indexOf(TRANSLATE_TAG) == -1 309 && twoLinesBefore.indexOf(TRANSLATE_TAG) == -1) 310 missingLogger.add(new InvalidTag("EXCEPTION", target.getName(), 311 System.getProperty("line.separator") 312 + twoLinesBefore.trim() 313 + System.getProperty("line.separator") 314 + previousLine.trim() 315 + System.getProperty("line.separator") + read.trim(), 316 line)); 317 } 318 else 319 { 320 } 322 } 323 } 324 325 if (read.indexOf(LOGGER_TAG) != -1) 327 { 328 while (read.indexOf(';') == -1) 329 { 330 read = read.trim() + br.readLine().trim(); 331 line++; 332 } 333 if (read.indexOf(TRANSLATE_TAG) == -1 334 && read.indexOf(LOGGER_TEST) == -1) 335 { 336 if (previousLine.indexOf(TRANSLATE_TAG) == -1 340 && twoLinesBefore.indexOf(TRANSLATE_TAG) == -1) 341 missingLogger.add(new InvalidTag("LOGGER", target.getName(), 342 System.getProperty("line.separator") + twoLinesBefore.trim() 343 + System.getProperty("line.separator") 344 + previousLine.trim() 345 + System.getProperty("line.separator") + read.trim(), 346 line)); 347 } 348 } 349 350 if ((index = read.indexOf(TRANSLATE_TAG)) != -1) 352 { 353 while (read.indexOf(';') == -1) 354 { 355 read = read.trim() + br.readLine().trim(); 356 line++; 357 } 358 read = read.trim(); 359 index = read.indexOf(TRANSLATE_TAG); 360 tmp = read.substring(index + TRANSLATE_TAG_LENGTH); 361 tmp = tmp.trim(); 362 363 if (tmp.indexOf(",") != -1) 364 tmp = tmp.substring(0, tmp.indexOf(",")); 365 if (tmp.lastIndexOf(')') > tmp.length() - 4) 366 { 367 try 368 { 369 tmp = tmp.substring(0, tmp.indexOf(')', tmp.length() - 4)); 370 } 371 catch (Exception e) 372 { 373 System.out.println("-->Substring failed in :" + target.getName()); 374 } 375 } 376 377 boolean valid = isValidTranslateTag(tmp); 378 379 if (valid && processValidTag(tmp)) 380 { 381 } 384 else 385 processUnValidTag("TRANSLATE", target, tmp, line); 386 } 387 twoLinesBefore = previousLine; 388 previousLine = read; 389 } 390 } 391 catch (Exception ignore) 392 { 393 ignore.printStackTrace(); 394 } 395 } 396 397 400 public ArrayList getConfigurationKeys() 401 { 402 return configurationKeys; 403 } 404 405 408 public ArrayList getJavaKeys() 409 { 410 return javaKeys; 411 } 412 413 416 public ArrayList getinvalidKeys() 417 { 418 return invalidKeys; 419 } 420 421 428 public boolean isTranslationUpToDate() 429 { 430 return configurationKeys.containsAll(javaKeys); 431 } 432 433 436 public void displayTranslationState() 437 { 438 System.out.println("Translation up to date:" + isTranslationUpToDate()); 439 System.out.println("### UNUSED XSL KEYS FROM PROPERTIES ###"); 440 ArrayList xslNotUsed = (ArrayList ) xslKeys.clone(); 441 xslNotUsed.removeAll(usedXslKeys); 442 for (int i = 0; i < xslNotUsed.size(); i++) 443 System.out.println(xslNotUsed.get(i)); 444 System.out.println("### MISSING XSL KEYS (" + missingXslKeys.size() 445 + ") ###"); 446 for (int i = 0; i < missingXslKeys.size(); i++) 447 System.out.println(missingXslKeys.get(i)); 448 System.out.println("### MISSING TRANSLATIONS (" 449 + missingTranslations.size() + ") ###"); 450 for (int i = 0; i < missingTranslations.size(); i++) 451 System.out.println(missingTranslations.get(i)); 452 System.out 453 .println("### ANALYSED JAVA FILES (" + javaFiles.size() + ") ###"); 454 System.out.println("### INVALID TAGS IN JAVA (" + invalidKeys.size() 457 + ") ###"); 458 for (int i = 0; i < invalidKeys.size(); i++) 459 { 460 InvalidTag tag = (InvalidTag) (invalidKeys.get(i)); 461 System.out.println("[" + tag.file + ":line:" + tag.line + "]:" + tag.tmp); 462 } 463 System.out.println("### MISSING TAGS IN CONFIGURATION ###"); 464 int count = 0; 465 for (int i = 0; i < javaKeys.size(); i++) 466 { 467 String key = (String ) javaKeys.get(i); 468 if (!configurationKeys.contains(key)) 469 { 470 System.out.println(key); 471 count++; 472 } 473 } 474 System.out.println("\t ### TOTAL MISSING TAGS " + count); 475 System.out.println("### MISSING TAGS IN JAVA ###"); 476 int count2 = 0; 477 for (int i = 0; i < configurationKeys.size(); i++) 478 { 479 String key = (String ) configurationKeys.get(i); 480 if (!javaKeys.contains(key)) 481 { 482 count2++; 483 System.out.println(key); 484 } 485 } 486 System.out.println("\t ### TOTAL MISSING TAGS " + count2); 487 System.out.println("### MISSING LOGGERS IN JAVA (" + missingLogger.size() 488 + ") ###"); 489 for (int i = 0; i < missingLogger.size(); i++) 490 { 491 InvalidTag tag = (InvalidTag) (missingLogger.get(i)); 492 System.out.println(" %%% [" + tag.description + "][" + tag.file 493 + ":line:" + tag.line + "] %%%" + tag.tmp); 494 } 495 System.out.println("*******************************************"); 496 System.out.println("************** SUMMARY ********************"); 497 System.out.println("### NUMBER OF ANALYSED XSL FILES:" + analysedXslFile); 498 System.out.println("### NUMBER OF ANALYSED JAVA FILES:" + analyseJavaFile); 499 System.out.println("### UNUSED XSL KEYS FROM PROPERTIES (" 500 + xslNotUsed.size() + ") ###"); 501 System.out.println("### MISSING XSL KEYS (" + missingXslKeys.size() 502 + ") ###"); 503 System.out.println("### MISSING TRANSLATION STRINGS IN PROPERTY FILE (" 504 + missingTranslations.size() + ") ###"); 505 System.out.println("### INVALID TAGS IN JAVA (" + invalidKeys.size() 506 + ") ###"); 507 System.out.println("### MISSING CONFIGURATION TAGS " + count); 508 System.out.println("### MISSING JAVA TAGS " + count2); 509 System.out.println("### MISSING LOGGERS IN JAVA (" + missingLogger.size() 510 + ") ###"); 511 System.out.println("*******************************************"); 512 } 513 514 } | Popular Tags |