Home | History | Annotate | Download | only in clearsilver
      1 /*
      2  * Copyright (C) 2010 Google Inc.
      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.clearsilver;
     18 
     19 import java.io.File;
     20 import java.util.LinkedList;
     21 import java.util.List;
     22 
     23 /**
     24  * Utility class containing helper methods
     25  */
     26 public final class CSUtil {
     27 
     28   private CSUtil() { }
     29 
     30   public static final String HDF_LOADPATHS = "hdf.loadpaths";
     31 
     32   /**
     33    * Helper function that returns a concatenation of the loadpaths in the
     34    * provided HDF.
     35    * @param hdf an HDF structure containing load paths.
     36    * @return A list of loadpaths in order in which to search.
     37    * @throws NullPointerException if no loadpaths are found.
     38    */
     39   public static List<String> getLoadPaths(HDF hdf) {
     40     return getLoadPaths(hdf, false);
     41   }
     42 
     43   /**
     44    * Helper function that returns a concatenation of the loadpaths in the
     45    * provided HDF.
     46    * @param hdf an HDF structure containing load paths.
     47    * @param allowEmpty if {@code true} then this will return an empty list when
     48    *     no loadpaths are found in the HDF object, otherwise a
     49    *     {@link NullPointerException} is thrown. Loadpaths are not needed if
     50    *     no files are read in or are all specified by absolute paths.
     51    * @return A list of loadpaths in order in which to search.
     52    * @throws NullPointerException if no loadpaths are found and allowEmpty is
     53    *     {@code false}.
     54    */
     55   public static List<String> getLoadPaths(HDF hdf, boolean allowEmpty) {
     56     List<String> list = new LinkedList<String>();
     57     HDF loadpathsHdf = hdf.getObj(HDF_LOADPATHS);
     58     if (loadpathsHdf == null) {
     59       if (allowEmpty) {
     60         return list;
     61       } else {
     62         throw new NullPointerException("No HDF loadpaths located in the "
     63             + "specified HDF structure");
     64       }
     65     }
     66     for (HDF lpHdf = loadpathsHdf.objChild(); lpHdf != null;
     67         lpHdf = lpHdf.objNext()) {
     68       list.add(lpHdf.objValue());
     69     }
     70     return list;
     71   }
     72 
     73   /**
     74    * Given an ordered list of directories to look in, locate the specified file.
     75    * Returns <code>null</code> if file not found.
     76    * @param loadpaths the ordered list of paths to search.
     77    * @param filename the name of the file.
     78    * @return a File object corresponding to the file. <code>null</code> if
     79    *     file not found.
     80    */
     81   public static File locateFile(List<String> loadpaths, String filename) {
     82     if (filename == null) {
     83       throw new NullPointerException("No filename provided");
     84     }
     85     if (loadpaths == null) {
     86       throw new NullPointerException("No loadpaths provided.");
     87     }
     88     for (String path : loadpaths) {
     89       File file = new File(path, filename);
     90       if (file.exists()) {
     91         return file;
     92       }
     93     }
     94     return null;
     95   }
     96 }
     97