1 package org.clearsilver; 2 3 import java.util.LinkedList; 4 import java.util.List; 5 import java.io.File; 6 7 /** 8 * Utility class containing helper methods 9 * 10 * @author smarti@google.com (Sergio Marti) 11 */ 12 public class CSUtil { 13 CSUtil()14 private CSUtil() { } 15 16 public static final String HDF_LOADPATHS = "hdf.loadpaths"; 17 18 /** 19 * Helper function that returns a concatenation of the loadpaths in the 20 * provided HDF. 21 * @param hdf an HDF structure containing load paths. 22 * @return A list of loadpaths in order in which to search. 23 * @throws NullPointerException if no loadpaths are found. 24 */ getLoadPaths(HDF hdf)25 public static List<String> getLoadPaths(HDF hdf) { 26 List<String> list = new LinkedList<String>(); 27 HDF loadpathsHdf = hdf.getObj(HDF_LOADPATHS); 28 if (loadpathsHdf == null) { 29 throw new NullPointerException("No HDF loadpaths located in the specified" 30 + " HDF structure"); 31 } 32 for (HDF lpHdf = loadpathsHdf.objChild(); lpHdf != null; 33 lpHdf = lpHdf.objNext()) { 34 list.add(lpHdf.objValue()); 35 } 36 return list; 37 } 38 39 /** 40 * Given an ordered list of directories to look in, locate the specified file. 41 * Returns <code>null</code> if file not found. 42 * @param loadpaths the ordered list of paths to search. 43 * @param filename the name of the file. 44 * @return a File object corresponding to the file. <code>null</code> if 45 * file not found. 46 */ locateFile(List<String> loadpaths, String filename)47 public static File locateFile(List<String> loadpaths, String filename) { 48 if (filename == null) { 49 throw new NullPointerException("No filename provided"); 50 } 51 if (loadpaths == null) { 52 throw new NullPointerException("No loadpaths provided."); 53 } 54 for (String path : loadpaths) { 55 File file = new File(path, filename); 56 if (file.exists()) { 57 return file; 58 } 59 } 60 return null; 61 } 62 } 63