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 org.apache.tools.ant.types;
|
---|
18 |
|
---|
19 | import java.io.File;
|
---|
20 | import java.util.Stack;
|
---|
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.zip.UnixStat;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * A ZipFileSet is a FileSet with extra attributes useful in the context of
|
---|
28 | * Zip/Jar tasks.
|
---|
29 | *
|
---|
30 | * A ZipFileSet extends FileSets with the ability to extract a subset of the
|
---|
31 | * entries of a Zip file for inclusion in another Zip file. It also includes
|
---|
32 | * a prefix attribute which is prepended to each entry in the output Zip file.
|
---|
33 | *
|
---|
34 | * Since ant 1.6 ZipFileSet can be defined with an id and referenced in packaging tasks
|
---|
35 | *
|
---|
36 | */
|
---|
37 | public class ZipFileSet extends FileSet {
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Default value for the dirmode attribute.
|
---|
41 | *
|
---|
42 | * @since Ant 1.5.2
|
---|
43 | */
|
---|
44 | public static final int DEFAULT_DIR_MODE =
|
---|
45 | UnixStat.DIR_FLAG | UnixStat.DEFAULT_DIR_PERM;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Default value for the filemode attribute.
|
---|
49 | *
|
---|
50 | * @since Ant 1.5.2
|
---|
51 | */
|
---|
52 | public static final int DEFAULT_FILE_MODE =
|
---|
53 | UnixStat.FILE_FLAG | UnixStat.DEFAULT_FILE_PERM;
|
---|
54 |
|
---|
55 | private File srcFile = null;
|
---|
56 | private String prefix = "";
|
---|
57 | private String fullpath = "";
|
---|
58 | private boolean hasDir = false;
|
---|
59 | private int fileMode = DEFAULT_FILE_MODE;
|
---|
60 | private int dirMode = DEFAULT_DIR_MODE;
|
---|
61 |
|
---|
62 | private boolean fileModeHasBeenSet = false;
|
---|
63 | private boolean dirModeHasBeenSet = false;
|
---|
64 |
|
---|
65 | public ZipFileSet() {
|
---|
66 | super();
|
---|
67 | }
|
---|
68 |
|
---|
69 | protected ZipFileSet(FileSet fileset) {
|
---|
70 | super(fileset);
|
---|
71 | }
|
---|
72 |
|
---|
73 | protected ZipFileSet(ZipFileSet fileset) {
|
---|
74 | super(fileset);
|
---|
75 | srcFile = fileset.srcFile;
|
---|
76 | prefix = fileset.prefix;
|
---|
77 | fullpath = fileset.fullpath;
|
---|
78 | hasDir = fileset.hasDir;
|
---|
79 | fileMode = fileset.fileMode;
|
---|
80 | dirMode = fileset.dirMode;
|
---|
81 | fileModeHasBeenSet = fileset.fileModeHasBeenSet;
|
---|
82 | dirModeHasBeenSet = fileset.dirModeHasBeenSet;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Set the directory for the fileset. Prevents both "dir" and "src"
|
---|
87 | * from being specified.
|
---|
88 | */
|
---|
89 | public void setDir(File dir) throws BuildException {
|
---|
90 | if (isReference()) {
|
---|
91 | throw tooManyAttributes();
|
---|
92 | }
|
---|
93 | if (srcFile != null) {
|
---|
94 | throw new BuildException("Cannot set both dir and src attributes");
|
---|
95 | } else {
|
---|
96 | super.setDir(dir);
|
---|
97 | hasDir = true;
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Set the source Zip file for the zipfileset. Prevents both
|
---|
103 | * "dir" and "src" from being specified.
|
---|
104 | *
|
---|
105 | * @param srcFile The zip file from which to extract entries.
|
---|
106 | */
|
---|
107 | public void setSrc(File srcFile) {
|
---|
108 | if (isReference()) {
|
---|
109 | throw tooManyAttributes();
|
---|
110 | }
|
---|
111 | if (hasDir) {
|
---|
112 | throw new BuildException("Cannot set both dir and src attributes");
|
---|
113 | }
|
---|
114 | this.srcFile = srcFile;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Get the zip file from which entries will be extracted.
|
---|
119 | * References are not followed, since it is not possible
|
---|
120 | * to have a reference to a ZipFileSet, only to a FileSet.
|
---|
121 | */
|
---|
122 | public File getSrc(Project p) {
|
---|
123 | if (isReference()) {
|
---|
124 | return ((ZipFileSet) getRef(p)).getSrc(p);
|
---|
125 | }
|
---|
126 | return srcFile;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Prepend this prefix to the path for each zip entry.
|
---|
131 | * Prevents both prefix and fullpath from being specified
|
---|
132 | *
|
---|
133 | * @param prefix The prefix to prepend to entries in the zip file.
|
---|
134 | */
|
---|
135 | public void setPrefix(String prefix) {
|
---|
136 | if (!prefix.equals("") && !fullpath.equals("")) {
|
---|
137 | throw new BuildException("Cannot set both fullpath and prefix attributes");
|
---|
138 | }
|
---|
139 | this.prefix = prefix;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Return the prefix prepended to entries in the zip file.
|
---|
144 | */
|
---|
145 | public String getPrefix(Project p) {
|
---|
146 | if (isReference()) {
|
---|
147 | return ((ZipFileSet) getRef(p)).getPrefix(p);
|
---|
148 | }
|
---|
149 | return prefix;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Set the full pathname of the single entry in this fileset.
|
---|
154 | * Prevents both prefix and fullpath from being specified
|
---|
155 | *
|
---|
156 | * @param fullpath the full pathname of the single entry in this fileset.
|
---|
157 | */
|
---|
158 | public void setFullpath(String fullpath) {
|
---|
159 | if (!prefix.equals("") && !fullpath.equals("")) {
|
---|
160 | throw new BuildException("Cannot set both fullpath and prefix attributes");
|
---|
161 | }
|
---|
162 | this.fullpath = fullpath;
|
---|
163 | }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Return the full pathname of the single entry in this fileset.
|
---|
167 | */
|
---|
168 | public String getFullpath(Project p) {
|
---|
169 | if (isReference()) {
|
---|
170 | return ((ZipFileSet) getRef(p)).getFullpath(p);
|
---|
171 | }
|
---|
172 | return fullpath;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Return the DirectoryScanner associated with this FileSet.
|
---|
177 | * If the ZipFileSet defines a source Zip file, then a ZipScanner
|
---|
178 | * is returned instead.
|
---|
179 | */
|
---|
180 | public DirectoryScanner getDirectoryScanner(Project p) {
|
---|
181 | if (isReference()) {
|
---|
182 | return getRef(p).getDirectoryScanner(p);
|
---|
183 | }
|
---|
184 | if (srcFile != null) {
|
---|
185 | ZipScanner zs = new ZipScanner();
|
---|
186 | zs.setSrc(srcFile);
|
---|
187 | super.setDir(p.getBaseDir());
|
---|
188 | setupDirectoryScanner(zs, p);
|
---|
189 | zs.init();
|
---|
190 | return zs;
|
---|
191 | } else {
|
---|
192 | return super.getDirectoryScanner(p);
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * A 3 digit octal string, specify the user, group and
|
---|
198 | * other modes in the standard Unix fashion;
|
---|
199 | * optional, default=0644
|
---|
200 | *
|
---|
201 | * @since Ant 1.5.2
|
---|
202 | */
|
---|
203 | public void setFileMode(String octalString) {
|
---|
204 | fileModeHasBeenSet = true;
|
---|
205 | this.fileMode =
|
---|
206 | UnixStat.FILE_FLAG | Integer.parseInt(octalString, 8);
|
---|
207 | }
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * @since Ant 1.5.2
|
---|
211 | */
|
---|
212 | public int getFileMode(Project p) {
|
---|
213 | if (isReference()) {
|
---|
214 | return ((ZipFileSet) getRef(p)).getFileMode(p);
|
---|
215 | }
|
---|
216 | return fileMode;
|
---|
217 | }
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Whether the user has specified the mode explicitly.
|
---|
221 | *
|
---|
222 | * @since Ant 1.6
|
---|
223 | */
|
---|
224 | public boolean hasFileModeBeenSet() {
|
---|
225 | if (isReference()) {
|
---|
226 | return ((ZipFileSet) getRef(getProject())).hasFileModeBeenSet();
|
---|
227 | }
|
---|
228 | return fileModeHasBeenSet;
|
---|
229 | }
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * A 3 digit octal string, specify the user, group and
|
---|
233 | * other modes in the standard Unix fashion;
|
---|
234 | * optional, default=0755
|
---|
235 | *
|
---|
236 | * @since Ant 1.5.2
|
---|
237 | */
|
---|
238 | public void setDirMode(String octalString) {
|
---|
239 | dirModeHasBeenSet = true;
|
---|
240 | this.dirMode =
|
---|
241 | UnixStat.DIR_FLAG | Integer.parseInt(octalString, 8);
|
---|
242 | }
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * @since Ant 1.5.2
|
---|
246 | */
|
---|
247 | public int getDirMode(Project p) {
|
---|
248 | if (isReference()) {
|
---|
249 | return ((ZipFileSet) getRef(p)).getDirMode(p);
|
---|
250 | }
|
---|
251 | return dirMode;
|
---|
252 | }
|
---|
253 |
|
---|
254 | /**
|
---|
255 | * Whether the user has specified the mode explicitly.
|
---|
256 | *
|
---|
257 | * @since Ant 1.6
|
---|
258 | */
|
---|
259 | public boolean hasDirModeBeenSet() {
|
---|
260 | if (isReference()) {
|
---|
261 | return ((ZipFileSet) getRef(getProject())).hasDirModeBeenSet();
|
---|
262 | }
|
---|
263 | return dirModeHasBeenSet;
|
---|
264 | }
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * A ZipFileset accepts another ZipFileSet or a FileSet as reference
|
---|
268 | * FileSets are often used by the war task for the lib attribute
|
---|
269 | */
|
---|
270 | protected AbstractFileSet getRef(Project p) {
|
---|
271 | if (!isChecked()) {
|
---|
272 | Stack stk = new Stack();
|
---|
273 | stk.push(this);
|
---|
274 | dieOnCircularReference(stk, p);
|
---|
275 | }
|
---|
276 | Object o = getRefid().getReferencedObject(p);
|
---|
277 | if (o instanceof ZipFileSet) {
|
---|
278 | return (AbstractFileSet) o;
|
---|
279 | } else if (o instanceof FileSet) {
|
---|
280 | ZipFileSet zfs = new ZipFileSet((FileSet) o);
|
---|
281 | zfs.setPrefix(prefix);
|
---|
282 | zfs.setFullpath(fullpath);
|
---|
283 | zfs.fileModeHasBeenSet = fileModeHasBeenSet;
|
---|
284 | zfs.fileMode = fileMode;
|
---|
285 | zfs.dirModeHasBeenSet = dirModeHasBeenSet;
|
---|
286 | zfs.dirMode = dirMode;
|
---|
287 | return zfs;
|
---|
288 | } else {
|
---|
289 | String msg = getRefid().getRefId() + " doesn\'t denote a zipfileset or a fileset";
|
---|
290 | throw new BuildException(msg);
|
---|
291 | }
|
---|
292 | }
|
---|
293 | /**
|
---|
294 | * Return a ZipFileSet that has the same properties
|
---|
295 | * as this one.
|
---|
296 | * @since Ant 1.6
|
---|
297 | */
|
---|
298 | public Object clone() {
|
---|
299 | if (isReference()) {
|
---|
300 | return ((ZipFileSet) getRef(getProject())).clone();
|
---|
301 | } else {
|
---|
302 | return super.clone();
|
---|
303 | }
|
---|
304 | }
|
---|
305 | }
|
---|