KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > inspectors > MaxLinesInFileRule


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Hammurapi Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * URL: http://www.hammurapi.org
21  * e-Mail: support@hammurapi.biz
22  */

23 package org.hammurapi.inspectors;
24
25 import org.hammurapi.InspectorBase;
26
27 import com.pavelvlasov.antlr.AST;
28 import com.pavelvlasov.antlr.Token;
29 import com.pavelvlasov.config.ConfigurationException;
30 import com.pavelvlasov.config.Parameterizable;
31 import com.pavelvlasov.jsel.CompilationUnit;
32
33
34 /**
35  * ER-019
36  * Source file is too long
37  * @author Janos Czako
38  * @version $Revision: 1.8 $
39  */

40 public class MaxLinesInFileRule
41     extends InspectorBase implements Parameterizable {
42     
43     /**
44      * Reviews the compilation unit, if it has more lines than the
45      * configured maximum value.
46      *
47      * @param element the unit to be reviewed.
48      */

49     public void visit(CompilationUnit element) {
50         AST ast = element.getAst();
51         if (ast==null) {
52             context.addMetric(element, "File length", 0);
53             context.reportViolation(element, "Empty file");
54         } else {
55             Token token = ast.getToken();
56             while (token!=null && token.getNextToken()!=null) {
57                 token = token.getNextToken();
58             }
59                 
60             int lastLineNbr = token==null ? 0 : token.getLine()-1;
61             
62             context.addMetric(element, "File length", lastLineNbr);
63             
64             if (maxLine!=null && lastLineNbr>maxLine.intValue()) {
65                 context.reportViolation(element);
66             }
67         }
68     }
69     
70     /**
71      * Stores the setting form the configuration for the maximum allowed
72      * linenumber inside of a file.
73      */

74     private Integer JavaDoc maxLine;
75     
76     /**
77      * Configures the rule. Reads in the values of the parameter maximum-line.
78      *
79      * @param name the name of the parameter being loaded from Hammurapi configuration
80      * @param value the value of the parameter being loaded from Hammurapi configuration
81      * @exception ConfigurationException in case of a not supported parameter
82      */

83     public boolean setParameter(String JavaDoc name, Object JavaDoc parameter) throws ConfigurationException {
84         if ("max-lines".equals(name)) {
85             maxLine = (Integer JavaDoc) parameter;
86         } else {
87             throw new ConfigurationException("Parameter '"+name+"' is not supported by "+getClass().getName());
88         }
89         return true;
90     }
91
92     /**
93      * Gives back the preconfigured values.
94      */

95     public String JavaDoc getConfigInfo() {
96         if (maxLine==null) {
97             return super.getConfigInfo();
98         } else {
99             StringBuffer JavaDoc ret=new StringBuffer JavaDoc("Allowed maximum file length:\n");
100             ret.append("max-lines: " + maxLine + "\n");
101             return ret.toString();
102         }
103     }
104 }
105
Popular Tags