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