KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > ArchiveFileSet


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.types;
19
20 import java.io.File JavaDoc;
21 import java.util.Iterator JavaDoc;
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 /**
29  * A ArchiveFileSet is a FileSet with extra attributes useful in the
30  * context of archiving tasks.
31  *
32  * It includes a prefix attribute which is prepended to each entry in
33  * the output archive file as well as a fullpath ttribute. It also
34  * supports Unix file permissions for files and directories.
35  *
36  * @since Ant 1.7
37  */

38 public abstract class ArchiveFileSet extends FileSet {
39
40     private static final int BASE_OCTAL = 8;
41
42     /**
43      * Default value for the dirmode attribute.
44      *
45      * @since Ant 1.5.2
46      */

47     public static final int DEFAULT_DIR_MODE =
48         UnixStat.DIR_FLAG | UnixStat.DEFAULT_DIR_PERM;
49
50     /**
51      * Default value for the filemode attribute.
52      *
53      * @since Ant 1.5.2
54      */

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 JavaDoc prefix = "";
60     private String JavaDoc 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     /** Constructor for ArchiveFileSet */
69     public ArchiveFileSet() {
70         super();
71     }
72
73     /**
74      * Constructor using a fileset arguement.
75      * @param fileset the fileset to use
76      */

77     protected ArchiveFileSet(FileSet fileset) {
78         super(fileset);
79     }
80
81     /**
82      * Constructor using a archive fileset arguement.
83      * @param fileset the archivefileset to use
84      */

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     /**
98      * Set the directory for the fileset.
99      * @param dir the directory for the fileset
100      * @throws BuildException on error
101      */

102     public void setDir(File JavaDoc 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     /**
113      * Set the source Archive file for the archivefileset. Prevents both
114      * "dir" and "src" from being specified.
115      * @param a the archive as a single element Resource collection.
116      */

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     /**
127      * Set the source Archive file for the archivefileset. Prevents both
128      * "dir" and "src" from being specified.
129      *
130      * @param srcFile The archive from which to extract entries.
131      */

132     public void setSrc(File JavaDoc srcFile) {
133         setSrcResource(new FileResource(srcFile));
134     }
135
136     /**
137      * Set the source Archive file for the archivefileset. Prevents both
138      * "dir" and "src" from being specified.
139      *
140      * @param src The archive from which to extract entries.
141      */

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     /**
151      * Get the archive from which entries will be extracted.
152      * @param p the project to use
153      * @return the source file
154      */

155     public File JavaDoc getSrc(Project p) {
156         if (isReference()) {
157             return ((ArchiveFileSet) getRef(p)).getSrc(p);
158         }
159         return getSrc();
160     }
161
162     /**
163      * Get the archive file from which entries will be extracted.
164      * @return the archive in case the archive is a file, null otherwise.
165      */

166     public File JavaDoc getSrc() {
167         if (src instanceof FileResource) {
168             return ((FileResource) src).getFile();
169         }
170         return null;
171     }
172
173     /**
174      * Prepend this prefix to the path for each archive entry.
175      * Prevents both prefix and fullpath from being specified
176      *
177      * @param prefix The prefix to prepend to entries in the archive file.
178      */

179     public void setPrefix(String JavaDoc 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     /**
188      * Return the prefix prepended to entries in the archive file.
189      * @param p the project to use
190      * @return the prefix
191      */

192     public String JavaDoc getPrefix(Project p) {
193         if (isReference()) {
194             return ((ArchiveFileSet) getRef(p)).getPrefix(p);
195         }
196         return prefix;
197     }
198
199     /**
200      * Set the full pathname of the single entry in this fileset.
201      * Prevents both prefix and fullpath from being specified
202      *
203      * @param fullpath the full pathname of the single entry in this fileset.
204      */

205     public void setFullpath(String JavaDoc 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     /**
214      * Return the full pathname of the single entry in this fileset.
215      * @param p the project to use
216      * @return the full path
217      */

218     public String JavaDoc getFullpath(Project p) {
219         if (isReference()) {
220             return ((ArchiveFileSet) getRef(p)).getFullpath(p);
221         }
222         return fullpath;
223     }
224
225     /**
226      * Creates a scanner for this type of archive.
227      * @return the scanner.
228      */

229     protected abstract ArchiveScanner newArchiveScanner();
230
231     /**
232      * Return the DirectoryScanner associated with this FileSet.
233      * If the ArchiveFileSet defines a source Archive file, then a ArchiveScanner
234      * is returned instead.
235      * @param p the project to use
236      * @return a directory scanner
237      */

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     /**
260      * Fulfill the ResourceCollection contract.
261      * @return Iterator of Resources.
262      * @since Ant 1.7
263      */

264     public Iterator JavaDoc 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     /**
276      * Fulfill the ResourceCollection contract.
277      * @return size of the collection as int.
278      * @since Ant 1.7
279      */

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     /**
292      * Indicate whether this ResourceCollection is composed entirely of
293      * Resources accessible via local filesystem conventions. If true,
294      * all Resources returned from this ResourceCollection should be
295      * instances of FileResource.
296      * @return whether this is a filesystem-only resource collection.
297      * @since Ant 1.7
298      */

299     public boolean isFilesystemOnly() {
300         return src == null;
301     }
302
303     /**
304      * A 3 digit octal string, specify the user, group and
305      * other modes in the standard Unix fashion;
306      * optional, default=0644
307      * @param octalString a <code>String</code> value
308      */

309     public void setFileMode(String JavaDoc octalString) {
310         checkArchiveAttributesAllowed();
311         integerSetFileMode(Integer.parseInt(octalString, BASE_OCTAL));
312     }
313
314     /**
315      * specify the user, group and
316      * other modes in the standard Unix fashion;
317      * optional, default=0644
318      *
319      * <p>We use the strange name so this method doesn't appear in
320      * IntrospectionHelpers list of attribute setters.</p>
321      * @param mode a <code>int</code> value
322      * @since Ant 1.7
323      */

324     public void integerSetFileMode(int mode) {
325         fileModeHasBeenSet = true;
326         this.fileMode = UnixStat.FILE_FLAG | mode;
327     }
328
329     /**
330      * Get the mode of the archive fileset
331      * @param p the project to use
332      * @return the mode
333      */

334     public int getFileMode(Project p) {
335         if (isReference()) {
336             return ((ArchiveFileSet) getRef(p)).getFileMode(p);
337         }
338         return fileMode;
339     }
340
341     /**
342      * Whether the user has specified the mode explicitly.
343      * @return true if it has been set
344      */

345     public boolean hasFileModeBeenSet() {
346         if (isReference()) {
347             return ((ArchiveFileSet) getRef(getProject())).hasFileModeBeenSet();
348         }
349         return fileModeHasBeenSet;
350     }
351
352     /**
353      * A 3 digit octal string, specify the user, group and
354      * other modes in the standard Unix fashion;
355      * optional, default=0755
356      * @param octalString a <code>String</code> value
357      */

358     public void setDirMode(String JavaDoc octalString) {
359         checkArchiveAttributesAllowed();
360         integerSetDirMode(Integer.parseInt(octalString, BASE_OCTAL));
361     }
362
363     /**
364      * specify the user, group and
365      * other modes in the standard Unix fashion;
366      * optional, default=0755
367      * <p>We use the strange name so this method doesn't appear in
368      * IntrospectionHelpers list of attribute setters.</p>
369      * @param mode a <code>int</code> value
370      * @since Ant 1.7
371      */

372     public void integerSetDirMode(int mode) {
373         dirModeHasBeenSet = true;
374         this.dirMode = UnixStat.DIR_FLAG | mode;
375     }
376
377     /**
378      * Get the dir mode of the archive fileset
379      * @param p the project to use
380      * @return the mode
381      */

382     public int getDirMode(Project p) {
383         if (isReference()) {
384             return ((ArchiveFileSet) getRef(p)).getDirMode(p);
385         }
386         return dirMode;
387     }
388
389     /**
390      * Whether the user has specified the mode explicitly.
391      *
392      * @return true if it has been set
393      */

394     public boolean hasDirModeBeenSet() {
395         if (isReference()) {
396             return ((ArchiveFileSet) getRef(getProject())).hasDirModeBeenSet();
397         }
398         return dirModeHasBeenSet;
399     }
400
401     /**
402      * A ArchiveFileset accepts another ArchiveFileSet or a FileSet as reference
403      * FileSets are often used by the war task for the lib attribute
404      * @param zfs the project to use
405      */

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     /**
416      * Return a ArchiveFileSet that has the same properties
417      * as this one.
418      * @return the cloned archiveFileSet
419      * @since Ant 1.6
420      */

421     public Object JavaDoc clone() {
422         if (isReference()) {
423             return ((ArchiveFileSet) getRef(getProject())).clone();
424         } else {
425             return super.clone();
426         }
427     }
428
429     /**
430      * for file based zipfilesets, return the same as for normal filesets
431      * else just return the path of the zip
432      * @return for file based archivefilesets, included files as a list
433      * of semicolon-separated filenames. else just the name of the zip.
434      */

435     public String JavaDoc 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     /**
446      * Return the prefix prepended to entries in the archive file.
447      * @return the prefix.
448      * @deprecated since 1.7.
449      */

450     public String JavaDoc getPrefix() {
451         return prefix;
452     }
453
454     /**
455      * Return the full pathname of the single entryZ in this fileset.
456      * @return the full pathname.
457      * @deprecated since 1.7.
458      */

459     public String JavaDoc getFullpath() {
460         return fullpath;
461     }
462
463     /**
464      * @return the file mode.
465      * @deprecated since 1.7.
466      */

467     public int getFileMode() {
468         return fileMode;
469     }
470
471     /**
472      * @return the dir mode.
473      * @deprecated since 1.7.
474      */

475     public int getDirMode() {
476         return dirMode;
477     }
478
479     /**
480      * A check attributes for archiveFileSet.
481      * If there is a reference, and
482      * it is a ArchiveFileSet, the archive fileset attributes
483      * cannot be used.
484      * (Note, we can only see if the reference is an archive
485      * fileset if the project has been set).
486      */

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