KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.InputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.math.BigInteger JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.NoSuchElementException JavaDoc;
26
27 /**
28  * Describes a "File-like" resource (File, ZipEntry, etc.).
29  *
30  * This class is meant to be used by classes needing to record path
31  * and date/time information about a file, a zip entry or some similar
32  * resource (URL, archive in a version control repository, ...).
33  *
34  * @since Ant 1.5.2
35  * @see org.apache.tools.ant.types.resources.Touchable
36  */

37 public class Resource extends DataType
38     implements Cloneable JavaDoc, Comparable JavaDoc, ResourceCollection {
39
40     /** Constant unknown size */
41     public static final long UNKNOWN_SIZE = -1;
42
43     /** Constant unknown datetime for getLastModified */
44     public static final long UNKNOWN_DATETIME = 0L;
45
46     /** Magic number */
47     protected static final int MAGIC = getMagicNumber("Resource".getBytes());
48
49     private static final int NULL_NAME = getMagicNumber("null name".getBytes());
50
51     /**
52      * Create a "magic number" for use in hashCode calculations.
53      * @param seed byte[] to seed with.
54      * @return a magic number as int.
55      */

56     protected static int getMagicNumber(byte[] seed) {
57         return new BigInteger JavaDoc(seed).intValue();
58     }
59
60     private String JavaDoc name = null;
61     private Boolean JavaDoc exists = null;
62     private Long JavaDoc lastmodified = null;
63     private Boolean JavaDoc directory = null;
64     private Long JavaDoc size = null;
65
66     /**
67      * Default constructor.
68      */

69     public Resource() {
70     }
71
72     /**
73      * Only sets the name.
74      *
75      * <p>This is a dummy, used for not existing resources.</p>
76      *
77      * @param name relative path of the resource. Expects
78      * &quot;/&quot; to be used as the directory separator.
79      */

80     public Resource(String JavaDoc name) {
81         this(name, false, 0, false);
82     }
83
84     /**
85      * Sets the name, lastmodified flag, and exists flag.
86      *
87      * @param name relative path of the resource. Expects
88      * &quot;/&quot; to be used as the directory separator.
89      * @param exists if true, this resource exists.
90      * @param lastmodified the last modification time of this resource.
91      */

92     public Resource(String JavaDoc name, boolean exists, long lastmodified) {
93         this(name, exists, lastmodified, false);
94     }
95
96     /**
97      * Sets the name, lastmodified flag, exists flag, and directory flag.
98      *
99      * @param name relative path of the resource. Expects
100      * &quot;/&quot; to be used as the directory separator.
101      * @param exists if true the resource exists
102      * @param lastmodified the last modification time of the resource
103      * @param directory if true, this resource is a directory
104      */

105     public Resource(String JavaDoc name, boolean exists, long lastmodified,
106                     boolean directory) {
107         this(name, exists, lastmodified, directory, UNKNOWN_SIZE);
108     }
109
110     /**
111      * Sets the name, lastmodified flag, exists flag, directory flag, and size.
112      *
113      * @param name relative path of the resource. Expects
114      * &quot;/&quot; to be used as the directory separator.
115      * @param exists if true the resource exists
116      * @param lastmodified the last modification time of the resource
117      * @param directory if true, this resource is a directory
118      * @param size the size of this resource.
119      */

120     public Resource(String JavaDoc name, boolean exists, long lastmodified,
121                     boolean directory, long size) {
122         this.name = name;
123         setName(name);
124         setExists(exists);
125         setLastModified(lastmodified);
126         setDirectory(directory);
127         setSize(size);
128     }
129
130     /**
131      * Name attribute will contain the path of a file relative to the
132      * root directory of its fileset or the recorded path of a zip
133      * entry.
134      *
135      * <p>example for a file with fullpath /var/opt/adm/resource.txt
136      * in a file set with root dir /var/opt it will be
137      * adm/resource.txt.</p>
138      *
139      * <p>&quot;/&quot; will be used as the directory separator.</p>
140      * @return the name of this resource.
141      */

142     public String JavaDoc getName() {
143         return isReference() ? ((Resource) getCheckedRef()).getName() : name;
144     }
145
146     /**
147      * Set the name of this Resource.
148      * @param name relative path of the resource. Expects
149      * &quot;/&quot; to be used as the directory separator.
150      */

151     public void setName(String JavaDoc name) {
152         checkAttributesAllowed();
153         this.name = name;
154     }
155
156     /**
157      * The exists attribute tells whether a file exists.
158      * @return true if this resource exists.
159      */

160     public boolean isExists() {
161         if (isReference()) {
162             return ((Resource) getCheckedRef()).isExists();
163         }
164         //default true:
165
return exists == null || exists.booleanValue();
166     }
167
168     /**
169      * Set the exists attribute.
170      * @param exists if true, this resource exists.
171      */

172     public void setExists(boolean exists) {
173         checkAttributesAllowed();
174         this.exists = exists ? Boolean.TRUE : Boolean.FALSE;
175     }
176
177     /**
178      * Tells the modification time in milliseconds since 01.01.1970 .
179      *
180      * @return 0 if the resource does not exist to mirror the behavior
181      * of {@link java.io.File File}.
182      */

183     public long getLastModified() {
184         if (isReference()) {
185             return ((Resource) getCheckedRef()).getLastModified();
186         }
187         if (!isExists() || lastmodified == null) {
188             return UNKNOWN_DATETIME;
189         }
190         long result = lastmodified.longValue();
191         return result < UNKNOWN_DATETIME ? UNKNOWN_DATETIME : result;
192     }
193
194     /**
195      * Set the last modification attribute.
196      * @param lastmodified the modification time in milliseconds since 01.01.1970.
197      */

198     public void setLastModified(long lastmodified) {
199         checkAttributesAllowed();
200         this.lastmodified = new Long JavaDoc(lastmodified);
201     }
202
203     /**
204      * Tells if the resource is a directory.
205      * @return boolean flag indicating if the resource is a directory.
206      */

207     public boolean isDirectory() {
208         if (isReference()) {
209             return ((Resource) getCheckedRef()).isDirectory();
210         }
211         //default false:
212
return directory != null && directory.booleanValue();
213     }
214
215     /**
216      * Set the directory attribute.
217      * @param directory if true, this resource is a directory.
218      */

219     public void setDirectory(boolean directory) {
220         checkAttributesAllowed();
221         this.directory = directory ? Boolean.TRUE : Boolean.FALSE;
222     }
223
224     /**
225      * Set the size of this Resource.
226      * @param size the size, as a long.
227      * @since Ant 1.6.3
228      */

229     public void setSize(long size) {
230         checkAttributesAllowed();
231         this.size = new Long JavaDoc(size > UNKNOWN_SIZE ? size : UNKNOWN_SIZE);
232     }
233
234     /**
235      * Get the size of this Resource.
236      * @return the size, as a long, 0 if the Resource does not exist (for
237      * compatibility with java.io.File), or UNKNOWN_SIZE if not known.
238      * @since Ant 1.6.3
239      */

240     public long getSize() {
241         if (isReference()) {
242             return ((Resource) getCheckedRef()).getSize();
243         }
244         return isExists()
245             ? (size != null ? size.longValue() : UNKNOWN_SIZE)
246             : 0L;
247     }
248
249     /**
250      * Clone this Resource.
251      * @return copy of this.
252      */

253     public Object JavaDoc clone() {
254         try {
255             return super.clone();
256         } catch (CloneNotSupportedException JavaDoc e) {
257             throw new UnsupportedOperationException JavaDoc(
258                     "CloneNotSupportedException for a Resource caught. "
259                     + "Derived classes must support cloning.");
260         }
261     }
262
263     /**
264      * Delegates to a comparison of names.
265      * @param other the object to compare to.
266      * @return a negative integer, zero, or a positive integer as this Resource
267      * is less than, equal to, or greater than the specified Resource.
268      * @since Ant 1.6
269      */

270     public int compareTo(Object JavaDoc other) {
271         if (isReference()) {
272             return ((Comparable JavaDoc) getCheckedRef()).compareTo(other);
273         }
274         if (!(other instanceof Resource)) {
275             throw new IllegalArgumentException JavaDoc(
276                 "Can only be compared with Resources");
277         }
278         return toString().compareTo(other.toString());
279     }
280
281     /**
282      * Implement basic Resource equality.
283      * @param other the object to check against.
284      * @return true if the specified Object is equal to this Resource.
285      * @since Ant 1.7
286      */

287     public boolean equals(Object JavaDoc other) {
288         if (isReference()) {
289             return getCheckedRef().equals(other);
290         }
291         return other.getClass().equals(getClass()) && compareTo(other) == 0;
292     }
293
294     /**
295      * Get the hash code for this Resource.
296      * @return hash code as int.
297      * @since Ant 1.7
298      */

299     public int hashCode() {
300         if (isReference()) {
301             return getCheckedRef().hashCode();
302         }
303         String JavaDoc name = getName();
304         return MAGIC * (name == null ? NULL_NAME : name.hashCode());
305     }
306
307     /**
308      * Get an InputStream for the Resource.
309      * @return an InputStream containing this Resource's content.
310      * @throws IOException if unable to provide the content of this
311      * Resource as a stream.
312      * @throws UnsupportedOperationException if InputStreams are not
313      * supported for this Resource type.
314      * @since Ant 1.7
315      */

316     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
317         if (isReference()) {
318             return ((Resource) getCheckedRef()).getInputStream();
319         }
320         throw new UnsupportedOperationException JavaDoc();
321     }
322
323     /**
324      * Get an OutputStream for the Resource.
325      * @return an OutputStream to which content can be written.
326      * @throws IOException if unable to provide the content of this
327      * Resource as a stream.
328      * @throws UnsupportedOperationException if OutputStreams are not
329      * supported for this Resource type.
330      * @since Ant 1.7
331      */

332     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
333         if (isReference()) {
334             return ((Resource) getCheckedRef()).getOutputStream();
335         }
336         throw new UnsupportedOperationException JavaDoc();
337     }
338
339     /**
340      * Fulfill the ResourceCollection contract.
341      * @return an Iterator of Resources.
342      * @since Ant 1.7
343      */

344     public Iterator JavaDoc iterator() {
345         return isReference() ? ((Resource) getCheckedRef()).iterator()
346             : new Iterator JavaDoc() {
347             private boolean done = false;
348             public boolean hasNext() {
349                 return !done;
350             }
351             public Object JavaDoc next() {
352                 if (done) {
353                     throw new NoSuchElementException JavaDoc();
354                 }
355                 done = true;
356                 return Resource.this;
357             }
358             public void remove() {
359                 throw new UnsupportedOperationException JavaDoc();
360             }
361         };
362     }
363
364     /**
365      * Fulfill the ResourceCollection contract.
366      * @return the size of this ResourceCollection.
367      * @since Ant 1.7
368      */

369     public int size() {
370         return isReference() ? ((Resource) getCheckedRef()).size() : 1;
371     }
372
373     /**
374      * Fulfill the ResourceCollection contract.
375      * @return whether this Resource is a FileResource.
376      * @since Ant 1.7
377      */

378     public boolean isFilesystemOnly() {
379         //default false:
380
return isReference() && ((Resource) getCheckedRef()).isFilesystemOnly();
381     }
382
383     /**
384      * Get the string representation of this Resource.
385      * @return this Resource formatted as a String.
386      * @since Ant 1.7
387      */

388     public String JavaDoc toString() {
389         if (isReference()) {
390             return getCheckedRef().toString();
391         }
392         String JavaDoc n = getName();
393         return n == null ? "(anonymous)" : n;
394     }
395
396     /**
397      * Get a long String representation of this Resource.
398      * This typically should be the value of <code>toString()</code>
399      * prefixed by a type description.
400      * @return this Resource formatted as a long String.
401      * @since Ant 1.7
402      */

403     public final String JavaDoc toLongString() {
404         return isReference() ? ((Resource) getCheckedRef()).toLongString()
405             : getDataTypeName() + " \"" + toString() + '"';
406     }
407
408     /**
409      * Overrides the base version.
410      * @param r the Reference to set.
411      */

412     public void setRefid(Reference r) {
413         if (name != null
414             || exists != null
415             || lastmodified != null
416             || directory != null
417             || size != null) {
418             throw tooManyAttributes();
419         }
420         super.setRefid(r);
421     }
422
423 }
424
Popular Tags