| 1 | |
package org.apache.maven.plugin.pmd; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 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 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | 5 | public class CpdReport |
| 55 | |
extends AbstractPmdReport |
| 56 | |
{ |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
private int minimumTokens; |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
private boolean skip; |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
private String sourceEncoding; |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
public String getName( Locale locale ) |
| 84 | |
{ |
| 85 | 5 | return getBundle( locale ).getString( "report.cpd.name" ); |
| 86 | |
} |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
public String getDescription( Locale locale ) |
| 92 | |
{ |
| 93 | 0 | return getBundle( locale ).getString( "report.cpd.description" ); |
| 94 | |
} |
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 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 | |
} |