Coverage Report - org.apache.maven.plugin.pmd.CpdReport
 
Classes in this File Line Coverage Branch Coverage Complexity
CpdReport
74%
43/58
55%
12/22
3,429
 
 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.ByteArrayOutputStream;
 23  
 import java.io.File;
 24  
 import java.io.FileOutputStream;
 25  
 import java.io.FileWriter;
 26  
 import java.io.IOException;
 27  
 import java.io.OutputStreamWriter;
 28  
 import java.io.UnsupportedEncodingException;
 29  
 import java.io.Writer;
 30  
 import java.nio.charset.Charset;
 31  
 import java.util.Iterator;
 32  
 import java.util.Locale;
 33  
 import java.util.Map;
 34  
 import java.util.ResourceBundle;
 35  
 
 36  
 import net.sourceforge.pmd.cpd.CPD;
 37  
 import net.sourceforge.pmd.cpd.CSVRenderer;
 38  
 import net.sourceforge.pmd.cpd.JavaLanguage;
 39  
 import net.sourceforge.pmd.cpd.Renderer;
 40  
 import net.sourceforge.pmd.cpd.XMLRenderer;
 41  
 
 42  
 import org.apache.maven.reporting.MavenReportException;
 43  
 
 44  
 /**
 45  
  * Creates a report for PMD's CPD tool.  See
 46  
  * <a href="http://pmd.sourceforge.net/cpd.html">http://pmd.sourceforge.net/cpd.html</a>
 47  
  * for more detail.
 48  
  *
 49  
  * @author Mike Perham
 50  
  * @version $Id: PmdReport.java,v 1.3 2005/02/23 00:08:53 brett Exp $
 51  
  * @goal cpd
 52  
  * @todo needs to support the multiple source roots
 53  
  */
 54  5
 public class CpdReport
 55  
     extends AbstractPmdReport
 56  
 {
 57  
     /**
 58  
      * The minimum number of tokens that need to be duplicated before it causes a violation.
 59  
      *
 60  
      * @parameter expression="${minimumTokens}" default-value="100"
 61  
      */
 62  
     private int minimumTokens;
 63  
 
 64  
     /**
 65  
      * Skip the CPD report generation.  Most useful on the command line
 66  
      * via "-Dcpd.skip=true".
 67  
      *
 68  
      * @parameter expression="${cpd.skip}" default-value="false"
 69  
      */
 70  
     private boolean skip;
 71  
 
 72  
     /**
 73  
      * The file encoding to use when reading the java source.
 74  
      *
 75  
      * @parameter
 76  
      * @since 2.3
 77  
      */
 78  
     private String sourceEncoding;
 79  
 
 80  
     /**
 81  
      * @see org.apache.maven.reporting.MavenReport#getName(java.util.Locale)
 82  
      */
 83  
     public String getName( Locale locale )
 84  
     {
 85  5
         return getBundle( locale ).getString( "report.cpd.name" );
 86  
     }
 87  
 
 88  
     /**
 89  
      * @see org.apache.maven.reporting.MavenReport#getDescription(java.util.Locale)
 90  
      */
 91  
     public String getDescription( Locale locale )
 92  
     {
 93  0
         return getBundle( locale ).getString( "report.cpd.description" );
 94  
     }
 95  
 
 96  
     /**
 97  
      * @see org.apache.maven.reporting.AbstractMavenReport#executeReport(java.util.Locale)
 98  
      */
 99  
     public void executeReport( Locale locale )
 100  
         throws MavenReportException
 101  
     {
 102  4
         if ( !skip && canGenerateReport() )
 103  
         {         
 104  3
             ClassLoader origLoader = Thread.currentThread().getContextClassLoader();
 105  
             try
 106  
             {
 107  3
                 Thread.currentThread().setContextClassLoader( this.getClass().getClassLoader() );
 108  
                 
 109  3
                 CPD cpd = new CPD( minimumTokens, new JavaLanguage() );
 110  3
                 Map files = null;
 111  
                 try
 112  
                 {
 113  3
                     if ( sourceEncoding != null )
 114  
                     {
 115  0
                         cpd.setEncoding( sourceEncoding );
 116  
 
 117  
                         // test encoding as CPD will convert exception into a RuntimeException
 118  0
                         new OutputStreamWriter( new ByteArrayOutputStream() , sourceEncoding );
 119  
                     }
 120  3
                     files = getFilesToProcess( );
 121  3
                     for ( Iterator it = files.keySet().iterator(); it.hasNext(); ) 
 122  
                     {
 123  9
                         cpd.add( (File) it.next() );
 124  
                     }
 125  
                 }
 126  0
                 catch ( UnsupportedEncodingException e )
 127  
                 {
 128  0
                     throw new MavenReportException( "Encoding '" + sourceEncoding + "' is not supported.", e );
 129  
                 }
 130  0
                 catch ( IOException e )
 131  
                 {
 132  0
                     throw new MavenReportException( e.getMessage(), e );
 133  3
                 }
 134  3
                 cpd.go();
 135  
     
 136  3
                 CpdReportGenerator gen =
 137  
                     new CpdReportGenerator( getSink(), files, getBundle( locale ), aggregate );
 138  3
                 gen.generate( cpd.getMatches() );
 139  
     
 140  3
                 if ( !isHtml() )
 141  
                 {
 142  3
                     writeNonHtml( cpd );
 143  
                 }
 144  
             }
 145  
             finally
 146  
             {
 147  3
                 Thread.currentThread().setContextClassLoader( origLoader );
 148  3
             }
 149  
 
 150  
         }
 151  3
     }
 152  
 
 153  
     void writeNonHtml( CPD cpd ) throws MavenReportException
 154  
     {
 155  4
         Renderer r = createRenderer();
 156  4
         String buffer = r.render( cpd.getMatches() );
 157  
         try
 158  
         {
 159  4
             targetDirectory.mkdirs();
 160  4
             FileOutputStream tStream = new FileOutputStream( new File( targetDirectory, "cpd." + format ) );
 161  4
             Writer writer = new OutputStreamWriter( tStream, Charset.forName( "UTF-8" ) );
 162  4
             writer.write( buffer, 0, buffer.length() );
 163  4
             writer.close();
 164  
             
 165  
             
 166  4
             File siteDir = new File( targetDirectory, "site" );
 167  4
             siteDir.mkdirs();
 168  4
             writer = new FileWriter( new File( siteDir,
 169  
                                                  "cpd." + format ) );
 170  4
             writer.write( buffer, 0, buffer.length() );
 171  4
             writer.close();
 172  
             
 173  
         }
 174  0
         catch ( IOException ioe )
 175  
         {
 176  0
             throw new MavenReportException( ioe.getMessage(), ioe );
 177  4
         }
 178  4
     }
 179  
 
 180  
     /**
 181  
      * @see org.apache.maven.reporting.MavenReport#getOutputName()
 182  
      */
 183  
     public String getOutputName()
 184  
     {
 185  8
         return "cpd";
 186  
     }
 187  
 
 188  
     private static ResourceBundle getBundle( Locale locale )
 189  
     {
 190  9
         return ResourceBundle.getBundle( "cpd-report", locale, CpdReport.class.getClassLoader() );
 191  
     }
 192  
 
 193  
     /**
 194  
      * Create and return the correct renderer for the output type.
 195  
      *
 196  
      * @return the renderer based on the configured output
 197  
      * @throws org.apache.maven.reporting.MavenReportException
 198  
      *          if no renderer found for the output type
 199  
      */
 200  
     public Renderer createRenderer()
 201  
         throws MavenReportException
 202  
     {
 203  4
         Renderer renderer = null;
 204  4
         if ( "xml".equals( format ) )
 205  
         {
 206  3
             renderer = new XMLRenderer( "UTF-8" );
 207  
         }
 208  1
         else if ( "csv".equals( format ) )
 209  
         {
 210  1
             renderer = new CSVRenderer();
 211  
         }
 212  0
         else if ( !"".equals( format ) && !"none".equals( format ) )
 213  
         {
 214  
             try
 215  
             {
 216  0
                 renderer = (Renderer) Class.forName( format ).newInstance();
 217  
             }
 218  0
             catch ( Exception e )
 219  
             {
 220  0
                 throw new MavenReportException(
 221  
                     "Can't find the custom format " + format + ": " + e.getClass().getName() );
 222  0
             }
 223  
         }
 224  
 
 225  4
         if ( renderer == null )
 226  
         {
 227  0
             throw new MavenReportException( "Can't create report with format of " + format );
 228  
         }
 229  
 
 230  4
         return renderer;
 231  
     }
 232  
 }