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