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$
 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  6
     {
 51  6
         this.sink = sink;
 52  6
         this.fileMap = fileMap;
 53  6
         this.bundle = bundle;
 54  6
         this.aggregate = aggregate;
 55  6
     }
 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  12
         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  6
         sink.head();
 73  6
         sink.title();
 74  6
         sink.text( getTitle() );
 75  6
         sink.title_();
 76  6
         sink.head_();
 77  
 
 78  6
         sink.body();
 79  
 
 80  6
         sink.section1();
 81  6
         sink.sectionTitle1();
 82  6
         sink.text( getTitle() );
 83  6
         sink.sectionTitle1_();
 84  
 
 85  6
         sink.paragraph();
 86  6
         sink.text( bundle.getString( "report.cpd.cpdlink" ) + " " );
 87  6
         sink.link( "http://pmd.sourceforge.net/cpd.html" );
 88  6
         sink.text( "CPD" );
 89  6
         sink.link_();
 90  6
         sink.text( " " + AbstractPmdReport.getPmdVersion() + "." );
 91  6
         sink.paragraph_();
 92  
 
 93  6
         sink.section1_();
 94  
 
 95  
         // TODO overall summary
 96  
 
 97  6
         sink.section1();
 98  6
         sink.sectionTitle1();
 99  6
         sink.text( bundle.getString( "report.cpd.dupes" ) );
 100  6
         sink.sectionTitle1_();
 101  
 
 102  
         // TODO files summary
 103  6
     }
 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  6
         beginDocument();
 113  
 
 114  6
         if ( !matches.hasNext() )
 115  
         {
 116  0
             sink.paragraph();
 117  0
             sink.text( bundle.getString( "report.cpd.noProblems" ) );
 118  0
             sink.paragraph_();
 119  
         }
 120  
 
 121  12
         while ( matches.hasNext() )
 122  
         {
 123  6
             Match match = (Match) matches.next();
 124  6
             String filename1 = match.getFirstMark().getTokenSrcID();
 125  
 
 126  6
             File file = new File( filename1 );
 127  6
             PmdFileInfo fileInfo = (PmdFileInfo) fileMap.get( file );
 128  6
             File sourceDirectory = fileInfo.getSourceDirectory();
 129  6
             String xrefLocation = fileInfo.getXrefLocation();
 130  6
             MavenProject projectFile1 = fileInfo.getProject();
 131  
 
 132  6
             filename1 = StringUtils.substring( filename1, sourceDirectory.getAbsolutePath().length() + 1 );
 133  
 
 134  6
             String filename2 = match.getSecondMark().getTokenSrcID();
 135  6
             file = new File( filename2 );
 136  6
             fileInfo = (PmdFileInfo) fileMap.get( file );
 137  6
             sourceDirectory = fileInfo.getSourceDirectory();
 138  6
             String xrefLocation2 = fileInfo.getXrefLocation();
 139  6
             filename2 = StringUtils.substring( filename2, sourceDirectory.getAbsolutePath().length() + 1 );
 140  6
             MavenProject projectFile2 = fileInfo.getProject();
 141  
 
 142  6
             String code = match.getSourceCodeSlice();
 143  6
             int line1 = match.getFirstMark().getBeginLine();
 144  6
             int line2 = match.getSecondMark().getBeginLine();
 145  
 
 146  6
             sink.table();
 147  6
             sink.tableRow();
 148  6
             sink.tableHeaderCell();
 149  6
             sink.text( bundle.getString( "report.cpd.column.file" ) );
 150  6
             sink.tableHeaderCell_();
 151  6
             if ( aggregate )
 152  
             {
 153  0
                 sink.tableHeaderCell();
 154  0
                 sink.text( bundle.getString( "report.cpd.column.project" ) );
 155  0
                 sink.tableHeaderCell_();
 156  
             }
 157  6
             sink.tableHeaderCell();
 158  6
             sink.text( bundle.getString( "report.cpd.column.line" ) );
 159  6
             sink.tableHeaderCell_();
 160  6
             sink.tableRow_();
 161  
 
 162  
             // File 1
 163  6
             sink.tableRow();
 164  6
             sink.tableCell();
 165  6
             sink.text( filename1 );
 166  6
             sink.tableCell_();
 167  6
             if ( aggregate )
 168  
             {
 169  0
                 sink.tableCell();
 170  0
                 sink.text( projectFile1.getName() );
 171  0
                 sink.tableCell_();
 172  
             }
 173  6
             sink.tableCell();
 174  
 
 175  6
             if ( xrefLocation != null )
 176  
             {
 177  0
                 sink.link( xrefLocation + "/" + filename1.replaceAll( "\\.java$", ".html" ).replace( '\\', '/' )
 178  
                            + "#" + line1 );
 179  
             }
 180  6
             sink.text( String.valueOf( line1 ) );
 181  6
             if ( xrefLocation != null )
 182  
             {
 183  0
                 sink.link_();
 184  
             }
 185  
 
 186  6
             sink.tableCell_();
 187  6
             sink.tableRow_();
 188  
 
 189  
             // File 2
 190  6
             sink.tableRow();
 191  6
             sink.tableCell();
 192  6
             sink.text( filename2 );
 193  6
             sink.tableCell_();
 194  6
             if ( aggregate )
 195  
             {
 196  0
                 sink.tableCell();
 197  0
                 sink.text( projectFile2.getName() );
 198  0
                 sink.tableCell_();
 199  
             }
 200  6
             sink.tableCell();
 201  
 
 202  6
             if ( xrefLocation != null )
 203  
             {
 204  0
                 sink.link( xrefLocation2 + "/" + filename2.replaceAll( "\\.java$", ".html" ).replace( '\\', '/' )
 205  
                            + "#" + line2 );
 206  
             }
 207  6
             sink.text( String.valueOf( line2 ) );
 208  6
             if ( xrefLocation != null )
 209  
             {
 210  0
                 sink.link_();
 211  
             }
 212  6
             sink.tableCell_();
 213  6
             sink.tableRow_();
 214  
 
 215  
             // Source snippet
 216  6
             sink.tableRow();
 217  
 
 218  
 
 219  6
             int colspan = 2;
 220  6
             if ( aggregate )
 221  
             {
 222  0
                 ++colspan;
 223  
             }
 224  
             // TODO Cleaner way to do this?
 225  6
             sink.rawText( "<td colspan='" + colspan + "'>" );
 226  6
             sink.verbatim( false );
 227  6
             sink.text( code );
 228  6
             sink.verbatim_();
 229  6
             sink.rawText( "</td>" );
 230  6
             sink.tableRow_();
 231  6
             sink.table_();
 232  6
         }
 233  
 
 234  6
         sink.section1_();
 235  6
         sink.body_();
 236  6
         sink.flush();
 237  6
         sink.close();
 238  6
     }
 239  
 }