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 java.io.File;
026:
027: import org.hammurapi.InspectorBase;
028: import org.hammurapi.HammurapiException;
029: import org.hammurapi.inspectors.formatting.FormattingChecker;
030: import org.hammurapi.inspectors.formatting.FormattingCheckerFactory;
031:
032: import com.pavelvlasov.config.ConfigurationException;
033: import com.pavelvlasov.config.Parameterizable;
034: import com.pavelvlasov.jsel.CompilationUnit;
035: import com.pavelvlasov.jsel.Method;
036: import com.pavelvlasov.jsel.Package;
037: import com.pavelvlasov.jsel.impl.Token;
038: import com.pavelvlasov.review.SimpleSourceMarker;
039:
040: /**
041: * Hammurapi inspector for checking code formatting
042: *
043: * @author Jochen Skulj
044: * @version $Revision: 1.3 $
045: */
046: public class FormattingRule extends InspectorBase implements
047: Parameterizable {
048:
049: /**
050: * Parameter name for configuring the coding style
051: */
052: public final static String PARAMETER_STYLE = "coding-style";
053:
054: /**
055: * Formatting checker instance
056: */
057: private FormattingChecker checker = null;
058:
059: /**
060: * inspects the rule on method level
061: *
062: * @param aMethod
063: * method to inspect
064: * @throws HammurapiException
065: */
066: public void visit(Method aMethod) throws HammurapiException {
067: SimpleSourceMarker source = new SimpleSourceMarker(aMethod);
068: CompilationUnit cu = aMethod.getCompilationUnit();
069: Package pkg = cu.getPackage();
070: if (pkg.getName().length() == 0) {
071: source.setSourceURL(cu.getName());
072: } else {
073: source.setSourceURL(pkg.getName().replace('.',
074: File.separatorChar)
075: + File.separator + cu.getName());
076: }
077: Token token = (Token) aMethod.getAst().getFirstToken();
078: while (token != null) {
079: if (checker.check(token)) {
080: source.setLine(token.getLine());
081: source.setColumn(token.getColumn());
082: context.reportViolation(source);
083: }
084: if (token == (Token) aMethod.getAst().getLastToken()) {
085: token = null;
086: } else {
087: token = (Token) token.getNextToken();
088: }
089: }
090: }
091:
092: /**
093: * sets parameters for the inspector
094: *
095: * @param aName
096: * parameter name
097: * @param aParameter
098: * parameter value
099: */
100: public boolean setParameter(String aName, Object aParameter)
101: throws ConfigurationException {
102: if (aName.equals(PARAMETER_STYLE)) {
103: String codingStyle = (String) aParameter;
104: checker = FormattingCheckerFactory.create(codingStyle);
105: if (checker == null) {
106: throw new ConfigurationException("Parameter value '"
107: + codingStyle
108: + "' does not specify a supported coding style");
109: }
110: return true;
111: } else {
112: throw new ConfigurationException("Parameter '" + aName
113: + "' is not supported by "
114: + this.getClass().getName());
115: }
116: }
117: }
|