1 18 package org.apache.tools.ant.types; 19 20 import java.io.File ; 21 import java.util.Vector ; 22 import java.util.Enumeration ; 23 24 import org.apache.tools.ant.Project; 25 import org.apache.tools.ant.FileScanner; 26 import org.apache.tools.ant.BuildException; 27 import org.apache.tools.ant.DirectoryScanner; 28 import org.apache.tools.ant.types.selectors.OrSelector; 29 import org.apache.tools.ant.types.selectors.AndSelector; 30 import org.apache.tools.ant.types.selectors.NotSelector; 31 import org.apache.tools.ant.types.selectors.DateSelector; 32 import org.apache.tools.ant.types.selectors.FileSelector; 33 import org.apache.tools.ant.types.selectors.NoneSelector; 34 import org.apache.tools.ant.types.selectors.SizeSelector; 35 import org.apache.tools.ant.types.selectors.TypeSelector; 36 import org.apache.tools.ant.types.selectors.DepthSelector; 37 import org.apache.tools.ant.types.selectors.DependSelector; 38 import org.apache.tools.ant.types.selectors.ExtendSelector; 39 import org.apache.tools.ant.types.selectors.SelectSelector; 40 import org.apache.tools.ant.types.selectors.PresentSelector; 41 import org.apache.tools.ant.types.selectors.SelectorScanner; 42 import org.apache.tools.ant.types.selectors.ContainsSelector; 43 import org.apache.tools.ant.types.selectors.FilenameSelector; 44 import org.apache.tools.ant.types.selectors.MajoritySelector; 45 import org.apache.tools.ant.types.selectors.DifferentSelector; 46 import org.apache.tools.ant.types.selectors.SelectorContainer; 47 import org.apache.tools.ant.types.selectors.ContainsRegexpSelector; 48 import org.apache.tools.ant.types.selectors.modifiedselector.ModifiedSelector; 49 50 57 public abstract class AbstractFileSet extends DataType 58 implements Cloneable , SelectorContainer { 59 60 private PatternSet defaultPatterns = new PatternSet(); 61 private Vector additionalPatterns = new Vector (); 62 private Vector selectors = new Vector (); 63 64 private File dir; 65 private boolean useDefaultExcludes = true; 66 private boolean caseSensitive = true; 67 private boolean followSymlinks = true; 68 69 70 private DirectoryScanner directoryScanner = null; 71 72 75 public AbstractFileSet() { 76 super(); 77 } 78 79 84 protected AbstractFileSet(AbstractFileSet fileset) { 85 this.dir = fileset.dir; 86 this.defaultPatterns = fileset.defaultPatterns; 87 this.additionalPatterns = fileset.additionalPatterns; 88 this.selectors = fileset.selectors; 89 this.useDefaultExcludes = fileset.useDefaultExcludes; 90 this.caseSensitive = fileset.caseSensitive; 91 this.followSymlinks = fileset.followSymlinks; 92 setProject(fileset.getProject()); 93 } 94 95 103 public void setRefid(Reference r) throws BuildException { 104 if (dir != null || defaultPatterns.hasPatterns(getProject())) { 105 throw tooManyAttributes(); 106 } 107 if (!additionalPatterns.isEmpty()) { 108 throw noChildrenAllowed(); 109 } 110 if (!selectors.isEmpty()) { 111 throw noChildrenAllowed(); 112 } 113 super.setRefid(r); 114 } 115 116 121 public synchronized void setDir(File dir) throws BuildException { 122 if (isReference()) { 123 throw tooManyAttributes(); 124 } 125 this.dir = dir; 126 directoryScanner = null; 127 } 128 129 133 public File getDir() { 134 return getDir(getProject()); 135 } 136 137 143 public synchronized File getDir(Project p) { 144 return (isReference()) ? getRef(p).getDir(p) : dir; 145 } 146 147 151 public synchronized PatternSet createPatternSet() { 152 if (isReference()) { 153 throw noChildrenAllowed(); 154 } 155 PatternSet patterns = new PatternSet(); 156 additionalPatterns.addElement(patterns); 157 directoryScanner = null; 158 return patterns; 159 } 160 161 165 public synchronized PatternSet.NameEntry createInclude() { 166 if (isReference()) { 167 throw noChildrenAllowed(); 168 } 169 directoryScanner = null; 170 return defaultPatterns.createInclude(); 171 } 172 173 177 public synchronized PatternSet.NameEntry createIncludesFile() { 178 if (isReference()) { 179 throw noChildrenAllowed(); 180 } 181 directoryScanner = null; 182 return defaultPatterns.createIncludesFile(); 183 } 184 185 189 public synchronized PatternSet.NameEntry createExclude() { 190 if (isReference()) { 191 throw noChildrenAllowed(); 192 } 193 directoryScanner = null; 194 return defaultPatterns.createExclude(); 195 } 196 197 201 public synchronized PatternSet.NameEntry createExcludesFile() { 202 if (isReference()) { 203 throw noChildrenAllowed(); 204 } 205 directoryScanner = null; 206 return defaultPatterns.createExcludesFile(); 207 } 208 209 214 public synchronized void setFile(File file) { 215 if (isReference()) { 216 throw tooManyAttributes(); 217 } 218 setDir(file.getParentFile()); 219 createInclude().setName(file.getName()); 220 } 221 222 230 public synchronized void setIncludes(String includes) { 231 if (isReference()) { 232 throw tooManyAttributes(); 233 } 234 defaultPatterns.setIncludes(includes); 235 directoryScanner = null; 236 } 237 238 245 public synchronized void appendIncludes(String [] includes) { 246 if (isReference()) { 247 throw tooManyAttributes(); 248 } 249 if (includes != null) { 250 for (int i = 0; i < includes.length; i++) { 251 defaultPatterns.createInclude().setName(includes[i]); 252 } 253 directoryScanner = null; 254 } 255 } 256 257 265 public synchronized void setExcludes(String excludes) { 266 if (isReference()) { 267 throw tooManyAttributes(); 268 } 269 defaultPatterns.setExcludes(excludes); 270 directoryScanner = null; 271 } 272 273 280 public synchronized void appendExcludes(String [] excludes) { 281 if (isReference()) { 282 throw tooManyAttributes(); 283 } 284 if (excludes != null) { 285 for (int i = 0; i < excludes.length; i++) { 286 defaultPatterns.createExclude().setName(excludes[i]); 287 } 288 directoryScanner = null; 289 } 290 } 291 292 298 public synchronized void setIncludesfile(File incl) throws BuildException { 299 if (isReference()) { 300 throw tooManyAttributes(); 301 } 302 defaultPatterns.setIncludesfile(incl); 303 directoryScanner = null; 304 } 305 306 312 public synchronized void setExcludesfile(File excl) throws BuildException { 313 if (isReference()) { 314 throw tooManyAttributes(); 315 } 316 defaultPatterns.setExcludesfile(excl); 317 directoryScanner = null; 318 } 319 320 325 public synchronized void setDefaultexcludes(boolean useDefaultExcludes) { 326 if (isReference()) { 327 throw tooManyAttributes(); 328 } 329 this.useDefaultExcludes = useDefaultExcludes; 330 directoryScanner = null; 331 } 332 333 338 public synchronized boolean getDefaultexcludes() { 339 return (isReference()) 340 ? getRef(getProject()).getDefaultexcludes() : useDefaultExcludes; 341 } 342 343 348 public synchronized void setCaseSensitive(boolean caseSensitive) { 349 if (isReference()) { 350 throw tooManyAttributes(); 351 } 352 this.caseSensitive = caseSensitive; 353 directoryScanner = null; 354 } 355 356 364 public synchronized boolean isCaseSensitive() { 365 return (isReference()) 366 ? getRef(getProject()).isCaseSensitive() : caseSensitive; 367 } 368 369 374 public synchronized void setFollowSymlinks(boolean followSymlinks) { 375 if (isReference()) { 376 throw tooManyAttributes(); 377 } 378 this.followSymlinks = followSymlinks; 379 directoryScanner = null; 380 } 381 382 390 public synchronized boolean isFollowSymlinks() { 391 return (isReference()) 392 ? getRef(getProject()).isFollowSymlinks() : followSymlinks; 393 } 394 395 399 public DirectoryScanner getDirectoryScanner() { 400 return getDirectoryScanner(getProject()); 401 } 402 403 408 public DirectoryScanner getDirectoryScanner(Project p) { 409 if (isReference()) { 410 return getRef(p).getDirectoryScanner(p); 411 } 412 DirectoryScanner ds = null; 413 synchronized (this) { 414 if (directoryScanner != null && p == getProject()) { 415 ds = directoryScanner; 416 } else { 417 if (dir == null) { 418 throw new BuildException("No directory specified for " 419 + getDataTypeName() + "."); 420 } 421 if (!dir.exists()) { 422 throw new BuildException(dir.getAbsolutePath() 423 + " not found."); 424 } 425 if (!dir.isDirectory()) { 426 throw new BuildException(dir.getAbsolutePath() 427 + " is not a directory."); 428 } 429 ds = new DirectoryScanner(); 430 setupDirectoryScanner(ds, p); 431 ds.setFollowSymlinks(followSymlinks); 432 directoryScanner = (p == getProject()) ? ds : directoryScanner; 433 } 434 } 435 ds.scan(); 436 return ds; 437 } 438 439 444 public void setupDirectoryScanner(FileScanner ds) { 445 setupDirectoryScanner(ds, getProject()); 446 } 447 448 453 public synchronized void setupDirectoryScanner(FileScanner ds, Project p) { 454 if (isReference()) { 455 getRef(p).setupDirectoryScanner(ds, p); 456 return; 457 } 458 if (ds == null) { 459 throw new IllegalArgumentException ("ds cannot be null"); 460 } 461 ds.setBasedir(dir); 462 463 PatternSet ps = mergePatterns(p); 464 p.log(getDataTypeName() + ": Setup scanner in dir " + dir 465 + " with " + ps, Project.MSG_DEBUG); 466 467 ds.setIncludes(ps.getIncludePatterns(p)); 468 ds.setExcludes(ps.getExcludePatterns(p)); 469 if (ds instanceof SelectorScanner) { 470 SelectorScanner ss = (SelectorScanner) ds; 471 ss.setSelectors(getSelectors(p)); 472 } 473 if (useDefaultExcludes) { 474 ds.addDefaultExcludes(); 475 } 476 ds.setCaseSensitive(caseSensitive); 477 } 478 479 485 protected AbstractFileSet getRef(Project p) { 486 return (AbstractFileSet) getCheckedRef(p); 487 } 488 489 491 496 public synchronized boolean hasSelectors() { 497 return (isReference() && getProject() != null) 498 ? getRef(getProject()).hasSelectors() : !(selectors.isEmpty()); 499 } 500 501 506 public synchronized boolean hasPatterns() { 507 if (isReference() && getProject() != null) { 508 return getRef(getProject()).hasPatterns(); 509 } 510 if (defaultPatterns.hasPatterns(getProject())) { 511 return true; 512 } 513 Enumeration e = additionalPatterns.elements(); 514 while (e.hasMoreElements()) { 515 PatternSet ps = (PatternSet) e.nextElement(); 516 if (ps.hasPatterns(getProject())) { 517 return true; 518 } 519 } 520 return false; 521 } 522 523 528 public synchronized int selectorCount() { 529 return (isReference() && getProject() != null) 530 ? getRef(getProject()).selectorCount() : selectors.size(); 531 } 532 533 538 public synchronized FileSelector[] getSelectors(Project p) { 539 return (isReference()) 540 ? getRef(p).getSelectors(p) : (FileSelector[]) (selectors.toArray( 541 new FileSelector[selectors.size()])); 542 } 543 544 549 public synchronized Enumeration selectorElements() { 550 return (isReference() && getProject() != null) 551 ? getRef(getProject()).selectorElements() : selectors.elements(); 552 } 553 554 559 public synchronized void appendSelector(FileSelector selector) { 560 if (isReference()) { 561 throw noChildrenAllowed(); 562 } 563 selectors.addElement(selector); 564 directoryScanner = null; 565 } 566 567 568 569 573 public void addSelector(SelectSelector selector) { 574 appendSelector(selector); 575 } 576 577 581 public void addAnd(AndSelector selector) { 582 appendSelector(selector); 583 } 584 585 589 public void addOr(OrSelector selector) { 590 appendSelector(selector); 591 } 592 593 597 public void addNot(NotSelector selector) { 598 appendSelector(selector); 599 } 600 601 605 public void addNone(NoneSelector selector) { 606 appendSelector(selector); 607 } 608 609 613 public void addMajority(MajoritySelector selector) { 614 appendSelector(selector); 615 } 616 617 621 public void addDate(DateSelector selector) { 622 appendSelector(selector); 623 } 624 625 629 public void addSize(SizeSelector selector) { 630 appendSelector(selector); 631 } 632 633 637 public void addDifferent(DifferentSelector selector) { 638 appendSelector(selector); 639 } 640 641 645 public void addFilename(FilenameSelector selector) { 646 appendSelector(selector); 647 } 648 649 653 public void addType(TypeSelector selector) { 654 appendSelector(selector); 655 } 656 657 661 public void addCustom(ExtendSelector selector) { 662 appendSelector(selector); 663 } 664 665 669 public void addContains(ContainsSelector selector) { 670 appendSelector(selector); 671 } 672 673 677 public void addPresent(PresentSelector selector) { 678 appendSelector(selector); 679 } 680 681 685 public void addDepth(DepthSelector selector) { 686 appendSelector(selector); 687 } 688 689 693 public void addDepend(DependSelector selector) { 694 appendSelector(selector); 695 } 696 697 701 public void addContainsRegexp(ContainsRegexpSelector selector) { 702 appendSelector(selector); 703 } 704 705 710 public void addModified(ModifiedSelector selector) { 711 appendSelector(selector); 712 } 713 714 719 public void add(FileSelector selector) { 720 appendSelector(selector); 721 } 722 723 728 public String toString() { 729 DirectoryScanner ds = getDirectoryScanner(getProject()); 730 String [] files = ds.getIncludedFiles(); 731 StringBuffer sb = new StringBuffer (); 732 733 for (int i = 0; i < files.length; i++) { 734 if (i > 0) { 735 sb.append(';'); 736 } 737 sb.append(files[i]); 738 } 739 return sb.toString(); 740 } 741 742 749 public synchronized Object clone() { 750 if (isReference()) { 751 return (getRef(getProject())).clone(); 752 } else { 753 try { 754 AbstractFileSet fs = (AbstractFileSet) super.clone(); 755 fs.defaultPatterns = (PatternSet) defaultPatterns.clone(); 756 fs.additionalPatterns = new Vector (additionalPatterns.size()); 757 Enumeration e = additionalPatterns.elements(); 758 while (e.hasMoreElements()) { 759 fs.additionalPatterns 760 .addElement(((PatternSet) e.nextElement()).clone()); 761 } 762 fs.selectors = new Vector (selectors); 763 return fs; 764 } catch (CloneNotSupportedException e) { 765 throw new BuildException(e); 766 } 767 } 768 } 769 770 778 public String [] mergeIncludes(Project p) { 779 return mergePatterns(p).getIncludePatterns(p); 780 } 781 782 790 public String [] mergeExcludes(Project p) { 791 return mergePatterns(p).getExcludePatterns(p); 792 } 793 794 802 public synchronized PatternSet mergePatterns(Project p) { 803 if (isReference()) { 804 return getRef(p).mergePatterns(p); 805 } 806 PatternSet ps = (PatternSet) defaultPatterns.clone(); 807 final int count = additionalPatterns.size(); 808 for (int i = 0; i < count; i++) { 809 Object o = additionalPatterns.elementAt(i); 810 ps.append((PatternSet) o, p); 811 } 812 return ps; 813 } 814 815 } 816 | Popular Tags |