KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > ant > zip > ZipFileSet


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

17 package net.sourceforge.groboutils.codecoverage.v2.ant.zip;
18
19 import java.io.File JavaDoc;
20 import java.util.Stack JavaDoc;
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.DirectoryScanner;
23 import org.apache.tools.ant.Project;
24 import org.apache.tools.ant.types.Reference;
25
26 // this originally came from here, in Ant 1.6.1
27
import org.apache.tools.ant.types.FileSet;
28 //import org.apache.tools.ant.types.ZipScanner;
29
import org.apache.tools.ant.types.AbstractFileSet;
30
31 /**
32  * A ZipFileSet is a FileSet with extra attributes useful in the context of
33  * Zip/Jar tasks.
34  *
35  * A ZipFileSet extends FileSets with the ability to extract a subset of the
36  * entries of a Zip file for inclusion in another Zip file. It also includes
37  * a prefix attribute which is prepended to each entry in the output Zip file.
38  *
39  * Since ant 1.6 ZipFileSet can be defined with an id and referenced in packaging tasks
40  *
41  * @author Don Ferguson <a HREF="mailto:don@bea.com">don@bea.com</a>
42  * @author <a HREF="mailto:levylambert@tiscali-dsl.de">Antoine Levy-Lambert</a>
43  */

44 public class ZipFileSet extends FileSet {
45
46     /**
47      * Default value for the dirmode attribute.
48      *
49      * @since Ant 1.5.2
50      */

51     public static final int DEFAULT_DIR_MODE =
52         UnixStat.DIR_FLAG | UnixStat.DEFAULT_DIR_PERM;
53
54     /**
55      * Default value for the filemode attribute.
56      *
57      * @since Ant 1.5.2
58      */

59     public static final int DEFAULT_FILE_MODE =
60         UnixStat.FILE_FLAG | UnixStat.DEFAULT_FILE_PERM;
61
62     private File JavaDoc srcFile = null;
63     private String JavaDoc prefix = "";
64     private String JavaDoc fullpath = "";
65     private boolean hasDir = false;
66     private int fileMode = DEFAULT_FILE_MODE;
67     private int dirMode = DEFAULT_DIR_MODE;
68
69     private boolean fileModeHasBeenSet = false;
70     private boolean dirModeHasBeenSet = false;
71
72     public ZipFileSet() {
73         super();
74     }
75
76     protected ZipFileSet(FileSet fileset) {
77         super(fileset);
78     }
79
80     protected ZipFileSet(ZipFileSet fileset) {
81         super(fileset);
82         srcFile = fileset.srcFile;
83         prefix = fileset.prefix;
84         fullpath = fileset.fullpath;
85         hasDir = fileset.hasDir;
86         fileMode = fileset.fileMode;
87         dirMode = fileset.dirMode;
88         fileModeHasBeenSet = fileset.fileModeHasBeenSet;
89         dirModeHasBeenSet = fileset.dirModeHasBeenSet;
90     }
91
92     /**
93      * Set the directory for the fileset. Prevents both "dir" and "src"
94      * from being specified.
95      */

96     public void setDir(File JavaDoc dir) throws BuildException {
97         if (isReference()) {
98              throw tooManyAttributes();
99          }
100         if (srcFile != null) {
101             throw new BuildException("Cannot set both dir and src attributes");
102         } else {
103             super.setDir(dir);
104             hasDir = true;
105         }
106     }
107
108     /**
109      * Set the source Zip file for the zipfileset. Prevents both
110      * "dir" and "src" from being specified.
111      *
112      * @param srcFile The zip file from which to extract entries.
113      */

114     public void setSrc(File JavaDoc srcFile) {
115         if (isReference()) {
116              throw tooManyAttributes();
117          }
118         if (hasDir) {
119             throw new BuildException("Cannot set both dir and src attributes");
120         }
121         this.srcFile = srcFile;
122     }
123
124     /**
125      * Get the zip file from which entries will be extracted.
126      * References are not followed, since it is not possible
127      * to have a reference to a ZipFileSet, only to a FileSet.
128      */

129     public File JavaDoc getSrc(Project p) {
130         if (isReference()) {
131             return ((ZipFileSet) getRef(p)).getSrc(p);
132         }
133         return srcFile;
134     }
135
136     /**
137      * Prepend this prefix to the path for each zip entry.
138      * Prevents both prefix and fullpath from being specified
139      *
140      * @param prefix The prefix to prepend to entries in the zip file.
141      */

142     public void setPrefix(String JavaDoc prefix) {
143         if (!prefix.equals("") && !fullpath.equals("")) {
144             throw new BuildException("Cannot set both fullpath and prefix attributes");
145         }
146         this.prefix = prefix;
147     }
148
149     /**
150      * Return the prefix prepended to entries in the zip file.
151      */

152     public String JavaDoc getPrefix(Project p) {
153         if (isReference()) {
154             return ((ZipFileSet) getRef(p)).getPrefix(p);
155         }
156         return prefix;
157     }
158
159     /**
160      * Set the full pathname of the single entry in this fileset.
161      * Prevents both prefix and fullpath from being specified
162      *
163      * @param fullpath the full pathname of the single entry in this fileset.
164      */

165     public void setFullpath(String JavaDoc fullpath) {
166         if (!prefix.equals("") && !fullpath.equals("")) {
167             throw new BuildException("Cannot set both fullpath and prefix attributes");
168         }
169         this.fullpath = fullpath;
170     }
171
172     /**
173      * Return the full pathname of the single entry in this fileset.
174      */

175     public String JavaDoc getFullpath(Project p) {
176         if (isReference()) {
177             return ((ZipFileSet) getRef(p)).getFullpath(p);
178         }
179         return fullpath;
180     }
181
182     /**
183      * Return the DirectoryScanner associated with this FileSet.
184      * If the ZipFileSet defines a source Zip file, then a ZipScanner
185      * is returned instead.
186      */

187     public DirectoryScanner getDirectoryScanner(Project p) {
188         if (isReference()) {
189             return getRef(p).getDirectoryScanner(p);
190         }
191         if (srcFile != null) {
192             ZipScanner zs = new ZipScanner();
193             zs.setSrc(srcFile);
194             super.setDir(p.getBaseDir());
195             setupDirectoryScanner(zs, p);
196             zs.init();
197             return zs;
198         } else {
199             return super.getDirectoryScanner(p);
200         }
201     }
202
203     /**
204      * A 3 digit octal string, specify the user, group and
205      * other modes in the standard Unix fashion;
206      * optional, default=0644
207      *
208      * @since Ant 1.5.2
209      */

210     public void setFileMode(String JavaDoc octalString) {
211         fileModeHasBeenSet = true;
212         this.fileMode =
213             UnixStat.FILE_FLAG | Integer.parseInt(octalString, 8);
214     }
215
216     /**
217      * @since Ant 1.5.2
218      */

219     public int getFileMode(Project p) {
220         if (isReference()) {
221             return ((ZipFileSet) getRef(p)).getFileMode(p);
222         }
223         return fileMode;
224     }
225
226     /**
227      * Whether the user has specified the mode explicitly.
228      *
229      * @since Ant 1.6
230      */

231     public boolean hasFileModeBeenSet() {
232         if (isReference()) {
233             return ((ZipFileSet) getRef(getProject())).hasFileModeBeenSet();
234         }
235         return fileModeHasBeenSet;
236     }
237
238     /**
239      * A 3 digit octal string, specify the user, group and
240      * other modes in the standard Unix fashion;
241      * optional, default=0755
242      *
243      * @since Ant 1.5.2
244      */

245     public void setDirMode(String JavaDoc octalString) {
246         dirModeHasBeenSet = true;
247         this.dirMode =
248             UnixStat.DIR_FLAG | Integer.parseInt(octalString, 8);
249     }
250
251     /**
252      * @since Ant 1.5.2
253      */

254     public int getDirMode(Project p) {
255         if (isReference()) {
256             return ((ZipFileSet) getRef(p)).getDirMode(p);
257         }
258         return dirMode;
259     }
260
261     /**
262      * Whether the user has specified the mode explicitly.
263      *
264      * @since Ant 1.6
265      */

266     public boolean hasDirModeBeenSet() {
267         if (isReference()) {
268             return ((ZipFileSet) getRef(getProject())).hasDirModeBeenSet();
269         }
270         return dirModeHasBeenSet;
271     }
272
273     /**
274      * A ZipFileset accepts another ZipFileSet or a FileSet as reference
275      * FileSets are often used by the war task for the lib attribute
276      */

277     protected AbstractFileSet getRef(Project p) {
278         if (!isChecked()) {
279             Stack JavaDoc stk = new Stack JavaDoc();
280             stk.push(this);
281             dieOnCircularReference(stk, p);
282         }
283         Object JavaDoc o = getRefid().getReferencedObject(p);
284         if (o instanceof ZipFileSet) {
285             return (AbstractFileSet) o;
286         } else if (o instanceof FileSet) {
287             ZipFileSet zfs = new ZipFileSet((FileSet) o);
288             zfs.setPrefix(prefix);
289             zfs.setFullpath(fullpath);
290             zfs.fileModeHasBeenSet = fileModeHasBeenSet;
291             zfs.fileMode = fileMode;
292             zfs.dirModeHasBeenSet = dirModeHasBeenSet;
293             zfs.dirMode = dirMode;
294             return zfs;
295         } else {
296             String JavaDoc msg = getRefid().getRefId() + " doesn\'t denote a zipfileset or a fileset";
297             throw new BuildException(msg);
298         }
299     }
300     /**
301      * Return a ZipFileSet that has the same properties
302      * as this one.
303      * @since Ant 1.6
304      */

305     public Object JavaDoc clone() {
306         if (isReference()) {
307             return ((ZipFileSet) getRef(getProject())).clone();
308         } else {
309             return super.clone();
310         }
311     }
312     
313     
314     // added after Ant 1.5.1
315
public Reference getRefid() {
316         return this.ref;
317     }
318     
319     // added after Ant 1.5.1
320
public boolean isChecked() {
321         return this.checked;
322     }
323 }
324
Popular Tags