1 package com.puppycrawl.tools.checkstyle; 20 21 import java.io.File ; 22 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.Locale ; 26 import java.util.Stack ; 27 import java.util.StringTokenizer ; 28 29 import com.puppycrawl.tools.checkstyle.api.SeverityLevelCounter; 30 import com.puppycrawl.tools.checkstyle.api.AutomaticBean; 31 import com.puppycrawl.tools.checkstyle.api.MessageDispatcher; 32 import com.puppycrawl.tools.checkstyle.api.SeverityLevel; 33 import com.puppycrawl.tools.checkstyle.api.Context; 34 import com.puppycrawl.tools.checkstyle.api.FilterSet; 35 import com.puppycrawl.tools.checkstyle.api.CheckstyleException; 36 import com.puppycrawl.tools.checkstyle.api.LocalizedMessage; 37 import com.puppycrawl.tools.checkstyle.api.Configuration; 38 import com.puppycrawl.tools.checkstyle.api.FileSetCheck; 39 import com.puppycrawl.tools.checkstyle.api.Filter; 40 import com.puppycrawl.tools.checkstyle.api.AuditListener; 41 import com.puppycrawl.tools.checkstyle.api.Utils; 42 import com.puppycrawl.tools.checkstyle.api.AuditEvent; 43 44 50 public class Checker extends AutomaticBean 51 implements MessageDispatcher 52 { 53 54 private final SeverityLevelCounter mCounter = 55 new SeverityLevelCounter(SeverityLevel.ERROR); 56 57 58 private final ArrayList mListeners = new ArrayList (); 59 60 61 private final ArrayList mFileSetChecks = new ArrayList (); 62 63 64 private ClassLoader mLoader = 65 Thread.currentThread().getContextClassLoader(); 66 67 68 private String mBasedir; 69 70 71 private String mLocaleCountry = Locale.getDefault().getCountry(); 72 73 private String mLocaleLanguage = Locale.getDefault().getLanguage(); 74 75 76 private ModuleFactory mModuleFactory; 77 78 79 private Context mChildContext; 80 81 82 private final FilterSet mFilters = new FilterSet(); 83 84 94 private SeverityLevel mSeverityLevel = SeverityLevel.ERROR; 95 96 102 public Checker() 103 throws CheckstyleException 104 { 105 addListener(mCounter); 106 } 107 108 109 public void finishLocalSetup() throws CheckstyleException 110 { 111 final Locale locale = new Locale (mLocaleLanguage, mLocaleCountry); 112 LocalizedMessage.setLocale(locale); 113 114 if (mModuleFactory == null) { 115 mModuleFactory = PackageNamesLoader.loadModuleFactory(Thread 116 .currentThread().getContextClassLoader()); 117 } 118 119 final DefaultContext context = new DefaultContext(); 120 context.add("classLoader", mLoader); 121 context.add("moduleFactory", mModuleFactory); 122 context.add("severity", mSeverityLevel.getName()); 123 context.add("basedir", mBasedir); 124 mChildContext = context; 125 } 126 127 135 protected void setupChild(Configuration aChildConf) 136 throws CheckstyleException 137 { 138 final String name = aChildConf.getName(); 139 try { 140 final Object child = mModuleFactory.createModule(name); 141 if (child instanceof AutomaticBean) { 142 final AutomaticBean bean = (AutomaticBean) child; 143 bean.contextualize(mChildContext); 144 bean.configure(aChildConf); 145 } 146 if (child instanceof FileSetCheck) { 147 final FileSetCheck fsc = (FileSetCheck) child; 148 addFileSetCheck(fsc); 149 } 150 else if (child instanceof Filter) { 151 final Filter filter = (Filter) child; 152 addFilter(filter); 153 } 154 else if (child instanceof AuditListener) { 155 final AuditListener listener = (AuditListener) child; 156 addListener(listener); 157 } 158 else { 159 throw new CheckstyleException(name 160 + " is not allowed as a child in Checker"); 161 } 162 } 163 catch (final Exception ex) { 164 throw new CheckstyleException( 166 "cannot initialize module " 167 + name + " - " + ex.getMessage(), ex); 168 } 169 } 170 171 176 public void addFileSetCheck(FileSetCheck aFileSetCheck) 177 { 178 aFileSetCheck.setMessageDispatcher(this); 179 mFileSetChecks.add(aFileSetCheck); 180 } 181 182 186 public void addFilter(Filter aFilter) 187 { 188 mFilters.addFilter(aFilter); 189 } 190 191 195 public void removeFilter(Filter aFilter) 196 { 197 mFilters.removeFilter(aFilter); 198 } 199 200 201 public void destroy() 202 { 203 mListeners.clear(); 204 mFilters.clear(); 205 } 206 207 211 public void addListener(AuditListener aListener) 212 { 213 mListeners.add(aListener); 214 } 215 216 220 public void removeListener(AuditListener aListener) 221 { 222 mListeners.remove(aListener); 223 } 224 225 233 public int process(File [] aFiles) 234 { 235 fireAuditStarted(); 236 for (int i = 0; i < mFileSetChecks.size(); i++) { 237 final FileSetCheck fileSetCheck = 238 (FileSetCheck) mFileSetChecks.get(i); 239 fileSetCheck.process(aFiles); 240 fileSetCheck.destroy(); 241 } 242 final int errorCount = mCounter.getCount(); 243 fireAuditFinished(); 244 return errorCount; 245 } 246 247 248 253 private String getStrippedFileName(final String aFileName) 254 { 255 return Utils.getStrippedFileName(mBasedir, aFileName); 256 } 257 258 259 public void setBasedir(String aBasedir) 260 { 261 mBasedir = normalize(aBasedir); 265 } 266 267 284 public String normalize(String aPath) 285 { 286 final String osName = 287 System.getProperty("os.name").toLowerCase(Locale.US); 288 final boolean onNetWare = (osName.indexOf("netware") > -1); 289 290 final String orig = aPath; 291 292 aPath = aPath.replace('/', File.separatorChar) 293 .replace('\\', File.separatorChar); 294 295 final int colon = aPath.indexOf(":"); 297 298 if (!onNetWare) { 299 if (!aPath.startsWith(File.separator) 300 && !((aPath.length() >= 2) 301 && Character.isLetter(aPath.charAt(0)) 302 && (colon == 1))) 303 { 304 final String msg = aPath + " is not an absolute path"; 305 throw new IllegalArgumentException (msg); 306 } 307 } 308 else { 309 if (!aPath.startsWith(File.separator) 310 && (colon == -1)) 311 { 312 final String msg = aPath + " is not an absolute path"; 313 throw new IllegalArgumentException (msg); 314 } 315 } 316 317 boolean dosWithDrive = false; 318 String root = null; 319 if ((!onNetWare 321 && (aPath.length() >= 2) 322 && Character.isLetter(aPath.charAt(0)) 323 && (aPath.charAt(1) == ':')) 324 || (onNetWare && (colon > -1))) 325 { 326 327 dosWithDrive = true; 328 329 final char[] ca = aPath.replace('/', '\\').toCharArray(); 330 final StringBuffer sbRoot = new StringBuffer (); 331 for (int i = 0; i < colon; i++) { 332 sbRoot.append(Character.toUpperCase(ca[i])); 333 } 334 sbRoot.append(':'); 335 if (colon + 1 < aPath.length()) { 336 sbRoot.append(File.separatorChar); 337 } 338 root = sbRoot.toString(); 339 340 final StringBuffer sbPath = new StringBuffer (); 342 for (int i = colon + 1; i < ca.length; i++) { 343 if ((ca[i] != '\\') 344 || ((ca[i] == '\\') && (ca[i - 1] != '\\'))) 345 { 346 sbPath.append(ca[i]); 347 } 348 } 349 aPath = sbPath.toString().replace('\\', File.separatorChar); 350 351 } 352 else { 353 if (aPath.length() == 1) { 354 root = File.separator; 355 aPath = ""; 356 } 357 else if (aPath.charAt(1) == File.separatorChar) { 358 root = File.separator + File.separator; 360 aPath = aPath.substring(2); 361 } 362 else { 363 root = File.separator; 364 aPath = aPath.substring(1); 365 } 366 } 367 368 final Stack s = new Stack (); 369 s.push(root); 370 final StringTokenizer tok = new StringTokenizer (aPath, File.separator); 371 while (tok.hasMoreTokens()) { 372 final String thisToken = tok.nextToken(); 373 if (".".equals(thisToken)) { 374 continue; 375 } 376 else if ("..".equals(thisToken)) { 377 if (s.size() < 2) { 378 throw new IllegalArgumentException ("Cannot resolve path " 379 + orig); 380 } 381 s.pop(); 382 } 383 else { s.push(thisToken); 385 } 386 } 387 388 final StringBuffer sb = new StringBuffer (); 389 for (int i = 0; i < s.size(); i++) { 390 if (i > 1) { 391 sb.append(File.separatorChar); 394 } 395 sb.append(s.elementAt(i)); 396 } 397 398 399 aPath = sb.toString(); 400 if (dosWithDrive) { 401 aPath = aPath.replace('/', '\\'); 402 } 403 return aPath; 404 } 405 406 407 public final String getBasedir() 408 { 409 return mBasedir; 410 } 411 412 413 protected void fireAuditStarted() 414 { 415 final AuditEvent evt = new AuditEvent(this); 416 final Iterator it = mListeners.iterator(); 417 while (it.hasNext()) { 418 final AuditListener listener = (AuditListener) it.next(); 419 listener.auditStarted(evt); 420 } 421 } 422 423 424 protected void fireAuditFinished() 425 { 426 final AuditEvent evt = new AuditEvent(this); 427 final Iterator it = mListeners.iterator(); 428 while (it.hasNext()) { 429 final AuditListener listener = (AuditListener) it.next(); 430 listener.auditFinished(evt); 431 } 432 } 433 434 440 public void fireFileStarted(String aFileName) 441 { 442 final String stripped = getStrippedFileName(aFileName); 443 final AuditEvent evt = new AuditEvent(this, stripped); 444 final Iterator it = mListeners.iterator(); 445 while (it.hasNext()) { 446 final AuditListener listener = (AuditListener) it.next(); 447 listener.fileStarted(evt); 448 } 449 } 450 451 457 public void fireFileFinished(String aFileName) 458 { 459 final String stripped = getStrippedFileName(aFileName); 460 final AuditEvent evt = new AuditEvent(this, stripped); 461 final Iterator it = mListeners.iterator(); 462 while (it.hasNext()) { 463 final AuditListener listener = (AuditListener) it.next(); 464 listener.fileFinished(evt); 465 } 466 } 467 468 476 public void fireErrors(String aFileName, LocalizedMessage[] aErrors) 477 { 478 final String stripped = getStrippedFileName(aFileName); 479 for (int i = 0; i < aErrors.length; i++) { 480 final AuditEvent evt = new AuditEvent(this, stripped, aErrors[i]); 481 if (mFilters.accept(evt)) { 482 final Iterator it = mListeners.iterator(); 483 while (it.hasNext()) { 484 final AuditListener listener = (AuditListener) it.next(); 485 listener.addError(evt); 486 } 487 } 488 } 489 } 490 491 496 public void setModuleFactory(ModuleFactory aModuleFactory) 497 { 498 mModuleFactory = aModuleFactory; 499 } 500 501 502 public void setLocaleCountry(String aLocaleCountry) 503 { 504 mLocaleCountry = aLocaleCountry; 505 } 506 507 508 public void setLocaleLanguage(String aLocaleLanguage) 509 { 510 mLocaleLanguage = aLocaleLanguage; 511 } 512 513 520 public final void setSeverity(String aSeverity) 521 { 522 mSeverityLevel = SeverityLevel.getInstance(aSeverity); 523 } 524 525 532 public final void setClassloader(ClassLoader aLoader) 533 { 534 mLoader = aLoader; 535 } 536 } 537 | Popular Tags |