001: /*
002: * Copyright 2001-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017: package net.sourceforge.groboutils.codecoverage.v2.ant.zip;
018:
019: import java.io.File;
020: import java.util.Stack;
021: import org.apache.tools.ant.BuildException;
022: import org.apache.tools.ant.DirectoryScanner;
023: import org.apache.tools.ant.Project;
024: import org.apache.tools.ant.types.Reference;
025:
026: // this originally came from here, in Ant 1.6.1
027: import org.apache.tools.ant.types.FileSet; //import org.apache.tools.ant.types.ZipScanner;
028: import org.apache.tools.ant.types.AbstractFileSet;
029:
030: /**
031: * A ZipFileSet is a FileSet with extra attributes useful in the context of
032: * Zip/Jar tasks.
033: *
034: * A ZipFileSet extends FileSets with the ability to extract a subset of the
035: * entries of a Zip file for inclusion in another Zip file. It also includes
036: * a prefix attribute which is prepended to each entry in the output Zip file.
037: *
038: * Since ant 1.6 ZipFileSet can be defined with an id and referenced in packaging tasks
039: *
040: * @author Don Ferguson <a href="mailto:don@bea.com">don@bea.com</a>
041: * @author <a href="mailto:levylambert@tiscali-dsl.de">Antoine Levy-Lambert</a>
042: */
043: public class ZipFileSet extends FileSet {
044:
045: /**
046: * Default value for the dirmode attribute.
047: *
048: * @since Ant 1.5.2
049: */
050: public static final int DEFAULT_DIR_MODE = UnixStat.DIR_FLAG
051: | UnixStat.DEFAULT_DIR_PERM;
052:
053: /**
054: * Default value for the filemode attribute.
055: *
056: * @since Ant 1.5.2
057: */
058: public static final int DEFAULT_FILE_MODE = UnixStat.FILE_FLAG
059: | UnixStat.DEFAULT_FILE_PERM;
060:
061: private File srcFile = null;
062: private String prefix = "";
063: private String fullpath = "";
064: private boolean hasDir = false;
065: private int fileMode = DEFAULT_FILE_MODE;
066: private int dirMode = DEFAULT_DIR_MODE;
067:
068: private boolean fileModeHasBeenSet = false;
069: private boolean dirModeHasBeenSet = false;
070:
071: public ZipFileSet() {
072: super ();
073: }
074:
075: protected ZipFileSet(FileSet fileset) {
076: super (fileset);
077: }
078:
079: protected ZipFileSet(ZipFileSet fileset) {
080: super (fileset);
081: srcFile = fileset.srcFile;
082: prefix = fileset.prefix;
083: fullpath = fileset.fullpath;
084: hasDir = fileset.hasDir;
085: fileMode = fileset.fileMode;
086: dirMode = fileset.dirMode;
087: fileModeHasBeenSet = fileset.fileModeHasBeenSet;
088: dirModeHasBeenSet = fileset.dirModeHasBeenSet;
089: }
090:
091: /**
092: * Set the directory for the fileset. Prevents both "dir" and "src"
093: * from being specified.
094: */
095: public void setDir(File dir) throws BuildException {
096: if (isReference()) {
097: throw tooManyAttributes();
098: }
099: if (srcFile != null) {
100: throw new BuildException(
101: "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 srcFile) {
115: if (isReference()) {
116: throw tooManyAttributes();
117: }
118: if (hasDir) {
119: throw new BuildException(
120: "Cannot set both dir and src attributes");
121: }
122: this .srcFile = srcFile;
123: }
124:
125: /**
126: * Get the zip file from which entries will be extracted.
127: * References are not followed, since it is not possible
128: * to have a reference to a ZipFileSet, only to a FileSet.
129: */
130: public File getSrc(Project p) {
131: if (isReference()) {
132: return ((ZipFileSet) getRef(p)).getSrc(p);
133: }
134: return srcFile;
135: }
136:
137: /**
138: * Prepend this prefix to the path for each zip entry.
139: * Prevents both prefix and fullpath from being specified
140: *
141: * @param prefix The prefix to prepend to entries in the zip file.
142: */
143: public void setPrefix(String prefix) {
144: if (!prefix.equals("") && !fullpath.equals("")) {
145: throw new BuildException(
146: "Cannot set both fullpath and prefix attributes");
147: }
148: this .prefix = prefix;
149: }
150:
151: /**
152: * Return the prefix prepended to entries in the zip file.
153: */
154: public String getPrefix(Project p) {
155: if (isReference()) {
156: return ((ZipFileSet) getRef(p)).getPrefix(p);
157: }
158: return prefix;
159: }
160:
161: /**
162: * Set the full pathname of the single entry in this fileset.
163: * Prevents both prefix and fullpath from being specified
164: *
165: * @param fullpath the full pathname of the single entry in this fileset.
166: */
167: public void setFullpath(String fullpath) {
168: if (!prefix.equals("") && !fullpath.equals("")) {
169: throw new BuildException(
170: "Cannot set both fullpath and prefix attributes");
171: }
172: this .fullpath = fullpath;
173: }
174:
175: /**
176: * Return the full pathname of the single entry in this fileset.
177: */
178: public String getFullpath(Project p) {
179: if (isReference()) {
180: return ((ZipFileSet) getRef(p)).getFullpath(p);
181: }
182: return fullpath;
183: }
184:
185: /**
186: * Return the DirectoryScanner associated with this FileSet.
187: * If the ZipFileSet defines a source Zip file, then a ZipScanner
188: * is returned instead.
189: */
190: public DirectoryScanner getDirectoryScanner(Project p) {
191: if (isReference()) {
192: return getRef(p).getDirectoryScanner(p);
193: }
194: if (srcFile != null) {
195: ZipScanner zs = new ZipScanner();
196: zs.setSrc(srcFile);
197: super .setDir(p.getBaseDir());
198: setupDirectoryScanner(zs, p);
199: zs.init();
200: return zs;
201: } else {
202: return super .getDirectoryScanner(p);
203: }
204: }
205:
206: /**
207: * A 3 digit octal string, specify the user, group and
208: * other modes in the standard Unix fashion;
209: * optional, default=0644
210: *
211: * @since Ant 1.5.2
212: */
213: public void setFileMode(String octalString) {
214: fileModeHasBeenSet = true;
215: this .fileMode = UnixStat.FILE_FLAG
216: | Integer.parseInt(octalString, 8);
217: }
218:
219: /**
220: * @since Ant 1.5.2
221: */
222: public int getFileMode(Project p) {
223: if (isReference()) {
224: return ((ZipFileSet) getRef(p)).getFileMode(p);
225: }
226: return fileMode;
227: }
228:
229: /**
230: * Whether the user has specified the mode explicitly.
231: *
232: * @since Ant 1.6
233: */
234: public boolean hasFileModeBeenSet() {
235: if (isReference()) {
236: return ((ZipFileSet) getRef(getProject()))
237: .hasFileModeBeenSet();
238: }
239: return fileModeHasBeenSet;
240: }
241:
242: /**
243: * A 3 digit octal string, specify the user, group and
244: * other modes in the standard Unix fashion;
245: * optional, default=0755
246: *
247: * @since Ant 1.5.2
248: */
249: public void setDirMode(String octalString) {
250: dirModeHasBeenSet = true;
251: this .dirMode = UnixStat.DIR_FLAG
252: | Integer.parseInt(octalString, 8);
253: }
254:
255: /**
256: * @since Ant 1.5.2
257: */
258: public int getDirMode(Project p) {
259: if (isReference()) {
260: return ((ZipFileSet) getRef(p)).getDirMode(p);
261: }
262: return dirMode;
263: }
264:
265: /**
266: * Whether the user has specified the mode explicitly.
267: *
268: * @since Ant 1.6
269: */
270: public boolean hasDirModeBeenSet() {
271: if (isReference()) {
272: return ((ZipFileSet) getRef(getProject()))
273: .hasDirModeBeenSet();
274: }
275: return dirModeHasBeenSet;
276: }
277:
278: /**
279: * A ZipFileset accepts another ZipFileSet or a FileSet as reference
280: * FileSets are often used by the war task for the lib attribute
281: */
282: protected AbstractFileSet getRef(Project p) {
283: if (!isChecked()) {
284: Stack stk = new Stack();
285: stk.push(this );
286: dieOnCircularReference(stk, p);
287: }
288: Object o = getRefid().getReferencedObject(p);
289: if (o instanceof ZipFileSet) {
290: return (AbstractFileSet) o;
291: } else if (o instanceof FileSet) {
292: ZipFileSet zfs = new ZipFileSet((FileSet) o);
293: zfs.setPrefix(prefix);
294: zfs.setFullpath(fullpath);
295: zfs.fileModeHasBeenSet = fileModeHasBeenSet;
296: zfs.fileMode = fileMode;
297: zfs.dirModeHasBeenSet = dirModeHasBeenSet;
298: zfs.dirMode = dirMode;
299: return zfs;
300: } else {
301: String msg = getRefid().getRefId()
302: + " doesn\'t denote a zipfileset or a fileset";
303: throw new BuildException(msg);
304: }
305: }
306:
307: /**
308: * Return a ZipFileSet that has the same properties
309: * as this one.
310: * @since Ant 1.6
311: */
312: public Object clone() {
313: if (isReference()) {
314: return ((ZipFileSet) getRef(getProject())).clone();
315: } else {
316: return super .clone();
317: }
318: }
319:
320: // added after Ant 1.5.1
321: public Reference getRefid() {
322: return this .ref;
323: }
324:
325: // added after Ant 1.5.1
326: public boolean isChecked() {
327: return this.checked;
328: }
329: }
|