KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > resources > ArchiveResource


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.resources;
19
20 import java.io.File JavaDoc;
21
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.types.Resource;
24 import org.apache.tools.ant.types.ResourceCollection;
25 import org.apache.tools.ant.types.Reference;
26
27 /**
28  * A Resource representation of an entry inside an archive.
29  * @since Ant 1.7
30  */

31 public abstract class ArchiveResource extends Resource {
32     private static final int NULL_ARCHIVE
33         = Resource.getMagicNumber("null archive".getBytes());
34
35     private Resource archive;
36     private boolean haveEntry = false;
37     private boolean modeSet = false;
38     private int mode = 0;
39
40     /**
41      * Default constructor.
42      */

43     public ArchiveResource() {
44     }
45
46     /**
47      * Construct a ArchiveResource representing the specified
48      * entry in the specified archive.
49      * @param a the archive as File.
50      */

51     public ArchiveResource(File JavaDoc a) {
52         this(a, false);
53     }
54
55     /**
56      * Construct a ArchiveResource representing the specified
57      * entry in the specified archive.
58      * @param a the archive as File.
59      * @param withEntry if the entry has been specified.
60      */

61     public ArchiveResource(File JavaDoc a, boolean withEntry) {
62         setArchive(a);
63         haveEntry = withEntry;
64     }
65
66     /**
67      * Construct a ArchiveResource representing the specified
68      * entry in the specified archive.
69      * @param a the archive as Resource.
70      * @param withEntry if the entry has been specified.
71      */

72     public ArchiveResource(Resource a, boolean withEntry) {
73         addConfigured(a);
74         haveEntry = withEntry;
75     }
76
77     /**
78      * Set the archive that holds this Resource.
79      * @param a the archive as a File.
80      */

81     public void setArchive(File JavaDoc a) {
82         checkAttributesAllowed();
83         archive = new FileResource(a);
84     }
85
86     /**
87      * Sets the file or dir mode for this resource.
88      * @param mode integer representation of Unix permission mask.
89      */

90     public void setMode(int mode) {
91         checkAttributesAllowed();
92         this.mode = mode;
93         modeSet = true;
94     }
95
96     /**
97      * Sets the archive that holds this as a single element Resource
98      * collection.
99      * @param a the archive as a single element Resource collection.
100      */

101     public void addConfigured(ResourceCollection a) {
102         checkChildrenAllowed();
103         if (archive != null) {
104             throw new BuildException("you must not specify more than one"
105                                      + " archive");
106         }
107         if (a.size() != 1) {
108             throw new BuildException("only single argument resource collections"
109                                      + " are supported as archives");
110         }
111         archive = (Resource) a.iterator().next();
112     }
113
114     /**
115      * Get the archive that holds this Resource.
116      * @return the archive as a Resource.
117      */

118     public Resource getArchive() {
119         return isReference()
120             ? ((ArchiveResource) getCheckedRef()).getArchive() : archive;
121     }
122
123     /**
124      * Get the last modified date of this Resource.
125      * @return the last modification date.
126      */

127     public long getLastModified() {
128         if (isReference()) {
129             return ((Resource) getCheckedRef()).getLastModified();
130         }
131         checkEntry();
132         return super.getLastModified();
133     }
134
135     /**
136      * Get the size of this Resource.
137      * @return the long size of this Resource.
138      */

139     public long getSize() {
140         if (isReference()) {
141             return ((Resource) getCheckedRef()).getSize();
142         }
143         checkEntry();
144         return super.getSize();
145     }
146
147     /**
148      * Learn whether this Resource represents a directory.
149      * @return boolean flag indicating whether the entry is a directory.
150      */

151     public boolean isDirectory() {
152         if (isReference()) {
153             return ((Resource) getCheckedRef()).isDirectory();
154         }
155         checkEntry();
156         return super.isDirectory();
157     }
158
159     /**
160      * Find out whether this Resource represents an existing Resource.
161      * @return boolean existence flag.
162      */

163     public boolean isExists() {
164         if (isReference()) {
165             return ((Resource) getCheckedRef()).isExists();
166         }
167         checkEntry();
168         return super.isExists();
169     }
170
171     /**
172      * Get the file or dir mode for this Resource.
173      * @return integer representation of Unix permission mask.
174      */

175     public int getMode() {
176         if (isReference()) {
177             return ((ArchiveResource) getCheckedRef()).getMode();
178         }
179         checkEntry();
180         return mode;
181     }
182
183     /**
184      * Overrides the super version.
185      * @param r the Reference to set.
186      */

187     public void setRefid(Reference r) {
188         if (archive != null || modeSet) {
189             throw tooManyAttributes();
190         }
191         super.setRefid(r);
192     }
193
194     /**
195      * Compare this ArchiveResource to another Resource.
196      * @param another the other Resource against which to compare.
197      * @return a negative integer, zero, or a positive integer as this Resource
198      * is less than, equal to, or greater than the specified Resource.
199      */

200     public int compareTo(Object JavaDoc another) {
201         return this.equals(another) ? 0 : super.compareTo(another);
202     }
203
204     /**
205      * Compare another Object to this ArchiveResource for equality.
206      * @param another the other Object to compare.
207      * @return true if another is a Resource representing
208      * the same entry in the same archive.
209      */

210     public boolean equals(Object JavaDoc another) {
211         if (this == another) {
212             return true;
213         }
214         if (isReference()) {
215             return getCheckedRef().equals(another);
216         }
217         if (!(another.getClass().equals(getClass()))) {
218             return false;
219         }
220         ArchiveResource r = (ArchiveResource) another;
221         return getArchive().equals(r.getArchive())
222             && getName().equals(r.getName());
223     }
224
225     /**
226      * Get the hash code for this Resource.
227      * @return hash code as int.
228      */

229     public int hashCode() {
230         return super.hashCode()
231             * (getArchive() == null ? NULL_ARCHIVE : getArchive().hashCode());
232     }
233
234     /**
235      * Format this Resource as a String.
236      * @return String representatation of this Resource.
237      */

238     public String JavaDoc toString() {
239         return isReference() ? getCheckedRef().toString()
240             : getArchive().toString() + ':' + getName();
241     }
242
243     private synchronized void checkEntry() throws BuildException {
244         if (haveEntry) {
245             return;
246         }
247         String JavaDoc name = getName();
248         if (name == null) {
249             throw new BuildException("entry name not set");
250         }
251         Resource r = getArchive();
252         if (r == null) {
253             throw new BuildException("archive attribute not set");
254         }
255         if (!r.isExists()) {
256             throw new BuildException(r.toString() + " does not exist.");
257         }
258         if (r.isDirectory()) {
259             throw new BuildException(r + " denotes a directory.");
260         }
261         fetchEntry();
262         haveEntry = true;
263     }
264
265     /**
266      * fetches information from the named entry inside the archive.
267      */

268     protected abstract void fetchEntry();
269 }
270
Popular Tags