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.Package;
030: import com.pavelvlasov.review.SimpleSourceMarker;
031:
032: /**
033: * ER-059
034: * Packages should be neither too lean nor too fat.
035: * @author Janos Czako
036: * @version $Revision: 1.3 $
037: */
038: public class FilesPerPackage extends InspectorBase implements
039: Parameterizable {
040:
041: /**
042: * Reviews the package, if it contains more files, than the allowed max.
043: *
044: * @param element the package to be reviewed.
045: */
046: public void visit(Package element) {
047: SimpleSourceMarker ssm = new SimpleSourceMarker(0, 0, (element
048: .getName().length() == 0 ? "." : element.getName()
049: .replace('.', '/'))
050: + "/", null);
051:
052: int packageSize = element.getCompilationUnits().size();
053: if (maxPackageSize != null
054: && packageSize > maxPackageSize.intValue()) {
055: context.reportViolationEx(ssm, new Object[] {
056: maxPackageSize, new Integer(packageSize) }, "MAX");
057: }
058:
059: if (minPackageSize != null
060: && packageSize < minPackageSize.intValue()) {
061: context.reportViolationEx(ssm, new Object[] {
062: minPackageSize, new Integer(packageSize) }, "MIN");
063: }
064: }
065:
066: /**
067: * Stores the setting form the configuration for the maximum allowed
068: * number of files in a package.
069: */
070: private Integer maxPackageSize;
071:
072: /**
073: * Stores the setting form the configuration for the maximum allowed
074: * number of files in a package.
075: */
076: private Integer minPackageSize;
077:
078: /**
079: * Configures rule. Reads in the values of the parameters containing the
080: * allowed number of files in a package.
081: *
082: * @param name the name of the parameter being loaded from Hammurapi configuration
083: * @param value the value of the parameter being loaded from Hammurapi configuration
084: * @exception ConfigurationException in case of a not supported parameter name, or value.
085: */
086: public boolean setParameter(String name, Object parameter)
087: throws ConfigurationException {
088: if ("max-files".equals(name)) {
089: maxPackageSize = (Integer) parameter;
090: } else if ("min-files".equals(name)) {
091: minPackageSize = (Integer) parameter;
092: } else {
093: throw new ConfigurationException("Parameter '" + name
094: + "' is not supported by " + getClass().getName());
095: }
096: return true;
097: }
098:
099: /**
100: * Gives back the preconfigured values.
101: */
102: public String getConfigInfo() {
103: StringBuffer ret = new StringBuffer(
104: "Allowed maximum files per package:\n");
105: if (maxPackageSize != null) {
106: ret.append("max-files: " + maxPackageSize + "\n");
107: }
108:
109: if (minPackageSize != null) {
110: ret.append("min-files: " + minPackageSize + "\n");
111: }
112: return ret.toString();
113: }
114: }
|