1 package org.codehaus.mojo.javancss;
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.util.HashSet;
24 import java.util.List;
25 import java.util.Set;
26
27 import org.apache.maven.plugin.AbstractMojo;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.MojoFailureException;
30 import org.apache.maven.plugins.annotations.Execute;
31 import org.apache.maven.plugins.annotations.LifecyclePhase;
32 import org.apache.maven.plugins.annotations.Mojo;
33 import org.apache.maven.plugins.annotations.Parameter;
34 import org.dom4j.Document;
35 import org.dom4j.DocumentException;
36 import org.dom4j.Node;
37 import org.dom4j.io.SAXReader;
38
39
40
41
42
43
44
45 @Mojo( name = "check", defaultPhase = LifecyclePhase.VERIFY )
46 @Execute( goal = "report" )
47 public class NcssViolationCheckMojo
48 extends AbstractMojo
49 {
50
51
52
53 @Parameter( defaultValue = "${project.build.sourceDirectory}", readonly = true, required = true )
54 private File sourceDirectory;
55
56
57
58
59
60 @Parameter( defaultValue = "${project.build.directory}", readonly = true, required = true )
61 private File xmlOutputDirectory;
62
63
64
65
66 @Parameter( defaultValue = "true" )
67 private boolean failOnViolation;
68
69
70
71
72
73 @Parameter( defaultValue = "javancss-raw-report.xml" )
74 private String tempFileName;
75
76
77
78
79 @Parameter( defaultValue = "10" )
80 private int ccnLimit;
81
82
83
84
85 @Parameter( defaultValue = "100" )
86 private int ncssLimit;
87
88
89
90
91
92
93
94 @Parameter( property = "ncss.skip", defaultValue = "false" )
95 private boolean skip;
96
97 public void execute()
98 throws MojoExecutionException, MojoFailureException
99 {
100 if ( skip || ( sourceDirectory == null ) || !sourceDirectory.exists() )
101 {
102 return;
103 }
104 Set<String> ccnViolation = new HashSet<String>();
105 Set<String> ncssViolation = new HashSet<String>();
106 List<Node> methodList = loadDocument().selectNodes( "//javancss/functions/function" );
107
108 for ( Node node : methodList )
109 {
110
111 int ccn = new Integer( node.valueOf( "ccn" ) ).intValue();
112 if ( ccn > ccnLimit )
113 {
114 ccnViolation.add( node.valueOf( "name" ) );
115 }
116
117 int ncss = new Integer( node.valueOf( "ncss" ) ).intValue();
118 if ( ncss > ncssLimit )
119 {
120 ncssViolation.add( node.valueOf( "name" ) );
121 }
122 }
123
124 reportViolation( "ccn", ccnViolation, ccnLimit );
125 reportViolation( "ncss", ncssViolation, ncssLimit );
126 }
127
128 private Document loadDocument()
129 throws MojoFailureException
130 {
131
132 File ncssXmlFile = new File( xmlOutputDirectory, tempFileName );
133 try
134 {
135 return new SAXReader().read( ncssXmlFile );
136 }
137 catch ( DocumentException de )
138 {
139 throw new MojoFailureException( "Can't read javancss xml output file : " + ncssXmlFile );
140 }
141 }
142
143 private void reportViolation( String statName, Set<String> violationSet, int limit )
144 throws MojoFailureException
145 {
146 getLog().debug( statName + " Violation = " + violationSet.size() );
147 if ( violationSet.size() > 0 )
148 {
149 String violationString =
150 "Your code has " + violationSet.size() + " method(s) with a " + statName + " greater than " + limit;
151 getLog().warn( violationString );
152 for ( String violation : violationSet )
153 {
154 getLog().warn( " " + violation );
155 }
156 if ( failOnViolation )
157 {
158 throw new MojoFailureException( violationString );
159 }
160 }
161 }
162 }