1 21 package org.apache.webdav.ant; 22 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import org.apache.tools.ant.types.selectors.SelectorUtils; 28 29 104 public abstract class Scanner { 105 106 protected static final String SEPARATOR = "/"; 107 protected static final char SEPARATOR_CHAR = '/'; 108 109 123 126 private List includes = new ArrayList (); 127 128 131 private List excludes = new ArrayList (); 132 133 137 protected List filesIncluded; 138 139 142 protected List filesNotIncluded; 143 144 148 protected List filesExcluded; 149 150 154 protected List dirsIncluded; 155 156 159 protected List dirsNotIncluded; 160 161 165 protected List dirsExcluded; 166 167 protected boolean isCaseSensitive = true; 168 169 170 171 174 public Scanner() { 175 } 176 177 178 179 188 public void setIncludes(String [] includes) { 189 this.includes = new ArrayList (); 190 addIncludes(includes); 191 } 192 public void addIncludes(String [] includes) { 193 if (includes != null) { 194 for(int i = 0; i < includes.length ; i++) { 195 this.includes.add(noramlizePattern(includes[i])); 196 } 197 } 198 } 199 200 209 public void setExcludes(String [] excludes) { 210 this.excludes = new ArrayList (); 211 addExcludes(excludes); 212 } 213 public void addExcludes(String [] excludes) { 214 if (excludes != null) { 215 for (int i = 0; i < excludes.length; i++) { 216 this.excludes.add(noramlizePattern(excludes[i])); 217 } 218 } 219 } 220 221 private String noramlizePattern(String pattern) { 222 if (pattern.endsWith(SEPARATOR)) { 225 pattern += "**"; 226 } 227 return pattern; 228 } 229 230 231 public void setCaseSensitive(boolean val) { 232 this.isCaseSensitive = val; 233 } 234 235 236 242 public abstract void scan(); 243 244 245 246 253 protected boolean isIncluded(String name) { 254 for (Iterator i = this.includes.iterator(); i.hasNext();) { 255 if (matchPath((String )i.next(), name, this.isCaseSensitive)) { 256 return true; 257 } 258 } 259 return false; 260 } 261 262 263 264 271 protected boolean isExcluded(String name) { 272 for (Iterator i = this.excludes.iterator(); i.hasNext();) { 273 if (matchPath((String )i.next(), name, this.isCaseSensitive)) { 274 return true; 275 } 276 } 277 return false; 278 } 279 280 281 282 289 public String [] getIncludedFiles() { 290 int count = filesIncluded.size(); 291 String [] files = new String [count]; 292 for (int i = 0; i < count; i++) { 293 files[i] = (String )filesIncluded.get(i); 294 } 295 return files; 296 } 297 298 299 300 306 public String [] getNotIncludedFiles() { 307 int count = filesNotIncluded.size(); 308 String [] files = new String [count]; 309 for (int i = 0; i < count; i++) { 310 files[i] = (String )filesNotIncluded.get(i); 311 } 312 return files; 313 } 314 315 316 317 324 public String [] getExcludedFiles() { 325 int count = filesExcluded.size(); 326 String [] files = new String [count]; 327 for (int i = 0; i < count; i++) { 328 files[i] = (String )filesExcluded.get(i); 329 } 330 return files; 331 } 332 333 334 335 342 public String [] getIncludedDirectories() { 343 int count = dirsIncluded.size(); 344 String [] directories = new String [count]; 345 for (int i = 0; i < count; i++) { 346 directories[i] = (String )dirsIncluded.get(i); 347 } 348 return directories; 349 } 350 351 352 353 360 public String [] getNotIncludedDirectories() { 361 int count = dirsNotIncluded.size(); 362 String [] directories = new String [count]; 363 for (int i = 0; i < count; i++) { 364 directories[i] = (String )dirsNotIncluded.get(i); 365 } 366 return directories; 367 } 368 369 370 371 378 public String [] getExcludedDirectories() { 379 int count = dirsExcluded.size(); 380 String [] directories = new String [count]; 381 for (int i = 0; i < count; i++) { 382 directories[i] = (String )dirsExcluded.get(i); 383 } 384 return directories; 385 } 386 387 388 389 400 406 407 424 public static boolean match(String pattern, String str, 425 boolean isCaseSensitive) 426 { 427 return SelectorUtils.match(pattern, str, isCaseSensitive); 428 } 429 430 445 public static boolean matchPath(String pattern, String str, 446 boolean isCaseSensitive) 447 { 448 if (str.startsWith(SEPARATOR) != pattern.startsWith(SEPARATOR)) { 453 return false; 454 } 455 456 String [] patDirs = tokenizePathAsArray(pattern); 457 String [] strDirs = tokenizePathAsArray(str); 458 459 int patIdxStart = 0; 460 int patIdxEnd = patDirs.length - 1; 461 int strIdxStart = 0; 462 int strIdxEnd = strDirs.length - 1; 463 464 while (patIdxStart <= patIdxEnd && strIdxStart <= strIdxEnd) { 466 String patDir = patDirs[patIdxStart]; 467 if (patDir.equals("**")) { 468 break; 469 } 470 if (!match(patDir, strDirs[strIdxStart], isCaseSensitive)) { 471 patDirs = null; 472 strDirs = null; 473 return false; 474 } 475 patIdxStart++; 476 strIdxStart++; 477 } 478 if (strIdxStart > strIdxEnd) { 479 for (int i = patIdxStart; i <= patIdxEnd; i++) { 481 if (!patDirs[i].equals("**")) { 482 patDirs = null; 483 strDirs = null; 484 return false; 485 } 486 } 487 return true; 488 } else { 489 if (patIdxStart > patIdxEnd) { 490 patDirs = null; 492 strDirs = null; 493 return false; 494 } 495 } 496 497 while (patIdxStart <= patIdxEnd && strIdxStart <= strIdxEnd) { 499 String patDir = patDirs[patIdxEnd]; 500 if (patDir.equals("**")) { 501 break; 502 } 503 if (!match(patDir, strDirs[strIdxEnd], isCaseSensitive)) { 504 patDirs = null; 505 strDirs = null; 506 return false; 507 } 508 patIdxEnd--; 509 strIdxEnd--; 510 } 511 if (strIdxStart > strIdxEnd) { 512 for (int i = patIdxStart; i <= patIdxEnd; i++) { 514 if (!patDirs[i].equals("**")) { 515 patDirs = null; 516 strDirs = null; 517 return false; 518 } 519 } 520 return true; 521 } 522 523 while (patIdxStart != patIdxEnd && strIdxStart <= strIdxEnd) { 524 int patIdxTmp = -1; 525 for (int i = patIdxStart + 1; i <= patIdxEnd; i++) { 526 if (patDirs[i].equals("**")) { 527 patIdxTmp = i; 528 break; 529 } 530 } 531 if (patIdxTmp == patIdxStart + 1) { 532 patIdxStart++; 534 continue; 535 } 536 int patLength = (patIdxTmp - patIdxStart - 1); 539 int strLength = (strIdxEnd - strIdxStart + 1); 540 int foundIdx = -1; 541 strLoop: 542 for (int i = 0; i <= strLength - patLength; i++) { 543 for (int j = 0; j < patLength; j++) { 544 String subPat = patDirs[patIdxStart + j + 1]; 545 String subStr = strDirs[strIdxStart + i + j]; 546 if (!match(subPat, subStr, isCaseSensitive)) { 547 continue strLoop; 548 } 549 } 550 551 foundIdx = strIdxStart + i; 552 break; 553 } 554 555 if (foundIdx == -1) { 556 patDirs = null; 557 strDirs = null; 558 return false; 559 } 560 561 patIdxStart = patIdxTmp; 562 strIdxStart = foundIdx + patLength; 563 } 564 565 for (int i = patIdxStart; i <= patIdxEnd; i++) { 566 if (!patDirs[i].equals("**")) { 567 patDirs = null; 568 strDirs = null; 569 return false; 570 } 571 } 572 573 return true; 574 } 575 576 private static String [] tokenizePathAsArray(String path) { 577 char sep = SEPARATOR_CHAR; 578 int start = 0; 579 int len = path.length(); 580 int count = 0; 581 for (int pos = 0; pos < len; pos++) { 582 if (path.charAt(pos) == sep) { 583 if (pos != start) { 584 count++; 585 } 586 start = pos + 1; 587 } 588 } 589 if (len != start) { 590 count++; 591 } 592 String [] l = new String [count]; 593 count = 0; 594 start = 0; 595 for (int pos = 0; pos < len; pos++) { 596 if (path.charAt(pos) == sep) { 597 if (pos != start) { 598 String tok = path.substring(start, pos); 599 l[count++] = tok; 600 } 601 start = pos + 1; 602 } 603 } 604 if (len != start) { 605 String tok = path.substring(start); 606 l[count] = tok; 607 } 608 return l; 609 } 610 } 611 | Popular Tags |