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