001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.utils;
014
015import static org.apache.juneau.internal.FileUtils.*;
016
017import java.io.*;
018import java.util.*;
019import java.util.ResourceBundle.*;
020
021/**
022 * Utility class for finding resources for a class.
023 * 
024 * <p>
025 * Same as {@link Class#getResourceAsStream(String)} except looks for resources with localized file names.
026 * 
027 * <p>
028 * If the <code>locale</code> is specified, then we look for resources whose name matches that locale.
029 * For example, if looking for the resource <js>"MyResource.txt"</js> for the Japanese locale, we will look for
030 * files in the following order:
031 * <ol>
032 *    <li><js>"MyResource_ja_JP.txt"</js>
033 *    <li><js>"MyResource_ja.txt"</js>
034 *    <li><js>"MyResource.txt"</js>
035 * </ol>
036 */
037public class ClasspathResourceFinderSimple implements ClasspathResourceFinder {
038
039   private static final ResourceBundle.Control RB_CONTROL = ResourceBundle.Control.getControl(Control.FORMAT_DEFAULT);
040   private static final List<Locale> ROOT_LOCALE = Arrays.asList(Locale.ROOT);
041
042   
043   @Override /* ClasspathResourceFinder */
044   public InputStream findResource(Class<?> baseClass, String name, Locale locale) throws IOException {
045      return findClasspathResource(baseClass, name, locale);
046   }
047   
048   /**
049    * Workhorse method for retrieving a resource from the classpath.
050    * 
051    * <p>
052    * This method can be overridden by subclasses to provide customized handling of resource retrieval from the classpath.
053    * 
054    * @param baseClass The base class providing the classloader.
055    * @param name The resource name.
056    * @param locale 
057    *    The resource locale.
058    *    <br>If <jk>null</jk>, won't look for localized file names.
059    * @return The resource stream, or <jk>null</jk> if it couldn't be found.
060    * @throws IOException
061    */
062   protected InputStream findClasspathResource(Class<?> baseClass, String name, Locale locale) throws IOException {
063      if (locale == null) 
064         return baseClass.getResourceAsStream(name);
065      for (String n : getCandidateFileNames(name, locale)) {
066         InputStream is = baseClass.getResourceAsStream(n);
067         if (is != null)
068            return is;
069      }
070      return null;
071   }
072   
073   /**
074    * Returns the candidate file names for the specified file name in the specified locale.
075    * 
076    * <p>
077    * For example, if looking for the <js>"MyResource.txt"</js> file in the Japanese locale, the iterator will return
078    * names in the following order:
079    * <ol>
080    *    <li><js>"MyResource_ja_JP.txt"</js>
081    *    <li><js>"MyResource_ja.txt"</js>
082    *    <li><js>"MyResource.txt"</js>
083    * </ol>
084    * 
085    * <p>
086    * If the locale is <jk>null</jk>, then it will only return <js>"MyResource.txt"</js>.
087    * 
088    * @param fileName The name of the file to get candidate file names on.
089    * @param l 
090    *    The locale.
091    *    <br>If <jk>null</jk>, won't look for localized file names.
092    * @return An iterator of file names to look at.
093    */
094   protected static Iterable<String> getCandidateFileNames(final String fileName, final Locale l) {
095      return new Iterable<String>() {
096         @Override
097         public Iterator<String> iterator() {
098            return new Iterator<String>() {
099               final Iterator<Locale> locales = getCandidateLocales(l).iterator();
100               String baseName, ext;
101
102               @Override
103               public boolean hasNext() {
104                  return locales.hasNext();
105               }
106
107               @Override
108               public String next() {
109                  Locale l2 = locales.next();
110                  if (l2.toString().isEmpty())
111                     return fileName;
112                  if (baseName == null)
113                     baseName = getBaseName(fileName);
114                  if (ext == null)
115                     ext = getExtension(fileName);
116                  return baseName + "_" + l2.toString() + (ext.isEmpty() ? "" : ('.' + ext));
117               }
118               @Override
119               public void remove() {
120                  throw new UnsupportedOperationException();
121               }
122            };
123         }
124      };
125   }
126
127   /**
128    * Returns the candidate locales for the specified locale.
129    * 
130    * <p>
131    * For example, if <code>locale</code> is <js>"ja_JP"</js>, then this method will return:
132    * <ol>
133    *    <li><js>"ja_JP"</js>
134    *    <li><js>"ja"</js>
135    *    <li><js>""</js>
136    * </ol>
137    * 
138    * @param locale The locale to get the list of candidate locales for.
139    * @return The list of candidate locales.
140    */
141   static final List<Locale> getCandidateLocales(Locale locale) {
142      if (locale == null)
143         return ROOT_LOCALE;
144      return RB_CONTROL.getCandidateLocales("", locale);
145   }
146}