1 19 20 21 package org.netbeans.modules.search.types; 22 23 import java.util.regex.Pattern ; 24 import java.util.regex.PatternSyntaxException ; 25 import java.util.regex.Matcher ; 26 27 28 39 public abstract class TextType extends DataObjectType { 40 41 private static final long serialVersionUID = 3L; 42 44 45 private static final String WORD_SEPARATORS 46 = " \t,;.:+-*/\\()[]{}<>=&|\"\'`~!?@#%^\n\r"; 48 49 protected boolean caseSensitive; 50 55 protected boolean wholeWords; 56 57 63 protected transient String ciMatchString; 64 69 protected String matchString; 70 75 protected String reString; 76 81 protected transient Pattern pattern; 82 83 private transient Matcher matcher; 84 85 86 private void readObject(java.io.ObjectInputStream ois) 87 throws java.io.IOException , ClassNotFoundException { 88 ois.defaultReadObject(); 89 if (!caseSensitive && matchString != null) { 90 ciMatchString = matchString.toUpperCase(); 91 } 92 if (reString != null) { 93 setRe(reString); 94 } 95 } 96 97 109 protected boolean match(String text) { 110 if (matchString != null) { 111 112 if (text.length() < matchString.length()) { 113 return false; 114 } 115 116 if (!caseSensitive) { 117 text = text.toUpperCase(); 118 } 119 int fromIndex = 1; 120 int fromIndexMax = text.length() - matchString.length() + 1; 121 int matchIndex; 122 123 do { 124 matchIndex = matchString(text, fromIndex); 125 fromIndex = Math.abs(matchIndex) + 1; 126 } while ((matchIndex < 0) && (fromIndex <= fromIndexMax)); 127 128 return matchIndex > 0; 129 } else { 130 return matchRE(text); 131 } 132 } 133 134 143 protected boolean matchRE(String line) { 144 matcher = pattern.matcher(line); 145 return matcher.find(); 146 } 147 148 149 protected Matcher getMatcher() { 150 return matcher; 151 } 152 153 169 protected int matchString(String text, int fromIndex) { 170 assert fromIndex >= 1; 171 172 int index = text.indexOf(caseSensitive ? matchString : ciMatchString, 173 fromIndex - 1); 174 175 int retValue = index + 1; 176 177 int boundaryIndex; 178 if (wholeWords && (retValue != 0) 180 && ((((boundaryIndex = index - 1) >= 0) 181 && WORD_SEPARATORS.indexOf(text.charAt(boundaryIndex)) < 0) 182 || 183 (((boundaryIndex = index + matchString.length()) < text.length()) 184 && WORD_SEPARATORS.indexOf(text.charAt(boundaryIndex)) < 0))) { 185 retValue = -retValue; 186 } 187 188 return retValue; 189 } 190 191 192 194 201 public String getMatchString() { 202 if (matchString == null) { 203 return ""; } else { 205 return matchString; 206 } 207 } 208 209 218 public void setMatchString(String substring) { 219 if (substring == null) { 220 setValid(false); 221 throw new IllegalArgumentException (); 222 } 223 224 if (substring.length() == 0) { 225 substring = null; 226 } else if (!caseSensitive) { 227 ciMatchString = substring.toUpperCase(); 228 } 229 this.matchString = substring; 230 pattern = null; 231 reString = null; 232 233 setValid(substring != null); 234 } 235 236 243 public String getRe() { 244 if (reString == null) { 245 return ""; } else { 247 return reString; 248 } 249 } 250 251 253 public String getMatchTemplateDescr() { 254 return (matchString != null) ? matchString : reString; 255 } 256 257 259 public Pattern getSearchPattern() { 260 if (pattern == null) { 261 assert matchString != null; 262 263 String quoted = quoteString(matchString); 264 String regexp; 265 if (!wholeWords) { 266 regexp = quoted; 267 } else { 268 StringBuilder buf = new StringBuilder (quoted.length() + 6); 269 if (wholeWords) { 270 buf.append("\\b"); } 272 buf.append(quoted); 273 if (wholeWords) { 274 buf.append("\\b"); } 276 regexp = buf.toString(); 277 } 278 279 int flags = Pattern.UNICODE_CASE; 280 if (!caseSensitive) { 281 flags |= Pattern.CASE_INSENSITIVE; 282 } 283 284 pattern = Pattern.compile(regexp, flags); 285 } 286 return pattern; 287 } 288 289 290 private static String quoteString(String string) { 291 if (string.length() == 0) { 292 return string; 293 } 294 295 StringBuilder buf; 296 297 int startIndex = 0; 298 int endIndex = string.indexOf('\\'); 300 if (endIndex == -1) { 301 buf = new StringBuilder (string.length() + 4); 302 buf.append("\\Q").append(string).append("\\E"); } else { 304 buf = new StringBuilder (string.length() + 16); 305 do { 306 if (endIndex != startIndex) { 307 buf.append("\\Q"); buf.append(string.substring(startIndex, endIndex)); 309 buf.append("\\E"); } 311 buf.append('\\').append('\\'); 312 startIndex = endIndex + 1; 313 endIndex = string.indexOf('\\', startIndex); 314 } while (endIndex != -1); 315 if (startIndex != string.length()) { 316 buf.append("\\Q"); buf.append(string.substring(startIndex)); 318 buf.append("\\E"); } 320 } 321 return buf.toString(); 322 } 323 324 336 public void setRe(String re) { 337 setReImpl(re); 338 } 339 340 349 private void setReImpl(String exp) { 350 if (exp == null) { 351 setValid(false); 352 throw new IllegalArgumentException (); 353 } 354 355 if (exp.length() == 0) { 356 exp = null; 357 } else { 358 try { 359 pattern = Pattern.compile(exp); 360 } catch (PatternSyntaxException ex) { 361 setValid(false); 362 throw new IllegalArgumentException (); 363 } 364 } 365 reString = exp; 366 matchString = null; 367 368 setValid(exp != null); 369 } 370 371 377 public boolean isCaseSensitive() { 378 return caseSensitive; 379 } 380 381 388 public void setCaseSensitive(boolean caseSensitive) { 389 if (caseSensitive == this.caseSensitive) { 390 return; 391 } 392 this.caseSensitive = caseSensitive; 393 394 if (matchString != null) { 395 ciMatchString = caseSensitive ? null : matchString.toUpperCase(); 396 } 397 } 398 399 405 public boolean getWholeWords() { 406 return wholeWords; 407 } 408 409 415 public void setWholeWords(boolean wholeWords) { 416 if (wholeWords == this.wholeWords) { 417 return; 418 } 419 this.wholeWords = wholeWords; 420 } 421 422 } 423 | Popular Tags |