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.SeverityLevel; |
26 | |
import org.codehaus.plexus.util.StringUtils; |
27 | |
|
28 | |
import java.io.File; |
29 | |
import java.util.ArrayList; |
30 | |
import java.util.Iterator; |
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 sourceDirectories; |
45 | |
|
46 | |
private CheckstyleResults results; |
47 | |
|
48 | |
private String currentFile; |
49 | |
|
50 | |
private List events; |
51 | |
|
52 | |
private SeverityLevel severityLevel; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public CheckstyleReportListener( File sourceDirectory ) |
58 | 12 | { |
59 | 12 | this.sourceDirectories = new ArrayList(); |
60 | 12 | this.sourceDirectories.add( sourceDirectory ); |
61 | 12 | } |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public void addSourceDirectory( File sourceDirectory ) |
67 | |
{ |
68 | 0 | this.sourceDirectories.add( sourceDirectory ); |
69 | 0 | } |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public void setSeverityLevelFilter( SeverityLevel severityLevel ) |
75 | |
{ |
76 | 4 | this.severityLevel = severityLevel; |
77 | 4 | } |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public SeverityLevel getSeverityLevelFilter() |
83 | |
{ |
84 | 4 | return severityLevel; |
85 | |
} |
86 | |
|
87 | |
|
88 | |
public void auditStarted( AuditEvent event ) |
89 | |
{ |
90 | 12 | setResults( new CheckstyleResults() ); |
91 | 12 | } |
92 | |
|
93 | |
|
94 | |
public void auditFinished( AuditEvent event ) |
95 | |
{ |
96 | |
|
97 | 12 | } |
98 | |
|
99 | |
|
100 | |
public void fileStarted( AuditEvent event ) |
101 | |
{ |
102 | 24 | for ( Iterator it = sourceDirectories.iterator(); it.hasNext(); ) |
103 | |
{ |
104 | 24 | File sourceDirectory = (File) it.next(); |
105 | |
|
106 | 24 | currentFile = StringUtils.substring( event.getFileName(), sourceDirectory.getPath().length() + 1 ); |
107 | 24 | currentFile = StringUtils.replace( currentFile, "\\", "/" ); |
108 | |
|
109 | 24 | events = getResults().getFileViolations( currentFile ); |
110 | 24 | } |
111 | |
|
112 | 24 | if ( events == null ) |
113 | |
{ |
114 | 0 | events = new ArrayList(); |
115 | |
} |
116 | 24 | } |
117 | |
|
118 | |
|
119 | |
public void fileFinished( AuditEvent event ) |
120 | |
{ |
121 | 24 | getResults().setFileViolations( currentFile, events ); |
122 | 24 | currentFile = null; |
123 | 24 | } |
124 | |
|
125 | |
|
126 | |
public void addError( AuditEvent event ) |
127 | |
{ |
128 | 82 | if ( SeverityLevel.IGNORE.equals( event.getSeverityLevel() ) ) |
129 | |
{ |
130 | 16 | return; |
131 | |
} |
132 | |
|
133 | 66 | if ( severityLevel == null || severityLevel.equals( event.getSeverityLevel() ) ) |
134 | |
{ |
135 | 48 | events.add( event ); |
136 | |
} |
137 | 66 | } |
138 | |
|
139 | |
|
140 | |
public void addException( AuditEvent event, Throwable throwable ) |
141 | |
{ |
142 | |
|
143 | 0 | } |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
public CheckstyleResults getResults() |
149 | |
{ |
150 | 59 | return results; |
151 | |
} |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
public void setResults( CheckstyleResults results ) |
157 | |
{ |
158 | 12 | this.results = results; |
159 | 12 | } |
160 | |
} |
161 | |
|