View Javadoc

1   /**
2    * Copyright 2010-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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  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 	 * Null safe method to unconditionally attempt to delete <code>filename</code> without throwing an exception. If <code>filename</code>
103 	 * is a directory, delete it and all sub-directories.
104 	 */
105 	public static final boolean deleteFileQuietly(String filename) {
106 		File file = getFileQuietly(filename);
107 		return FileUtils.deleteQuietly(file);
108 	}
109 
110 	/**
111 	 * Null safe method for getting a <code>File</code> handle from <code>filename</code>. If <code>filename</code> is null, null is
112 	 * returned.
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 	 * Get the contents of <code>location</code> as a <code>String</code> using the platform's default character encoding.
124 	 */
125 	public static final String toString(String location) {
126 		return toString(location, null);
127 	}
128 
129 	/**
130 	 * Get the contents of <code>location</code> as a <code>String</code> using the specified character encoding.
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 	 * Get the contents of <code>s</code> as a list of <code>String's</code> one entry per line
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 	 * Get the contents of <code>location</code> as a list of <code>String's</code> one entry per line using the platform default encoding
184 	 */
185 	public static final List<String> readLines(String location) {
186 		return readLines(location, null);
187 	}
188 
189 	/**
190 	 * Get the contents of <code>location</code> as a list of <code>String's</code> one entry per line using the encoding indicated.
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 	 * Return a <code>BufferedReader</code> for the location indicated using the platform default encoding.
206 	 */
207 	public static final BufferedReader getBufferedReader(String location) throws IOException {
208 		return getBufferedReader(location, null);
209 	}
210 
211 	/**
212 	 * Return a <code>BufferedReader</code> for the location indicated using the encoding indicated.
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 	 * Return a <code>BufferedReader</code> that reads from <code>s</code>
225 	 */
226 	public static final BufferedReader getBufferedReaderFromString(String s) {
227 		return new BufferedReader(new StringReader(s));
228 	}
229 
230 	/**
231 	 * Return a <code>Writer</code> that writes to <code>out</code> using the indicated encoding. <code>null</code> means use the platform's
232 	 * default encoding.
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 	 * Return a <code>BufferedReader</code> that reads from <code>file</code> using the indicated encoding. <code>null</code> means use the
244 	 * platform's default encoding.
245 	 */
246 	public static final BufferedReader getBufferedReader(File file, String encoding) throws IOException {
247 		return getBufferedReader(FileUtils.openInputStream(file), encoding);
248 	}
249 
250 	/**
251 	 * Return a <code>BufferedReader</code> that reads from <code>in</code> using the indicated encoding. <code>null</code> means use the
252 	 * platform's default encoding.
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 	 * Null safe method for determining if <code>location</code> is an existing file.
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 	 * Null safe method for determining if <code>location</code> exists.
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 	 * Open an <code>InputStream</code> to <code>location</code>. If <code>location</code> is the path to an existing <code>File</code> on
290 	 * the local file system, a <code>FileInputStream</code> is returned. Otherwise Spring's resource loading framework is used to open an
291 	 * <code>InputStream</code> to <code>location</code>.
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 }