1 20 package com.puppycrawl.tools.checkstyle.checks.sizes; 21 22 import com.puppycrawl.tools.checkstyle.api.Check; 23 import com.puppycrawl.tools.checkstyle.api.DetailAST; 24 import com.puppycrawl.tools.checkstyle.api.Utils; 25 26 import java.util.regex.Pattern ; 27 import java.util.regex.PatternSyntaxException ; 28 29 import org.apache.commons.beanutils.ConversionException; 30 31 79 public class LineLengthCheck extends Check 80 { 81 82 private static final int DEFAULT_MAX_COLUMNS = 80; 83 84 85 private int mMax = DEFAULT_MAX_COLUMNS; 86 87 88 private Pattern mIgnorePattern; 89 90 93 public LineLengthCheck() 94 { 95 setIgnorePattern("^$"); 96 } 97 98 99 public int[] getDefaultTokens() 100 { 101 return new int[0]; 102 } 103 104 105 public void beginTree(DetailAST aRootAST) 106 { 107 final String [] lines = getLines(); 108 for (int i = 0; i < lines.length; i++) { 109 110 final String line = lines[i]; 111 final int realLength = Utils.lengthExpandedTabs( 112 line, line.length(), getTabWidth()); 113 114 115 if ((realLength > mMax) 116 && !mIgnorePattern.matcher(line).find()) 117 { 118 log(i + 1, "maxLineLen", new Integer (mMax)); 119 } 120 } 121 } 122 123 126 public void setMax(int aLength) 127 { 128 mMax = aLength; 129 } 130 131 136 public void setIgnorePattern(String aFormat) 137 throws ConversionException 138 { 139 try { 140 mIgnorePattern = Utils.getPattern(aFormat); 141 } 142 catch (final PatternSyntaxException e) { 143 throw new ConversionException("unable to parse " + aFormat, e); 144 } 145 } 146 147 } 148 | Popular Tags |