KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.tools.ant.BuildException;
21 import org.apache.tools.ant.Project;
22
23 /**
24  * A ZipFileSet is a FileSet with extra attributes useful in the context of
25  * Zip/Jar tasks.
26  *
27  * A ZipFileSet extends FileSets with the ability to extract a subset of the
28  * entries of a Zip file for inclusion in another Zip file. It also includes
29  * a prefix attribute which is prepended to each entry in the output Zip file.
30  *
31  * Since ant 1.6 ZipFileSet can be defined with an id and referenced in packaging tasks
32  *
33  */

34 public class ZipFileSet extends ArchiveFileSet {
35
36     private String JavaDoc encoding = null;
37
38     /** Constructor for ZipFileSet */
39     public ZipFileSet() {
40         super();
41     }
42
43     /**
44      * Constructor using a fileset arguement.
45      * @param fileset the fileset to use
46      */

47     protected ZipFileSet(FileSet fileset) {
48         super(fileset);
49     }
50
51     /**
52      * Constructor using a zipfileset arguement.
53      * @param fileset the zipfileset to use
54      */

55     protected ZipFileSet(ZipFileSet fileset) {
56         super(fileset);
57         encoding = fileset.encoding;
58     }
59
60     /**
61      * Set the encoding used for this ZipFileSet.
62      * @param enc encoding as String.
63      * @since Ant 1.7
64      */

65     public void setEncoding(String JavaDoc enc) {
66         checkZipFileSetAttributesAllowed();
67         this.encoding = enc;
68     }
69
70     /**
71      * Get the encoding used for this ZipFileSet.
72      * @return String encoding.
73      * @since Ant 1.7
74      */

75     public String JavaDoc getEncoding() {
76         if (isReference()) {
77             AbstractFileSet ref = getRef(getProject());
78             if (ref instanceof ZipFileSet) {
79                 return ((ZipFileSet) ref).getEncoding();
80             } else {
81                 return null;
82             }
83         }
84         return encoding;
85     }
86
87     /**
88      * Return a new archive scanner based on this one.
89      * @return a new ZipScanner with the same encoding as this one.
90      */

91     protected ArchiveScanner newArchiveScanner() {
92         ZipScanner zs = new ZipScanner();
93         zs.setEncoding(encoding);
94         return zs;
95     }
96
97     /**
98      * A ZipFileset accepts another ZipFileSet or a FileSet as reference
99      * FileSets are often used by the war task for the lib attribute
100      * @param p the project to use
101      * @return the abstract fileset instance
102      */

103     protected AbstractFileSet getRef(Project p) {
104         dieOnCircularReference(p);
105         Object JavaDoc o = getRefid().getReferencedObject(p);
106         if (o instanceof ZipFileSet) {
107             return (AbstractFileSet) o;
108         } else if (o instanceof FileSet) {
109             ZipFileSet zfs = new ZipFileSet((FileSet) o);
110             configureFileSet(zfs);
111             return zfs;
112         } else {
113             String JavaDoc msg = getRefid().getRefId() + " doesn\'t denote a zipfileset or a fileset";
114             throw new BuildException(msg);
115         }
116     }
117
118     /**
119      * Return a ZipFileSet that has the same properties
120      * as this one.
121      * @return the cloned zipFileSet
122      */

123     public Object JavaDoc clone() {
124         if (isReference()) {
125             return ((ZipFileSet) getRef(getProject())).clone();
126         } else {
127             return super.clone();
128         }
129     }
130
131     /**
132      * A check attributes for zipFileSet.
133      * If there is a reference, and
134      * it is a ZipFileSet, the zip fileset attributes
135      * cannot be used.
136      */

137     private void checkZipFileSetAttributesAllowed() {
138         if (getProject() == null
139             || (isReference()
140                 && (getRefid().getReferencedObject(
141                         getProject())
142                     instanceof ZipFileSet))) {
143             checkAttributesAllowed();
144         }
145     }
146
147 }
148
Popular Tags