|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| FileFinder.java | 100% | 100% | 100% | 100% |
|
||||||||||||||
| 1 | /* | |
| 2 | * DynamicJava - Copyright (C) 1999-2001 | |
| 3 | * | |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a | |
| 5 | * copy of this software and associated documentation files | |
| 6 | * (the "Software"), to deal in the Software without restriction, including | |
| 7 | * without limitation the rights to use, copy, modify, merge, publish, | |
| 8 | * distribute, sublicense, and/or sell copies of the Software, and to permit | |
| 9 | * persons to whom the Software is furnished to do so, subject to the | |
| 10 | * following conditions: | |
| 11 | * The above copyright notice and this permission notice shall be included | |
| 12 | * in all copies or substantial portions of the Software. | |
| 13 | * | |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
| 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
| 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
| 17 | * IN NO EVENT SHALL DYADE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
| 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
| 20 | * DEALINGS IN THE SOFTWARE. | |
| 21 | * | |
| 22 | * Except as contained in this notice, the name of Dyade shall not be | |
| 23 | * used in advertising or otherwise to promote the sale, use or other | |
| 24 | * dealings in this Software without prior written authorization from | |
| 25 | * Dyade. | |
| 26 | * | |
| 27 | */ | |
| 28 | ||
| 29 | package koala.dynamicjava.util; | |
| 30 | ||
| 31 | import java.io.*; | |
| 32 | import java.util.*; | |
| 33 | ||
| 34 | /** | |
| 35 | * This class represents an object that manages a set of path where to | |
| 36 | * find files. | |
| 37 | * | |
| 38 | * @author Stephane Hillion | |
| 39 | * @version 1.0 - 1999/05/18 | |
| 40 | */ | |
| 41 | ||
| 42 | public class FileFinder { | |
| 43 | /** | |
| 44 | * The paths | |
| 45 | */ | |
| 46 | private List<String> paths; | |
| 47 | ||
| 48 | /** | |
| 49 | * Creates a new file finder | |
| 50 | */ | |
| 51 | 1 | public FileFinder() { |
| 52 | 1 | paths = new LinkedList<String>(); |
| 53 | } | |
| 54 | ||
| 55 | /** | |
| 56 | * Adds a search path. This path becomes the one with the highest priority. | |
| 57 | * @param path the path to add | |
| 58 | */ | |
| 59 | 2 | public void addPath(String path) { |
| 60 | 2 | String s = (path.endsWith("/")) ? path : path + "/"; |
| 61 | 2 | paths.remove(s); |
| 62 | 2 | paths.add(0, s); |
| 63 | } | |
| 64 | ||
| 65 | /** | |
| 66 | * Searches and returns a file. | |
| 67 | * @param name the base name of the file to find | |
| 68 | * @throws IOException if no file was found | |
| 69 | */ | |
| 70 | 9 | public File findFile(String name) throws IOException { |
| 71 | 9 | for (String s : paths) { |
| 72 | 8 | File f = new File(s + name); |
| 73 | 8 | if (f.exists()) { |
| 74 | 3 | return f; |
| 75 | } | |
| 76 | } | |
| 77 | 6 | throw new IOException("File Not Found: " + name); |
| 78 | } | |
| 79 | } |
|
||||||||||