1 18 package org.apache.tools.ant.types; 19 20 import java.io.File ; 21 import java.util.Iterator ; 22 import org.apache.tools.ant.BuildException; 23 import org.apache.tools.ant.DirectoryScanner; 24 import org.apache.tools.ant.Project; 25 import org.apache.tools.ant.types.resources.FileResource; 26 import org.apache.tools.zip.UnixStat; 27 28 38 public abstract class ArchiveFileSet extends FileSet { 39 40 private static final int BASE_OCTAL = 8; 41 42 47 public static final int DEFAULT_DIR_MODE = 48 UnixStat.DIR_FLAG | UnixStat.DEFAULT_DIR_PERM; 49 50 55 public static final int DEFAULT_FILE_MODE = 56 UnixStat.FILE_FLAG | UnixStat.DEFAULT_FILE_PERM; 57 58 private Resource src = null; 59 private String prefix = ""; 60 private String fullpath = ""; 61 private boolean hasDir = false; 62 private int fileMode = DEFAULT_FILE_MODE; 63 private int dirMode = DEFAULT_DIR_MODE; 64 65 private boolean fileModeHasBeenSet = false; 66 private boolean dirModeHasBeenSet = false; 67 68 69 public ArchiveFileSet() { 70 super(); 71 } 72 73 77 protected ArchiveFileSet(FileSet fileset) { 78 super(fileset); 79 } 80 81 85 protected ArchiveFileSet(ArchiveFileSet fileset) { 86 super(fileset); 87 src = fileset.src; 88 prefix = fileset.prefix; 89 fullpath = fileset.fullpath; 90 hasDir = fileset.hasDir; 91 fileMode = fileset.fileMode; 92 dirMode = fileset.dirMode; 93 fileModeHasBeenSet = fileset.fileModeHasBeenSet; 94 dirModeHasBeenSet = fileset.dirModeHasBeenSet; 95 } 96 97 102 public void setDir(File dir) throws BuildException { 103 checkAttributesAllowed(); 104 if (src != null) { 105 throw new BuildException("Cannot set both dir and src attributes"); 106 } else { 107 super.setDir(dir); 108 hasDir = true; 109 } 110 } 111 112 117 public void addConfigured(ResourceCollection a) { 118 checkChildrenAllowed(); 119 if (a.size() != 1) { 120 throw new BuildException("only single argument resource collections" 121 + " are supported as archives"); 122 } 123 setSrcResource((Resource) a.iterator().next()); 124 } 125 126 132 public void setSrc(File srcFile) { 133 setSrcResource(new FileResource(srcFile)); 134 } 135 136 142 public void setSrcResource(Resource src) { 143 checkArchiveAttributesAllowed(); 144 if (hasDir) { 145 throw new BuildException("Cannot set both dir and src attributes"); 146 } 147 this.src = src; 148 } 149 150 155 public File getSrc(Project p) { 156 if (isReference()) { 157 return ((ArchiveFileSet) getRef(p)).getSrc(p); 158 } 159 return getSrc(); 160 } 161 162 166 public File getSrc() { 167 if (src instanceof FileResource) { 168 return ((FileResource) src).getFile(); 169 } 170 return null; 171 } 172 173 179 public void setPrefix(String prefix) { 180 checkArchiveAttributesAllowed(); 181 if (!prefix.equals("") && !fullpath.equals("")) { 182 throw new BuildException("Cannot set both fullpath and prefix attributes"); 183 } 184 this.prefix = prefix; 185 } 186 187 192 public String getPrefix(Project p) { 193 if (isReference()) { 194 return ((ArchiveFileSet) getRef(p)).getPrefix(p); 195 } 196 return prefix; 197 } 198 199 205 public void setFullpath(String fullpath) { 206 checkArchiveAttributesAllowed(); 207 if (!prefix.equals("") && !fullpath.equals("")) { 208 throw new BuildException("Cannot set both fullpath and prefix attributes"); 209 } 210 this.fullpath = fullpath; 211 } 212 213 218 public String getFullpath(Project p) { 219 if (isReference()) { 220 return ((ArchiveFileSet) getRef(p)).getFullpath(p); 221 } 222 return fullpath; 223 } 224 225 229 protected abstract ArchiveScanner newArchiveScanner(); 230 231 238 public DirectoryScanner getDirectoryScanner(Project p) { 239 if (isReference()) { 240 return getRef(p).getDirectoryScanner(p); 241 } 242 if (src == null) { 243 return super.getDirectoryScanner(p); 244 } 245 if (!src.isExists()) { 246 throw new BuildException("the archive doesn't exist"); 247 } 248 if (src.isDirectory()) { 249 throw new BuildException("the archive can't be a directory"); 250 } 251 ArchiveScanner as = newArchiveScanner(); 252 as.setSrc(src); 253 super.setDir(p.getBaseDir()); 254 setupDirectoryScanner(as, p); 255 as.init(); 256 return as; 257 } 258 259 264 public Iterator iterator() { 265 if (isReference()) { 266 return ((ResourceCollection) (getRef(getProject()))).iterator(); 267 } 268 if (src == null) { 269 return super.iterator(); 270 } 271 ArchiveScanner as = (ArchiveScanner) getDirectoryScanner(getProject()); 272 return as.getResourceFiles(); 273 } 274 275 280 public int size() { 281 if (isReference()) { 282 return ((ResourceCollection) (getRef(getProject()))).size(); 283 } 284 if (src == null) { 285 return super.size(); 286 } 287 ArchiveScanner as = (ArchiveScanner) getDirectoryScanner(getProject()); 288 return as.getIncludedFilesCount(); 289 } 290 291 299 public boolean isFilesystemOnly() { 300 return src == null; 301 } 302 303 309 public void setFileMode(String octalString) { 310 checkArchiveAttributesAllowed(); 311 integerSetFileMode(Integer.parseInt(octalString, BASE_OCTAL)); 312 } 313 314 324 public void integerSetFileMode(int mode) { 325 fileModeHasBeenSet = true; 326 this.fileMode = UnixStat.FILE_FLAG | mode; 327 } 328 329 334 public int getFileMode(Project p) { 335 if (isReference()) { 336 return ((ArchiveFileSet) getRef(p)).getFileMode(p); 337 } 338 return fileMode; 339 } 340 341 345 public boolean hasFileModeBeenSet() { 346 if (isReference()) { 347 return ((ArchiveFileSet) getRef(getProject())).hasFileModeBeenSet(); 348 } 349 return fileModeHasBeenSet; 350 } 351 352 358 public void setDirMode(String octalString) { 359 checkArchiveAttributesAllowed(); 360 integerSetDirMode(Integer.parseInt(octalString, BASE_OCTAL)); 361 } 362 363 372 public void integerSetDirMode(int mode) { 373 dirModeHasBeenSet = true; 374 this.dirMode = UnixStat.DIR_FLAG | mode; 375 } 376 377 382 public int getDirMode(Project p) { 383 if (isReference()) { 384 return ((ArchiveFileSet) getRef(p)).getDirMode(p); 385 } 386 return dirMode; 387 } 388 389 394 public boolean hasDirModeBeenSet() { 395 if (isReference()) { 396 return ((ArchiveFileSet) getRef(getProject())).hasDirModeBeenSet(); 397 } 398 return dirModeHasBeenSet; 399 } 400 401 406 protected void configureFileSet(ArchiveFileSet zfs) { 407 zfs.setPrefix(prefix); 408 zfs.setFullpath(fullpath); 409 zfs.fileModeHasBeenSet = fileModeHasBeenSet; 410 zfs.fileMode = fileMode; 411 zfs.dirModeHasBeenSet = dirModeHasBeenSet; 412 zfs.dirMode = dirMode; 413 } 414 415 421 public Object clone() { 422 if (isReference()) { 423 return ((ArchiveFileSet) getRef(getProject())).clone(); 424 } else { 425 return super.clone(); 426 } 427 } 428 429 435 public String toString() { 436 if (hasDir && getProject() != null) { 437 return super.toString(); 438 } else if (src != null) { 439 return src.getName(); 440 } else { 441 return null; 442 } 443 } 444 445 450 public String getPrefix() { 451 return prefix; 452 } 453 454 459 public String getFullpath() { 460 return fullpath; 461 } 462 463 467 public int getFileMode() { 468 return fileMode; 469 } 470 471 475 public int getDirMode() { 476 return dirMode; 477 } 478 479 487 private void checkArchiveAttributesAllowed() { 488 if (getProject() == null 489 || (isReference() 490 && (getRefid().getReferencedObject( 491 getProject()) 492 instanceof ArchiveFileSet))) { 493 checkAttributesAllowed(); 494 } 495 } 496 } 497 | Popular Tags |