Coverage Report - org.apache.maven.plugin.pmd.CpdReportGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
CpdReportGenerator
85 %
99/116
55 %
11/20
3,5
 
 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.Iterator;
 24  
 import java.util.Map;
 25  
 import java.util.ResourceBundle;
 26  
 
 27  
 import net.sourceforge.pmd.cpd.Match;
 28  
 
 29  
 import org.apache.maven.doxia.sink.Sink;
 30  
 import org.apache.maven.project.MavenProject;
 31  
 import org.codehaus.plexus.util.StringUtils;
 32  
 
 33  
 /**
 34  
  * Class that generated the CPD report.
 35  
  *
 36  
  * @author mperham
 37  
  * @version $Id: org.apache.maven.plugin.pmd.CpdReportGenerator.html 816691 2012-05-08 15:16:42Z hboutemy $
 38  
  */
 39  
 public class CpdReportGenerator
 40  
 {
 41  
     private Sink sink;
 42  
 
 43  
     private Map fileMap;
 44  
 
 45  
     private ResourceBundle bundle;
 46  
 
 47  
     private boolean aggregate;
 48  
 
 49  
     public CpdReportGenerator( Sink sink, Map fileMap, ResourceBundle bundle, boolean aggregate )
 50  4
     {
 51  4
         this.sink = sink;
 52  4
         this.fileMap = fileMap;
 53  4
         this.bundle = bundle;
 54  4
         this.aggregate = aggregate;
 55  4
     }
 56  
 
 57  
     /**
 58  
      * Method that returns the title of the CPD Report
 59  
      *
 60  
      * @return a String that contains the title
 61  
      */
 62  
     private String getTitle()
 63  
     {
 64  8
         return bundle.getString( "report.cpd.title" );
 65  
     }
 66  
 
 67  
     /**
 68  
      * Method that generates the start of the CPD report.
 69  
      */
 70  
     public void beginDocument()
 71  
     {
 72  4
         sink.head();
 73  4
         sink.title();
 74  4
         sink.text( getTitle() );
 75  4
         sink.title_();
 76  4
         sink.head_();
 77  
 
 78  4
         sink.body();
 79  
 
 80  4
         sink.section1();
 81  4
         sink.sectionTitle1();
 82  4
         sink.text( getTitle() );
 83  4
         sink.sectionTitle1_();
 84  
 
 85  4
         sink.paragraph();
 86  4
         sink.text( bundle.getString( "report.cpd.cpdlink" ) + " " );
 87  4
         sink.link( "http://pmd.sourceforge.net/cpd.html" );
 88  4
         sink.text( "CPD" );
 89  4
         sink.link_();
 90  4
         sink.text( " " + AbstractPmdReport.getPmdVersion() + "." );
 91  4
         sink.paragraph_();
 92  
 
 93  4
         sink.section1_();
 94  
 
 95  
         // TODO overall summary
 96  
 
 97  4
         sink.section1();
 98  4
         sink.sectionTitle1();
 99  4
         sink.text( bundle.getString( "report.cpd.dupes" ) );
 100  4
         sink.sectionTitle1_();
 101  
 
 102  
         // TODO files summary
 103  4
     }
 104  
 
 105  
     /**
 106  
      * Method that generates the contents of the CPD report
 107  
      *
 108  
      * @param matches
 109  
      */
 110  
     public void generate( Iterator matches )
 111  
     {
 112  4
         beginDocument();
 113  
 
 114  4
         if ( !matches.hasNext() )
 115  
         {
 116  0
             sink.paragraph();
 117  0
             sink.text( bundle.getString( "report.cpd.noProblems" ) );
 118  0
             sink.paragraph_();
 119  
         }
 120  
 
 121  8
         while ( matches.hasNext() )
 122  
         {
 123  4
             Match match = (Match) matches.next();
 124  4
             String filename1 = match.getFirstMark().getTokenSrcID();
 125  
 
 126  4
             File file = new File( filename1 );
 127  4
             PmdFileInfo fileInfo = (PmdFileInfo) fileMap.get( file );
 128  4
             File sourceDirectory = fileInfo.getSourceDirectory();
 129  4
             String xrefLocation = fileInfo.getXrefLocation();
 130  4
             MavenProject projectFile1 = fileInfo.getProject();
 131  
 
 132  4
             filename1 = StringUtils.substring( filename1, sourceDirectory.getAbsolutePath().length() + 1 );
 133  
 
 134  4
             String filename2 = match.getSecondMark().getTokenSrcID();
 135  4
             file = new File( filename2 );
 136  4
             fileInfo = (PmdFileInfo) fileMap.get( file );
 137  4
             sourceDirectory = fileInfo.getSourceDirectory();
 138  4
             String xrefLocation2 = fileInfo.getXrefLocation();
 139  4
             filename2 = StringUtils.substring( filename2, sourceDirectory.getAbsolutePath().length() + 1 );
 140  4
             MavenProject projectFile2 = fileInfo.getProject();
 141  
 
 142  4
             String code = match.getSourceCodeSlice();
 143  4
             int line1 = match.getFirstMark().getBeginLine();
 144  4
             int line2 = match.getSecondMark().getBeginLine();
 145  
 
 146  4
             sink.table();
 147  4
             sink.tableRow();
 148  4
             sink.tableHeaderCell();
 149  4
             sink.text( bundle.getString( "report.cpd.column.file" ) );
 150  4
             sink.tableHeaderCell_();
 151  4
             if ( aggregate )
 152  
             {
 153  0
                 sink.tableHeaderCell();
 154  0
                 sink.text( bundle.getString( "report.cpd.column.project" ) );
 155  0
                 sink.tableHeaderCell_();
 156  
             }
 157  4
             sink.tableHeaderCell();
 158  4
             sink.text( bundle.getString( "report.cpd.column.line" ) );
 159  4
             sink.tableHeaderCell_();
 160  4
             sink.tableRow_();
 161  
 
 162  
             // File 1
 163  4
             sink.tableRow();
 164  4
             sink.tableCell();
 165  4
             sink.text( filename1 );
 166  4
             sink.tableCell_();
 167  4
             if ( aggregate )
 168  
             {
 169  0
                 sink.tableCell();
 170  0
                 sink.text( projectFile1.getName() );
 171  0
                 sink.tableCell_();
 172  
             }
 173  4
             sink.tableCell();
 174  
 
 175  4
             if ( xrefLocation != null )
 176  
             {
 177  0
                 sink.link( xrefLocation + "/" + filename1.replaceAll( "\\.java$", ".html" ).replace( '\\', '/' )
 178  
                            + "#" + line1 );
 179  
             }
 180  4
             sink.text( String.valueOf( line1 ) );
 181  4
             if ( xrefLocation != null )
 182  
             {
 183  0
                 sink.link_();
 184  
             }
 185  
 
 186  4
             sink.tableCell_();
 187  4
             sink.tableRow_();
 188  
 
 189  
             // File 2
 190  4
             sink.tableRow();
 191  4
             sink.tableCell();
 192  4
             sink.text( filename2 );
 193  4
             sink.tableCell_();
 194  4
             if ( aggregate )
 195  
             {
 196  0
                 sink.tableCell();
 197  0
                 sink.text( projectFile2.getName() );
 198  0
                 sink.tableCell_();
 199  
             }
 200  4
             sink.tableCell();
 201  
 
 202  4
             if ( xrefLocation != null )
 203  
             {
 204  0
                 sink.link( xrefLocation2 + "/" + filename2.replaceAll( "\\.java$", ".html" ).replace( '\\', '/' )
 205  
                            + "#" + line2 );
 206  
             }
 207  4
             sink.text( String.valueOf( line2 ) );
 208  4
             if ( xrefLocation != null )
 209  
             {
 210  0
                 sink.link_();
 211  
             }
 212  4
             sink.tableCell_();
 213  4
             sink.tableRow_();
 214  
 
 215  
             // Source snippet
 216  4
             sink.tableRow();
 217  
 
 218  
 
 219  4
             int colspan = 2;
 220  4
             if ( aggregate )
 221  
             {
 222  0
                 ++colspan;
 223  
             }
 224  
             // TODO Cleaner way to do this?
 225  4
             sink.rawText( "<td colspan='" + colspan + "'>" );
 226  4
             sink.verbatim( false );
 227  4
             sink.text( code );
 228  4
             sink.verbatim_();
 229  4
             sink.rawText( "</td>" );
 230  4
             sink.tableRow_();
 231  4
             sink.table_();
 232  4
         }
 233  
 
 234  4
         sink.section1_();
 235  4
         sink.body_();
 236  4
         sink.flush();
 237  4
         sink.close();
 238  4
     }
 239  
 }