1 18 package org.apache.tools.ant.types.resources; 19 20 import java.io.File ; 21 import java.util.Vector ; 22 import java.util.Iterator ; 23 import java.util.Collections ; 24 25 import org.apache.tools.ant.Project; 26 import org.apache.tools.ant.BuildException; 27 import org.apache.tools.ant.DirectoryScanner; 28 import org.apache.tools.ant.types.Reference; 29 import org.apache.tools.ant.types.PatternSet; 30 import org.apache.tools.ant.types.ResourceCollection; 31 import org.apache.tools.ant.types.selectors.FileSelector; 32 import org.apache.tools.ant.types.selectors.AbstractSelectorContainer; 33 34 38 public class Files extends AbstractSelectorContainer 39 implements Cloneable , ResourceCollection { 40 41 private static final Iterator EMPTY_ITERATOR 42 = Collections.EMPTY_SET.iterator(); 43 44 private PatternSet defaultPatterns = new PatternSet(); 45 private Vector additionalPatterns = new Vector (); 46 private Vector selectors = new Vector (); 47 48 private boolean useDefaultExcludes = true; 49 private boolean caseSensitive = true; 50 private boolean followSymlinks = true; 51 52 53 private DirectoryScanner ds = null; 54 55 58 public Files() { 59 super(); 60 } 61 62 67 protected Files(Files f) { 68 this.defaultPatterns = f.defaultPatterns; 69 this.additionalPatterns = f.additionalPatterns; 70 this.selectors = f.selectors; 71 this.useDefaultExcludes = f.useDefaultExcludes; 72 this.caseSensitive = f.caseSensitive; 73 this.followSymlinks = f.followSymlinks; 74 this.ds = f.ds; 75 setProject(f.getProject()); 76 } 77 78 86 public void setRefid(Reference r) throws BuildException { 87 if (hasPatterns(defaultPatterns)) { 88 throw tooManyAttributes(); 89 } 90 if (!additionalPatterns.isEmpty()) { 91 throw noChildrenAllowed(); 92 } 93 if (!selectors.isEmpty()) { 94 throw noChildrenAllowed(); 95 } 96 super.setRefid(r); 97 } 98 99 103 public synchronized PatternSet createPatternSet() { 104 if (isReference()) { 105 throw noChildrenAllowed(); 106 } 107 PatternSet patterns = new PatternSet(); 108 additionalPatterns.addElement(patterns); 109 ds = null; 110 return patterns; 111 } 112 113 117 public synchronized PatternSet.NameEntry createInclude() { 118 if (isReference()) { 119 throw noChildrenAllowed(); 120 } 121 ds = null; 122 return defaultPatterns.createInclude(); 123 } 124 125 129 public synchronized PatternSet.NameEntry createIncludesFile() { 130 if (isReference()) { 131 throw noChildrenAllowed(); 132 } 133 ds = null; 134 return defaultPatterns.createIncludesFile(); 135 } 136 137 141 public synchronized PatternSet.NameEntry createExclude() { 142 if (isReference()) { 143 throw noChildrenAllowed(); 144 } 145 ds = null; 146 return defaultPatterns.createExclude(); 147 } 148 149 153 public synchronized PatternSet.NameEntry createExcludesFile() { 154 if (isReference()) { 155 throw noChildrenAllowed(); 156 } 157 ds = null; 158 return defaultPatterns.createExcludesFile(); 159 } 160 161 169 public synchronized void setIncludes(String includes) { 170 checkAttributesAllowed(); 171 defaultPatterns.setIncludes(includes); 172 ds = null; 173 } 174 175 181 public synchronized void appendIncludes(String [] includes) { 182 checkAttributesAllowed(); 183 if (includes != null) { 184 for (int i = 0; i < includes.length; i++) { 185 defaultPatterns.createInclude().setName(includes[i]); 186 } 187 ds = null; 188 } 189 } 190 191 199 public synchronized void setExcludes(String excludes) { 200 checkAttributesAllowed(); 201 defaultPatterns.setExcludes(excludes); 202 ds = null; 203 } 204 205 211 public synchronized void appendExcludes(String [] excludes) { 212 checkAttributesAllowed(); 213 if (excludes != null) { 214 for (int i = 0; i < excludes.length; i++) { 215 defaultPatterns.createExclude().setName(excludes[i]); 216 } 217 ds = null; 218 } 219 } 220 221 227 public synchronized void setIncludesfile(File incl) throws BuildException { 228 checkAttributesAllowed(); 229 defaultPatterns.setIncludesfile(incl); 230 ds = null; 231 } 232 233 239 public synchronized void setExcludesfile(File excl) throws BuildException { 240 checkAttributesAllowed(); 241 defaultPatterns.setExcludesfile(excl); 242 ds = null; 243 } 244 245 250 public synchronized void setDefaultexcludes(boolean useDefaultExcludes) { 251 checkAttributesAllowed(); 252 this.useDefaultExcludes = useDefaultExcludes; 253 ds = null; 254 } 255 256 260 public synchronized boolean getDefaultexcludes() { 261 return (isReference()) 262 ? getRef().getDefaultexcludes() : useDefaultExcludes; 263 } 264 265 270 public synchronized void setCaseSensitive(boolean caseSensitive) { 271 checkAttributesAllowed(); 272 this.caseSensitive = caseSensitive; 273 ds = null; 274 } 275 276 282 public synchronized boolean isCaseSensitive() { 283 return (isReference()) 284 ? getRef().isCaseSensitive() : caseSensitive; 285 } 286 287 292 public synchronized void setFollowSymlinks(boolean followSymlinks) { 293 checkAttributesAllowed(); 294 this.followSymlinks = followSymlinks; 295 ds = null; 296 } 297 298 304 public synchronized boolean isFollowSymlinks() { 305 return (isReference()) 306 ? getRef().isFollowSymlinks() : followSymlinks; 307 } 308 309 313 public synchronized Iterator iterator() { 314 if (isReference()) { 315 return getRef().iterator(); 316 } 317 ensureDirectoryScannerSetup(); 318 ds.scan(); 319 int fct = ds.getIncludedFilesCount(); 320 int dct = ds.getIncludedDirsCount(); 321 if (fct + dct == 0) { 322 return EMPTY_ITERATOR; 323 } 324 FileResourceIterator result = new FileResourceIterator(); 325 if (fct > 0) { 326 result.addFiles(ds.getIncludedFiles()); 327 } 328 if (dct > 0) { 329 result.addFiles(ds.getIncludedDirectories()); 330 } 331 return result; 332 } 333 334 338 public synchronized int size() { 339 if (isReference()) { 340 return getRef().size(); 341 } 342 ensureDirectoryScannerSetup(); 343 ds.scan(); 344 return ds.getIncludedFilesCount() + ds.getIncludedDirsCount(); 345 } 346 347 352 public synchronized boolean hasPatterns() { 353 if (isReference()) { 354 return getRef().hasPatterns(); 355 } 356 if (hasPatterns(defaultPatterns)) { 357 return true; 358 } 359 for (Iterator i = additionalPatterns.iterator(); i.hasNext();) { 360 if (hasPatterns((PatternSet) i.next())) { 361 return true; 362 } 363 } 364 return false; 365 } 366 367 372 public synchronized void appendSelector(FileSelector selector) { 373 if (isReference()) { 374 throw noChildrenAllowed(); 375 } 376 super.appendSelector(selector); 377 ds = null; 378 } 379 380 384 public String toString() { 385 if (isReference()) { 386 return getRef().toString(); 387 } 388 Iterator i = iterator(); 389 if (!i.hasNext()) { 390 return ""; 391 } 392 StringBuffer sb = new StringBuffer (); 393 while (i.hasNext()) { 394 if (sb.length() > 0) { 395 sb.append(File.pathSeparatorChar); 396 } 397 sb.append(i.next()); 398 } 399 return sb.toString(); 400 } 401 402 407 public synchronized Object clone() { 408 if (isReference()) { 409 return getRef().clone(); 410 } 411 try { 412 Files f = (Files) super.clone(); 413 f.defaultPatterns = (PatternSet) defaultPatterns.clone(); 414 f.additionalPatterns = new Vector (additionalPatterns.size()); 415 for (Iterator iter = additionalPatterns.iterator(); iter.hasNext();) { 416 PatternSet ps = (PatternSet) iter.next(); 417 f.additionalPatterns.add(ps.clone()); 418 } 419 f.selectors = new Vector (selectors); 420 return f; 421 } catch (CloneNotSupportedException e) { 422 throw new BuildException(e); 423 } 424 } 425 426 432 public String [] mergeIncludes(Project p) { 433 return mergePatterns(p).getIncludePatterns(p); 434 } 435 436 442 public String [] mergeExcludes(Project p) { 443 return mergePatterns(p).getExcludePatterns(p); 444 } 445 446 452 public synchronized PatternSet mergePatterns(Project p) { 453 if (isReference()) { 454 return getRef().mergePatterns(p); 455 } 456 PatternSet ps = new PatternSet(); 457 ps.append(defaultPatterns, p); 458 final int count = additionalPatterns.size(); 459 for (int i = 0; i < count; i++) { 460 Object o = additionalPatterns.elementAt(i); 461 ps.append((PatternSet) o, p); 462 } 463 return ps; 464 } 465 466 471 public boolean isFilesystemOnly() { 472 return true; 473 } 474 475 480 protected Files getRef() { 481 return (Files) getCheckedRef(); 482 } 483 484 private synchronized void ensureDirectoryScannerSetup() { 485 if (ds == null) { 486 ds = new DirectoryScanner(); 487 PatternSet ps = mergePatterns(getProject()); 488 ds.setIncludes(ps.getIncludePatterns(getProject())); 489 ds.setExcludes(ps.getExcludePatterns(getProject())); 490 ds.setSelectors(getSelectors(getProject())); 491 if (useDefaultExcludes) { 492 ds.addDefaultExcludes(); 493 } 494 ds.setCaseSensitive(caseSensitive); 495 ds.setFollowSymlinks(followSymlinks); 496 } 497 } 498 499 private boolean hasPatterns(PatternSet ps) { 500 return ps.getIncludePatterns(getProject()).length > 0 501 || ps.getExcludePatterns(getProject()).length > 0; 502 } 503 504 } 505 | Popular Tags |