1 17 package org.alfresco.filesys.util; 18 19 29 public final class WildCard 30 { 31 32 34 public static final int MULTICHAR_WILDCARD = '*'; 35 36 38 public static final int SINGLECHAR_WILDCARD = '?'; 39 40 47 public static final int SINGLECHAR_UNICODE_WILDCARD = '>'; 48 public static final int DOT_UNICODE_WILDCARD = '"'; 49 public static final int MULTICHAR_UNICODE_WILDCARD = '<'; 50 51 53 public static final int WILDCARD_NONE = 0; public static final int WILDCARD_ALL = 1; public static final int WILDCARD_NAME = 2; public static final int WILDCARD_EXT = 3; public static final int WILDCARD_COMPLEX = 4; 59 public static final int WILDCARD_INVALID = -1; 60 61 63 private String m_pattern; 64 private int m_type; 65 66 68 private String m_matchPart; 69 private boolean m_caseSensitive; 70 71 73 private char[] m_patternChars; 74 75 78 public WildCard() 79 { 80 setType(WILDCARD_INVALID); 81 } 82 83 89 public WildCard(String pattern, boolean caseSensitive) 90 { 91 setPattern(pattern, caseSensitive); 92 } 93 94 99 public final int isType() 100 { 101 return m_type; 102 } 103 104 109 public final boolean isCaseSensitive() 110 { 111 return m_caseSensitive; 112 } 113 114 119 public final String getPattern() 120 { 121 return m_pattern; 122 } 123 124 129 public final String getMatchPart() 130 { 131 return m_matchPart; 132 } 133 134 140 public final boolean matchesPattern(String str) 141 { 142 143 145 boolean sts = false; 146 147 switch (isType()) 148 { 149 150 152 case WILDCARD_ALL: 153 sts = true; 154 break; 155 156 158 case WILDCARD_NAME: 159 if (isCaseSensitive()) 160 { 161 162 164 sts = str.endsWith(m_matchPart); 165 } 166 else 167 { 168 169 171 String upStr = str.toUpperCase(); 172 sts = upStr.endsWith(m_matchPart); 173 } 174 break; 175 176 178 case WILDCARD_EXT: 179 if (isCaseSensitive()) 180 { 181 182 184 sts = str.startsWith(m_matchPart); 185 } 186 else 187 { 188 189 191 String upStr = str.toUpperCase(); 192 sts = upStr.startsWith(m_matchPart); 193 } 194 break; 195 196 198 case WILDCARD_COMPLEX: 199 if (isCaseSensitive()) 200 sts = matchComplexWildcard(str); 201 else 202 { 203 204 206 String upStr = str.toUpperCase(); 207 sts = matchComplexWildcard(upStr); 208 } 209 break; 210 211 213 case WILDCARD_NONE: 214 if (isCaseSensitive()) 215 { 216 if (str.compareTo(m_pattern) == 0) 217 sts = true; 218 } 219 else if (str.equalsIgnoreCase(m_pattern)) 220 sts = true; 221 break; 222 } 223 224 226 return sts; 227 } 228 229 235 protected final boolean matchComplexWildcard(String str) 236 { 237 238 240 char[] strChars = str.toCharArray(); 241 242 244 int wpos = 0; 245 int wlen = m_patternChars.length; 246 247 int spos = 0; 248 int slen = strChars.length; 249 250 char patChar; 251 boolean matchFailed = false; 252 253 while (matchFailed == false && wpos < m_patternChars.length) 254 { 255 256 258 patChar = m_patternChars[wpos++]; 259 260 switch (patChar) 261 { 262 263 265 case SINGLECHAR_WILDCARD: 266 if (spos < slen) 267 spos++; 268 else 269 matchFailed = true; 270 break; 271 272 274 case MULTICHAR_WILDCARD: 275 276 278 if (wpos < wlen) 279 { 280 281 283 patChar = m_patternChars[wpos]; 284 if (patChar != SINGLECHAR_WILDCARD && patChar != MULTICHAR_WILDCARD) 285 { 286 287 289 while (spos < slen && strChars[spos] != patChar) 290 spos++; 291 if (spos >= slen) 292 matchFailed = true; 293 } 294 } 295 else 296 { 297 298 301 spos = slen; 302 } 303 break; 304 305 307 default: 308 if (spos >= slen || strChars[spos] != patChar) 309 matchFailed = true; 310 else 311 spos++; 312 break; 313 } 314 } 315 316 318 if (matchFailed == false && spos == slen) 319 return true; 320 return false; 321 } 322 323 329 public final void setPattern(String pattern, boolean caseSensitive) 330 { 331 332 334 m_pattern = pattern; 335 m_caseSensitive = caseSensitive; 336 337 setType(WILDCARD_INVALID); 338 339 341 if (pattern == null || pattern.length() == 0) 342 return; 343 344 346 if (pattern.compareTo("*.*") == 0 || pattern.compareTo("*") == 0) 347 { 348 setType(WILDCARD_ALL); 349 return; 350 } 351 352 354 if (pattern.startsWith("*.")) 355 { 356 357 359 if (pattern.length() > 2) 360 m_matchPart = pattern.substring(1); 361 else 362 m_matchPart = ""; 363 364 366 if (isCaseSensitive() == false) 367 m_matchPart = m_matchPart.toUpperCase(); 368 369 371 if (containsWildcards(m_matchPart) == false) 372 { 373 setType(WILDCARD_NAME); 374 return; 375 } 376 } 377 378 380 if (pattern.endsWith(".*")) 381 { 382 383 385 if (pattern.length() > 2) 386 m_matchPart = pattern.substring(0, pattern.length() - 2); 387 else 388 m_matchPart = ""; 389 390 392 if (isCaseSensitive() == false) 393 m_matchPart = m_matchPart.toUpperCase(); 394 395 397 if (containsWildcards(m_matchPart) == false) 398 { 399 setType(WILDCARD_EXT); 400 return; 401 } 402 } 403 404 406 if (isCaseSensitive() == false) 407 m_patternChars = m_pattern.toUpperCase().toCharArray(); 408 else 409 m_patternChars = m_pattern.toCharArray(); 410 411 setType(WILDCARD_COMPLEX); 412 } 413 414 419 private final void setType(int typ) 420 { 421 m_type = typ; 422 } 423 424 429 public String toString() 430 { 431 StringBuffer str = new StringBuffer (); 432 str.append("["); 433 str.append(getPattern()); 434 str.append(","); 435 str.append(isType()); 436 str.append(","); 437 438 if (m_matchPart != null) 439 str.append(m_matchPart); 440 441 if (isCaseSensitive()) 442 str.append(",Case"); 443 else 444 str.append(",NoCase"); 445 str.append("]"); 446 447 return str.toString(); 448 } 449 450 456 public final static boolean containsWildcards(String str) 457 { 458 459 461 if (str.indexOf(MULTICHAR_WILDCARD) != -1) 462 return true; 463 464 if (str.indexOf(SINGLECHAR_WILDCARD) != -1) 465 return true; 466 467 469 return false; 470 } 471 472 478 public final static boolean containsUnicodeWildcard(String str) 479 { 480 481 483 if (str.indexOf(SINGLECHAR_UNICODE_WILDCARD) != -1 || str.indexOf(MULTICHAR_UNICODE_WILDCARD) != -1 484 || str.indexOf(DOT_UNICODE_WILDCARD) != -1) 485 return true; 486 return false; 487 } 488 489 495 public final static String convertUnicodeWildcardToDOS(String str) 496 { 497 498 500 StringBuffer newStr = new StringBuffer (str.length()); 501 502 504 for (int i = 0; i < str.length(); i++) 505 { 506 507 509 char ch = str.charAt(i); 510 511 513 if (ch == SINGLECHAR_UNICODE_WILDCARD) 514 { 515 516 518 ch = SINGLECHAR_WILDCARD; 519 } 520 else if (ch == MULTICHAR_UNICODE_WILDCARD) 521 { 522 523 527 if (i < (str.length() - 1) && str.charAt(i + 1) == '.') 528 ch = MULTICHAR_WILDCARD; 529 } 530 else if (ch == DOT_UNICODE_WILDCARD) 531 { 532 533 536 if (i < (str.length() - 1)) 537 { 538 char nextCh = str.charAt(i + 1); 539 if (nextCh == SINGLECHAR_WILDCARD || nextCh == MULTICHAR_WILDCARD 540 || nextCh == SINGLECHAR_UNICODE_WILDCARD) 541 ch = '.'; 542 } 543 } 544 545 547 newStr.append(ch); 548 } 549 550 552 return newStr.toString(); 553 } 554 555 561 public final static String convertToRegexp(String path) 562 { 563 564 567 char[] smbPattern = path.toCharArray(); 568 boolean endsWithQ = smbPattern[smbPattern.length - 1] == '?'; 569 570 572 StringBuffer sb = new StringBuffer (); 573 sb.append('^'); 574 575 for (int i = 0; i < smbPattern.length; i++) 576 { 577 578 580 switch (smbPattern[i]) 581 { 582 583 585 case '*': 586 sb.append(".*"); 587 break; 588 589 591 case '?': 592 if (endsWithQ) 593 { 594 boolean restQ = true; 595 for (int j = i + 1; j < smbPattern.length; j++) 596 { 597 if (smbPattern[j] != '?') 598 { 599 restQ = false; 600 break; 601 } 602 } 603 if (restQ) 604 sb.append(".?"); 605 else 606 sb.append('.'); 607 } 608 else 609 sb.append('.'); 610 break; 611 612 614 case '.': 615 case '+': 616 case '\\': 617 case '[': 618 case ']': 619 case '^': 620 case '$': 621 case '(': 622 case ')': 623 sb.append('\\'); 624 sb.append(smbPattern[i]); 625 break; 626 627 629 default: 630 sb.append(smbPattern[i]); 631 break; 632 } 633 } 634 sb.append('$'); 635 636 638 return sb.toString(); 639 } 640 } | Popular Tags |