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.IOException;
24 import java.io.Reader;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import org.apache.maven.plugin.AbstractMojo;
29 import org.apache.maven.plugin.MojoExecutionException;
30 import org.apache.maven.plugin.MojoFailureException;
31 import org.apache.maven.project.MavenProject;
32 import org.codehaus.plexus.util.IOUtil;
33 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
34
35
36
37
38
39
40
41 public abstract class AbstractPmdViolationCheckMojo<D>
42 extends AbstractMojo
43 {
44
45
46
47
48
49
50 private File targetDirectory;
51
52
53
54
55
56
57
58 private boolean failOnViolation;
59
60
61
62
63
64
65
66
67 private String language;
68
69
70
71
72
73
74
75 protected boolean aggregate;
76
77
78
79
80
81
82 private boolean verbose;
83
84
85
86
87
88
89
90
91 protected MavenProject project;
92
93 protected void executeCheck( String filename, String tagName, String key, int failurePriority )
94 throws MojoFailureException, MojoExecutionException
95 {
96 if ( aggregate && !project.isExecutionRoot() )
97 {
98 return;
99 }
100
101 if ( "java".equals( language ) || aggregate )
102 {
103 File outputFile = new File( targetDirectory, filename );
104
105 if ( outputFile.exists() )
106 {
107 Reader reader = null;
108 try
109 {
110 ViolationDetails<D> violations = getViolations( outputFile, failurePriority );
111
112 List<D> failures = violations.getFailureDetails();
113 List<D> warnings = violations.getWarningDetails();
114
115 if ( verbose )
116 {
117 printErrors( failures, warnings );
118 }
119
120 int failureCount = failures.size();
121 int warningCount = warnings.size();
122
123 String message = getMessage( failureCount, warningCount, key, outputFile );
124
125 if ( failureCount > 0 && failOnViolation )
126 {
127 throw new MojoFailureException( message );
128 }
129
130 this.getLog().info( message );
131 }
132 catch ( IOException e )
133 {
134 throw new MojoExecutionException(
135 "Unable to read PMD results xml: " + outputFile.getAbsolutePath(),
136 e );
137 }
138 catch ( XmlPullParserException e )
139 {
140 throw new MojoExecutionException(
141 "Unable to read PMD results xml: " + outputFile.getAbsolutePath(),
142 e );
143 }
144 finally
145 {
146 IOUtil.close( reader );
147 }
148 }
149 else
150 {
151 throw new MojoFailureException( "Unable to perform check, " + "unable to find " + outputFile );
152 }
153 }
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167 private ViolationDetails<D> getViolations( File analysisFile, int failurePriority )
168 throws XmlPullParserException, IOException
169 {
170 List<D> failures = new ArrayList<D>();
171 List<D> warnings = new ArrayList<D>();
172
173 List<D> violations = getErrorDetails( analysisFile );
174
175 for( D violation : violations )
176 {
177 int priority = getPriority( violation );
178 if ( priority <= failurePriority )
179 {
180 failures.add( violation );
181 }
182 else
183 {
184 warnings.add( violation );
185 }
186 }
187
188 ViolationDetails<D> details = newViolationDetailsInstance();
189 details.setFailureDetails( failures );
190 details.setWarningDetails( warnings );
191 return details;
192 }
193
194 protected abstract int getPriority( D errorDetail );
195
196 protected abstract ViolationDetails<D> newViolationDetailsInstance();
197
198
199
200
201
202
203
204
205
206 protected void printErrors( List<D> failures, List<D> warnings )
207 {
208 for ( D warning : warnings )
209 {
210 printError( warning, "Warning" );
211 }
212
213 for ( D failure : failures )
214 {
215 printError( failure, "Failure" );
216 }
217 }
218
219
220
221
222
223
224
225
226
227
228 private String getMessage( int failureCount, int warningCount, String key, File outputFile )
229 {
230 StringBuffer message = new StringBuffer();
231 if ( failureCount > 0 || warningCount > 0 )
232 {
233 if ( failureCount > 0 )
234 {
235 message.append( "You have " + failureCount + " " + key + ( failureCount > 1 ? "s" : "" ) );
236 }
237
238 if ( warningCount > 0 )
239 {
240 if ( failureCount > 0 )
241 {
242 message.append( " and " );
243 }
244 else
245 {
246 message.append( "You have " );
247 }
248 message.append( warningCount + " warning" + ( warningCount > 1 ? "s" : "" ) );
249 }
250
251 message.append( ". For more details see:" ).append( outputFile.getAbsolutePath() );
252 }
253 return message.toString();
254 }
255
256
257
258
259
260
261 protected abstract void printError( D item, String severity );
262
263
264
265
266
267
268
269
270
271 protected abstract List<D> getErrorDetails( File analisysFile )
272 throws XmlPullParserException, IOException;
273 }