KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26
27 import org.apache.tools.ant.Project;
28 import org.apache.tools.ant.BuildException;
29 import org.apache.tools.ant.util.FileUtils;
30 import org.apache.tools.ant.types.Resource;
31 import org.apache.tools.ant.types.Reference;
32
33 /**
34  * A Resource representation of a File.
35  * @since Ant 1.7
36  */

37 public class FileResource extends Resource implements Touchable {
38
39     private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
40     private static final int NULL_FILE
41         = Resource.getMagicNumber("null file".getBytes());
42
43     private File JavaDoc file;
44     private File JavaDoc baseDir;
45
46     /**
47      * Default constructor.
48      */

49     public FileResource() {
50     }
51
52     /**
53      * Construct a new FileResource using the specified basedir and relative name.
54      * @param b the basedir as File.
55      * @param name the relative filename.
56      */

57     public FileResource(File JavaDoc b, String JavaDoc name) {
58         setFile(FILE_UTILS.resolveFile(b, name));
59         setBaseDir(b);
60     }
61
62     /**
63      * Construct a new FileResource from a File.
64      * @param f the File represented.
65      */

66     public FileResource(File JavaDoc f) {
67         setFile(f);
68     }
69
70     /**
71      * Constructor for Ant attribute introspection.
72      * @param p the Project against which to resolve <code>s</code>.
73      * @param s the absolute or Project-relative filename as a String.
74      * @see org.apache.tools.ant.IntrospectionHelper
75      */

76     public FileResource(Project p, String JavaDoc s) {
77         this(p.resolveFile(s));
78         setProject(p);
79     }
80
81     /**
82      * Set the File for this FileResource.
83      * @param f the File to be represented.
84      */

85     public void setFile(File JavaDoc f) {
86         checkAttributesAllowed();
87         file = f;
88     }
89
90     /**
91      * Get the file represented by this FileResource.
92      * @return the File.
93      */

94     public File JavaDoc getFile() {
95         return isReference() ? ((FileResource) getCheckedRef()).getFile() : file;
96     }
97
98     /**
99      * Set the basedir for this FileResource.
100      * @param b the basedir as File.
101      */

102     public void setBaseDir(File JavaDoc b) {
103         checkAttributesAllowed();
104         baseDir = b;
105     }
106
107     /**
108      * Return the basedir to which the name is relative.
109      * @return the basedir as File.
110      */

111     public File JavaDoc getBaseDir() {
112         return isReference()
113             ? ((FileResource) getCheckedRef()).getBaseDir() : baseDir;
114     }
115
116     /**
117      * Overrides the super version.
118      * @param r the Reference to set.
119      */

120     public void setRefid(Reference r) {
121         if (file != null || baseDir != null) {
122             throw tooManyAttributes();
123         }
124         super.setRefid(r);
125     }
126
127     /**
128      * Get the name of this FileResource. If the basedir is set,
129      * the name will be relative to that. Otherwise the basename
130      * only will be returned.
131      * @return the name of this resource.
132      */

133     public String JavaDoc getName() {
134         if (isReference()) {
135             return ((Resource) getCheckedRef()).getName();
136         }
137         File JavaDoc b = getBaseDir();
138         return b == null ? getNotNullFile().getName()
139             : FILE_UTILS.removeLeadingPath(b, getNotNullFile());
140     }
141
142     /**
143      * Learn whether this file exists.
144      * @return true if this resource exists.
145      */

146     public boolean isExists() {
147         return isReference() ? ((Resource) getCheckedRef()).isExists()
148             : getNotNullFile().exists();
149     }
150
151     /**
152      * Get the modification time in milliseconds since 01.01.1970 .
153      * @return 0 if the resource does not exist.
154      */

155     public long getLastModified() {
156         return isReference()
157             ? ((Resource) getCheckedRef()).getLastModified()
158             : getNotNullFile().lastModified();
159     }
160
161     /**
162      * Learn whether the resource is a directory.
163      * @return boolean flag indicating if the resource is a directory.
164      */

165     public boolean isDirectory() {
166         return isReference() ? ((Resource) getCheckedRef()).isDirectory()
167             : getNotNullFile().isDirectory();
168     }
169
170     /**
171      * Get the size of this Resource.
172      * @return the size, as a long, 0 if the Resource does not exist.
173      */

174     public long getSize() {
175         return isReference() ? ((Resource) getCheckedRef()).getSize()
176             : getNotNullFile().length();
177     }
178
179     /**
180      * Return an InputStream for reading the contents of this Resource.
181      * @return an InputStream object.
182      * @throws IOException if an error occurs.
183      */

184     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
185         return isReference()
186             ? ((Resource) getCheckedRef()).getInputStream()
187             : new FileInputStream JavaDoc(getNotNullFile());
188     }
189
190     /**
191      * Get an OutputStream for the Resource.
192      * @return an OutputStream to which content can be written.
193      * @throws IOException if unable to provide the content of this
194      * Resource as a stream.
195      * @throws UnsupportedOperationException if OutputStreams are not
196      * supported for this Resource type.
197      */

198     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
199         if (isReference()) {
200             return ((Resource) getCheckedRef()).getOutputStream();
201         }
202         File JavaDoc f = getNotNullFile();
203         if (f.exists()) {
204             if (f.isFile()) {
205                 f.delete();
206             }
207         } else {
208             File JavaDoc p = f.getParentFile();
209             if (p != null && !(p.exists())) {
210                 p.mkdirs();
211             }
212         }
213         return new FileOutputStream JavaDoc(f);
214     }
215
216     /**
217      * Compare this FileResource to another Resource.
218      * @param another the other Resource against which to compare.
219      * @return a negative integer, zero, or a positive integer as this FileResource
220      * is less than, equal to, or greater than the specified Resource.
221      */

222     public int compareTo(Object JavaDoc another) {
223         if (isReference()) {
224             return ((Comparable JavaDoc) getCheckedRef()).compareTo(another);
225         }
226         if (this.equals(another)) {
227             return 0;
228         }
229         if (another.getClass().equals(getClass())) {
230             FileResource otherfr = (FileResource) another;
231             File JavaDoc f = getFile();
232             if (f == null) {
233                 return -1;
234             }
235             File JavaDoc of = otherfr.getFile();
236             if (of == null) {
237                 return 1;
238             }
239             return f.compareTo(of);
240         }
241         return super.compareTo(another);
242     }
243
244     /**
245      * Compare another Object to this FileResource for equality.
246      * @param another the other Object to compare.
247      * @return true if another is a FileResource representing the same file.
248      */

249     public boolean equals(Object JavaDoc another) {
250         if (this == another) {
251             return true;
252         }
253         if (isReference()) {
254             return getCheckedRef().equals(another);
255         }
256         if (!(another.getClass().equals(getClass()))) {
257             return false;
258         }
259         FileResource otherfr = (FileResource) another;
260         return getFile() == null
261             ? otherfr.getFile() == null
262             : getFile().equals(otherfr.getFile());
263     }
264
265     /**
266      * Get the hash code for this Resource.
267      * @return hash code as int.
268      */

269     public int hashCode() {
270         if (isReference()) {
271             return getCheckedRef().hashCode();
272         }
273         return MAGIC * (getFile() == null ? NULL_FILE : getFile().hashCode());
274     }
275
276     /**
277      * Get the string representation of this Resource.
278      * @return this FileResource formatted as a String.
279      */

280     public String JavaDoc toString() {
281         if (isReference()) {
282             return getCheckedRef().toString();
283         }
284         if (file == null) {
285             return "(unbound file resource)";
286         }
287         String JavaDoc absolutePath = file.getAbsolutePath();
288         return FILE_UTILS.normalize(absolutePath).getAbsolutePath();
289     }
290
291     /**
292      * Fulfill the ResourceCollection contract.
293      * @return whether this Resource is a FileResource.
294      */

295     public boolean isFilesystemOnly() {
296         return !isReference()
297             || ((FileResource) getCheckedRef()).isFilesystemOnly();
298     }
299
300     /**
301      * Implement the Touchable interface.
302      * @param modTime new last modification time.
303      */

304     public void touch(long modTime) {
305         if (isReference()) {
306             ((FileResource) getCheckedRef()).touch(modTime);
307             return;
308         }
309         getNotNullFile().setLastModified(modTime);
310     }
311
312     /**
313      * Get the file represented by this FileResource, ensuring it is not null.
314      * @return the not-null File.
315      * @throws BuildException if file is null.
316      */

317     protected File JavaDoc getNotNullFile() {
318         if (getFile() == null) {
319             throw new BuildException("file attribute is null!");
320         }
321         return getFile();
322     }
323
324 }
325
Popular Tags