1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 or later
5    * as published by the Free Software Foundation. This program is distributed
6    * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
7    * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * Copyright: 2005
18   *     The copyright to this program is held by its authors.
19   *
20   */
21  package org.crosswire.common.util;
22  
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.net.URL;
26  import java.util.MissingResourceException;
27  
28  import org.crosswire.jsword.JSOtherMsg;
29  
30  /**
31   * Better implementations of the getResource methods with less ambiguity and
32   * that are less dependent on the specific class loader situation.
33   * 
34   * @see gnu.lgpl.License The GNU Lesser General Public License for details.
35   * @author Joe Walker
36   * @author DM Smith
37   */
38  public final class ResourceUtil {
39      /**
40       * Prevent Instantiation
41       */
42      private ResourceUtil() {
43      }
44  
45      /**
46       * Generic resource URL fetcher. One way or the other we'll find it! Either
47       * as a relative or an absolute reference.
48       * 
49       * @param search
50       *            The name of the resource (without a leading /) to find
51       * @return The requested resource
52       * @throws MissingResourceException
53       *             if the resource can not be found
54       */
55      public static URL getResource(String search) throws MissingResourceException {
56          return getResource(CallContext.getCallingClass(), search);
57      }
58  
59      /**
60       * Generic resource URL fetcher. One way or the other we'll find it! Either
61       * as a relative or an absolute reference.
62       * 
63       * @param <T> the type of the resource
64       * @param clazz the basis to search for the resource first.
65       * @param resourceName
66       *            The resource to find
67       * @return The requested resource
68       * @throws MissingResourceException
69       *             if the resource can not be found
70       */
71      public static <T> URL getResource(Class<T> clazz, String resourceName) throws MissingResourceException {
72          URL resource = CWClassLoader.instance(clazz).findResource(resourceName);
73  
74          if (resource == null) {
75              throw new MissingResourceException(JSOtherMsg.lookupText("Cannot find resource: {0}", resourceName), clazz.getName(), resourceName);
76          }
77  
78          return resource;
79      }
80  
81      /**
82       * Generic resource URL fetcher
83       * 
84       * @param search
85       *            The name of the resource (without a leading /) to find
86       * @return The requested resource
87       * @throws IOException
88       *             if there is a problem reading the file
89       * @throws MissingResourceException
90       *             if the resource can not be found
91       */
92      public static InputStream getResourceAsStream(String search) throws IOException, MissingResourceException {
93          return getResourceAsStream(CallContext.getCallingClass(), search);
94      }
95  
96      /**
97       * Generic resource URL fetcher
98       * 
99       * @param <T> the type of the resource
100      * @param clazz the basis to search for the resource first.
101      * @param search
102      *            The name of the resource (without a leading /) to find
103      * @return The requested resource
104      * @throws IOException
105      *             if there is a problem reading the file
106      * @throws MissingResourceException
107      *             if the resource can not be found
108      */
109     public static <T> InputStream getResourceAsStream(Class<T> clazz, String search) throws IOException, MissingResourceException {
110         return ResourceUtil.getResource(clazz, search).openStream();
111     }
112 
113     /**
114      * Get and load a properties file from the writable area or if that fails
115      * from the classpath (where a default ought to be stored)
116      * 
117      * @param subject
118      *            The name of the desired resource (without any extension)
119      * @return The found and loaded properties file
120      * @throws IOException
121      *             if the resource can not be loaded
122      */
123     public static PropertyMap getProperties(String subject) throws IOException {
124         return getProperties(CallContext.getCallingClass(), subject);
125     }
126 
127     /**
128      * Get and load a properties file from the writable area or if that fails
129      * from the classpath (where a default ought to be stored)
130      * 
131      * @param <T> the type of the resource
132      * @param clazz
133      *            The name of the desired resource
134      * @return The found and loaded properties file
135      * @throws IOException
136      *             if the resource can not be loaded
137      */
138     public static <T> PropertyMap getProperties(Class<T> clazz) throws IOException {
139         return getProperties(clazz, ClassUtil.getShortClassName(clazz));
140     }
141 
142     /**
143      * Get and load a properties file from the writable area or if that fails
144      * from the classpath (where a default ought to be stored)
145      * 
146      * @param <T> the type of the resource
147      * @param clazz
148      *            The name of the desired resource
149      * @param subject
150      *            The name of the desired resource (without any extension)
151      * @return The found and loaded properties file
152      * @throws IOException
153      *             if the resource can not be loaded
154      */
155     private static <T> PropertyMap getProperties(Class<T> clazz, String subject) throws IOException {
156         try {
157             String lookup = subject + FileUtil.EXTENSION_PROPERTIES;
158             InputStream in = getResourceAsStream(clazz, lookup);
159 
160             PropertyMap prop = new PropertyMap();
161             prop.load(in);
162             return prop;
163         } catch (MissingResourceException e) {
164             return new PropertyMap();
165         }
166     }
167 }
168