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.antlr.AST;
028: import com.pavelvlasov.antlr.Token;
029: import com.pavelvlasov.config.ConfigurationException;
030: import com.pavelvlasov.config.Parameterizable;
031: import com.pavelvlasov.jsel.CompilationUnit;
032:
033: /**
034: * ER-019
035: * Source file is too long
036: * @author Janos Czako
037: * @version $Revision: 1.8 $
038: */
039: public class MaxLinesInFileRule extends InspectorBase implements
040: Parameterizable {
041:
042: /**
043: * Reviews the compilation unit, if it has more lines than the
044: * configured maximum value.
045: *
046: * @param element the unit to be reviewed.
047: */
048: public void visit(CompilationUnit element) {
049: AST ast = element.getAst();
050: if (ast == null) {
051: context.addMetric(element, "File length", 0);
052: context.reportViolation(element, "Empty file");
053: } else {
054: Token token = ast.getToken();
055: while (token != null && token.getNextToken() != null) {
056: token = token.getNextToken();
057: }
058:
059: int lastLineNbr = token == null ? 0 : token.getLine() - 1;
060:
061: context.addMetric(element, "File length", lastLineNbr);
062:
063: if (maxLine != null && lastLineNbr > maxLine.intValue()) {
064: context.reportViolation(element);
065: }
066: }
067: }
068:
069: /**
070: * Stores the setting form the configuration for the maximum allowed
071: * linenumber inside of a file.
072: */
073: private Integer maxLine;
074:
075: /**
076: * Configures the rule. Reads in the values of the parameter maximum-line.
077: *
078: * @param name the name of the parameter being loaded from Hammurapi configuration
079: * @param value the value of the parameter being loaded from Hammurapi configuration
080: * @exception ConfigurationException in case of a not supported parameter
081: */
082: public boolean setParameter(String name, Object parameter)
083: throws ConfigurationException {
084: if ("max-lines".equals(name)) {
085: maxLine = (Integer) parameter;
086: } else {
087: throw new ConfigurationException("Parameter '" + name
088: + "' is not supported by " + getClass().getName());
089: }
090: return true;
091: }
092:
093: /**
094: * Gives back the preconfigured values.
095: */
096: public String getConfigInfo() {
097: if (maxLine == null) {
098: return super .getConfigInfo();
099: } else {
100: StringBuffer ret = new StringBuffer(
101: "Allowed maximum file length:\n");
102: ret.append("max-lines: " + maxLine + "\n");
103: return ret.toString();
104: }
105: }
106: }
|