1 | |
package org.apache.maven.plugin.pmd; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.util.ArrayList; |
24 | |
import java.util.Collections; |
25 | |
import java.util.Comparator; |
26 | |
import java.util.Iterator; |
27 | |
import java.util.List; |
28 | |
import java.util.ResourceBundle; |
29 | |
|
30 | |
import net.sourceforge.pmd.IRuleViolation; |
31 | |
import net.sourceforge.pmd.ReportListener; |
32 | |
import net.sourceforge.pmd.stat.Metric; |
33 | |
|
34 | |
import org.apache.maven.doxia.sink.Sink; |
35 | |
import org.codehaus.plexus.util.StringUtils; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public class PmdReportListener |
44 | |
implements ReportListener |
45 | |
{ |
46 | |
private Sink sink; |
47 | |
|
48 | |
private String currentFilename; |
49 | |
|
50 | |
private boolean fileInitialized; |
51 | |
|
52 | |
private ResourceBundle bundle; |
53 | |
|
54 | |
private PmdFileInfo fileInfo; |
55 | |
|
56 | 5 | private List violations = new ArrayList(); |
57 | |
|
58 | |
private boolean aggregate; |
59 | |
|
60 | |
|
61 | 5 | private int fileCount = 0; |
62 | |
|
63 | |
|
64 | |
|
65 | |
public PmdReportListener( Sink sink, ResourceBundle bundle, boolean aggregate ) |
66 | 5 | { |
67 | 5 | this.sink = sink; |
68 | 5 | this.bundle = bundle; |
69 | 5 | this.aggregate = aggregate; |
70 | 5 | } |
71 | |
|
72 | |
private String getTitle() |
73 | |
{ |
74 | 8 | return bundle.getString( "report.pmd.title" ); |
75 | |
} |
76 | |
|
77 | |
|
78 | |
public void ruleViolationAdded( IRuleViolation ruleViolation ) |
79 | |
{ |
80 | 23 | if ( !fileInitialized ) |
81 | |
{ |
82 | 11 | sink.section2(); |
83 | 11 | sink.sectionTitle2(); |
84 | 11 | String title = currentFilename; |
85 | 11 | if ( aggregate ) |
86 | |
{ |
87 | 0 | title = fileInfo.getProject().getName() + " - " + currentFilename; |
88 | |
} |
89 | 11 | sink.text( title ); |
90 | 11 | sink.sectionTitle2_(); |
91 | |
|
92 | 11 | sink.table(); |
93 | 11 | sink.tableRow(); |
94 | 11 | sink.tableHeaderCell(); |
95 | 11 | sink.text( bundle.getString( "report.pmd.column.violation" ) ); |
96 | 11 | sink.tableHeaderCell_(); |
97 | 11 | sink.tableHeaderCell(); |
98 | 11 | sink.text( bundle.getString( "report.pmd.column.line" ) ); |
99 | 11 | sink.tableHeaderCell_(); |
100 | 11 | sink.tableRow_(); |
101 | |
|
102 | 11 | fileInitialized = true; |
103 | |
} |
104 | 23 | violations.add( ruleViolation ); |
105 | 23 | } |
106 | |
|
107 | |
|
108 | |
|
109 | |
private void processViolations() |
110 | |
{ |
111 | 11 | fileCount++; |
112 | 11 | Collections.sort( violations, new Comparator() |
113 | 11 | { |
114 | |
|
115 | |
public int compare( Object o1, Object o2 ) |
116 | |
{ |
117 | 13 | return ( (IRuleViolation) o1 ).getBeginLine() |
118 | |
- ( (IRuleViolation) o2 ).getBeginLine(); |
119 | |
} |
120 | |
} ); |
121 | |
|
122 | 11 | for ( Iterator it = violations.iterator(); it.hasNext(); ) |
123 | |
{ |
124 | 23 | IRuleViolation ruleViolation = (IRuleViolation) it.next(); |
125 | |
|
126 | 23 | sink.tableRow(); |
127 | 23 | sink.tableCell(); |
128 | 23 | sink.text( ruleViolation.getDescription() ); |
129 | 23 | sink.tableCell_(); |
130 | 23 | sink.tableCell(); |
131 | |
|
132 | 23 | int beginLine = ruleViolation.getBeginLine(); |
133 | 23 | outputLineLink( beginLine ); |
134 | 23 | int endLine = ruleViolation.getEndLine(); |
135 | 23 | if ( endLine != beginLine ) |
136 | |
{ |
137 | 9 | sink.text( " - " ); |
138 | 9 | outputLineLink( endLine ); |
139 | |
} |
140 | |
|
141 | 23 | sink.tableCell_(); |
142 | 23 | sink.tableRow_(); |
143 | 23 | } |
144 | 11 | violations.clear(); |
145 | 11 | } |
146 | |
|
147 | |
private void outputLineLink( int line ) |
148 | |
{ |
149 | 32 | String xrefLocation = fileInfo.getXrefLocation(); |
150 | 32 | if ( xrefLocation != null ) |
151 | |
{ |
152 | 32 | sink.link( xrefLocation + "/" + currentFilename.replaceAll( "\\.java$", ".html" ) + "#" + line ); |
153 | |
} |
154 | 32 | sink.text( String.valueOf( line ) ); |
155 | 32 | if ( xrefLocation != null ) |
156 | |
{ |
157 | 32 | sink.link_(); |
158 | |
} |
159 | 32 | } |
160 | |
|
161 | |
|
162 | |
public void metricAdded( Metric metric ) |
163 | |
{ |
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | 0 | } |
170 | |
|
171 | |
public void beginDocument() |
172 | |
{ |
173 | 5 | sink.head(); |
174 | 4 | sink.title(); |
175 | 4 | sink.text( getTitle() ); |
176 | 4 | sink.title_(); |
177 | 4 | sink.head_(); |
178 | |
|
179 | 4 | sink.body(); |
180 | |
|
181 | 4 | sink.section1(); |
182 | 4 | sink.sectionTitle1(); |
183 | 4 | sink.text( getTitle() ); |
184 | 4 | sink.sectionTitle1_(); |
185 | |
|
186 | 4 | sink.paragraph(); |
187 | 4 | sink.text( bundle.getString( "report.pmd.pmdlink" ) + " " ); |
188 | 4 | sink.link( "http://pmd.sourceforge.net/" ); |
189 | 4 | sink.text( "PMD" ); |
190 | 4 | sink.link_(); |
191 | 4 | sink.text( " " + AbstractPmdReport.getPmdVersion() + "." ); |
192 | 4 | sink.paragraph_(); |
193 | |
|
194 | 4 | sink.section1_(); |
195 | |
|
196 | |
|
197 | |
|
198 | 4 | sink.section1(); |
199 | 4 | sink.sectionTitle1(); |
200 | 4 | sink.text( bundle.getString( "report.pmd.files" ) ); |
201 | 4 | sink.sectionTitle1_(); |
202 | |
|
203 | |
|
204 | 4 | } |
205 | |
|
206 | |
public void beginFile( File file, PmdFileInfo finfo ) |
207 | |
{ |
208 | 11 | fileInfo = finfo; |
209 | 11 | currentFilename = StringUtils.substring( file.getAbsolutePath(), |
210 | |
finfo.getSourceDirectory().getAbsolutePath().length() + 1 ); |
211 | 11 | currentFilename = StringUtils.replace( currentFilename, "\\", "/" ); |
212 | 11 | fileInitialized = false; |
213 | 11 | } |
214 | |
|
215 | |
public void endFile( File file ) |
216 | |
{ |
217 | 11 | if ( fileInitialized ) |
218 | |
{ |
219 | 11 | processViolations(); |
220 | 11 | sink.table_(); |
221 | 11 | sink.section2_(); |
222 | |
} |
223 | 11 | } |
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
public void endDocument() |
284 | |
{ |
285 | 4 | if ( fileCount == 0 ) |
286 | |
{ |
287 | 0 | sink.paragraph(); |
288 | 0 | sink.text( bundle.getString( "report.pmd.noProblems" ) ); |
289 | 0 | sink.paragraph_(); |
290 | |
} |
291 | |
|
292 | 4 | sink.section1_(); |
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | 4 | sink.body_(); |
302 | |
|
303 | 4 | sink.flush(); |
304 | |
|
305 | 4 | sink.close(); |
306 | 4 | } |
307 | |
} |