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.File; |
23 | |
import java.io.FileReader; |
24 | |
import java.io.IOException; |
25 | |
import java.util.List; |
26 | |
|
27 | |
import org.apache.maven.plugin.MojoExecutionException; |
28 | |
import org.apache.maven.plugin.MojoFailureException; |
29 | |
import org.apache.maven.plugin.pmd.model.CpdErrorDetail; |
30 | |
import org.apache.maven.plugin.pmd.model.CpdFile; |
31 | |
import org.apache.maven.plugin.pmd.model.Duplication; |
32 | |
import org.apache.maven.plugin.pmd.model.io.xpp3.CpdXpp3Reader; |
33 | |
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 4 | public class CpdViolationCheckMojo |
46 | |
extends AbstractPmdViolationCheckMojo<Duplication> |
47 | |
{ |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
private boolean skip; |
56 | |
|
57 | |
|
58 | |
public void execute() |
59 | |
throws MojoExecutionException, MojoFailureException |
60 | |
{ |
61 | 1 | if ( !skip ) |
62 | |
{ |
63 | 1 | executeCheck( "cpd.xml", "duplication", "CPD duplication", 10 ); |
64 | |
} |
65 | 1 | } |
66 | |
|
67 | |
|
68 | |
protected void printError( Duplication item, String severity ) |
69 | |
{ |
70 | 0 | int lines = item.getLines(); |
71 | |
|
72 | |
|
73 | 0 | StringBuffer buff = new StringBuffer( 100 ); |
74 | 0 | buff.append( "CPD " + severity + ": Found " ); |
75 | 0 | buff.append( lines ).append( " lines of duplicated code at locations:" ); |
76 | 0 | this.getLog().info( buff.toString() ); |
77 | |
|
78 | |
|
79 | 0 | for( CpdFile file : item.getFiles() ) |
80 | |
{ |
81 | 0 | buff.setLength( 0 ); |
82 | 0 | buff.append( " " ); |
83 | 0 | buff.append( file.getPath() ); |
84 | 0 | buff.append( " line " ).append( file.getLine() ); |
85 | 0 | this.getLog().info( buff.toString() ); |
86 | |
} |
87 | |
|
88 | 0 | this.getLog().debug( "CPD " + severity + ": Code Fragment " ); |
89 | 0 | this.getLog().debug( item.getCodefragment() ); |
90 | 0 | } |
91 | |
|
92 | |
|
93 | |
protected List<Duplication> getErrorDetails( File cpdFile ) |
94 | |
throws XmlPullParserException, IOException |
95 | |
{ |
96 | 1 | CpdXpp3Reader reader = new CpdXpp3Reader(); |
97 | 1 | CpdErrorDetail details = reader.read( new FileReader( cpdFile ), false ); |
98 | 1 | return details.getDuplications(); |
99 | |
} |
100 | |
|
101 | |
@Override |
102 | |
protected int getPriority( Duplication errorDetail ) |
103 | |
{ |
104 | 1 | return 0; |
105 | |
} |
106 | |
|
107 | |
@Override |
108 | |
protected ViolationDetails<Duplication> newViolationDetailsInstance() |
109 | |
{ |
110 | 1 | return new ViolationDetails<Duplication>(); |
111 | |
} |
112 | |
} |