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 private List violations = new ArrayList();
57
58 private boolean aggregate;
59
60
61 private int fileCount = 0;
62
63
64
65 public PmdReportListener( Sink sink, ResourceBundle bundle, boolean aggregate )
66 {
67 this.sink = sink;
68 this.bundle = bundle;
69 this.aggregate = aggregate;
70 }
71
72 private String getTitle()
73 {
74 return bundle.getString( "report.pmd.title" );
75 }
76
77
78 public void ruleViolationAdded( IRuleViolation ruleViolation )
79 {
80 if ( !fileInitialized )
81 {
82 sink.section2();
83 sink.sectionTitle2();
84 String title = currentFilename;
85 if ( aggregate )
86 {
87 title = fileInfo.getProject().getName() + " - " + currentFilename;
88 }
89 sink.text( title );
90 sink.sectionTitle2_();
91
92 sink.table();
93 sink.tableRow();
94 sink.tableHeaderCell();
95 sink.text( bundle.getString( "report.pmd.column.violation" ) );
96 sink.tableHeaderCell_();
97 sink.tableHeaderCell();
98 sink.text( bundle.getString( "report.pmd.column.line" ) );
99 sink.tableHeaderCell_();
100 sink.tableRow_();
101
102 fileInitialized = true;
103 }
104 violations.add( ruleViolation );
105 }
106
107
108
109 private void processViolations()
110 {
111 fileCount++;
112 Collections.sort( violations, new Comparator()
113 {
114
115 public int compare( Object o1, Object o2 )
116 {
117 return ( (IRuleViolation) o1 ).getBeginLine()
118 - ( (IRuleViolation) o2 ).getBeginLine();
119 }
120 } );
121
122 for ( Iterator it = violations.iterator(); it.hasNext(); )
123 {
124 IRuleViolation ruleViolation = (IRuleViolation) it.next();
125
126 sink.tableRow();
127 sink.tableCell();
128 sink.text( ruleViolation.getDescription() );
129 sink.tableCell_();
130 sink.tableCell();
131
132 int beginLine = ruleViolation.getBeginLine();
133 outputLineLink( beginLine );
134 int endLine = ruleViolation.getEndLine();
135 if ( endLine != beginLine )
136 {
137 sink.text( " - " );
138 outputLineLink( endLine );
139 }
140
141 sink.tableCell_();
142 sink.tableRow_();
143 }
144 violations.clear();
145 }
146
147 private void outputLineLink( int line )
148 {
149 String xrefLocation = fileInfo.getXrefLocation();
150 if ( xrefLocation != null )
151 {
152 sink.link( xrefLocation + "/" + currentFilename.replaceAll( "\\.java$", ".html" ) + "#" + line );
153 }
154 sink.text( String.valueOf( line ) );
155 if ( xrefLocation != null )
156 {
157 sink.link_();
158 }
159 }
160
161
162 public void metricAdded( Metric metric )
163 {
164
165
166
167
168
169 }
170
171 public void beginDocument()
172 {
173 sink.head();
174 sink.title();
175 sink.text( getTitle() );
176 sink.title_();
177 sink.head_();
178
179 sink.body();
180
181 sink.section1();
182 sink.sectionTitle1();
183 sink.text( getTitle() );
184 sink.sectionTitle1_();
185
186 sink.paragraph();
187 sink.text( bundle.getString( "report.pmd.pmdlink" ) + " " );
188 sink.link( "http://pmd.sourceforge.net/" );
189 sink.text( "PMD" );
190 sink.link_();
191 sink.text( " " + AbstractPmdReport.getPmdVersion() + "." );
192 sink.paragraph_();
193
194 sink.section1_();
195
196
197
198 sink.section1();
199 sink.sectionTitle1();
200 sink.text( bundle.getString( "report.pmd.files" ) );
201 sink.sectionTitle1_();
202
203
204 }
205
206 public void beginFile( File file, PmdFileInfo finfo )
207 {
208 fileInfo = finfo;
209 currentFilename = StringUtils.substring( file.getAbsolutePath(),
210 finfo.getSourceDirectory().getAbsolutePath().length() + 1 );
211 currentFilename = StringUtils.replace( currentFilename, "\\", "/" );
212 fileInitialized = false;
213 }
214
215 public void endFile( File file )
216 {
217 if ( fileInitialized )
218 {
219 processViolations();
220 sink.table_();
221 sink.section2_();
222 }
223 }
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 if ( fileCount == 0 )
286 {
287 sink.paragraph();
288 sink.text( bundle.getString( "report.pmd.noProblems" ) );
289 sink.paragraph_();
290 }
291
292 sink.section1_();
293
294
295
296
297
298
299
300
301 sink.body_();
302
303 sink.flush();
304
305 sink.close();
306 }
307 }