KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > io > FileSystemResource


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.core.io;
18
19 import java.io.File JavaDoc;
20 import java.io.FileInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.net.URL JavaDoc;
24
25 import org.springframework.util.Assert;
26 import org.springframework.util.ResourceUtils;
27 import org.springframework.util.StringUtils;
28
29 /**
30  * {@link Resource} implementation for <code>java.io.File</code> handles.
31  * Obviously supports resolution as File, and also as URL.
32  *
33  * @author Juergen Hoeller
34  * @since 28.12.2003
35  * @see java.io.File
36  */

37 public class FileSystemResource extends AbstractResource {
38
39     private final File JavaDoc file;
40
41     private final String JavaDoc path;
42
43
44     /**
45      * Create a new FileSystemResource.
46      * @param file a File handle
47      */

48     public FileSystemResource(File JavaDoc file) {
49         Assert.notNull(file, "File must not be null");
50         this.file = file;
51         this.path = StringUtils.cleanPath(file.getPath());
52     }
53
54     /**
55      * Create a new FileSystemResource.
56      * @param path a file path
57      */

58     public FileSystemResource(String JavaDoc path) {
59         Assert.notNull(path, "Path must not be null");
60         this.file = new File JavaDoc(path);
61         this.path = StringUtils.cleanPath(path);
62     }
63
64     /**
65      * Return the file path for this resource.
66      */

67     public final String JavaDoc getPath() {
68         return this.path;
69     }
70
71
72     /**
73      * This implementation returns whether the underlying file exists.
74      * @see java.io.File#exists()
75      */

76     public boolean exists() {
77         return this.file.exists();
78     }
79
80     /**
81      * This implementation opens a FileInputStream for the underlying file.
82      * @see java.io.FileInputStream
83      */

84     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
85         return new FileInputStream JavaDoc(this.file);
86     }
87
88     /**
89      * This implementation returns a URL for the underlying file.
90      * @see java.io.File#getAbsolutePath()
91      */

92     public URL JavaDoc getURL() throws IOException JavaDoc {
93         return new URL JavaDoc(ResourceUtils.FILE_URL_PREFIX + this.file.getAbsolutePath());
94     }
95
96     /**
97      * This implementation returns the underlying File reference.
98      */

99     public File JavaDoc getFile() {
100         return file;
101     }
102
103     /**
104      * This implementation creates a FileSystemResource, applying the given path
105      * relative to the path of the underlying file of this resource descriptor.
106      * @see org.springframework.util.StringUtils#applyRelativePath(String, String)
107      */

108     public Resource createRelative(String JavaDoc relativePath) {
109         String JavaDoc pathToUse = StringUtils.applyRelativePath(this.path, relativePath);
110         return new FileSystemResource(pathToUse);
111     }
112
113     /**
114      * This implementation returns the name of the file.
115      * @see java.io.File#getName()
116      */

117     public String JavaDoc getFilename() {
118         return this.file.getName();
119     }
120
121     /**
122      * This implementation returns a description that includes the absolute
123      * path of the file.
124      * @see java.io.File#getAbsolutePath()
125      */

126     public String JavaDoc getDescription() {
127         return "file [" + this.file.getAbsolutePath() + "]";
128     }
129
130
131     /**
132      * This implementation compares the underlying File references.
133      */

134     public boolean equals(Object JavaDoc obj) {
135         return (obj == this ||
136             (obj instanceof FileSystemResource && this.path.equals(((FileSystemResource) obj).path)));
137     }
138
139     /**
140      * This implementation returns the hash code of the underlying File reference.
141      */

142     public int hashCode() {
143         return this.path.hashCode();
144     }
145
146 }
147
Popular Tags