| 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.FileReader; |
| 24 | |
import java.io.IOException; |
| 25 | |
import java.util.ArrayList; |
| 26 | |
import java.util.List; |
| 27 | |
|
| 28 | |
import org.apache.maven.plugin.MojoExecutionException; |
| 29 | |
import org.apache.maven.plugin.MojoFailureException; |
| 30 | |
import org.apache.maven.plugin.pmd.model.PmdErrorDetail; |
| 31 | |
import org.apache.maven.plugin.pmd.model.PmdFile; |
| 32 | |
import org.apache.maven.plugin.pmd.model.Violation; |
| 33 | |
import org.apache.maven.plugin.pmd.model.io.xpp3.PmdXpp3Reader; |
| 34 | |
import org.codehaus.plexus.util.StringUtils; |
| 35 | |
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | 116 | public class PmdViolationCheckMojo |
| 48 | |
extends AbstractPmdViolationCheckMojo<Violation> |
| 49 | |
{ |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
private int failurePriority; |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
private boolean skip; |
| 68 | |
|
| 69 | |
|
| 70 | |
public void execute() |
| 71 | |
throws MojoExecutionException, MojoFailureException |
| 72 | |
{ |
| 73 | 12 | if ( !skip ) |
| 74 | |
{ |
| 75 | 12 | executeCheck( "pmd.xml", "violation", "PMD violation", failurePriority ); |
| 76 | |
} |
| 77 | 8 | } |
| 78 | |
|
| 79 | |
|
| 80 | |
protected void printError( Violation item, String severity ) |
| 81 | |
{ |
| 82 | |
|
| 83 | 24 | StringBuffer buff = new StringBuffer( 100 ); |
| 84 | 24 | buff.append( "PMD " + severity + ": " ); |
| 85 | 24 | if ( item.getViolationClass() != null ) |
| 86 | |
{ |
| 87 | 20 | if ( item.getViolationPackage() != null ) |
| 88 | |
{ |
| 89 | 20 | buff.append( item.getViolationPackage() ); |
| 90 | 20 | buff.append( "." ); |
| 91 | |
} |
| 92 | 20 | buff.append( item.getViolationClass() ); |
| 93 | |
} |
| 94 | |
else |
| 95 | |
{ |
| 96 | 4 | buff.append( item.getFileName() ); |
| 97 | |
} |
| 98 | 24 | buff.append( ":" ); |
| 99 | 24 | buff.append( item.getBeginline() ); |
| 100 | 24 | buff.append( " Rule:" ).append( item.getRule() ); |
| 101 | 24 | buff.append( " Priority:" ).append( item.getPriority() ); |
| 102 | 24 | buff.append( " " ).append( item.getText() ).append( "." ); |
| 103 | |
|
| 104 | 24 | this.getLog().info( buff.toString() ); |
| 105 | 24 | } |
| 106 | |
|
| 107 | |
@Override |
| 108 | |
protected List<Violation> getErrorDetails( File pmdFile ) |
| 109 | |
throws XmlPullParserException, IOException |
| 110 | |
{ |
| 111 | 12 | PmdXpp3Reader reader = new PmdXpp3Reader(); |
| 112 | 12 | PmdErrorDetail details = reader.read( new FileReader( pmdFile ), false ); |
| 113 | |
|
| 114 | 12 | List<Violation> violations = new ArrayList<Violation>(); |
| 115 | 12 | for( PmdFile file : details.getFiles() ) |
| 116 | |
{ |
| 117 | 36 | String fullPath = file.getName(); |
| 118 | |
|
| 119 | 36 | for ( Violation violation : file.getViolations() ) |
| 120 | |
{ |
| 121 | 72 | violation.setFileName( getFilename( fullPath, violation.getViolationPackage() ) ); |
| 122 | 72 | violations.add( violation ); |
| 123 | |
} |
| 124 | 36 | } |
| 125 | 12 | return violations; |
| 126 | |
} |
| 127 | |
|
| 128 | |
@Override |
| 129 | |
protected int getPriority( Violation errorDetail ) |
| 130 | |
{ |
| 131 | 72 | return errorDetail.getPriority(); |
| 132 | |
} |
| 133 | |
|
| 134 | |
@Override |
| 135 | |
protected ViolationDetails<Violation> newViolationDetailsInstance() |
| 136 | |
{ |
| 137 | 12 | return new ViolationDetails<Violation>(); |
| 138 | |
} |
| 139 | |
|
| 140 | |
private String getFilename( String fullpath, String pkg ) |
| 141 | |
{ |
| 142 | 72 | int index = fullpath.lastIndexOf( File.separatorChar ); |
| 143 | |
|
| 144 | 144 | while ( StringUtils.isNotEmpty( pkg ) ) |
| 145 | |
{ |
| 146 | 144 | index = fullpath.substring( 0, index ).lastIndexOf( File.separatorChar ); |
| 147 | |
|
| 148 | 144 | int dot = pkg.indexOf( '.' ); |
| 149 | |
|
| 150 | 144 | if ( dot < 0 ) |
| 151 | |
{ |
| 152 | 72 | break; |
| 153 | |
} |
| 154 | 72 | pkg = pkg.substring( dot + 1 ); |
| 155 | 72 | } |
| 156 | |
|
| 157 | 72 | return fullpath.substring( index + 1 ); |
| 158 | |
} |
| 159 | |
} |