1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util;
17
18 import java.io.BufferedReader;
19 import java.io.BufferedWriter;
20 import java.io.File;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.InputStreamReader;
24 import java.io.OutputStream;
25 import java.io.OutputStreamWriter;
26 import java.io.Reader;
27 import java.io.StringReader;
28 import java.io.Writer;
29 import java.net.MalformedURLException;
30 import java.net.URI;
31 import java.net.URL;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.List;
35
36 import org.apache.commons.io.FileUtils;
37 import org.apache.commons.io.IOUtils;
38 import org.springframework.core.io.DefaultResourceLoader;
39 import org.springframework.core.io.Resource;
40 import org.springframework.core.io.ResourceLoader;
41
42 public class LocationUtils {
43
44 public static final List<String> getLocations(String location, LocationType type, String encoding) {
45 switch (type) {
46 case LOCATION:
47 return Collections.singletonList(location);
48 case LOCATIONLIST:
49 return getLocations(location, encoding);
50 default:
51 throw new IllegalArgumentException("Location type '" + type + "' is unknown");
52 }
53 }
54
55 public static final List<String> getLocations(String location, LocationType type) {
56 return getLocations(location, type, null);
57 }
58
59 public static final List<String> getLocations(String locationListing) {
60 return getLocations(Collections.singletonList(locationListing), null);
61 }
62
63 public static final List<String> getLocations(String locationListing, String encoding) {
64 return getLocations(Collections.singletonList(locationListing), encoding);
65 }
66
67 public static final List<String> getLocations(List<String> locationListings) {
68 return getLocations(locationListings, null);
69 }
70
71 public static final List<String> getLocations(List<String> locationListings, String encoding) {
72 List<String> locations = new ArrayList<String>();
73 for (String locationListing : locationListings) {
74 List<String> lines = readLines(locationListing, encoding);
75 locations.addAll(lines);
76 }
77 return locations;
78 }
79
80 public static final String getURLString(File file) {
81 if (file == null) {
82 return null;
83 }
84 try {
85 URI uri = file.toURI();
86 URL url = uri.toURL();
87 return url.toExternalForm();
88 } catch (MalformedURLException e) {
89 throw new IllegalArgumentException(e);
90 }
91 }
92
93 public static final String getCanonicalPath(File file) {
94 try {
95 return file.getCanonicalPath();
96 } catch (IOException e) {
97 throw new IllegalArgumentException("Unexpected IO error", e);
98 }
99 }
100
101
102
103
104
105 public static final boolean deleteFileQuietly(String filename) {
106 File file = getFileQuietly(filename);
107 return FileUtils.deleteQuietly(file);
108 }
109
110
111
112
113
114 public static final File getFileQuietly(String filename) {
115 if (filename == null) {
116 return null;
117 } else {
118 return new File(filename);
119 }
120 }
121
122
123
124
125 public static final String toString(String location) {
126 return toString(location, null);
127 }
128
129
130
131
132 public static final String toString(String location, String encoding) {
133 InputStream in = null;
134 try {
135 in = getInputStream(location);
136 if (encoding == null) {
137 return IOUtils.toString(in);
138 } else {
139 return IOUtils.toString(in, encoding);
140 }
141 } catch (IOException e) {
142 throw new IllegalStateException("Unexpected IO error", e);
143 } finally {
144 IOUtils.closeQuietly(in);
145 }
146 }
147
148
149
150
151 public static final List<String> readLinesFromString(String s) {
152 Reader reader = getBufferedReaderFromString(s);
153 return readLinesAndClose(reader);
154 }
155
156 public static final List<String> readLinesAndClose(InputStream in) {
157 return readLinesAndClose(in, null);
158 }
159
160 public static final List<String> readLinesAndClose(InputStream in, String encoding) {
161 Reader reader = null;
162 try {
163 reader = getBufferedReader(in, encoding);
164 return readLinesAndClose(reader);
165 } catch (IOException e) {
166 throw new IllegalStateException("Unexpected IO error", e);
167 } finally {
168 IOUtils.closeQuietly(reader);
169 }
170 }
171
172 public static final List<String> readLinesAndClose(Reader reader) {
173 try {
174 return IOUtils.readLines(reader);
175 } catch (IOException e) {
176 throw new IllegalStateException("Unexpected IO error", e);
177 } finally {
178 IOUtils.closeQuietly(reader);
179 }
180 }
181
182
183
184
185 public static final List<String> readLines(String location) {
186 return readLines(location, null);
187 }
188
189
190
191
192 public static final List<String> readLines(String location, String encoding) {
193 Reader reader = null;
194 try {
195 reader = getBufferedReader(location, encoding);
196 return readLinesAndClose(reader);
197 } catch (IOException e) {
198 throw new IllegalStateException("Unexpected IO error", e);
199 } finally {
200 IOUtils.closeQuietly(reader);
201 }
202 }
203
204
205
206
207 public static final BufferedReader getBufferedReader(String location) throws IOException {
208 return getBufferedReader(location, null);
209 }
210
211
212
213
214 public static final BufferedReader getBufferedReader(String location, String encoding) throws IOException {
215 try {
216 InputStream in = getInputStream(location);
217 return getBufferedReader(in, encoding);
218 } catch (IOException e) {
219 throw new IOException("Unexpected IO error", e);
220 }
221 }
222
223
224
225
226 public static final BufferedReader getBufferedReaderFromString(String s) {
227 return new BufferedReader(new StringReader(s));
228 }
229
230
231
232
233
234 public static final Writer getWriter(OutputStream out, String encoding) throws IOException {
235 if (encoding == null) {
236 return new BufferedWriter(new OutputStreamWriter(out));
237 } else {
238 return new BufferedWriter(new OutputStreamWriter(out, encoding));
239 }
240 }
241
242
243
244
245
246 public static final BufferedReader getBufferedReader(File file, String encoding) throws IOException {
247 return getBufferedReader(FileUtils.openInputStream(file), encoding);
248 }
249
250
251
252
253
254 public static final BufferedReader getBufferedReader(InputStream in, String encoding) throws IOException {
255 if (encoding == null) {
256 return new BufferedReader(new InputStreamReader(in));
257 } else {
258 return new BufferedReader(new InputStreamReader(in, encoding));
259 }
260 }
261
262
263
264
265 public static final boolean isExistingFile(String location) {
266 if (location == null) {
267 return false;
268 }
269 File file = new File(location);
270 return file.exists();
271 }
272
273
274
275
276 public static final boolean exists(String location) {
277 if (location == null) {
278 return false;
279 }
280 if (isExistingFile(location)) {
281 return true;
282 } else {
283 Resource resource = getResource(location);
284 return resource.exists();
285 }
286 }
287
288
289
290
291
292
293 public static final InputStream getInputStream(String location) throws IOException {
294 if (isExistingFile(location)) {
295 return FileUtils.openInputStream(new File(location));
296 }
297 Resource resource = getResource(location);
298 return resource.getInputStream();
299 }
300
301 public static final Resource getResource(String location) {
302 if (location == null) {
303 return null;
304 }
305 ResourceLoader loader = new DefaultResourceLoader();
306 return loader.getResource(location);
307 }
308
309 public static final String getFilename(String location) {
310 if (location == null) {
311 return null;
312 }
313 if (isExistingFile(location)) {
314 return getFileQuietly(location).getName();
315 } else {
316 Resource resource = getResource(location);
317 return resource.getFilename();
318 }
319 }
320
321 }