Coverage Report - org.apache.maven.plugin.pmd.PmdReportListener
 
Classes in this File Line Coverage Branch Coverage Complexity
PmdReportListener
94 %
93/98
68 %
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.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  
  * Handle events from PMD, converting them into Doxia events.
 39  
  *
 40  
  * @author Brett Porter
 41  
  * @version $Id: org.apache.maven.plugin.pmd.PmdReportListener.html 816691 2012-05-08 15:16:42Z hboutemy $
 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  5
     private List violations = new ArrayList();
 57  
 
 58  
     private boolean aggregate;
 59  
 
 60  
     // The number of erroneous files
 61  5
     private int fileCount = 0;
 62  
 
 63  
     //private List metrics = new ArrayList();
 64  
 
 65  
     public PmdReportListener( Sink sink, ResourceBundle bundle, boolean aggregate )
 66  5
     {
 67  5
         this.sink = sink;
 68  5
         this.bundle = bundle;
 69  5
         this.aggregate = aggregate;
 70  5
     }
 71  
 
 72  
     private String getTitle()
 73  
     {
 74  8
         return bundle.getString( "report.pmd.title" );
 75  
     }
 76  
 
 77  
     /** {@inheritDoc} */
 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  11
         {
 114  
             /** {@inheritDoc} */
 115  
             public int compare( Object o1, Object o2 )
 116  
             {
 117  13
                 return ( (IRuleViolation) o1 ).getBeginLine()
 118  
                     - ( (IRuleViolation) o2 ).getBeginLine();
 119  
             }
 120  
         } );
 121  
 
 122  11
         for ( Iterator it = violations.iterator(); it.hasNext(); )
 123  
         {
 124  23
             IRuleViolation ruleViolation = (IRuleViolation) it.next();
 125  
 
 126  23
             sink.tableRow();
 127  23
             sink.tableCell();
 128  23
             sink.text( ruleViolation.getDescription() );
 129  23
             sink.tableCell_();
 130  23
             sink.tableCell();
 131  
 
 132  23
             int beginLine = ruleViolation.getBeginLine();
 133  23
             outputLineLink( beginLine );
 134  23
             int endLine = ruleViolation.getEndLine();
 135  23
             if ( endLine != beginLine )
 136  
             {
 137  9
                 sink.text( " - " );
 138  9
                 outputLineLink( endLine );
 139  
             }
 140  
 
 141  23
             sink.tableCell_();
 142  23
             sink.tableRow_();
 143  23
         }
 144  11
         violations.clear();
 145  11
     }
 146  
 
 147  
     private void outputLineLink( int line )
 148  
     {
 149  32
         String xrefLocation = fileInfo.getXrefLocation();
 150  32
         if ( xrefLocation != null )
 151  
         {
 152  32
             sink.link( xrefLocation + "/" + currentFilename.replaceAll( "\\.java$", ".html" ) + "#" + line );
 153  
         }
 154  32
         sink.text( String.valueOf( line ) );
 155  32
         if ( xrefLocation != null )
 156  
         {
 157  32
             sink.link_();
 158  
         }
 159  32
     }
 160  
 
 161  
     /** {@inheritDoc} */
 162  
     public void metricAdded( Metric metric )
 163  
     {
 164  
 //        if ( metric.getCount() != 0 )
 165  
 //        {
 166  
 //            // Skip metrics which have no data
 167  
 //            metrics.add( metric );
 168  
 //        }
 169  0
     }
 170  
 
 171  
     public void beginDocument()
 172  
     {
 173  5
         sink.head();
 174  4
         sink.title();
 175  4
         sink.text( getTitle() );
 176  4
         sink.title_();
 177  4
         sink.head_();
 178  
 
 179  4
         sink.body();
 180  
 
 181  4
         sink.section1();
 182  4
         sink.sectionTitle1();
 183  4
         sink.text( getTitle() );
 184  4
         sink.sectionTitle1_();
 185  
 
 186  4
         sink.paragraph();
 187  4
         sink.text( bundle.getString( "report.pmd.pmdlink" ) + " " );
 188  4
         sink.link( "http://pmd.sourceforge.net/" );
 189  4
         sink.text( "PMD" );
 190  4
         sink.link_();
 191  4
         sink.text( " " + AbstractPmdReport.getPmdVersion() + "." );
 192  4
         sink.paragraph_();
 193  
 
 194  4
         sink.section1_();
 195  
 
 196  
         // TODO overall summary
 197  
 
 198  4
         sink.section1();
 199  4
         sink.sectionTitle1();
 200  4
         sink.text( bundle.getString( "report.pmd.files" ) );
 201  4
         sink.sectionTitle1_();
 202  
 
 203  
         // TODO files summary
 204  4
     }
 205  
 
 206  
     public void beginFile( File file, PmdFileInfo finfo )
 207  
     {
 208  11
         fileInfo = finfo;
 209  11
         currentFilename = StringUtils.substring( file.getAbsolutePath(),
 210  
                                                  finfo.getSourceDirectory().getAbsolutePath().length() + 1 );
 211  11
         currentFilename = StringUtils.replace( currentFilename, "\\", "/" );
 212  11
         fileInitialized = false;
 213  11
     }
 214  
 
 215  
     public void endFile( File file )
 216  
     {
 217  11
         if ( fileInitialized )
 218  
         {
 219  11
             processViolations();
 220  11
             sink.table_();
 221  11
             sink.section2_();
 222  
         }
 223  11
     }
 224  
 
 225  
     /*
 226  
     private void processMetrics()
 227  
     {
 228  
         if ( metrics.size() == 0 )
 229  
         {
 230  
             return;
 231  
         }
 232  
 
 233  
         sink.section1();
 234  
         sink.sectionTitle1();
 235  
         sink.text( "Metrics" );
 236  
         sink.sectionTitle1_();
 237  
 
 238  
         sink.table();
 239  
         sink.tableRow();
 240  
         sink.tableHeaderCell();
 241  
         sink.text( "Name" );
 242  
         sink.tableHeaderCell_();
 243  
         sink.tableHeaderCell();
 244  
         sink.text( "Count" );
 245  
         sink.tableHeaderCell_();
 246  
         sink.tableHeaderCell();
 247  
         sink.text( "High" );
 248  
         sink.tableHeaderCell_();
 249  
         sink.tableHeaderCell();
 250  
         sink.text( "Low" );
 251  
         sink.tableHeaderCell_();
 252  
         sink.tableHeaderCell();
 253  
         sink.text( "Average" );
 254  
         sink.tableHeaderCell_();
 255  
         sink.tableRow_();
 256  
 
 257  
         for ( Iterator iter = metrics.iterator(); iter.hasNext(); )
 258  
         {
 259  
             Metric met = (Metric) iter.next();
 260  
             sink.tableRow();
 261  
             sink.tableCell();
 262  
             sink.text( met.getMetricName() );
 263  
             sink.tableCell_();
 264  
             sink.tableCell();
 265  
             sink.text( String.valueOf( met.getCount() ) );
 266  
             sink.tableCell_();
 267  
             sink.tableCell();
 268  
             sink.text( String.valueOf( met.getHighValue() ) );
 269  
             sink.tableCell_();
 270  
             sink.tableCell();
 271  
             sink.text( String.valueOf( met.getLowValue() ) );
 272  
             sink.tableCell_();
 273  
             sink.tableCell();
 274  
             sink.text( String.valueOf( met.getAverage() ) );
 275  
             sink.tableCell_();
 276  
             sink.tableRow_();
 277  
         }
 278  
         sink.table_();
 279  
         sink.section1_();
 280  
     }
 281  
     */
 282  
 
 283  
     public void endDocument()
 284  
     {
 285  4
         if ( fileCount == 0 )
 286  
         {
 287  0
             sink.paragraph();
 288  0
             sink.text( bundle.getString( "report.pmd.noProblems" ) );
 289  0
             sink.paragraph_();
 290  
         }
 291  
 
 292  4
         sink.section1_();
 293  
 
 294  
         // The Metrics report useless with the current PMD metrics impl.
 295  
         // For instance, run the coupling ruleset and you will get a boatload
 296  
         // of excessive imports metrics, none of which is really any use.
 297  
         // TODO Determine if we are going to just ignore metrics.
 298  
 
 299  
         // processMetrics();
 300  
 
 301  4
         sink.body_();
 302  
 
 303  4
         sink.flush();
 304  
 
 305  4
         sink.close();
 306  4
     }
 307  
 }