1 54 package org.apache.tools.ant.types; 55 56 import java.io.File ; 57 import java.util.Enumeration ; 58 import java.util.Hashtable ; 59 import java.util.Stack ; 60 import java.util.Vector ; 61 62 import org.apache.tools.ant.BuildException; 63 import org.apache.tools.ant.DirectoryScanner; 64 import org.apache.tools.ant.FileScanner; 65 import org.apache.tools.ant.Project; 66 import org.apache.tools.ant.types.AbstractFileSet; 67 import org.apache.tools.ant.types.DataType; 68 import org.apache.tools.ant.types.PatternSet; 69 import org.apache.tools.ant.types.Reference; 70 import org.apache.tools.ant.types.selectors.AndSelector; 71 import org.apache.tools.ant.types.selectors.ContainsRegexpSelector; 72 import org.apache.tools.ant.types.selectors.ContainsSelector; 73 import org.apache.tools.ant.types.selectors.DateSelector; 74 import org.apache.tools.ant.types.selectors.DependSelector; 75 import org.apache.tools.ant.types.selectors.DepthSelector; 76 import org.apache.tools.ant.types.selectors.DifferentSelector; 77 import org.apache.tools.ant.types.selectors.ExtendSelector; 78 import org.apache.tools.ant.types.selectors.FileSelector; 79 import org.apache.tools.ant.types.selectors.FilenameSelector; 80 import org.apache.tools.ant.types.selectors.MajoritySelector; 81 import org.apache.tools.ant.types.selectors.NoneSelector; 82 import org.apache.tools.ant.types.selectors.NotSelector; 83 import org.apache.tools.ant.types.selectors.OrSelector; 84 import org.apache.tools.ant.types.selectors.PresentSelector; 85 import org.apache.tools.ant.types.selectors.SelectSelector; 86 import org.apache.tools.ant.types.selectors.SelectorContainer; 87 import org.apache.tools.ant.types.selectors.SelectorScanner; 88 import org.apache.tools.ant.types.selectors.SizeSelector; 89 import org.apache.tools.ant.types.selectors.TypeSelector; 90 import org.apache.tools.ant.types.selectors.modifiedselector.ModifiedSelector; 91 92 107 public class DummyFileSet extends DataType implements Cloneable , 108 SelectorContainer { 109 110 private PatternSet defaultPatterns = new PatternSet(); 111 private Vector additionalPatterns = new Vector (); 112 private Vector selectors = new Vector (); 113 114 private File dir; 115 private boolean useDefaultExcludes = true; 116 private boolean isCaseSensitive = true; 117 private boolean followSymlinks = true; 118 119 public DummyFileSet() { 120 super(); 121 } 122 123 129 public void setRefid(Reference r) throws BuildException { 130 if (dir != null || defaultPatterns.hasPatterns(getProject())) { 131 throw tooManyAttributes(); 132 } 133 if (!additionalPatterns.isEmpty()) { 134 throw noChildrenAllowed(); 135 } 136 if (!selectors.isEmpty()) { 137 throw noChildrenAllowed(); 138 } 139 super.setRefid(r); 140 } 141 142 145 public void setDir(File dir) throws BuildException { 146 if (isReference()) { 147 throw tooManyAttributes(); 148 } 149 150 this.dir = dir; 151 } 152 153 156 public File getDir(Project p) { 157 if (isReference()) { 158 return getRef(p).getDir(p); 159 } 160 return dir; 161 } 162 163 166 public PatternSet createPatternSet() { 167 if (isReference()) { 168 throw noChildrenAllowed(); 169 } 170 PatternSet patterns = new PatternSet(); 171 additionalPatterns.addElement(patterns); 172 return patterns; 173 } 174 175 178 public PatternSet.NameEntry createInclude() { 179 if (isReference()) { 180 throw noChildrenAllowed(); 181 } 182 return defaultPatterns.createInclude(); 183 } 184 185 188 public PatternSet.NameEntry createIncludesFile() { 189 if (isReference()) { 190 throw noChildrenAllowed(); 191 } 192 return defaultPatterns.createIncludesFile(); 193 } 194 195 198 public PatternSet.NameEntry createExclude() { 199 if (isReference()) { 200 throw noChildrenAllowed(); 201 } 202 return defaultPatterns.createExclude(); 203 } 204 205 208 public PatternSet.NameEntry createExcludesFile() { 209 if (isReference()) { 210 throw noChildrenAllowed(); 211 } 212 return defaultPatterns.createExcludesFile(); 213 } 214 215 218 public void setFile(File file) { 219 if (isReference()) { 220 throw tooManyAttributes(); 221 } 222 setDir(file.getParentFile()); 223 224 PatternSet.NameEntry include = createInclude(); 225 include.setName(file.getName()); 226 } 227 228 236 public void setIncludes(String includes) { 237 if (isReference()) { 238 throw tooManyAttributes(); 239 } 240 241 defaultPatterns.setIncludes(includes); 242 } 243 244 252 public void setExcludes(String excludes) { 253 if (isReference()) { 254 throw tooManyAttributes(); 255 } 256 257 defaultPatterns.setExcludes(excludes); 258 } 259 260 265 public void setIncludesfile(File incl) throws BuildException { 266 if (isReference()) { 267 throw tooManyAttributes(); 268 } 269 270 defaultPatterns.setIncludesfile(incl); 271 } 272 273 278 public void setExcludesfile(File excl) throws BuildException { 279 if (isReference()) { 280 throw tooManyAttributes(); 281 } 282 283 defaultPatterns.setExcludesfile(excl); 284 } 285 286 293 public void setDefaultexcludes(boolean useDefaultExcludes) { 294 if (isReference()) { 295 throw tooManyAttributes(); 296 } 297 298 this.useDefaultExcludes = useDefaultExcludes; 299 } 300 301 307 public void setCaseSensitive(boolean isCaseSensitive) { 308 if (isReference()) { 309 throw tooManyAttributes(); 310 } 311 this.isCaseSensitive = isCaseSensitive; 312 } 313 314 319 public void setFollowSymlinks(boolean followSymlinks) { 320 if (isReference()) { 321 throw tooManyAttributes(); 322 } 323 this.followSymlinks = followSymlinks; 324 } 325 326 333 public boolean isFollowSymlinks() { 334 if (isReference()) { 335 return getRef(getProject()).isFollowSymlinks(); 336 } else { 337 return followSymlinks; 338 } 339 } 340 341 344 protected String getDataTypeName() { 345 Project project = getProject(); 347 if (project != null) { 348 Hashtable typedefs = project.getDataTypeDefinitions(); 349 for (Enumeration e = typedefs.keys(); e.hasMoreElements();) { 350 String typeName = (String ) e.nextElement(); 351 Class typeClass = (Class ) typedefs.get(typeName); 352 if (typeClass == getClass()) { 353 return typeName; 354 } 355 } 356 } 357 358 String classname = getClass().getName(); 359 360 int dotIndex = classname.lastIndexOf("."); 361 if (dotIndex == -1) { 362 return classname; 363 } 364 return classname.substring(dotIndex + 1); 365 } 366 367 370 public DirectoryScanner getDirectoryScanner(Project p) { 371 if (isReference()) { 372 return getRef(p).getDirectoryScanner(p); 373 } 374 375 if (dir == null) { 376 throw new BuildException("No directory specified for " 377 + getDataTypeName() + "."); 378 } 379 380 if (!dir.exists()) { 381 throw new BuildException(dir.getAbsolutePath() + " not found."); 382 } 383 if (!dir.isDirectory()) { 384 throw new BuildException(dir.getAbsolutePath() 385 + " is not a directory."); 386 } 387 388 DirectoryScanner ds = new DirectoryScanner(); 389 setupDirectoryScanner(ds, p); 390 ds.setFollowSymlinks(followSymlinks); 391 ds.scan(); 392 return ds; 393 } 394 395 public void setupDirectoryScanner(FileScanner ds, Project p) { 396 if (isReference()) { 397 getRef(p).setupDirectoryScanner(ds, p); 398 return; 399 } 400 401 if (ds == null) { 402 throw new IllegalArgumentException ("ds cannot be null"); 403 } 404 405 ds.setBasedir(dir); 406 407 final int count = additionalPatterns.size(); 408 for (int i = 0; i < count; i++) { 409 Object o = additionalPatterns.elementAt(i); 410 defaultPatterns.append((PatternSet) o, p); 411 } 412 413 p.log(getDataTypeName() + ": Setup scanner in dir " + dir 414 + " with " + defaultPatterns, Project.MSG_DEBUG); 415 416 ds.setIncludes(defaultPatterns.getIncludePatterns(p)); 417 ds.setExcludes(defaultPatterns.getExcludePatterns(p)); 418 if (ds instanceof SelectorScanner) { 419 SelectorScanner ss = (SelectorScanner) ds; 420 ss.setSelectors(getSelectors(p)); 421 } 422 423 if (useDefaultExcludes) { 424 ds.addDefaultExcludes(); 425 } 426 ds.setCaseSensitive(isCaseSensitive); 427 } 428 429 433 protected AbstractFileSet getRef(Project p) { 434 if (!isChecked()) { 435 Stack stk = new Stack (); 436 stk.push(this); 437 dieOnCircularReference(stk, p); 438 } 439 440 Object o = getRefid().getReferencedObject(p); 441 if (!getClass().isAssignableFrom(o.getClass())) { 442 String msg = getRefid().getRefId() + " doesn\'t denote a " 443 + getDataTypeName(); 444 throw new BuildException(msg); 445 } else { 446 return (AbstractFileSet) o; 447 } 448 } 449 450 452 457 public boolean hasSelectors() { 458 if (isReference() && getProject() != null) { 459 return getRef(getProject()).hasSelectors(); 460 } 461 return !(selectors.isEmpty()); 462 } 463 464 469 public boolean hasPatterns() { 470 if (isReference() && getProject() != null) { 471 return getRef(getProject()).hasPatterns(); 472 } 473 474 if (defaultPatterns.hasPatterns(getProject())) { 475 return true; 476 } 477 478 Enumeration e = additionalPatterns.elements(); 479 while (e.hasMoreElements()) { 480 PatternSet ps = (PatternSet) e.nextElement(); 481 if (ps.hasPatterns(getProject())) { 482 return true; 483 } 484 } 485 486 return false; 487 } 488 489 494 public int selectorCount() { 495 if (isReference() && getProject() != null) { 496 return getRef(getProject()).selectorCount(); 497 } 498 return selectors.size(); 499 } 500 501 506 public FileSelector[] getSelectors(Project p) { 507 if (isReference()) { 508 return getRef(p).getSelectors(p); 509 } else { 510 FileSelector[] result = new FileSelector[selectors.size()]; 511 selectors.copyInto(result); 512 return result; 513 } 514 } 515 516 521 public Enumeration selectorElements() { 522 if (isReference() && getProject() != null) { 523 return getRef(getProject()).selectorElements(); 524 } 525 return selectors.elements(); 526 } 527 528 533 public void appendSelector(FileSelector selector) { 534 if (isReference()) { 535 throw noChildrenAllowed(); 536 } 537 selectors.addElement(selector); 538 } 539 540 541 542 546 public void addSelector(SelectSelector selector) { 547 appendSelector(selector); 548 } 549 550 554 public void addAnd(AndSelector selector) { 555 appendSelector(selector); 556 } 557 558 562 public void addOr(OrSelector selector) { 563 appendSelector(selector); 564 } 565 566 570 public void addNot(NotSelector selector) { 571 appendSelector(selector); 572 } 573 574 578 public void addNone(NoneSelector selector) { 579 appendSelector(selector); 580 } 581 582 586 public void addMajority(MajoritySelector selector) { 587 appendSelector(selector); 588 } 589 590 594 public void addDate(DateSelector selector) { 595 appendSelector(selector); 596 } 597 598 602 public void addSize(SizeSelector selector) { 603 appendSelector(selector); 604 } 605 606 610 public void addDifferent(DifferentSelector selector) { 611 appendSelector(selector); 612 } 613 614 618 public void addFilename(FilenameSelector selector) { 619 appendSelector(selector); 620 } 621 622 626 public void addType(TypeSelector selector) { 627 appendSelector(selector); 628 } 629 630 634 public void addCustom(ExtendSelector selector) { 635 appendSelector(selector); 636 } 637 638 642 public void addContains(ContainsSelector selector) { 643 appendSelector(selector); 644 } 645 646 650 public void addPresent(PresentSelector selector) { 651 appendSelector(selector); 652 } 653 654 658 public void addDepth(DepthSelector selector) { 659 appendSelector(selector); 660 } 661 662 666 public void addDepend(DependSelector selector) { 667 appendSelector(selector); 668 } 669 670 674 public void addContainsRegexp(ContainsRegexpSelector selector) { 675 appendSelector(selector); 676 } 677 678 683 public void addModified(ModifiedSelector selector) { 684 appendSelector(selector); 685 } 686 687 692 public void add(FileSelector selector) { 693 appendSelector(selector); 694 } 695 696 701 public String toString() { 702 StringBuffer sb = new StringBuffer (); 703 sb.append("DefaultPatternset:\n"); 704 sb.append(defaultPatterns.toString()); 705 return sb.toString(); 706 } 707 708 } 709 | Popular Tags |