-
Notifications
You must be signed in to change notification settings - Fork 44
/
PmdReportGenerator.java
440 lines (369 loc) · 13.3 KB
/
PmdReportGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package org.apache.maven.plugins.pmd;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import org.apache.maven.doxia.sink.Sink;
import org.apache.maven.plugin.logging.Log;
import org.codehaus.plexus.util.StringUtils;
import net.sourceforge.pmd.Report.ProcessingError;
import net.sourceforge.pmd.RuleViolation;
/**
* Render the PMD violations into Doxia events.
*
* @author Brett Porter
* @version $Id$
*/
public class PmdReportGenerator
{
private Log log;
private Sink sink;
private String currentFilename;
private ResourceBundle bundle;
private Set<RuleViolation> violations = new HashSet<>();
private List<ProcessingError> processingErrors = new ArrayList<>();
private boolean aggregate;
private boolean renderRuleViolationPriority;
private Map<File, PmdFileInfo> files;
// private List<Metric> metrics = new ArrayList<Metric>();
public PmdReportGenerator( Log log, Sink sink, ResourceBundle bundle, boolean aggregate )
{
this.log = log;
this.sink = sink;
this.bundle = bundle;
this.aggregate = aggregate;
}
private String getTitle()
{
return bundle.getString( "report.pmd.title" );
}
public void setViolations( Collection<RuleViolation> violations )
{
this.violations = new HashSet<>( violations );
}
public List<RuleViolation> getViolations()
{
return new ArrayList<>( violations );
}
public void setProcessingErrors( Collection<ProcessingError> errors )
{
this.processingErrors = new ArrayList<>( errors );
}
public List<ProcessingError> getProcessingErrors()
{
return processingErrors;
}
// public List<Metric> getMetrics()
// {
// return metrics;
// }
//
// public void setMetrics( List<Metric> metrics )
// {
// this.metrics = metrics;
// }
private String shortenFilename( String filename, PmdFileInfo fileInfo )
{
String result = filename;
if ( fileInfo != null && fileInfo.getSourceDirectory() != null )
{
result = StringUtils.substring( result, fileInfo.getSourceDirectory().getAbsolutePath().length() + 1 );
}
return StringUtils.replace( result, "\\", "/" );
}
private String makeFileSectionName( String filename, PmdFileInfo fileInfo )
{
if ( aggregate && fileInfo != null && fileInfo.getProject() != null )
{
return fileInfo.getProject().getName() + " - " + filename;
}
return filename;
}
private PmdFileInfo determineFileInfo( String filename )
throws IOException
{
File canonicalFilename = new File( filename ).getCanonicalFile();
PmdFileInfo fileInfo = files.get( canonicalFilename );
if ( fileInfo == null )
{
log.warn( "Couldn't determine PmdFileInfo for file " + filename + " (canonical: " + canonicalFilename
+ "). XRef links won't be available." );
}
return fileInfo;
}
private void startFileSection( String currentFilename, PmdFileInfo fileInfo )
{
sink.section2();
sink.sectionTitle2();
// prepare the filename
this.currentFilename = shortenFilename( currentFilename, fileInfo );
sink.text( makeFileSectionName( this.currentFilename, fileInfo ) );
sink.sectionTitle2_();
sink.table();
sink.tableRow();
sink.tableHeaderCell();
sink.text( bundle.getString( "report.pmd.column.rule" ) );
sink.tableHeaderCell_();
sink.tableHeaderCell();
sink.text( bundle.getString( "report.pmd.column.violation" ) );
sink.tableHeaderCell_();
if ( this.renderRuleViolationPriority )
{
sink.tableHeaderCell();
sink.text( bundle.getString( "report.pmd.column.priority" ) );
sink.tableHeaderCell_();
}
sink.tableHeaderCell();
sink.text( bundle.getString( "report.pmd.column.line" ) );
sink.tableHeaderCell_();
sink.tableRow_();
}
private void endFileSection()
{
sink.table_();
sink.section2_();
}
private void processSingleRuleViolation( RuleViolation ruleViolation, PmdFileInfo fileInfo )
{
sink.tableRow();
sink.tableCell();
sink.link( ruleViolation.getRule().getExternalInfoUrl() );
sink.text( ruleViolation.getRule().getName() );
sink.link_();
sink.tableCell_();
sink.tableCell();
sink.text( ruleViolation.getDescription() );
sink.tableCell_();
if ( this.renderRuleViolationPriority )
{
sink.tableCell();
sink.text( String.valueOf( ruleViolation.getRule().getPriority().getPriority() ) );
sink.tableCell_();
}
sink.tableCell();
int beginLine = ruleViolation.getBeginLine();
outputLineLink( beginLine, fileInfo );
int endLine = ruleViolation.getEndLine();
if ( endLine != beginLine )
{
sink.text( "–" ); // \u2013 is a medium long dash character
outputLineLink( endLine, fileInfo );
}
sink.tableCell_();
sink.tableRow_();
}
// PMD might run the analysis multi-threaded, so the violations might be reported
// out of order. We sort them here by filename and line number before writing them to
// the report.
private void processViolations()
throws IOException
{
sink.section1();
sink.sectionTitle1();
sink.text( bundle.getString( "report.pmd.files" ) );
sink.sectionTitle1_();
// TODO files summary
List<RuleViolation> violations2 = new ArrayList<>( violations );
Collections.sort( violations2, new Comparator<RuleViolation>()
{
/** {@inheritDoc} */
public int compare( RuleViolation o1, RuleViolation o2 )
{
int filenames = o1.getFilename().compareTo( o2.getFilename() );
if ( filenames == 0 )
{
return o1.getBeginLine() - o2.getBeginLine();
}
else
{
return filenames;
}
}
} );
boolean fileSectionStarted = false;
String previousFilename = null;
for ( RuleViolation ruleViolation : violations2 )
{
String currentFn = ruleViolation.getFilename();
PmdFileInfo fileInfo = determineFileInfo( currentFn );
if ( !currentFn.equalsIgnoreCase( previousFilename ) && fileSectionStarted )
{
endFileSection();
fileSectionStarted = false;
}
if ( !fileSectionStarted )
{
startFileSection( currentFn, fileInfo );
fileSectionStarted = true;
}
processSingleRuleViolation( ruleViolation, fileInfo );
previousFilename = currentFn;
}
if ( fileSectionStarted )
{
endFileSection();
}
if ( violations.isEmpty() )
{
sink.paragraph();
sink.text( bundle.getString( "report.pmd.noProblems" ) );
sink.paragraph_();
}
sink.section1_();
}
private void outputLineLink( int line, PmdFileInfo fileInfo )
{
String xrefLocation = null;
if ( fileInfo != null )
{
xrefLocation = fileInfo.getXrefLocation();
}
if ( xrefLocation != null )
{
sink.link( xrefLocation + "/" + currentFilename.replaceAll( "\\.java$", ".html" ) + "#L" + line );
}
sink.text( String.valueOf( line ) );
if ( xrefLocation != null )
{
sink.link_();
}
}
private void processProcessingErrors() throws IOException
{
// sort the problem by filename first, since PMD is executed multi-threaded
// and might reports the results unsorted
Collections.sort( processingErrors, new Comparator<ProcessingError>()
{
@Override
public int compare( ProcessingError e1, ProcessingError e2 )
{
return e1.getFile().compareTo( e2.getFile() );
}
} );
sink.section1();
sink.sectionTitle1();
sink.text( bundle.getString( "report.pmd.processingErrors.title" ) );
sink.sectionTitle1_();
sink.table();
sink.tableRow();
sink.tableHeaderCell();
sink.text( bundle.getString( "report.pmd.processingErrors.column.filename" ) );
sink.tableHeaderCell_();
sink.tableHeaderCell();
sink.text( bundle.getString( "report.pmd.processingErrors.column.problem" ) );
sink.tableHeaderCell_();
sink.tableRow_();
for ( ProcessingError error : processingErrors )
{
processSingleProcessingError( error );
}
sink.table_();
sink.section1_();
}
private void processSingleProcessingError( ProcessingError error ) throws IOException
{
String filename = error.getFile();
PmdFileInfo fileInfo = determineFileInfo( filename );
filename = makeFileSectionName( shortenFilename( filename, fileInfo ), fileInfo );
sink.tableRow();
sink.tableCell();
sink.text( filename );
sink.tableCell_();
sink.tableCell();
sink.text( error.getMsg() );
sink.verbatim( null );
sink.rawText( error.getDetail() );
sink.verbatim_();
sink.tableCell_();
sink.tableRow_();
}
public void beginDocument()
{
sink.head();
sink.title();
sink.text( getTitle() );
sink.title_();
sink.head_();
sink.body();
sink.section1();
sink.sectionTitle1();
sink.text( getTitle() );
sink.sectionTitle1_();
sink.paragraph();
sink.text( bundle.getString( "report.pmd.pmdlink" ) + " " );
sink.link( "https://pmd.github.io" );
sink.text( "PMD" );
sink.link_();
sink.text( " " + AbstractPmdReport.getPmdVersion() + "." );
sink.paragraph_();
sink.section1_();
// TODO overall summary
}
/*
* private void processMetrics() { if ( metrics.size() == 0 ) { return; } sink.section1(); sink.sectionTitle1();
* sink.text( "Metrics" ); sink.sectionTitle1_(); sink.table(); sink.tableRow(); sink.tableHeaderCell(); sink.text(
* "Name" ); sink.tableHeaderCell_(); sink.tableHeaderCell(); sink.text( "Count" ); sink.tableHeaderCell_();
* sink.tableHeaderCell(); sink.text( "High" ); sink.tableHeaderCell_(); sink.tableHeaderCell(); sink.text( "Low" );
* sink.tableHeaderCell_(); sink.tableHeaderCell(); sink.text( "Average" ); sink.tableHeaderCell_();
* sink.tableRow_(); for ( Metric met : metrics ) { sink.tableRow(); sink.tableCell(); sink.text(
* met.getMetricName() ); sink.tableCell_(); sink.tableCell(); sink.text( String.valueOf( met.getCount() ) );
* sink.tableCell_(); sink.tableCell(); sink.text( String.valueOf( met.getHighValue() ) ); sink.tableCell_();
* sink.tableCell(); sink.text( String.valueOf( met.getLowValue() ) ); sink.tableCell_(); sink.tableCell();
* sink.text( String.valueOf( met.getAverage() ) ); sink.tableCell_(); sink.tableRow_(); } sink.table_();
* sink.section1_(); }
*/
public void render()
throws IOException
{
processViolations();
if ( !processingErrors.isEmpty() )
{
processProcessingErrors();
}
}
public void endDocument()
throws IOException
{
// The Metrics report useless with the current PMD metrics impl.
// For instance, run the coupling ruleset and you will get a boatload
// of excessive imports metrics, none of which is really any use.
// TODO Determine if we are going to just ignore metrics.
// processMetrics();
sink.body_();
sink.flush();
sink.close();
}
public void setFiles( Map<File, PmdFileInfo> files )
{
this.files = files;
}
public void setRenderRuleViolationPriority( boolean renderRuleViolationPriority )
{
this.renderRuleViolationPriority = renderRuleViolationPriority;
}
}