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