1 18 19 package org.apache.tools.ant.types.selectors; 20 21 import java.io.File ; 22 import java.util.StringTokenizer ; 23 import java.util.Vector ; 24 25 import org.apache.tools.ant.types.Resource; 26 import org.apache.tools.ant.util.FileUtils; 27 28 38 public final class SelectorUtils { 39 40 private static SelectorUtils instance = new SelectorUtils(); 41 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 42 43 46 private SelectorUtils() { 47 } 48 49 53 public static SelectorUtils getInstance() { 54 return instance; 55 } 56 57 73 public static boolean matchPatternStart(String pattern, String str) { 74 return matchPatternStart(pattern, str, true); 75 } 76 77 95 public static boolean matchPatternStart(String pattern, String str, 96 boolean isCaseSensitive) { 97 if (str.startsWith(File.separator) 102 != pattern.startsWith(File.separator)) { 103 return false; 104 } 105 106 String [] patDirs = tokenizePathAsArray(pattern); 107 String [] strDirs = tokenizePathAsArray(str); 108 109 int patIdxStart = 0; 110 int patIdxEnd = patDirs.length - 1; 111 int strIdxStart = 0; 112 int strIdxEnd = strDirs.length - 1; 113 114 while (patIdxStart <= patIdxEnd && strIdxStart <= strIdxEnd) { 116 String patDir = patDirs[patIdxStart]; 117 if (patDir.equals("**")) { 118 break; 119 } 120 if (!match(patDir, strDirs[strIdxStart], isCaseSensitive)) { 121 return false; 122 } 123 patIdxStart++; 124 strIdxStart++; 125 } 126 127 if (strIdxStart > strIdxEnd) { 128 return true; 130 } else if (patIdxStart > patIdxEnd) { 131 return false; 133 } else { 134 return true; 137 } 138 } 139 140 151 public static boolean matchPath(String pattern, String str) { 152 return matchPath(pattern, str, true); 153 } 154 155 168 public static boolean matchPath(String pattern, String str, 169 boolean isCaseSensitive) { 170 String [] patDirs = tokenizePathAsArray(pattern); 171 String [] strDirs = tokenizePathAsArray(str); 172 173 int patIdxStart = 0; 174 int patIdxEnd = patDirs.length - 1; 175 int strIdxStart = 0; 176 int strIdxEnd = strDirs.length - 1; 177 178 while (patIdxStart <= patIdxEnd && strIdxStart <= strIdxEnd) { 180 String patDir = patDirs[patIdxStart]; 181 if (patDir.equals("**")) { 182 break; 183 } 184 if (!match(patDir, strDirs[strIdxStart], isCaseSensitive)) { 185 patDirs = null; 186 strDirs = null; 187 return false; 188 } 189 patIdxStart++; 190 strIdxStart++; 191 } 192 if (strIdxStart > strIdxEnd) { 193 for (int i = patIdxStart; i <= patIdxEnd; i++) { 195 if (!patDirs[i].equals("**")) { 196 patDirs = null; 197 strDirs = null; 198 return false; 199 } 200 } 201 return true; 202 } else { 203 if (patIdxStart > patIdxEnd) { 204 patDirs = null; 206 strDirs = null; 207 return false; 208 } 209 } 210 211 while (patIdxStart <= patIdxEnd && strIdxStart <= strIdxEnd) { 213 String patDir = patDirs[patIdxEnd]; 214 if (patDir.equals("**")) { 215 break; 216 } 217 if (!match(patDir, strDirs[strIdxEnd], isCaseSensitive)) { 218 patDirs = null; 219 strDirs = null; 220 return false; 221 } 222 patIdxEnd--; 223 strIdxEnd--; 224 } 225 if (strIdxStart > strIdxEnd) { 226 for (int i = patIdxStart; i <= patIdxEnd; i++) { 228 if (!patDirs[i].equals("**")) { 229 patDirs = null; 230 strDirs = null; 231 return false; 232 } 233 } 234 return true; 235 } 236 237 while (patIdxStart != patIdxEnd && strIdxStart <= strIdxEnd) { 238 int patIdxTmp = -1; 239 for (int i = patIdxStart + 1; i <= patIdxEnd; i++) { 240 if (patDirs[i].equals("**")) { 241 patIdxTmp = i; 242 break; 243 } 244 } 245 if (patIdxTmp == patIdxStart + 1) { 246 patIdxStart++; 248 continue; 249 } 250 int patLength = (patIdxTmp - patIdxStart - 1); 253 int strLength = (strIdxEnd - strIdxStart + 1); 254 int foundIdx = -1; 255 strLoop: 256 for (int i = 0; i <= strLength - patLength; i++) { 257 for (int j = 0; j < patLength; j++) { 258 String subPat = patDirs[patIdxStart + j + 1]; 259 String subStr = strDirs[strIdxStart + i + j]; 260 if (!match(subPat, subStr, isCaseSensitive)) { 261 continue strLoop; 262 } 263 } 264 265 foundIdx = strIdxStart + i; 266 break; 267 } 268 269 if (foundIdx == -1) { 270 patDirs = null; 271 strDirs = null; 272 return false; 273 } 274 275 patIdxStart = patIdxTmp; 276 strIdxStart = foundIdx + patLength; 277 } 278 279 for (int i = patIdxStart; i <= patIdxEnd; i++) { 280 if (!patDirs[i].equals("**")) { 281 patDirs = null; 282 strDirs = null; 283 return false; 284 } 285 } 286 287 return true; 288 } 289 290 304 public static boolean match(String pattern, String str) { 305 return match(pattern, str, true); 306 } 307 308 325 public static boolean match(String pattern, String str, 326 boolean isCaseSensitive) { 327 char[] patArr = pattern.toCharArray(); 328 char[] strArr = str.toCharArray(); 329 int patIdxStart = 0; 330 int patIdxEnd = patArr.length - 1; 331 int strIdxStart = 0; 332 int strIdxEnd = strArr.length - 1; 333 char ch; 334 335 boolean containsStar = false; 336 for (int i = 0; i < patArr.length; i++) { 337 if (patArr[i] == '*') { 338 containsStar = true; 339 break; 340 } 341 } 342 343 if (!containsStar) { 344 if (patIdxEnd != strIdxEnd) { 346 return false; } 348 for (int i = 0; i <= patIdxEnd; i++) { 349 ch = patArr[i]; 350 if (ch != '?') { 351 if (isCaseSensitive && ch != strArr[i]) { 352 return false; } 354 if (!isCaseSensitive && Character.toUpperCase(ch) 355 != Character.toUpperCase(strArr[i])) { 356 return false; } 358 } 359 } 360 return true; } 362 363 if (patIdxEnd == 0) { 364 return true; } 366 367 while ((ch = patArr[patIdxStart]) != '*' && strIdxStart <= strIdxEnd) { 369 if (ch != '?') { 370 if (isCaseSensitive && ch != strArr[strIdxStart]) { 371 return false; } 373 if (!isCaseSensitive && Character.toUpperCase(ch) 374 != Character.toUpperCase(strArr[strIdxStart])) { 375 return false; } 377 } 378 patIdxStart++; 379 strIdxStart++; 380 } 381 if (strIdxStart > strIdxEnd) { 382 for (int i = patIdxStart; i <= patIdxEnd; i++) { 385 if (patArr[i] != '*') { 386 return false; 387 } 388 } 389 return true; 390 } 391 392 while ((ch = patArr[patIdxEnd]) != '*' && strIdxStart <= strIdxEnd) { 394 if (ch != '?') { 395 if (isCaseSensitive && ch != strArr[strIdxEnd]) { 396 return false; } 398 if (!isCaseSensitive && Character.toUpperCase(ch) 399 != Character.toUpperCase(strArr[strIdxEnd])) { 400 return false; } 402 } 403 patIdxEnd--; 404 strIdxEnd--; 405 } 406 if (strIdxStart > strIdxEnd) { 407 for (int i = patIdxStart; i <= patIdxEnd; i++) { 410 if (patArr[i] != '*') { 411 return false; 412 } 413 } 414 return true; 415 } 416 417 while (patIdxStart != patIdxEnd && strIdxStart <= strIdxEnd) { 420 int patIdxTmp = -1; 421 for (int i = patIdxStart + 1; i <= patIdxEnd; i++) { 422 if (patArr[i] == '*') { 423 patIdxTmp = i; 424 break; 425 } 426 } 427 if (patIdxTmp == patIdxStart + 1) { 428 patIdxStart++; 430 continue; 431 } 432 int patLength = (patIdxTmp - patIdxStart - 1); 435 int strLength = (strIdxEnd - strIdxStart + 1); 436 int foundIdx = -1; 437 strLoop: 438 for (int i = 0; i <= strLength - patLength; i++) { 439 for (int j = 0; j < patLength; j++) { 440 ch = patArr[patIdxStart + j + 1]; 441 if (ch != '?') { 442 if (isCaseSensitive && ch != strArr[strIdxStart + i 443 + j]) { 444 continue strLoop; 445 } 446 if (!isCaseSensitive 447 && Character.toUpperCase(ch) 448 != Character.toUpperCase(strArr[strIdxStart + i + j])) { 449 continue strLoop; 450 } 451 } 452 } 453 454 foundIdx = strIdxStart + i; 455 break; 456 } 457 458 if (foundIdx == -1) { 459 return false; 460 } 461 462 patIdxStart = patIdxTmp; 463 strIdxStart = foundIdx + patLength; 464 } 465 466 for (int i = patIdxStart; i <= patIdxEnd; i++) { 469 if (patArr[i] != '*') { 470 return false; 471 } 472 } 473 return true; 474 } 475 476 484 public static Vector tokenizePath (String path) { 485 return tokenizePath(path, File.separator); 486 } 487 488 497 public static Vector tokenizePath (String path, String separator) { 498 Vector ret = new Vector (); 499 if (FileUtils.isAbsolutePath(path)) { 500 String [] s = FILE_UTILS.dissect(path); 501 ret.add(s[0]); 502 path = s[1]; 503 } 504 StringTokenizer st = new StringTokenizer (path, separator); 505 while (st.hasMoreTokens()) { 506 ret.addElement(st.nextToken()); 507 } 508 return ret; 509 } 510 511 514 private static String [] tokenizePathAsArray(String path) { 515 String root = null; 516 if (FileUtils.isAbsolutePath(path)) { 517 String [] s = FILE_UTILS.dissect(path); 518 root = s[0]; 519 path = s[1]; 520 } 521 char sep = File.separatorChar; 522 int start = 0; 523 int len = path.length(); 524 int count = 0; 525 for (int pos = 0; pos < len; pos++) { 526 if (path.charAt(pos) == sep) { 527 if (pos != start) { 528 count++; 529 } 530 start = pos + 1; 531 } 532 } 533 if (len != start) { 534 count++; 535 } 536 String [] l = new String [count + ((root == null) ? 0 : 1)]; 537 538 if (root != null) { 539 l[0] = root; 540 count = 1; 541 } else { 542 count = 0; 543 } 544 start = 0; 545 for (int pos = 0; pos < len; pos++) { 546 if (path.charAt(pos) == sep) { 547 if (pos != start) { 548 String tok = path.substring(start, pos); 549 l[count++] = tok; 550 } 551 start = pos + 1; 552 } 553 } 554 if (len != start) { 555 String tok = path.substring(start); 556 l[count] = tok; 557 } 558 return l; 559 } 560 561 575 public static boolean isOutOfDate(File src, File target, int granularity) { 576 if (!src.exists()) { 577 return false; 578 } 579 if (!target.exists()) { 580 return true; 581 } 582 if ((src.lastModified() - granularity) > target.lastModified()) { 583 return true; 584 } 585 return false; 586 } 587 588 602 public static boolean isOutOfDate(Resource src, Resource target, 603 int granularity) { 604 return isOutOfDate(src, target, (long) granularity); 605 } 606 607 621 public static boolean isOutOfDate(Resource src, Resource target, 622 long granularity) { 623 if (!src.isExists()) { 624 return false; 625 } 626 if (!target.isExists()) { 627 return true; 628 } 629 if ((src.getLastModified() - granularity) > target.getLastModified()) { 630 return true; 631 } 632 return false; 633 } 634 635 643 public static String removeWhitespace(String input) { 644 StringBuffer result = new StringBuffer (); 645 if (input != null) { 646 StringTokenizer st = new StringTokenizer (input); 647 while (st.hasMoreTokens()) { 648 result.append(st.nextToken()); 649 } 650 } 651 return result.toString(); 652 } 653 654 659 public static boolean hasWildcards(String input) { 660 return (input.indexOf('*') != -1 || input.indexOf('?') != -1); 661 } 662 663 668 public static String rtrimWildcardTokens(String input) { 669 String [] tokens = tokenizePathAsArray(input); 670 StringBuffer sb = new StringBuffer (); 671 for (int i = 0; i < tokens.length; i++) { 672 if (hasWildcards(tokens[i])) { 673 break; 674 } 675 if (i > 0 && sb.charAt(sb.length() - 1) != File.separatorChar) { 676 sb.append(File.separator); 677 } 678 sb.append(tokens[i]); 679 } 680 return sb.toString(); 681 } 682 } 683 684 | Popular Tags |