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 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 private int maxAllowedViolations = 0;
84
85
86
87
88
89
90
91
92 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 if ( !skip )
115 {
116 if ( !"xml".equals( outputFileFormat ) )
117 {
118 throw new MojoExecutionException( "Output format is '" + outputFileFormat
119 + "', checkstyle:check requires format to be 'xml'." );
120 }
121
122 if ( !outputFile.exists() )
123 {
124 getLog().info(
125 "Unable to perform checkstyle:check, "
126 + "unable to find checkstyle:checkstyle outputFile." );
127 return;
128 }
129
130 try
131 {
132 XmlPullParser xpp = new MXParser();
133 Reader freader = ReaderFactory.newXmlReader( outputFile );
134 BufferedReader breader = new BufferedReader( freader );
135 xpp.setInput( breader );
136
137 int violations = countViolations( xpp );
138 if ( violations > maxAllowedViolations )
139 {
140 if ( failOnViolation )
141 {
142 String msg = "You have " + violations + " Checkstyle violation"
143 + ( ( violations > 1 ) ? "s" : "" ) + ".";
144 if ( maxAllowedViolations > 0 )
145 {
146 msg += " The maximum number of allowed violations is " + maxAllowedViolations + ".";
147 }
148 throw new MojoFailureException( msg );
149 }
150
151 getLog().warn( "checkstyle:check violations detected but failOnViolation set to false" );
152 }
153 }
154 catch ( IOException e )
155 {
156 throw new MojoExecutionException( "Unable to read Checkstyle results xml: "
157 + outputFile.getAbsolutePath(), e );
158 }
159 catch ( XmlPullParserException e )
160 {
161 throw new MojoExecutionException( "Unable to read Checkstyle results xml: "
162 + outputFile.getAbsolutePath(), e );
163 }
164 }
165 }
166
167 private int countViolations( XmlPullParser xpp )
168 throws XmlPullParserException, IOException
169 {
170 int count = 0;
171
172 int eventType = xpp.getEventType();
173 String file = "";
174 while ( eventType != XmlPullParser.END_DOCUMENT )
175 {
176 if ( eventType == XmlPullParser.START_TAG && "file".equals( xpp.getName() ) )
177 {
178 file = xpp.getAttributeValue( "", "name" );
179 file = file.substring( file.lastIndexOf( File.separatorChar ) + 1 );
180 }
181
182 if ( eventType == XmlPullParser.START_TAG && "error".equals( xpp.getName() )
183 && isViolation( xpp.getAttributeValue( "", "severity" ) ) )
184 {
185 if ( logViolationsToConsole )
186 {
187 StringBuffer stb = new StringBuffer();
188 stb.append( file );
189 stb.append( '[' );
190 stb.append( xpp.getAttributeValue( "", "line" ) );
191 stb.append( ':' );
192 stb.append( xpp.getAttributeValue( "", "column" ) );
193 stb.append( "] " );
194 stb.append( xpp.getAttributeValue( "", "message" ) );
195 getLog().error( stb.toString() );
196 }
197 count++;
198 }
199 eventType = xpp.next();
200 }
201
202 return count;
203 }
204
205
206
207
208
209
210
211 private boolean isViolation( String severity )
212 {
213 if ( "error".equals( severity ) )
214 {
215 return "error".equals( violationSeverity ) || "warning".equals( violationSeverity )
216 || "info".equals( violationSeverity );
217 }
218 else if ( "warning".equals( severity ) )
219 {
220 return "warning".equals( violationSeverity ) || "info".equals( violationSeverity );
221 }
222 else if ( "info".equals( severity ) )
223 {
224 return "info".equals( violationSeverity );
225 }
226 else
227 {
228 return false;
229 }
230 }
231 }