| 1 | |
package org.apache.maven.plugin.pmd; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
import java.io.File; |
| 23 | |
import java.io.FileInputStream; |
| 24 | |
import java.io.FileNotFoundException; |
| 25 | |
import java.io.FileOutputStream; |
| 26 | |
import java.io.IOException; |
| 27 | |
import java.io.InputStream; |
| 28 | |
import java.io.OutputStreamWriter; |
| 29 | |
import java.io.Reader; |
| 30 | |
import java.io.UnsupportedEncodingException; |
| 31 | |
import java.io.Writer; |
| 32 | |
import java.util.Locale; |
| 33 | |
import java.util.Map; |
| 34 | |
import java.util.ResourceBundle; |
| 35 | |
|
| 36 | |
import net.sourceforge.pmd.IRuleViolation; |
| 37 | |
import net.sourceforge.pmd.PMD; |
| 38 | |
import net.sourceforge.pmd.PMDException; |
| 39 | |
import net.sourceforge.pmd.Report; |
| 40 | |
import net.sourceforge.pmd.Rule; |
| 41 | |
import net.sourceforge.pmd.RuleContext; |
| 42 | |
import net.sourceforge.pmd.RuleSet; |
| 43 | |
import net.sourceforge.pmd.RuleSetFactory; |
| 44 | |
import net.sourceforge.pmd.SourceType; |
| 45 | |
import net.sourceforge.pmd.renderers.CSVRenderer; |
| 46 | |
import net.sourceforge.pmd.renderers.HTMLRenderer; |
| 47 | |
import net.sourceforge.pmd.renderers.Renderer; |
| 48 | |
import net.sourceforge.pmd.renderers.TextRenderer; |
| 49 | |
import net.sourceforge.pmd.renderers.XMLRenderer; |
| 50 | |
|
| 51 | |
import org.apache.maven.doxia.sink.Sink; |
| 52 | |
import org.apache.maven.reporting.MavenReportException; |
| 53 | |
import org.codehaus.plexus.resource.ResourceManager; |
| 54 | |
import org.codehaus.plexus.resource.loader.FileResourceCreationException; |
| 55 | |
import org.codehaus.plexus.resource.loader.FileResourceLoader; |
| 56 | |
import org.codehaus.plexus.resource.loader.ResourceNotFoundException; |
| 57 | |
import org.codehaus.plexus.util.FileUtils; |
| 58 | |
import org.codehaus.plexus.util.IOUtil; |
| 59 | |
import org.codehaus.plexus.util.ReaderFactory; |
| 60 | |
import org.codehaus.plexus.util.StringUtils; |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | 32 | public class PmdReport |
| 72 | |
extends AbstractPmdReport |
| 73 | |
{ |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
private String targetJdk; |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | 32 | private int minimumPriority = 5; |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
private boolean skip; |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | 32 | private String[] rulesets = |
| 111 | |
new String[]{ "rulesets/basic.xml", "rulesets/unusedcode.xml", "rulesets/imports.xml", }; |
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
private ResourceManager locator; |
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
public String getName( Locale locale ) |
| 124 | |
{ |
| 125 | 4 | return getBundle( locale ).getString( "report.pmd.name" ); |
| 126 | |
} |
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
public String getDescription( Locale locale ) |
| 132 | |
{ |
| 133 | 0 | return getBundle( locale ).getString( "report.pmd.description" ); |
| 134 | |
} |
| 135 | |
|
| 136 | |
public void setRulesets( String[] rules ) |
| 137 | |
{ |
| 138 | 8 | rulesets = rules; |
| 139 | 8 | } |
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
public void executeReport( Locale locale ) |
| 145 | |
throws MavenReportException |
| 146 | |
{ |
| 147 | |
try |
| 148 | |
{ |
| 149 | 24 | execute( locale ); |
| 150 | |
} |
| 151 | |
finally |
| 152 | |
{ |
| 153 | 24 | if ( getSink() != null ) |
| 154 | |
{ |
| 155 | 20 | getSink().close(); |
| 156 | |
} |
| 157 | |
} |
| 158 | 16 | } |
| 159 | |
|
| 160 | |
private void execute( Locale locale ) |
| 161 | |
throws MavenReportException |
| 162 | |
{ |
| 163 | |
|
| 164 | 24 | locator.addSearchPath( FileResourceLoader.ID, project.getFile().getParentFile().getAbsolutePath() ); |
| 165 | 24 | locator.addSearchPath( "url", "" ); |
| 166 | 24 | locator.setOutputDirectory( targetDirectory ); |
| 167 | |
|
| 168 | 24 | if ( !skip && canGenerateReport() ) |
| 169 | |
{ |
| 170 | 24 | ClassLoader origLoader = Thread.currentThread().getContextClassLoader(); |
| 171 | |
try |
| 172 | |
{ |
| 173 | 24 | Thread.currentThread().setContextClassLoader( this.getClass().getClassLoader() ); |
| 174 | |
|
| 175 | 24 | Report report = generateReport( locale ); |
| 176 | |
|
| 177 | 16 | if ( !isHtml() ) |
| 178 | |
{ |
| 179 | 16 | writeNonHtml( report ); |
| 180 | |
} |
| 181 | |
} |
| 182 | |
finally |
| 183 | |
{ |
| 184 | 24 | Thread.currentThread().setContextClassLoader( origLoader ); |
| 185 | 16 | } |
| 186 | |
} |
| 187 | 16 | } |
| 188 | |
|
| 189 | |
private Report generateReport( Locale locale ) |
| 190 | |
throws MavenReportException |
| 191 | |
{ |
| 192 | 24 | Sink sink = getSink(); |
| 193 | |
|
| 194 | 24 | PMD pmd = getPMD(); |
| 195 | 20 | RuleContext ruleContext = new RuleContext(); |
| 196 | 20 | Report report = new Report(); |
| 197 | 20 | PmdReportListener reportSink = new PmdReportListener( sink, getBundle( locale ), aggregate ); |
| 198 | |
|
| 199 | 20 | report.addListener( reportSink ); |
| 200 | 20 | ruleContext.setReport( report ); |
| 201 | 20 | reportSink.beginDocument(); |
| 202 | |
|
| 203 | 16 | RuleSetFactory ruleSetFactory = new RuleSetFactory(); |
| 204 | 16 | ruleSetFactory.setMinimumPriority( this.minimumPriority ); |
| 205 | 16 | RuleSet[] sets = new RuleSet[rulesets.length]; |
| 206 | |
try |
| 207 | |
{ |
| 208 | 68 | for ( int idx = 0; idx < rulesets.length; idx++ ) |
| 209 | |
{ |
| 210 | 52 | String set = rulesets[idx]; |
| 211 | 52 | getLog().debug( "Preparing ruleset: " + set ); |
| 212 | 52 | File ruleset = locator.getResourceAsFile( set, getLocationTemp( set ) ); |
| 213 | |
|
| 214 | 52 | if ( null == ruleset ) |
| 215 | |
{ |
| 216 | 0 | throw new MavenReportException( "Could not resolve " + set ); |
| 217 | |
} |
| 218 | |
|
| 219 | 52 | InputStream rulesInput = new FileInputStream( ruleset ); |
| 220 | |
try |
| 221 | |
{ |
| 222 | 52 | RuleSet ruleSet = ruleSetFactory.createRuleSet( rulesInput ); |
| 223 | 52 | sets[idx] = ruleSet; |
| 224 | |
|
| 225 | 52 | ruleSet.start( ruleContext ); |
| 226 | |
} |
| 227 | |
finally |
| 228 | |
{ |
| 229 | 52 | rulesInput.close(); |
| 230 | 52 | } |
| 231 | |
} |
| 232 | |
} |
| 233 | 0 | catch ( IOException e ) |
| 234 | |
{ |
| 235 | 0 | throw new MavenReportException( e.getMessage(), e ); |
| 236 | |
} |
| 237 | 0 | catch ( ResourceNotFoundException e ) |
| 238 | |
{ |
| 239 | 0 | throw new MavenReportException( e.getMessage(), e ); |
| 240 | |
} |
| 241 | 0 | catch ( FileResourceCreationException e ) |
| 242 | |
{ |
| 243 | 0 | throw new MavenReportException( e.getMessage(), e ); |
| 244 | 16 | } |
| 245 | |
|
| 246 | |
Map<File, PmdFileInfo> files; |
| 247 | |
try |
| 248 | |
{ |
| 249 | 16 | files = getFilesToProcess(); |
| 250 | |
} |
| 251 | 0 | catch ( IOException e ) |
| 252 | |
{ |
| 253 | 0 | throw new MavenReportException( "Can't get file list", e ); |
| 254 | 16 | } |
| 255 | |
|
| 256 | 16 | if ( StringUtils.isEmpty( getSourceEncoding() ) && !files.isEmpty() ) |
| 257 | |
{ |
| 258 | 0 | getLog().warn( "File encoding has not been set, using platform encoding " + ReaderFactory.FILE_ENCODING |
| 259 | |
+ ", i.e. build is platform dependent!" ); |
| 260 | |
} |
| 261 | |
|
| 262 | 16 | for ( Map.Entry<File, PmdFileInfo> entry : files.entrySet() ) |
| 263 | |
{ |
| 264 | 44 | File file = entry.getKey(); |
| 265 | 44 | PmdFileInfo fileInfo = entry.getValue(); |
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | 44 | reportSink.beginFile( file, fileInfo ); |
| 270 | 44 | ruleContext.setSourceCodeFilename( file.getAbsolutePath() ); |
| 271 | 184 | for ( int idx = 0; idx < rulesets.length; idx++ ) |
| 272 | |
{ |
| 273 | |
try |
| 274 | |
{ |
| 275 | |
|
| 276 | |
|
| 277 | |
Reader reader; |
| 278 | 140 | if ( StringUtils.isNotEmpty( getSourceEncoding() ) ) |
| 279 | |
{ |
| 280 | 140 | reader = ReaderFactory.newReader( file, getSourceEncoding() ); |
| 281 | |
} |
| 282 | |
else |
| 283 | |
{ |
| 284 | 0 | reader = ReaderFactory.newPlatformReader( file ); |
| 285 | |
} |
| 286 | |
|
| 287 | |
try |
| 288 | |
{ |
| 289 | 140 | pmd.processFile( reader, sets[idx], ruleContext ); |
| 290 | |
} |
| 291 | |
finally |
| 292 | |
{ |
| 293 | 140 | reader.close(); |
| 294 | 140 | } |
| 295 | |
} |
| 296 | 0 | catch ( UnsupportedEncodingException e1 ) |
| 297 | |
{ |
| 298 | 0 | throw new MavenReportException( "Encoding '" + getSourceEncoding() + "' is not supported.", e1 ); |
| 299 | |
} |
| 300 | 0 | catch ( PMDException pe ) |
| 301 | |
{ |
| 302 | 0 | String msg = pe.getLocalizedMessage(); |
| 303 | 0 | Throwable r = pe.getCause(); |
| 304 | 0 | if ( r != null ) |
| 305 | |
{ |
| 306 | 0 | msg = msg + ": " + r.getLocalizedMessage(); |
| 307 | |
} |
| 308 | 0 | getLog().warn( msg ); |
| 309 | 0 | reportSink.ruleViolationAdded( new ProcessingErrorRuleViolation( file, msg ) ); |
| 310 | |
} |
| 311 | 0 | catch ( FileNotFoundException e2 ) |
| 312 | |
{ |
| 313 | 0 | getLog().warn( "Error opening source file: " + file ); |
| 314 | 0 | reportSink.ruleViolationAdded( new ProcessingErrorRuleViolation( file, e2.getLocalizedMessage() ) ); |
| 315 | |
} |
| 316 | 0 | catch ( Exception e3 ) |
| 317 | |
{ |
| 318 | 0 | getLog().warn( "Failure executing PMD for: " + file, e3 ); |
| 319 | 0 | reportSink.ruleViolationAdded( new ProcessingErrorRuleViolation( file, e3.getLocalizedMessage() ) ); |
| 320 | 140 | } |
| 321 | |
} |
| 322 | 44 | reportSink.endFile( file ); |
| 323 | 44 | } |
| 324 | |
|
| 325 | 68 | for ( int idx = 0; idx < rulesets.length; idx++ ) |
| 326 | |
{ |
| 327 | 52 | sets[idx].end( ruleContext ); |
| 328 | |
} |
| 329 | |
|
| 330 | 16 | reportSink.endDocument(); |
| 331 | |
|
| 332 | 16 | return report; |
| 333 | |
} |
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
|
| 341 | |
private void writeNonHtml( Report report ) |
| 342 | |
throws MavenReportException |
| 343 | |
{ |
| 344 | 16 | Renderer r = createRenderer(); |
| 345 | |
|
| 346 | 16 | if ( r == null ) |
| 347 | |
{ |
| 348 | 0 | return; |
| 349 | |
} |
| 350 | |
|
| 351 | 16 | Writer writer = null; |
| 352 | |
|
| 353 | |
try |
| 354 | |
{ |
| 355 | 16 | targetDirectory.mkdirs(); |
| 356 | 16 | File targetFile = new File( targetDirectory, "pmd." + format ); |
| 357 | 16 | FileOutputStream tStream = new FileOutputStream( targetFile ); |
| 358 | 16 | writer = new OutputStreamWriter( tStream, getOutputEncoding() ); |
| 359 | |
|
| 360 | 16 | r.setWriter( writer ); |
| 361 | 16 | r.start(); |
| 362 | 16 | r.renderFileReport( report ); |
| 363 | 16 | r.end(); |
| 364 | 16 | writer.close(); |
| 365 | |
|
| 366 | 16 | File siteDir = getReportOutputDirectory(); |
| 367 | 16 | siteDir.mkdirs(); |
| 368 | 16 | FileUtils.copyFile( targetFile, new File( siteDir, "pmd." + format ) ); |
| 369 | |
} |
| 370 | 0 | catch ( IOException ioe ) |
| 371 | |
{ |
| 372 | 0 | throw new MavenReportException( ioe.getMessage(), ioe ); |
| 373 | |
} |
| 374 | |
finally |
| 375 | |
{ |
| 376 | 16 | IOUtil.close( writer ); |
| 377 | 16 | } |
| 378 | 16 | } |
| 379 | |
|
| 380 | |
|
| 381 | |
|
| 382 | |
|
| 383 | |
|
| 384 | |
|
| 385 | |
|
| 386 | |
protected String getLocationTemp( String name ) |
| 387 | |
{ |
| 388 | 56 | String loc = name; |
| 389 | 56 | if ( loc.indexOf( '/' ) != -1 ) |
| 390 | |
{ |
| 391 | 56 | loc = loc.substring( loc.lastIndexOf( '/' ) + 1 ); |
| 392 | |
} |
| 393 | 56 | if ( loc.indexOf( '\\' ) != -1 ) |
| 394 | |
{ |
| 395 | 0 | loc = loc.substring( loc.lastIndexOf( '\\' ) + 1 ); |
| 396 | |
} |
| 397 | |
|
| 398 | |
|
| 399 | |
|
| 400 | |
|
| 401 | |
|
| 402 | 56 | loc = loc.replaceAll( "[\\?\\:\\&\\=\\%]", "_" ); |
| 403 | |
|
| 404 | 56 | getLog().debug( "Before: " + name + " After: " + loc ); |
| 405 | 56 | return loc; |
| 406 | |
} |
| 407 | |
|
| 408 | |
|
| 409 | |
|
| 410 | |
|
| 411 | |
|
| 412 | |
|
| 413 | |
|
| 414 | |
|
| 415 | |
|
| 416 | |
public PMD getPMD() |
| 417 | |
throws MavenReportException |
| 418 | |
{ |
| 419 | 24 | PMD pmd = new PMD(); |
| 420 | |
|
| 421 | 24 | if ( null != targetJdk ) |
| 422 | |
{ |
| 423 | 8 | SourceType sourceType = SourceType.getSourceTypeForId( "java " + targetJdk ); |
| 424 | 8 | if ( sourceType == null ) |
| 425 | |
{ |
| 426 | 4 | throw new MavenReportException( "Unsupported targetJdk value '" + targetJdk + "'." ); |
| 427 | |
} |
| 428 | 4 | pmd.setJavaVersion( sourceType ); |
| 429 | |
} |
| 430 | |
|
| 431 | 20 | return pmd; |
| 432 | |
} |
| 433 | |
|
| 434 | |
|
| 435 | |
|
| 436 | |
|
| 437 | |
public String getOutputName() |
| 438 | |
{ |
| 439 | 20 | return "pmd"; |
| 440 | |
} |
| 441 | |
|
| 442 | |
private static ResourceBundle getBundle( Locale locale ) |
| 443 | |
{ |
| 444 | 24 | return ResourceBundle.getBundle( "pmd-report", locale, PmdReport.class.getClassLoader() ); |
| 445 | |
} |
| 446 | |
|
| 447 | |
|
| 448 | |
|
| 449 | |
|
| 450 | |
|
| 451 | |
|
| 452 | |
|
| 453 | |
|
| 454 | |
public final Renderer createRenderer() |
| 455 | |
throws MavenReportException |
| 456 | |
{ |
| 457 | 16 | Renderer renderer = null; |
| 458 | 16 | if ( "xml".equals( format ) ) |
| 459 | |
{ |
| 460 | 12 | renderer = new PmdXMLRenderer( getOutputEncoding() ); |
| 461 | |
} |
| 462 | 4 | else if ( "txt".equals( format ) ) |
| 463 | |
{ |
| 464 | 0 | renderer = new TextRenderer(); |
| 465 | |
} |
| 466 | 4 | else if ( "csv".equals( format ) ) |
| 467 | |
{ |
| 468 | 4 | renderer = new CSVRenderer(); |
| 469 | |
} |
| 470 | 0 | else if ( "html".equals( format ) ) |
| 471 | |
{ |
| 472 | 0 | renderer = new HTMLRenderer(); |
| 473 | |
} |
| 474 | 0 | else if ( !"".equals( format ) && !"none".equals( format ) ) |
| 475 | |
{ |
| 476 | |
try |
| 477 | |
{ |
| 478 | 0 | renderer = (Renderer) Class.forName( format ).newInstance(); |
| 479 | |
} |
| 480 | 0 | catch ( Exception e ) |
| 481 | |
{ |
| 482 | 0 | throw new MavenReportException( |
| 483 | |
"Can't find PMD custom format " + format + ": " + e.getClass().getName(), e ); |
| 484 | 0 | } |
| 485 | |
} |
| 486 | |
|
| 487 | 16 | return renderer; |
| 488 | |
} |
| 489 | |
|
| 490 | |
private static class PmdXMLRenderer |
| 491 | |
extends XMLRenderer |
| 492 | |
{ |
| 493 | |
public PmdXMLRenderer( String encoding ) |
| 494 | |
{ |
| 495 | 12 | super(); |
| 496 | 12 | this.encoding = encoding; |
| 497 | 12 | } |
| 498 | |
} |
| 499 | |
|
| 500 | |
|
| 501 | |
|
| 502 | |
|
| 503 | 32 | private static class ProcessingErrorRuleViolation |
| 504 | |
implements IRuleViolation |
| 505 | |
{ |
| 506 | |
|
| 507 | |
private String filename; |
| 508 | |
|
| 509 | |
private String description; |
| 510 | |
|
| 511 | |
public ProcessingErrorRuleViolation( File file, String description ) |
| 512 | 0 | { |
| 513 | 0 | filename = file.getPath(); |
| 514 | 0 | this.description = description; |
| 515 | 0 | } |
| 516 | |
|
| 517 | |
|
| 518 | |
|
| 519 | |
|
| 520 | |
public String getFilename() |
| 521 | |
{ |
| 522 | 0 | return this.filename; |
| 523 | |
} |
| 524 | |
|
| 525 | |
|
| 526 | |
|
| 527 | |
|
| 528 | |
public int getBeginLine() |
| 529 | |
{ |
| 530 | 0 | return 0; |
| 531 | |
} |
| 532 | |
|
| 533 | |
|
| 534 | |
|
| 535 | |
|
| 536 | |
public int getBeginColumn() |
| 537 | |
{ |
| 538 | 0 | return 0; |
| 539 | |
} |
| 540 | |
|
| 541 | |
|
| 542 | |
|
| 543 | |
|
| 544 | |
public int getEndLine() |
| 545 | |
{ |
| 546 | 0 | return 0; |
| 547 | |
} |
| 548 | |
|
| 549 | |
|
| 550 | |
|
| 551 | |
|
| 552 | |
public int getEndColumn() |
| 553 | |
{ |
| 554 | 0 | return 0; |
| 555 | |
} |
| 556 | |
|
| 557 | |
|
| 558 | |
|
| 559 | |
|
| 560 | |
public Rule getRule() |
| 561 | |
{ |
| 562 | 0 | return null; |
| 563 | |
} |
| 564 | |
|
| 565 | |
|
| 566 | |
|
| 567 | |
|
| 568 | |
public String getDescription() |
| 569 | |
{ |
| 570 | 0 | return this.description; |
| 571 | |
} |
| 572 | |
|
| 573 | |
|
| 574 | |
|
| 575 | |
|
| 576 | |
public String getPackageName() |
| 577 | |
{ |
| 578 | 0 | return null; |
| 579 | |
} |
| 580 | |
|
| 581 | |
|
| 582 | |
|
| 583 | |
|
| 584 | |
public String getMethodName() |
| 585 | |
{ |
| 586 | 0 | return null; |
| 587 | |
} |
| 588 | |
|
| 589 | |
|
| 590 | |
|
| 591 | |
|
| 592 | |
public String getClassName() |
| 593 | |
{ |
| 594 | 0 | return null; |
| 595 | |
} |
| 596 | |
|
| 597 | |
|
| 598 | |
|
| 599 | |
|
| 600 | |
public boolean isSuppressed() |
| 601 | |
{ |
| 602 | 0 | return false; |
| 603 | |
} |
| 604 | |
|
| 605 | |
|
| 606 | |
|
| 607 | |
|
| 608 | |
public String getVariableName() |
| 609 | |
{ |
| 610 | 0 | return null; |
| 611 | |
} |
| 612 | |
} |
| 613 | |
} |