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