KickJava   Java API By Example, From Geeks To Geeks.

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


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.config.ConfigurationException;
28 import com.pavelvlasov.config.Parameterizable;
29 import com.pavelvlasov.jsel.impl.Token;
30
31 /**
32  * ER-029
33  * Line is too long
34  * @author Pavel Vlasov
35  * @version $Revision: 1.8 $
36  */

37 public class LineLengthRule extends InspectorBase implements Parameterizable {
38     /**
39      * Stores the setting form the configuration for the allowed
40      * maximal line length.
41      */

42     private Integer JavaDoc maxLineLength;
43     
44     /**
45      * Reviews the actual line, if it is longer than the allowed maximum.
46      *
47      * @param element the element to be reviewed.
48      */

49     public void visit(Token element) {
50         if (maxLineLength == null || lastPosition(element) < maxLineLength.intValue()) {
51             return;
52         }
53         
54         Token nextToken = (Token) element.getNextToken();
55         if (nextToken==null || (nextToken != null && element.getLine() != nextToken.getLine())) {
56             Token violationToken=element;
57             String JavaDoc vtt=violationToken.getTypeName();
58             while ("WS".equals(vtt) || "NEW_LINE".equals(vtt)) {
59                 violationToken=violationToken.getPrevNonWhiteSpaceToken();
60                 if (violationToken==null || violationToken.getLine()!=element.getLine() || lastPosition(violationToken)<=maxLineLength.intValue()) {
61                     return;
62                 }
63                 vtt=violationToken.getTypeName();
64             }
65             
66             if ("ML_COMMENT".equals(element.getTypeName())) {
67                 // TODO break ML_COMMENT into lines and check their length.
68
} else {
69                 context.reportViolation(violationToken);
70             }
71         }
72     }
73     
74     private int lastPosition(Token element) {
75         return element.getColumn()+(element.getText()==null ? 0 : element.getText().length());
76     }
77
78     /**
79      * Configures rule. Reads in the values of the parameter line-max-length.
80      *
81      * @param name the name of the parameter being loaded from Hammurapi configuration
82      * @param value the value of the parameter being loaded from Hammurapi configuration
83      * @exception ConfigurationException in case of a not supported parameter name, or value.
84      */

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

97     public String JavaDoc getConfigInfo() {
98         StringBuffer JavaDoc ret=new StringBuffer JavaDoc("Allowed maximum line length:\n");
99         ret.append("line-max-length: " + maxLineLength + "\n");
100         return ret.toString();
101     }
102 }
103
Popular Tags