KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.File JavaDoc;
26
27 import org.hammurapi.InspectorBase;
28 import org.hammurapi.HammurapiException;
29 import org.hammurapi.inspectors.formatting.FormattingChecker;
30 import org.hammurapi.inspectors.formatting.FormattingCheckerFactory;
31
32 import com.pavelvlasov.config.ConfigurationException;
33 import com.pavelvlasov.config.Parameterizable;
34 import com.pavelvlasov.jsel.CompilationUnit;
35 import com.pavelvlasov.jsel.Method;
36 import com.pavelvlasov.jsel.Package;
37 import com.pavelvlasov.jsel.impl.Token;
38 import com.pavelvlasov.review.SimpleSourceMarker;
39
40 /**
41 * Hammurapi inspector for checking code formatting
42 *
43 * @author Jochen Skulj
44 * @version $Revision: 1.3 $
45 */

46 public class FormattingRule extends InspectorBase implements Parameterizable {
47
48  /**
49   * Parameter name for configuring the coding style
50   */

51  public final static String JavaDoc PARAMETER_STYLE = "coding-style";
52
53  /**
54   * Formatting checker instance
55   */

56  private FormattingChecker checker = null;
57
58  /**
59   * inspects the rule on method level
60   *
61   * @param aMethod
62   * method to inspect
63   * @throws HammurapiException
64   */

65  public void visit(Method aMethod) throws HammurapiException {
66    SimpleSourceMarker source = new SimpleSourceMarker(aMethod);
67    CompilationUnit cu = aMethod.getCompilationUnit();
68    Package JavaDoc pkg = cu.getPackage();
69    if (pkg.getName().length() == 0) {
70      source.setSourceURL(cu.getName());
71    } else {
72      source.setSourceURL(pkg.getName().replace('.', File.separatorChar)
73          + File.separator + cu.getName());
74    }
75    Token token = (Token) aMethod.getAst().getFirstToken();
76    while (token != null) {
77      if (checker.check(token)) {
78        source.setLine(token.getLine());
79        source.setColumn(token.getColumn());
80        context.reportViolation(source);
81      }
82      if (token == (Token) aMethod.getAst().getLastToken()) {
83        token = null;
84      } else {
85        token = (Token) token.getNextToken();
86      }
87    }
88  }
89
90  /**
91   * sets parameters for the inspector
92   *
93   * @param aName
94   * parameter name
95   * @param aParameter
96   * parameter value
97   */

98  public boolean setParameter(String JavaDoc aName, Object JavaDoc aParameter)
99      throws ConfigurationException {
100    if (aName.equals(PARAMETER_STYLE)) {
101      String JavaDoc codingStyle = (String JavaDoc) aParameter;
102      checker = FormattingCheckerFactory.create(codingStyle);
103      if (checker == null) {
104        throw new ConfigurationException("Parameter value '" + codingStyle
105            + "' does not specify a supported coding style");
106      }
107         return true;
108    } else {
109      throw new ConfigurationException("Parameter '" + aName
110          + "' is not supported by " + this.getClass().getName());
111    }
112  }
113 }
Popular Tags