Coverage Report - org.apache.maven.plugin.pmd.PmdReportListener
 
Classes in this File Line Coverage Branch Coverage Complexity
PmdReportListener
97%
92/95
69%
11/16
1,727
PmdReportListener$1
100%
2/2
N/A
1,727
 
 1  
 package org.apache.maven.plugin.pmd;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 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  
  * Handle events from PMD, converting them into Doxia events.
 40  
  *
 41  
  * @author Brett Porter
 42  
  * @version $Id: PmdReportListener.java,v 1.1.1.1 2005/02/17 07:16:22 brett Exp $
 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  
     // The number of erroneous files
 62  4
     private int fileCount = 0;
 63  
 
 64  
     //private List metrics = new ArrayList();
 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  
     // When dealing with multiple rulesets, the violations will get out of order
 108  
     // wrt their source line number.  We re-sort them before writing them to the report.
 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  
 //        if ( metric.getCount() != 0 )
 163  
 //        {
 164  
 //            // Skip metrics which have no data
 165  
 //            metrics.add( metric );
 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  
         // TODO overall summary
 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  
         // TODO files summary
 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  
     private void processMetrics()
 224  
     {
 225  
         if ( metrics.size() == 0 )
 226  
         {
 227  
             return;
 228  
         }
 229  
 
 230  
         sink.section1();
 231  
         sink.sectionTitle1();
 232  
         sink.text( "Metrics" );
 233  
         sink.sectionTitle1_();
 234  
 
 235  
         sink.table();
 236  
         sink.tableRow();
 237  
         sink.tableHeaderCell();
 238  
         sink.text( "Name" );
 239  
         sink.tableHeaderCell_();
 240  
         sink.tableHeaderCell();
 241  
         sink.text( "Count" );
 242  
         sink.tableHeaderCell_();
 243  
         sink.tableHeaderCell();
 244  
         sink.text( "High" );
 245  
         sink.tableHeaderCell_();
 246  
         sink.tableHeaderCell();
 247  
         sink.text( "Low" );
 248  
         sink.tableHeaderCell_();
 249  
         sink.tableHeaderCell();
 250  
         sink.text( "Average" );
 251  
         sink.tableHeaderCell_();
 252  
         sink.tableRow_();
 253  
 
 254  
         for ( Iterator iter = metrics.iterator(); iter.hasNext(); )
 255  
         {
 256  
             Metric met = (Metric) iter.next();
 257  
             sink.tableRow();
 258  
             sink.tableCell();
 259  
             sink.text( met.getMetricName() );
 260  
             sink.tableCell_();
 261  
             sink.tableCell();
 262  
             sink.text( String.valueOf( met.getCount() ) );
 263  
             sink.tableCell_();
 264  
             sink.tableCell();
 265  
             sink.text( String.valueOf( met.getHighValue() ) );
 266  
             sink.tableCell_();
 267  
             sink.tableCell();
 268  
             sink.text( String.valueOf( met.getLowValue() ) );
 269  
             sink.tableCell_();
 270  
             sink.tableCell();
 271  
             sink.text( String.valueOf( met.getAverage() ) );
 272  
             sink.tableCell_();
 273  
             sink.tableRow_();
 274  
         }
 275  
         sink.table_();
 276  
         sink.section1_();
 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  
         // The Metrics report useless with the current PMD metrics impl.
 290  
         // For instance, run the coupling ruleset and you will get a boatload
 291  
         // of excessive imports metrics, none of which is really any use.
 292  
         // TODO Determine if we are going to just ignore metrics.
 293  
 
 294  
         // processMetrics();
 295  
 
 296  4
         sink.body_();
 297  
 
 298  4
         sink.flush();
 299  
 
 300  4
         sink.close();
 301  4
     }
 302  
 
 303  
 }