1 23 package org.hammurapi.inspectors; 24 25 import org.hammurapi.InspectorBase; 26 27 import com.pavelvlasov.config.ConfigurationException; 28 import com.pavelvlasov.config.Parameterizable; 29 import com.pavelvlasov.jsel.impl.Token; 30 31 37 public class LineLengthRule extends InspectorBase implements Parameterizable { 38 42 private Integer maxLineLength; 43 44 49 public void visit(Token element) { 50 if (maxLineLength == null || lastPosition(element) < maxLineLength.intValue()) { 51 return; 52 } 53 54 Token nextToken = (Token) element.getNextToken(); 55 if (nextToken==null || (nextToken != null && element.getLine() != nextToken.getLine())) { 56 Token violationToken=element; 57 String vtt=violationToken.getTypeName(); 58 while ("WS".equals(vtt) || "NEW_LINE".equals(vtt)) { 59 violationToken=violationToken.getPrevNonWhiteSpaceToken(); 60 if (violationToken==null || violationToken.getLine()!=element.getLine() || lastPosition(violationToken)<=maxLineLength.intValue()) { 61 return; 62 } 63 vtt=violationToken.getTypeName(); 64 } 65 66 if ("ML_COMMENT".equals(element.getTypeName())) { 67 } else { 69 context.reportViolation(violationToken); 70 } 71 } 72 } 73 74 private int lastPosition(Token element) { 75 return element.getColumn()+(element.getText()==null ? 0 : element.getText().length()); 76 } 77 78 85 public boolean setParameter(String name, Object parameter) throws ConfigurationException { 86 if ("line-max-length".equals(name)) { 87 maxLineLength = (Integer ) parameter; 88 return true; 89 } else { 90 throw new ConfigurationException("Parameter '"+name+"' is not supported by "+getClass().getName()); 91 } 92 } 93 94 97 public String getConfigInfo() { 98 StringBuffer ret=new StringBuffer ("Allowed maximum line length:\n"); 99 ret.append("line-max-length: " + maxLineLength + "\n"); 100 return ret.toString(); 101 } 102 } 103 | Popular Tags |