1 23 package org.hammurapi.inspectors; 24 25 import java.io.File ; 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 46 public class FormattingRule extends InspectorBase implements Parameterizable { 47 48 51 public final static String PARAMETER_STYLE = "coding-style"; 52 53 56 private FormattingChecker checker = null; 57 58 65 public void visit(Method aMethod) throws HammurapiException { 66 SimpleSourceMarker source = new SimpleSourceMarker(aMethod); 67 CompilationUnit cu = aMethod.getCompilationUnit(); 68 Package 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 98 public boolean setParameter(String aName, Object aParameter) 99 throws ConfigurationException { 100 if (aName.equals(PARAMETER_STYLE)) { 101 String codingStyle = (String ) 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 |