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