1 | |
package org.apache.maven.plugin.checkstyle; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.plugin.AbstractMojo; |
23 | |
import org.apache.maven.plugin.MojoExecutionException; |
24 | |
import org.apache.maven.plugin.MojoFailureException; |
25 | |
import org.codehaus.plexus.util.ReaderFactory; |
26 | |
import org.codehaus.plexus.util.xml.pull.MXParser; |
27 | |
import org.codehaus.plexus.util.xml.pull.XmlPullParser; |
28 | |
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
29 | |
|
30 | |
import java.io.BufferedReader; |
31 | |
import java.io.File; |
32 | |
import java.io.IOException; |
33 | |
import java.io.Reader; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | 4 | public class CheckstyleViolationCheckMojo |
48 | |
extends AbstractMojo |
49 | |
{ |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
private File outputFile; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
private String outputFileFormat; |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
private boolean failOnViolation; |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | 4 | private int maxAllowedViolations = 0; |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | 4 | private String violationSeverity = "error"; |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
private boolean skip; |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
private boolean logViolationsToConsole; |
109 | |
|
110 | |
|
111 | |
public void execute() |
112 | |
throws MojoExecutionException, MojoFailureException |
113 | |
{ |
114 | 4 | if ( !skip ) |
115 | |
{ |
116 | 4 | if ( !"xml".equals( outputFileFormat ) ) |
117 | |
{ |
118 | 1 | throw new MojoExecutionException( "Output format is '" + outputFileFormat |
119 | |
+ "', checkstyle:check requires format to be 'xml'." ); |
120 | |
} |
121 | |
|
122 | 3 | if ( !outputFile.exists() ) |
123 | |
{ |
124 | 1 | getLog().info( |
125 | |
"Unable to perform checkstyle:check, " |
126 | |
+ "unable to find checkstyle:checkstyle outputFile." ); |
127 | 1 | return; |
128 | |
} |
129 | |
|
130 | |
try |
131 | |
{ |
132 | 2 | XmlPullParser xpp = new MXParser(); |
133 | 2 | Reader freader = ReaderFactory.newXmlReader( outputFile ); |
134 | 2 | BufferedReader breader = new BufferedReader( freader ); |
135 | 2 | xpp.setInput( breader ); |
136 | |
|
137 | 2 | int violations = countViolations( xpp ); |
138 | 2 | if ( violations > maxAllowedViolations ) |
139 | |
{ |
140 | 2 | if ( failOnViolation ) |
141 | |
{ |
142 | 1 | String msg = "You have " + violations + " Checkstyle violation" |
143 | |
+ ( ( violations > 1 ) ? "s" : "" ) + "."; |
144 | 1 | if ( maxAllowedViolations > 0 ) |
145 | |
{ |
146 | 0 | msg += " The maximum number of allowed violations is " + maxAllowedViolations + "."; |
147 | |
} |
148 | 1 | throw new MojoFailureException( msg ); |
149 | |
} |
150 | |
|
151 | 1 | getLog().warn( "checkstyle:check violations detected but failOnViolation set to false" ); |
152 | |
} |
153 | |
} |
154 | 0 | catch ( IOException e ) |
155 | |
{ |
156 | 0 | throw new MojoExecutionException( "Unable to read Checkstyle results xml: " |
157 | |
+ outputFile.getAbsolutePath(), e ); |
158 | |
} |
159 | 0 | catch ( XmlPullParserException e ) |
160 | |
{ |
161 | 0 | throw new MojoExecutionException( "Unable to read Checkstyle results xml: " |
162 | |
+ outputFile.getAbsolutePath(), e ); |
163 | 1 | } |
164 | |
} |
165 | 1 | } |
166 | |
|
167 | |
private int countViolations( XmlPullParser xpp ) |
168 | |
throws XmlPullParserException, IOException |
169 | |
{ |
170 | 2 | int count = 0; |
171 | |
|
172 | 2 | int eventType = xpp.getEventType(); |
173 | 2 | String file = ""; |
174 | 64 | while ( eventType != XmlPullParser.END_DOCUMENT ) |
175 | |
{ |
176 | 62 | if ( eventType == XmlPullParser.START_TAG && "file".equals( xpp.getName() ) ) |
177 | |
{ |
178 | 6 | file = xpp.getAttributeValue( "", "name" ); |
179 | 6 | file = file.substring( file.lastIndexOf( File.separatorChar ) + 1 ); |
180 | |
} |
181 | |
|
182 | 62 | if ( eventType == XmlPullParser.START_TAG && "error".equals( xpp.getName() ) |
183 | |
&& isViolation( xpp.getAttributeValue( "", "severity" ) ) ) |
184 | |
{ |
185 | 10 | if ( logViolationsToConsole ) |
186 | |
{ |
187 | 0 | StringBuffer stb = new StringBuffer(); |
188 | 0 | stb.append( file ); |
189 | 0 | stb.append( '[' ); |
190 | 0 | stb.append( xpp.getAttributeValue( "", "line" ) ); |
191 | 0 | stb.append( ':' ); |
192 | 0 | stb.append( xpp.getAttributeValue( "", "column" ) ); |
193 | 0 | stb.append( "] " ); |
194 | 0 | stb.append( xpp.getAttributeValue( "", "message" ) ); |
195 | 0 | getLog().error( stb.toString() ); |
196 | |
} |
197 | 10 | count++; |
198 | |
} |
199 | 62 | eventType = xpp.next(); |
200 | |
} |
201 | |
|
202 | 2 | return count; |
203 | |
} |
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
private boolean isViolation( String severity ) |
212 | |
{ |
213 | 10 | if ( "error".equals( severity ) ) |
214 | |
{ |
215 | 10 | return "error".equals( violationSeverity ) || "warning".equals( violationSeverity ) |
216 | |
|| "info".equals( violationSeverity ); |
217 | |
} |
218 | 0 | else if ( "warning".equals( severity ) ) |
219 | |
{ |
220 | 0 | return "warning".equals( violationSeverity ) || "info".equals( violationSeverity ); |
221 | |
} |
222 | 0 | else if ( "info".equals( severity ) ) |
223 | |
{ |
224 | 0 | return "info".equals( violationSeverity ); |
225 | |
} |
226 | |
else |
227 | |
{ |
228 | 0 | return false; |
229 | |
} |
230 | |
} |
231 | |
} |