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 com.puppycrawl.tools.checkstyle.api.AuditEvent; |
23 | |
import com.puppycrawl.tools.checkstyle.api.AuditListener; |
24 | |
import com.puppycrawl.tools.checkstyle.api.AutomaticBean; |
25 | |
import com.puppycrawl.tools.checkstyle.api.Configuration; |
26 | |
import com.puppycrawl.tools.checkstyle.api.SeverityLevel; |
27 | |
|
28 | |
import org.codehaus.plexus.util.StringUtils; |
29 | |
|
30 | |
import java.io.File; |
31 | |
import java.util.ArrayList; |
32 | |
import java.util.List; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public class CheckstyleReportListener |
42 | |
extends AutomaticBean |
43 | |
implements AuditListener |
44 | |
{ |
45 | |
private List<File> sourceDirectories; |
46 | |
|
47 | |
private CheckstyleResults results; |
48 | |
|
49 | |
private String currentFile; |
50 | |
|
51 | |
private List<AuditEvent> events; |
52 | |
|
53 | |
private SeverityLevel severityLevel; |
54 | |
|
55 | |
private Configuration checkstyleConfiguration; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public CheckstyleReportListener( File sourceDirectory ) |
61 | 8 | { |
62 | 8 | this.sourceDirectories = new ArrayList<File>(); |
63 | 8 | this.sourceDirectories.add( sourceDirectory ); |
64 | 8 | } |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public CheckstyleReportListener( File sourceDirectory, Configuration configuration ) |
71 | 0 | { |
72 | 0 | this.sourceDirectories = new ArrayList<File>(); |
73 | 0 | this.sourceDirectories.add( sourceDirectory ); |
74 | 0 | this.checkstyleConfiguration = configuration; |
75 | 0 | } |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public CheckstyleReportListener( Configuration configuration ) |
82 | 17 | { |
83 | 17 | this.sourceDirectories = new ArrayList<File>(); |
84 | 17 | this.checkstyleConfiguration = configuration; |
85 | 17 | } |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
public void addSourceDirectory( File sourceDirectory ) |
91 | |
{ |
92 | 21 | this.sourceDirectories.add( sourceDirectory ); |
93 | 21 | } |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public void setSeverityLevelFilter( SeverityLevel severityLevel ) |
99 | |
{ |
100 | 8 | this.severityLevel = severityLevel; |
101 | 8 | } |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public SeverityLevel getSeverityLevelFilter() |
107 | |
{ |
108 | 8 | return severityLevel; |
109 | |
} |
110 | |
|
111 | |
|
112 | |
public void auditStarted( AuditEvent event ) |
113 | |
{ |
114 | 25 | setResults( new CheckstyleResults() ); |
115 | 25 | } |
116 | |
|
117 | |
|
118 | |
public void auditFinished( AuditEvent event ) |
119 | |
{ |
120 | |
|
121 | 25 | } |
122 | |
|
123 | |
|
124 | |
public void fileStarted( AuditEvent event ) |
125 | |
{ |
126 | 49 | final String fileName = StringUtils.replace( event.getFileName(), "\\", "/" ); |
127 | |
|
128 | 49 | for ( File sourceDirectory : sourceDirectories ) |
129 | |
{ |
130 | 65 | String sourceDirectoryPath = StringUtils.replace( sourceDirectory.getPath(), "\\", "/" ); |
131 | |
|
132 | 65 | if ( fileName.startsWith( sourceDirectoryPath + "/" ) ) |
133 | |
{ |
134 | 49 | currentFile = StringUtils.substring( fileName, sourceDirectoryPath.length() + 1 ); |
135 | |
|
136 | 49 | events = getResults().getFileViolations( currentFile ); |
137 | |
} |
138 | 65 | } |
139 | |
|
140 | 49 | if ( events == null ) |
141 | |
{ |
142 | 0 | events = new ArrayList<AuditEvent>(); |
143 | |
} |
144 | 49 | } |
145 | |
|
146 | |
|
147 | |
public void fileFinished( AuditEvent event ) |
148 | |
{ |
149 | 49 | getResults().setFileViolations( currentFile, events ); |
150 | 49 | currentFile = null; |
151 | 49 | } |
152 | |
|
153 | |
|
154 | |
public void addError( AuditEvent event ) |
155 | |
{ |
156 | 169 | if ( SeverityLevel.IGNORE.equals( event.getSeverityLevel() ) ) |
157 | |
{ |
158 | 32 | return; |
159 | |
} |
160 | |
|
161 | 137 | if ( severityLevel == null || severityLevel.equals( event.getSeverityLevel() ) ) |
162 | |
{ |
163 | 101 | events.add( event ); |
164 | |
} |
165 | 137 | } |
166 | |
|
167 | |
|
168 | |
public void addException( AuditEvent event, Throwable throwable ) |
169 | |
{ |
170 | |
|
171 | 0 | } |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
public CheckstyleResults getResults() |
177 | |
{ |
178 | 122 | results.setConfiguration( checkstyleConfiguration ); |
179 | 122 | return results; |
180 | |
} |
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
public void setResults( CheckstyleResults results ) |
186 | |
{ |
187 | 25 | this.results = results; |
188 | 25 | } |
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
public Configuration getCheckstyleConfiguration() |
194 | |
{ |
195 | 0 | return checkstyleConfiguration; |
196 | |
} |
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
public void setCheckstyleConfiguration( Configuration checkstyleConfiguration ) |
202 | |
{ |
203 | 0 | this.checkstyleConfiguration = checkstyleConfiguration; |
204 | 0 | } |
205 | |
|
206 | |
} |
207 | |
|