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