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 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 if ( !skip )
62 {
63 executeCheck( "cpd.xml", "duplication", "CPD duplication", 10 );
64 }
65 }
66
67
68 protected void printError( Duplication item, String severity )
69 {
70 int lines = item.getLines();
71
72
73 StringBuffer buff = new StringBuffer( 100 );
74 buff.append( "CPD " + severity + ": Found " );
75 buff.append( lines ).append( " lines of duplicated code at locations:" );
76 this.getLog().info( buff.toString() );
77
78
79 for( CpdFile file : item.getFiles() )
80 {
81 buff.setLength( 0 );
82 buff.append( " " );
83 buff.append( file.getPath() );
84 buff.append( " line " ).append( file.getLine() );
85 this.getLog().info( buff.toString() );
86 }
87
88 this.getLog().debug( "CPD " + severity + ": Code Fragment " );
89 this.getLog().debug( item.getCodefragment() );
90 }
91
92
93 protected List<Duplication> getErrorDetails( File cpdFile )
94 throws XmlPullParserException, IOException
95 {
96 CpdXpp3Reader reader = new CpdXpp3Reader();
97 CpdErrorDetail details = reader.read( new FileReader( cpdFile ), false );
98 return details.getDuplications();
99 }
100
101 @Override
102 protected int getPriority( Duplication errorDetail )
103 {
104 return 0;
105 }
106
107 @Override
108 protected ViolationDetails<Duplication> newViolationDetailsInstance()
109 {
110 return new ViolationDetails<Duplication>();
111 }
112 }