001: /*
002: * Hammurapi
003: * Automated Java code review system.
004: * Copyright (C) 2004 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.org
021: * e-Mail: support@hammurapi.biz
022: */
023: package org.hammurapi.inspectors;
024:
025: import org.hammurapi.InspectorBase;
026:
027: import com.pavelvlasov.config.ConfigurationException;
028: import com.pavelvlasov.config.Parameterizable;
029: import com.pavelvlasov.jsel.impl.Token;
030:
031: /**
032: * ER-029
033: * Line is too long
034: * @author Pavel Vlasov
035: * @version $Revision: 1.8 $
036: */
037: public class LineLengthRule extends InspectorBase implements
038: Parameterizable {
039: /**
040: * Stores the setting form the configuration for the allowed
041: * maximal line length.
042: */
043: private Integer maxLineLength;
044:
045: /**
046: * Reviews the actual line, if it is longer than the allowed maximum.
047: *
048: * @param element the element to be reviewed.
049: */
050: public void visit(Token element) {
051: if (maxLineLength == null
052: || lastPosition(element) < maxLineLength.intValue()) {
053: return;
054: }
055:
056: Token nextToken = (Token) element.getNextToken();
057: if (nextToken == null
058: || (nextToken != null && element.getLine() != nextToken
059: .getLine())) {
060: Token violationToken = element;
061: String vtt = violationToken.getTypeName();
062: while ("WS".equals(vtt) || "NEW_LINE".equals(vtt)) {
063: violationToken = violationToken
064: .getPrevNonWhiteSpaceToken();
065: if (violationToken == null
066: || violationToken.getLine() != element
067: .getLine()
068: || lastPosition(violationToken) <= maxLineLength
069: .intValue()) {
070: return;
071: }
072: vtt = violationToken.getTypeName();
073: }
074:
075: if ("ML_COMMENT".equals(element.getTypeName())) {
076: // TODO break ML_COMMENT into lines and check their length.
077: } else {
078: context.reportViolation(violationToken);
079: }
080: }
081: }
082:
083: private int lastPosition(Token element) {
084: return element.getColumn()
085: + (element.getText() == null ? 0 : element.getText()
086: .length());
087: }
088:
089: /**
090: * Configures rule. Reads in the values of the parameter line-max-length.
091: *
092: * @param name the name of the parameter being loaded from Hammurapi configuration
093: * @param value the value of the parameter being loaded from Hammurapi configuration
094: * @exception ConfigurationException in case of a not supported parameter name, or value.
095: */
096: public boolean setParameter(String name, Object parameter)
097: throws ConfigurationException {
098: if ("line-max-length".equals(name)) {
099: maxLineLength = (Integer) parameter;
100: return true;
101: } else {
102: throw new ConfigurationException("Parameter '" + name
103: + "' is not supported by " + getClass().getName());
104: }
105: }
106:
107: /**
108: * Gives back the preconfigured values.
109: */
110: public String getConfigInfo() {
111: StringBuffer ret = new StringBuffer(
112: "Allowed maximum line length:\n");
113: ret.append("line-max-length: " + maxLineLength + "\n");
114: return ret.toString();
115: }
116: }
|