xref: /aosp_15_r20/external/jsilver/src/org/clearsilver/CSUtil.java (revision 650b9f7487be23191c9a5c1efcd9aa92af8ddcb8)
1*650b9f74SAndroid Build Coastguard Worker /*
2*650b9f74SAndroid Build Coastguard Worker  * Copyright (C) 2010 Google Inc.
3*650b9f74SAndroid Build Coastguard Worker  *
4*650b9f74SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*650b9f74SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*650b9f74SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*650b9f74SAndroid Build Coastguard Worker  *
8*650b9f74SAndroid Build Coastguard Worker  * http://www.apache.org/licenses/LICENSE-2.0
9*650b9f74SAndroid Build Coastguard Worker  *
10*650b9f74SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*650b9f74SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*650b9f74SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*650b9f74SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*650b9f74SAndroid Build Coastguard Worker  * limitations under the License.
15*650b9f74SAndroid Build Coastguard Worker  */
16*650b9f74SAndroid Build Coastguard Worker 
17*650b9f74SAndroid Build Coastguard Worker package org.clearsilver;
18*650b9f74SAndroid Build Coastguard Worker 
19*650b9f74SAndroid Build Coastguard Worker import java.io.File;
20*650b9f74SAndroid Build Coastguard Worker import java.util.LinkedList;
21*650b9f74SAndroid Build Coastguard Worker import java.util.List;
22*650b9f74SAndroid Build Coastguard Worker 
23*650b9f74SAndroid Build Coastguard Worker /**
24*650b9f74SAndroid Build Coastguard Worker  * Utility class containing helper methods
25*650b9f74SAndroid Build Coastguard Worker  */
26*650b9f74SAndroid Build Coastguard Worker public final class CSUtil {
27*650b9f74SAndroid Build Coastguard Worker 
CSUtil()28*650b9f74SAndroid Build Coastguard Worker   private CSUtil() { }
29*650b9f74SAndroid Build Coastguard Worker 
30*650b9f74SAndroid Build Coastguard Worker   public static final String HDF_LOADPATHS = "hdf.loadpaths";
31*650b9f74SAndroid Build Coastguard Worker 
32*650b9f74SAndroid Build Coastguard Worker   /**
33*650b9f74SAndroid Build Coastguard Worker    * Helper function that returns a concatenation of the loadpaths in the
34*650b9f74SAndroid Build Coastguard Worker    * provided HDF.
35*650b9f74SAndroid Build Coastguard Worker    * @param hdf an HDF structure containing load paths.
36*650b9f74SAndroid Build Coastguard Worker    * @return A list of loadpaths in order in which to search.
37*650b9f74SAndroid Build Coastguard Worker    * @throws NullPointerException if no loadpaths are found.
38*650b9f74SAndroid Build Coastguard Worker    */
getLoadPaths(HDF hdf)39*650b9f74SAndroid Build Coastguard Worker   public static List<String> getLoadPaths(HDF hdf) {
40*650b9f74SAndroid Build Coastguard Worker     return getLoadPaths(hdf, false);
41*650b9f74SAndroid Build Coastguard Worker   }
42*650b9f74SAndroid Build Coastguard Worker 
43*650b9f74SAndroid Build Coastguard Worker   /**
44*650b9f74SAndroid Build Coastguard Worker    * Helper function that returns a concatenation of the loadpaths in the
45*650b9f74SAndroid Build Coastguard Worker    * provided HDF.
46*650b9f74SAndroid Build Coastguard Worker    * @param hdf an HDF structure containing load paths.
47*650b9f74SAndroid Build Coastguard Worker    * @param allowEmpty if {@code true} then this will return an empty list when
48*650b9f74SAndroid Build Coastguard Worker    *     no loadpaths are found in the HDF object, otherwise a
49*650b9f74SAndroid Build Coastguard Worker    *     {@link NullPointerException} is thrown. Loadpaths are not needed if
50*650b9f74SAndroid Build Coastguard Worker    *     no files are read in or are all specified by absolute paths.
51*650b9f74SAndroid Build Coastguard Worker    * @return A list of loadpaths in order in which to search.
52*650b9f74SAndroid Build Coastguard Worker    * @throws NullPointerException if no loadpaths are found and allowEmpty is
53*650b9f74SAndroid Build Coastguard Worker    *     {@code false}.
54*650b9f74SAndroid Build Coastguard Worker    */
getLoadPaths(HDF hdf, boolean allowEmpty)55*650b9f74SAndroid Build Coastguard Worker   public static List<String> getLoadPaths(HDF hdf, boolean allowEmpty) {
56*650b9f74SAndroid Build Coastguard Worker     List<String> list = new LinkedList<String>();
57*650b9f74SAndroid Build Coastguard Worker     HDF loadpathsHdf = hdf.getObj(HDF_LOADPATHS);
58*650b9f74SAndroid Build Coastguard Worker     if (loadpathsHdf == null) {
59*650b9f74SAndroid Build Coastguard Worker       if (allowEmpty) {
60*650b9f74SAndroid Build Coastguard Worker         return list;
61*650b9f74SAndroid Build Coastguard Worker       } else {
62*650b9f74SAndroid Build Coastguard Worker         throw new NullPointerException("No HDF loadpaths located in the "
63*650b9f74SAndroid Build Coastguard Worker             + "specified HDF structure");
64*650b9f74SAndroid Build Coastguard Worker       }
65*650b9f74SAndroid Build Coastguard Worker     }
66*650b9f74SAndroid Build Coastguard Worker     for (HDF lpHdf = loadpathsHdf.objChild(); lpHdf != null;
67*650b9f74SAndroid Build Coastguard Worker         lpHdf = lpHdf.objNext()) {
68*650b9f74SAndroid Build Coastguard Worker       list.add(lpHdf.objValue());
69*650b9f74SAndroid Build Coastguard Worker     }
70*650b9f74SAndroid Build Coastguard Worker     return list;
71*650b9f74SAndroid Build Coastguard Worker   }
72*650b9f74SAndroid Build Coastguard Worker 
73*650b9f74SAndroid Build Coastguard Worker   /**
74*650b9f74SAndroid Build Coastguard Worker    * Given an ordered list of directories to look in, locate the specified file.
75*650b9f74SAndroid Build Coastguard Worker    * Returns <code>null</code> if file not found.
76*650b9f74SAndroid Build Coastguard Worker    * @param loadpaths the ordered list of paths to search.
77*650b9f74SAndroid Build Coastguard Worker    * @param filename the name of the file.
78*650b9f74SAndroid Build Coastguard Worker    * @return a File object corresponding to the file. <code>null</code> if
79*650b9f74SAndroid Build Coastguard Worker    *     file not found.
80*650b9f74SAndroid Build Coastguard Worker    */
locateFile(List<String> loadpaths, String filename)81*650b9f74SAndroid Build Coastguard Worker   public static File locateFile(List<String> loadpaths, String filename) {
82*650b9f74SAndroid Build Coastguard Worker     if (filename == null) {
83*650b9f74SAndroid Build Coastguard Worker       throw new NullPointerException("No filename provided");
84*650b9f74SAndroid Build Coastguard Worker     }
85*650b9f74SAndroid Build Coastguard Worker     if (loadpaths == null) {
86*650b9f74SAndroid Build Coastguard Worker       throw new NullPointerException("No loadpaths provided.");
87*650b9f74SAndroid Build Coastguard Worker     }
88*650b9f74SAndroid Build Coastguard Worker     for (String path : loadpaths) {
89*650b9f74SAndroid Build Coastguard Worker       File file = new File(path, filename);
90*650b9f74SAndroid Build Coastguard Worker       if (file.exists()) {
91*650b9f74SAndroid Build Coastguard Worker         return file;
92*650b9f74SAndroid Build Coastguard Worker       }
93*650b9f74SAndroid Build Coastguard Worker     }
94*650b9f74SAndroid Build Coastguard Worker     return null;
95*650b9f74SAndroid Build Coastguard Worker   }
96*650b9f74SAndroid Build Coastguard Worker }
97