File: CSUtil.java

package info (click to toggle)
clearsilver 0.10.5-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,304 kB
  • sloc: ansic: 24,586; python: 4,233; sh: 2,502; cs: 1,429; ruby: 819; java: 735; makefile: 589; perl: 120; lisp: 34; sql: 21
file content (62 lines) | stat: -rw-r--r-- 1,829 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package org.clearsilver;

import java.util.LinkedList;
import java.util.List;
import java.io.File;

/**
 * Utility class containing helper methods
 *
 * @author smarti@google.com (Sergio Marti)
 */
public class CSUtil {

  private CSUtil() { }

  public static final String HDF_LOADPATHS = "hdf.loadpaths";

  /**
   * Helper function that returns a concatenation of the loadpaths in the
   * provided HDF.
   * @param hdf an HDF structure containing load paths.
   * @return A list of loadpaths in order in which to search.
   * @throws NullPointerException if no loadpaths are found.
   */
  public static List<String> getLoadPaths(HDF hdf) {
    List<String> list = new LinkedList<String>();
    HDF loadpathsHdf = hdf.getObj(HDF_LOADPATHS);
    if (loadpathsHdf == null) {
      throw new NullPointerException("No HDF loadpaths located in the specified"
          + " HDF structure");
    }
    for (HDF lpHdf = loadpathsHdf.objChild(); lpHdf != null;
        lpHdf = lpHdf.objNext()) {
      list.add(lpHdf.objValue());
    }
    return list;
  }

  /**
   * Given an ordered list of directories to look in, locate the specified file.
   * Returns <code>null</code> if file not found.
   * @param loadpaths the ordered list of paths to search.
   * @param filename the name of the file.
   * @return a File object corresponding to the file. <code>null</code> if
   * file not found.
   */
  public static File locateFile(List<String> loadpaths, String filename) {
    if (filename == null) {
      throw new NullPointerException("No filename provided");
    }
    if (loadpaths == null) {
      throw new NullPointerException("No loadpaths provided.");
    }
    for (String path : loadpaths) {
      File file = new File(path, filename);
      if (file.exists()) {
        return file;
      }
    }
    return null;
  }
}