| 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.PMD; |
| 32 | |
import net.sourceforge.pmd.ReportListener; |
| 33 | |
import net.sourceforge.pmd.stat.Metric; |
| 34 | |
|
| 35 | |
import org.codehaus.doxia.sink.Sink; |
| 36 | |
import org.codehaus.plexus.util.StringUtils; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
public class PmdReportListener |
| 45 | |
implements ReportListener |
| 46 | |
{ |
| 47 | |
private Sink sink; |
| 48 | |
|
| 49 | |
private String currentFilename; |
| 50 | |
|
| 51 | |
private boolean fileInitialized; |
| 52 | |
|
| 53 | |
private ResourceBundle bundle; |
| 54 | |
|
| 55 | |
private PmdFileInfo fileInfo; |
| 56 | |
|
| 57 | 4 | private List violations = new ArrayList(); |
| 58 | |
|
| 59 | |
private boolean aggregate; |
| 60 | |
|
| 61 | |
|
| 62 | 4 | private int fileCount = 0; |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
public PmdReportListener( Sink sink, ResourceBundle bundle, boolean aggregate ) |
| 67 | 4 | { |
| 68 | 4 | this.sink = sink; |
| 69 | 4 | this.bundle = bundle; |
| 70 | 4 | this.aggregate = aggregate; |
| 71 | 4 | } |
| 72 | |
|
| 73 | |
private String getTitle() |
| 74 | |
{ |
| 75 | 8 | return bundle.getString( "report.pmd.title" ); |
| 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 | |
{ |
| 114 | 11 | public int compare( Object o1, Object o2 ) |
| 115 | |
{ |
| 116 | 13 | return ( (IRuleViolation) o1 ).getBeginLine() |
| 117 | |
- ( (IRuleViolation) o2 ).getBeginLine(); |
| 118 | |
} |
| 119 | |
} ); |
| 120 | |
|
| 121 | 11 | for ( Iterator it = violations.iterator(); it.hasNext(); ) |
| 122 | |
{ |
| 123 | 23 | IRuleViolation ruleViolation = (IRuleViolation) it.next(); |
| 124 | |
|
| 125 | 23 | sink.tableRow(); |
| 126 | 23 | sink.tableCell(); |
| 127 | 23 | sink.text( ruleViolation.getDescription() ); |
| 128 | 23 | sink.tableCell_(); |
| 129 | 23 | sink.tableCell(); |
| 130 | |
|
| 131 | 23 | int beginLine = ruleViolation.getBeginLine(); |
| 132 | 23 | outputLineLink( beginLine ); |
| 133 | 23 | int endLine = ruleViolation.getEndLine(); |
| 134 | 23 | if ( endLine != beginLine ) |
| 135 | |
{ |
| 136 | 9 | sink.text( " - " ); |
| 137 | 9 | outputLineLink( endLine ); |
| 138 | |
} |
| 139 | |
|
| 140 | 23 | sink.tableCell_(); |
| 141 | 23 | sink.tableRow_(); |
| 142 | |
} |
| 143 | 11 | violations.clear(); |
| 144 | 11 | } |
| 145 | |
|
| 146 | |
private void outputLineLink( int line ) |
| 147 | |
{ |
| 148 | 32 | String xrefLocation = fileInfo.getXrefLocation(); |
| 149 | 32 | if ( xrefLocation != null ) |
| 150 | |
{ |
| 151 | 32 | sink.link( xrefLocation + "/" + currentFilename.replaceAll( "\\.java$", ".html" ) + "#" + line ); |
| 152 | |
} |
| 153 | 32 | sink.text( String.valueOf( line ) ); |
| 154 | 32 | if ( xrefLocation != null ) |
| 155 | |
{ |
| 156 | 32 | sink.link_(); |
| 157 | |
} |
| 158 | 32 | } |
| 159 | |
|
| 160 | |
public void metricAdded( Metric metric ) |
| 161 | |
{ |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | 0 | } |
| 168 | |
|
| 169 | |
public void beginDocument() |
| 170 | |
{ |
| 171 | 4 | sink.head(); |
| 172 | 4 | sink.title(); |
| 173 | 4 | sink.text( getTitle() ); |
| 174 | 4 | sink.title_(); |
| 175 | 4 | sink.head_(); |
| 176 | |
|
| 177 | 4 | sink.body(); |
| 178 | |
|
| 179 | 4 | sink.section1(); |
| 180 | 4 | sink.sectionTitle1(); |
| 181 | 4 | sink.text( getTitle() ); |
| 182 | 4 | sink.sectionTitle1_(); |
| 183 | |
|
| 184 | 4 | sink.paragraph(); |
| 185 | 4 | sink.text( bundle.getString( "report.pmd.pmdlink" ) + " " ); |
| 186 | 4 | sink.link( "http://pmd.sourceforge.net/" ); |
| 187 | 4 | sink.text( "PMD" ); |
| 188 | 4 | sink.link_(); |
| 189 | 4 | sink.text( " " + PMD.VERSION + "." ); |
| 190 | 4 | sink.paragraph_(); |
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | 4 | sink.section1_(); |
| 195 | 4 | sink.section1(); |
| 196 | 4 | sink.sectionTitle1(); |
| 197 | 4 | sink.text( bundle.getString( "report.pmd.files" ) ); |
| 198 | 4 | sink.sectionTitle1_(); |
| 199 | |
|
| 200 | |
|
| 201 | 4 | } |
| 202 | |
|
| 203 | |
public void beginFile( File file, PmdFileInfo finfo ) |
| 204 | |
{ |
| 205 | 11 | fileInfo = finfo; |
| 206 | 11 | currentFilename = StringUtils.substring( file.getAbsolutePath(), |
| 207 | |
finfo.getSourceDirectory().getAbsolutePath().length() + 1 ); |
| 208 | 11 | currentFilename = StringUtils.replace( currentFilename, "\\", "/" ); |
| 209 | 11 | fileInitialized = false; |
| 210 | 11 | } |
| 211 | |
|
| 212 | |
public void endFile( File file ) |
| 213 | |
{ |
| 214 | 11 | if ( fileInitialized ) |
| 215 | |
{ |
| 216 | 11 | processViolations(); |
| 217 | 11 | sink.table_(); |
| 218 | 11 | sink.section2_(); |
| 219 | |
} |
| 220 | 11 | } |
| 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 | |
public void endDocument() |
| 281 | |
{ |
| 282 | 4 | if ( fileCount == 0 ) |
| 283 | |
{ |
| 284 | 0 | sink.text( "PMD found no problems in your source code." ); |
| 285 | |
} |
| 286 | |
|
| 287 | 4 | sink.section1_(); |
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | 4 | sink.body_(); |
| 297 | |
|
| 298 | 4 | sink.flush(); |
| 299 | |
|
| 300 | 4 | sink.close(); |
| 301 | 4 | } |
| 302 | |
|
| 303 | |
} |