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 <c>locale</c> 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   /**
040    * Reusable instance.
041    */
042   public static final ClasspathResourceFinderSimple INSTANCE = new ClasspathResourceFinderSimple();
043
044   private static final ResourceBundle.Control RB_CONTROL = ResourceBundle.Control.getControl(Control.FORMAT_DEFAULT);
045   private static final List<Locale> ROOT_LOCALE = Arrays.asList(Locale.ROOT);
046
047
048   @Override /* ClasspathResourceFinder */
049   public InputStream findResource(Class<?> baseClass, String name, Locale locale) throws IOException {
050      return findClasspathResource(baseClass, name, locale);
051   }
052
053   /**
054    * Workhorse method for retrieving a resource from the classpath.
055    *
056    * <p>
057    * This method can be overridden by subclasses to provide customized handling of resource retrieval from the classpath.
058    *
059    * @param baseClass The base class providing the classloader.
060    * @param name The resource name.
061    * @param locale
062    *    The resource locale.
063    *    <br>If <jk>null</jk>, won't look for localized file names.
064    * @return The resource stream, or <jk>null</jk> if it couldn't be found.
065    * @throws IOException Thrown by underlying stream.
066    */
067   protected InputStream findClasspathResource(Class<?> baseClass, String name, Locale locale) throws IOException {
068
069      if (locale == null)
070         return getResourceAsStream(baseClass, name);
071
072      for (String n : getCandidateFileNames(name, locale)) {
073         InputStream is = getResourceAsStream(baseClass, n);
074         if (is != null)
075            return is;
076      }
077      return null;
078   }
079
080   private InputStream getResourceAsStream(Class<?> baseClass, String name) {
081      return baseClass.getResourceAsStream(name);
082   }
083
084   /**
085    * Returns the candidate file names for the specified file name in the specified locale.
086    *
087    * <p>
088    * For example, if looking for the <js>"MyResource.txt"</js> file in the Japanese locale, the iterator will return
089    * names in the following order:
090    * <ol>
091    *    <li><js>"MyResource_ja_JP.txt"</js>
092    *    <li><js>"MyResource_ja.txt"</js>
093    *    <li><js>"MyResource.txt"</js>
094    * </ol>
095    *
096    * <p>
097    * If the locale is <jk>null</jk>, then it will only return <js>"MyResource.txt"</js>.
098    *
099    * @param fileName The name of the file to get candidate file names on.
100    * @param l
101    *    The locale.
102    *    <br>If <jk>null</jk>, won't look for localized file names.
103    * @return An iterator of file names to look at.
104    */
105   protected static Iterable<String> getCandidateFileNames(final String fileName, final Locale l) {
106      return new Iterable<String>() {
107         @Override
108         public Iterator<String> iterator() {
109            return new Iterator<String>() {
110               final Iterator<Locale> locales = getCandidateLocales(l).iterator();
111               String baseName, ext;
112
113               @Override
114               public boolean hasNext() {
115                  return locales.hasNext();
116               }
117
118               @Override
119               public String next() {
120                  Locale l2 = locales.next();
121                  if (l2.toString().isEmpty())
122                     return fileName;
123                  if (baseName == null)
124                     baseName = getBaseName(fileName);
125                  if (ext == null)
126                     ext = getExtension(fileName);
127                  return baseName + "_" + l2.toString() + (ext.isEmpty() ? "" : ('.' + ext));
128               }
129               @Override
130               public void remove() {
131                  throw new UnsupportedOperationException();
132               }
133            };
134         }
135      };
136   }
137
138   /**
139    * Returns the candidate locales for the specified locale.
140    *
141    * <p>
142    * For example, if <c>locale</c> is <js>"ja_JP"</js>, then this method will return:
143    * <ol>
144    *    <li><js>"ja_JP"</js>
145    *    <li><js>"ja"</js>
146    *    <li><js>""</js>
147    * </ol>
148    *
149    * @param locale The locale to get the list of candidate locales for.
150    * @return The list of candidate locales.
151    */
152   static final List<Locale> getCandidateLocales(Locale locale) {
153      if (locale == null)
154         return ROOT_LOCALE;
155      return RB_CONTROL.getCandidateLocales("", locale);
156   }
157}