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.ArrayList;
26 import java.util.List;
27
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.MojoFailureException;
30 import org.apache.maven.plugin.pmd.model.PmdErrorDetail;
31 import org.apache.maven.plugin.pmd.model.PmdFile;
32 import org.apache.maven.plugin.pmd.model.Violation;
33 import org.apache.maven.plugin.pmd.model.io.xpp3.PmdXpp3Reader;
34 import org.codehaus.plexus.util.StringUtils;
35 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
36
37
38
39
40
41
42
43
44
45
46
47 public class PmdViolationCheckMojo
48 extends AbstractPmdViolationCheckMojo<Violation>
49 {
50
51
52
53
54
55
56
57
58
59 private int failurePriority;
60
61
62
63
64
65
66
67 private boolean skip;
68
69
70 public void execute()
71 throws MojoExecutionException, MojoFailureException
72 {
73 if ( !skip )
74 {
75 executeCheck( "pmd.xml", "violation", "PMD violation", failurePriority );
76 }
77 }
78
79
80 protected void printError( Violation item, String severity )
81 {
82
83 StringBuffer buff = new StringBuffer( 100 );
84 buff.append( "PMD " + severity + ": " );
85 if ( item.getViolationClass() != null )
86 {
87 if ( item.getViolationPackage() != null )
88 {
89 buff.append( item.getViolationPackage() );
90 buff.append( "." );
91 }
92 buff.append( item.getViolationClass() );
93 }
94 else
95 {
96 buff.append( item.getFileName() );
97 }
98 buff.append( ":" );
99 buff.append( item.getBeginline() );
100 buff.append( " Rule:" ).append( item.getRule() );
101 buff.append( " Priority:" ).append( item.getPriority() );
102 buff.append( " " ).append( item.getText() ).append( "." );
103
104 this.getLog().info( buff.toString() );
105 }
106
107 @Override
108 protected List<Violation> getErrorDetails( File pmdFile )
109 throws XmlPullParserException, IOException
110 {
111 PmdXpp3Reader reader = new PmdXpp3Reader();
112 PmdErrorDetail details = reader.read( new FileReader( pmdFile ), false );
113
114 List<Violation> violations = new ArrayList<Violation>();
115 for( PmdFile file : details.getFiles() )
116 {
117 String fullPath = file.getName();
118
119 for ( Violation violation : file.getViolations() )
120 {
121 violation.setFileName( getFilename( fullPath, violation.getViolationPackage() ) );
122 violations.add( violation );
123 }
124 }
125 return violations;
126 }
127
128 @Override
129 protected int getPriority( Violation errorDetail )
130 {
131 return errorDetail.getPriority();
132 }
133
134 @Override
135 protected ViolationDetails<Violation> newViolationDetailsInstance()
136 {
137 return new ViolationDetails<Violation>();
138 }
139
140 private String getFilename( String fullpath, String pkg )
141 {
142 int index = fullpath.lastIndexOf( File.separatorChar );
143
144 while ( StringUtils.isNotEmpty( pkg ) )
145 {
146 index = fullpath.substring( 0, index ).lastIndexOf( File.separatorChar );
147
148 int dot = pkg.indexOf( '.' );
149
150 if ( dot < 0 )
151 {
152 break;
153 }
154 pkg = pkg.substring( dot + 1 );
155 }
156
157 return fullpath.substring( index + 1 );
158 }
159 }