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