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.IOException;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.apache.maven.plugin.MojoFailureException;
28 import org.codehaus.plexus.util.xml.pull.XmlPullParser;
29 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
30
31
32
33
34
35
36
37
38
39
40 public class CpdViolationCheckMojo
41 extends AbstractPmdViolationCheckMojo
42 {
43
44
45
46
47
48
49
50 private boolean skip;
51
52
53 public void execute()
54 throws MojoExecutionException, MojoFailureException
55 {
56 if ( !skip )
57 {
58 executeCheck( "cpd.xml", "duplication", "CPD duplication", 10 );
59 }
60 }
61
62
63 protected void printError( Map item, String severity )
64 {
65 String lines = (String) item.get( "lines" );
66
67
68 StringBuffer buff = new StringBuffer( 100 );
69 buff.append( "CPD " + severity + ": Found " );
70 buff.append( lines ).append( " lines of duplicated code at locations:" );
71 this.getLog().info( buff.toString() );
72
73 buff.setLength( 0 );
74 buff.append( " " );
75 Map file = (Map) item.get( "file" );
76 buff.append( file.get( "path" ) );
77 buff.append( " line " ).append( file.get( "line" ) );
78 this.getLog().info( buff.toString() );
79
80 buff.setLength( 0 );
81 buff.append( " " );
82 file = (Map) item.get( "file1" );
83 buff.append( file.get( "path" ) );
84 buff.append( " line " ).append( file.get( "line" ) );
85 this.getLog().info( buff.toString() );
86
87 Map codefrag = (Map) item.get( "codefragment" );
88 String codefragstr = (String) codefrag.get( "text" );
89 this.getLog().debug( "CPD " + severity + ": Code Fragment " );
90 this.getLog().debug( codefragstr );
91 }
92
93
94 protected Map getErrorDetails( XmlPullParser xpp )
95 throws XmlPullParserException, IOException
96 {
97 int index = 0;
98 int attributeCount = 0;
99 HashMap msgs = new HashMap();
100
101 attributeCount = xpp.getAttributeCount();
102 while ( index < attributeCount )
103 {
104 msgs.put( xpp.getAttributeName( index ), xpp.getAttributeValue( index ) );
105
106 index++;
107 }
108
109 int tp = xpp.next();
110 while ( tp != XmlPullParser.END_TAG )
111 {
112
113 switch ( tp )
114 {
115 case XmlPullParser.TEXT:
116 msgs.put( "text", xpp.getText().trim() );
117 break;
118 case XmlPullParser.START_TAG:
119 {
120 String nm = xpp.getName();
121 if ( msgs.containsKey( nm ) )
122 {
123 int cnt = 1;
124 while ( msgs.containsKey( nm + cnt ) )
125 {
126 ++cnt;
127 }
128 nm = nm + cnt;
129 }
130 msgs.put( nm, getErrorDetails( xpp ) );
131 break;
132 }
133 default:
134 }
135 tp = xpp.next();
136 }
137 return msgs;
138 }
139 }