View Javadoc
1   package org.codehaus.mojo.javancss;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Check the build if for any Method with a ccn greater than a limit in the source code. Fails the build if told so.
41   *
42   * @author <a href="jeanlaurentATgmail.com">Jean-Laurent de Morlhon</a>
43   * @version $Id$
44   */
45  @Mojo( name = "check", defaultPhase = LifecyclePhase.VERIFY )
46  @Execute( goal = "report" )
47  public class NcssViolationCheckMojo
48      extends AbstractMojo
49  {
50      /**
51       * Specifies the location of the source files to be used.
52       */
53      @Parameter( defaultValue = "${project.build.sourceDirectory}", readonly = true, required = true )
54      private File sourceDirectory;
55  
56      /**
57       * Specifies the directory where the XML report will be generated.
58       */
59      // FIXME : same variable, same value in NCSSReportMojo...
60      @Parameter( defaultValue = "${project.build.directory}", readonly = true, required = true )
61      private File xmlOutputDirectory;
62  
63      /**
64       * Whether to fail the build if the validation check fails.
65       */
66      @Parameter( defaultValue = "true" )
67      private boolean failOnViolation;
68  
69      /**
70       * Name of the file holding the xml file generated by JavaNCSS
71       */
72      // FIXME : same variable, same value in NCSSReportMojo...
73      @Parameter( defaultValue = "javancss-raw-report.xml" )
74      private String tempFileName;
75  
76      /**
77       * CCN Limit, any code with a ccn greater than this number will generate a violation
78       */
79      @Parameter( defaultValue = "10" )
80      private int ccnLimit;
81  
82      /**
83       * ncss Limit, any code with a ncss greater than this number will generate a violation
84       */
85      @Parameter( defaultValue = "100" )
86      private int ncssLimit;
87  
88      /**
89       * Skip entire check.
90       *
91       * @since 2.1
92       */
93      // FIXME : same variable, same value in NCSSReportMojo...
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         // Count ccn & ncss violations
108         for ( Node node : methodList )
109         {
110             // count ccn violation
111             int ccn = new Integer( node.valueOf( "ccn" ) ).intValue();
112             if ( ccn > ccnLimit )
113             {
114                 ccnViolation.add( node.valueOf( "name" ) );
115             }
116             // count ncss violation
117             int ncss = new Integer( node.valueOf( "ncss" ) ).intValue();
118             if ( ncss > ncssLimit )
119             {
120                 ncssViolation.add( node.valueOf( "name" ) );
121             }
122         }
123         // crappy....
124         reportViolation( "ccn", ccnViolation, ccnLimit );
125         reportViolation( "ncss", ncssViolation, ncssLimit );
126     }
127 
128     private Document loadDocument()
129         throws MojoFailureException
130     {
131         // FIXME: Building of File is strangely equivalent to method buildOutputFileName of NcssReportGenerator class...
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 }