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 | 29 | 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 | 3 | if ( !skip ) |
74 | |
{ |
75 | 3 | executeCheck( "pmd.xml", "violation", "PMD violation", failurePriority ); |
76 | |
} |
77 | 2 | } |
78 | |
|
79 | |
|
80 | |
protected void printError( Violation item, String severity ) |
81 | |
{ |
82 | |
|
83 | 6 | StringBuffer buff = new StringBuffer( 100 ); |
84 | 6 | buff.append( "PMD " + severity + ": " ); |
85 | 6 | if ( item.getViolationClass() != null ) |
86 | |
{ |
87 | 5 | if ( item.getViolationPackage() != null ) |
88 | |
{ |
89 | 5 | buff.append( item.getViolationPackage() ); |
90 | 5 | buff.append( "." ); |
91 | |
} |
92 | 5 | buff.append( item.getViolationClass() ); |
93 | |
} |
94 | |
else |
95 | |
{ |
96 | 1 | buff.append( item.getFileName() ); |
97 | |
} |
98 | 6 | buff.append( ":" ); |
99 | 6 | buff.append( item.getBeginline() ); |
100 | 6 | buff.append( " Rule:" ).append( item.getRule() ); |
101 | 6 | buff.append( " Priority:" ).append( item.getPriority() ); |
102 | 6 | buff.append( " " ).append( item.getText() ).append( "." ); |
103 | |
|
104 | 6 | this.getLog().info( buff.toString() ); |
105 | 6 | } |
106 | |
|
107 | |
@Override |
108 | |
protected List<Violation> getErrorDetails( File pmdFile ) |
109 | |
throws XmlPullParserException, IOException |
110 | |
{ |
111 | 3 | PmdXpp3Reader reader = new PmdXpp3Reader(); |
112 | 3 | PmdErrorDetail details = reader.read( new FileReader( pmdFile ), false ); |
113 | |
|
114 | 3 | List<Violation> violations = new ArrayList<Violation>(); |
115 | 3 | for( PmdFile file : details.getFiles() ) |
116 | |
{ |
117 | 9 | String fullPath = file.getName(); |
118 | |
|
119 | 9 | for ( Violation violation : file.getViolations() ) |
120 | |
{ |
121 | 18 | violation.setFileName( getFilename( fullPath, violation.getViolationPackage() ) ); |
122 | 18 | violations.add( violation ); |
123 | |
} |
124 | 9 | } |
125 | 3 | return violations; |
126 | |
} |
127 | |
|
128 | |
@Override |
129 | |
protected int getPriority( Violation errorDetail ) |
130 | |
{ |
131 | 18 | return errorDetail.getPriority(); |
132 | |
} |
133 | |
|
134 | |
@Override |
135 | |
protected ViolationDetails<Violation> newViolationDetailsInstance() |
136 | |
{ |
137 | 3 | return new ViolationDetails<Violation>(); |
138 | |
} |
139 | |
|
140 | |
private String getFilename( String fullpath, String pkg ) |
141 | |
{ |
142 | 18 | int index = fullpath.lastIndexOf( File.separatorChar ); |
143 | |
|
144 | 36 | while ( StringUtils.isNotEmpty( pkg ) ) |
145 | |
{ |
146 | 36 | index = fullpath.substring( 0, index ).lastIndexOf( File.separatorChar ); |
147 | |
|
148 | 36 | int dot = pkg.indexOf( '.' ); |
149 | |
|
150 | 36 | if ( dot < 0 ) |
151 | |
{ |
152 | 18 | break; |
153 | |
} |
154 | 18 | pkg = pkg.substring( dot + 1 ); |
155 | 18 | } |
156 | |
|
157 | 18 | return fullpath.substring( index + 1 ); |
158 | |
} |
159 | |
} |