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.xml.pull.MXParser;
26 import org.codehaus.plexus.util.xml.pull.XmlPullParser;
27 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
28
29 import java.io.BufferedReader;
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.io.InputStreamReader;
34 import java.io.Reader;
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 String violationSeverity = "error";
84
85
86
87
88
89
90
91 private boolean skip;
92
93
94
95
96 public void execute()
97 throws MojoExecutionException, MojoFailureException
98 {
99 if ( !skip )
100 {
101 if ( !"xml".equals( outputFileFormat ) )
102 {
103 throw new MojoExecutionException( "Output format is '" + outputFileFormat
104 + "', checkstyle:check requires format to be 'xml'." );
105 }
106
107 if ( !outputFile.exists() )
108 {
109 getLog().info(
110 "Unable to perform checkstyle:check, "
111 + "unable to find checkstyle:checkstyle outputFile." );
112 return;
113 }
114
115 try
116 {
117 XmlPullParser xpp = new MXParser();
118
119 Reader freader = new InputStreamReader( new FileInputStream( outputFile ), "UTF-8" );
120 BufferedReader breader = new BufferedReader( freader );
121 xpp.setInput( breader );
122
123 int violations = countViolations( xpp );
124 if ( violations > 0 )
125 {
126 if ( failOnViolation )
127 {
128 throw new MojoFailureException( "You have " + violations + " Checkstyle violation"
129 + ( ( violations > 1 ) ? "s" : "" ) + "." );
130 }
131
132 getLog().warn( "checkstyle:check violations detected but failOnViolation set to false" );
133 }
134 }
135 catch ( IOException e )
136 {
137 throw new MojoExecutionException( "Unable to read Checkstyle results xml: "
138 + outputFile.getAbsolutePath(), e );
139 }
140 catch ( XmlPullParserException e )
141 {
142 throw new MojoExecutionException( "Unable to read Checkstyle results xml: "
143 + outputFile.getAbsolutePath(), e );
144 }
145 }
146 }
147
148 private int countViolations( XmlPullParser xpp )
149 throws XmlPullParserException, IOException
150 {
151 int count = 0;
152
153 int eventType = xpp.getEventType();
154 while ( eventType != XmlPullParser.END_DOCUMENT )
155 {
156 if ( eventType == XmlPullParser.START_TAG && "error".equals( xpp.getName() )
157 && isViolation( xpp.getAttributeValue( "", "severity" ) ) )
158 {
159 count++;
160 }
161 eventType = xpp.next();
162 }
163
164 return count;
165 }
166
167
168
169
170
171
172
173 private boolean isViolation( String severity )
174 {
175 if ( "error".equals( severity ) )
176 {
177 return "error".equals( violationSeverity ) || "warning".equals( violationSeverity )
178 || "info".equals( violationSeverity );
179 }
180 else if ( "warning".equals( severity ) )
181 {
182 return "warning".equals( violationSeverity ) || "info".equals( violationSeverity );
183 }
184 else if ( "info".equals( severity ) )
185 {
186 return "info".equals( violationSeverity );
187 }
188 else
189 {
190 return false;
191 }
192 }
193 }