1 package com.puppycrawl.tools.checkstyle.checks; 20 21 import com.puppycrawl.tools.checkstyle.api.DetailAST; 22 import com.puppycrawl.tools.checkstyle.api.FileContents; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 import java.util.regex.Matcher ; 26 import java.util.regex.Pattern ; 27 28 52 public class RegexpCheck extends AbstractFormatCheck 53 { 54 55 private static final int DEFAULT_DUPLICATE_LIMIT = -1; 56 57 58 private static final int DEFAULT_ERROR_LIMIT = 100; 59 60 61 private static final String ERROR_LIMIT_EXCEEDED_MESSAGE = 62 "The error limit has been exceeded, " 63 + "the check is aborting, there may be more unreported errors."; 64 65 66 private String mMessage = ""; 67 68 69 private boolean mIgnoreComments; 70 71 72 private boolean mIllegalPattern; 73 74 75 private int mErrorLimit = DEFAULT_ERROR_LIMIT; 76 77 78 private int mDuplicateLimit; 79 80 81 private boolean mCheckForDuplicates; 82 83 84 private int mMatchCount; 85 86 87 private int mErrorCount; 88 89 90 private final List mCharacters = new ArrayList (); 91 92 93 private Matcher mMatcher; 94 95 98 public RegexpCheck() 99 { 100 super("$^", Pattern.MULTILINE); } 102 103 107 public void setMessage(String aMessage) 108 { 109 mMessage = (aMessage == null) ? "" : aMessage; 110 } 111 112 119 public String getMessage() 120 { 121 return mMessage; 122 } 123 124 128 public void setIgnoreComments(boolean aIgnoreComments) 129 { 130 mIgnoreComments = aIgnoreComments; 131 } 132 133 137 public void setIllegalPattern(boolean aIllegalPattern) 138 { 139 mIllegalPattern = aIllegalPattern; 140 } 141 142 146 public void setErrorLimit(int aErrorLimit) 147 { 148 mErrorLimit = aErrorLimit; 149 } 150 151 156 public void setDuplicateLimit(int aDuplicateLimit) 157 { 158 mDuplicateLimit = aDuplicateLimit; 159 mCheckForDuplicates = (mDuplicateLimit > DEFAULT_DUPLICATE_LIMIT); 160 } 161 162 163 public int[] getDefaultTokens() 164 { 165 return new int[0]; 166 } 167 168 169 public void beginTree(DetailAST aRootAST) 170 { 171 mCharacters.clear(); 172 final Pattern pattern = getRegexp(); 173 final String [] lines = getLines(); 174 final StringBuffer sb = new StringBuffer (); 175 for (int i = 0; i < lines.length; i++) { 176 sb.append(lines[i]); 177 sb.append('\n'); 178 for (int j = 0; j < (lines[i].length() + 1); j++) { 179 mCharacters.add(new Integer [] { 180 new Integer (i + 1), new Integer (j), }); 181 } 182 } 183 mMatcher = pattern.matcher(sb.toString()); 184 mMatchCount = 0; 185 mErrorCount = 0; 186 findMatch(); 187 } 188 189 190 private void findMatch() 191 { 192 int startLine; 193 int startColumn; 194 int endLine; 195 int endColumn; 196 boolean foundMatch; 197 boolean ignore = false; 198 199 foundMatch = mMatcher.find(); 200 if (!foundMatch && !mIllegalPattern && (mMatchCount == 0)) { 201 logMessage(0); 202 } 203 else if (foundMatch) { 204 startLine = ((Integer []) mCharacters.get(mMatcher.start()))[0]. 205 intValue(); 206 startColumn = ((Integer []) mCharacters.get(mMatcher.start()))[1]. 207 intValue(); 208 endLine = ((Integer []) mCharacters.get(mMatcher.end() - 1))[0]. 209 intValue(); 210 endColumn = ((Integer []) mCharacters.get(mMatcher.end() - 1))[1]. 211 intValue(); 212 if (mIgnoreComments) { 213 final FileContents theFileContents = getFileContents(); 214 ignore = theFileContents.hasIntersectionWithComment(startLine, 215 startColumn, endLine, endColumn); 216 } 217 if (!ignore) { 218 mMatchCount++; 219 if (mIllegalPattern || (mCheckForDuplicates 220 && ((mMatchCount - 1) > mDuplicateLimit))) 221 { 222 mErrorCount++; 223 logMessage(startLine); 224 } 225 } 226 if ((mErrorCount < mErrorLimit) 227 && (ignore || mIllegalPattern || mCheckForDuplicates)) 228 { 229 findMatch(); 230 } 231 } 232 } 233 234 238 private void logMessage(int aLineNumber) 239 { 240 String message = "".equals(getMessage()) ? getFormat() : mMessage; 241 if (mErrorCount >= mErrorLimit) { 242 message = ERROR_LIMIT_EXCEEDED_MESSAGE + message; 243 } 244 if (mIllegalPattern) { 245 log(aLineNumber, "illegal.regexp", message); 246 } 247 else { 248 if (aLineNumber > 0) { 249 log(aLineNumber, "duplicate.regexp", message); 250 } 251 else { 252 log(aLineNumber, "required.regexp", message); 253 } 254 } 255 } 256 } 257 258 | Popular Tags |