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 it's 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 classloader situation.
33   * 
34   * @see gnu.lgpl.License for license details.<br>
35   *      The copyright to this program is held by it's authors.
36   * @author Joe Walker [joe at eireneh dot com]
37   * @author DM Smith [ dmsmith555 at yahoo dot com ]
38   */
39  public final class ResourceUtil {
40      /**
41       * Prevent Instantiation
42       */
43      private ResourceUtil() {
44      }
45  
46      /**
47       * Generic resource URL fetcher. One way or the other we'll find it! Either
48       * as a relative or an absolute reference.
49       * 
50       * @param search
51       *            The name of the resource (without a leading /) to find
52       * @return The requested resource
53       * @throws MissingResourceException
54       *             if the resource can not be found
55       */
56      public static URL getResource(String search) throws MissingResourceException {
57          return getResource(CallContext.getCallingClass(), search);
58      }
59  
60      /**
61       * Generic resource URL fetcher. One way or the other we'll find it! Either
62       * as a relative or an absolute reference.
63       * 
64       * @param clazz
65       *            The resource to find
66       * @return The requested resource
67       * @throws MissingResourceException
68       *             if the resource can not be found
69       */
70      public static <T> URL getResource(Class<T> clazz, String resourceName) throws MissingResourceException {
71          URL resource = CWClassLoader.instance(clazz).findResource(resourceName);
72  
73          if (resource == null) {
74              throw new MissingResourceException(JSOtherMsg.lookupText("Cannot find resource: {0}", resourceName), clazz.getName(), resourceName);
75          }
76  
77          return resource;
78      }
79  
80      /**
81       * Generic resource URL fetcher
82       * 
83       * @return The requested resource
84       * @throws IOException
85       *             if there is a problem reading the file
86       * @throws MissingResourceException
87       *             if the resource can not be found
88       */
89      public static InputStream getResourceAsStream(String search) throws IOException, MissingResourceException {
90          return getResourceAsStream(CallContext.getCallingClass(), search);
91      }
92  
93      /**
94       * Generic resource URL fetcher
95       * 
96       * @return The requested resource
97       * @throws IOException
98       *             if there is a problem reading the file
99       * @throws MissingResourceException
100      *             if the resource can not be found
101      */
102     public static <T> InputStream getResourceAsStream(Class<T> clazz, String search) throws IOException, MissingResourceException {
103         return ResourceUtil.getResource(clazz, search).openStream();
104     }
105 
106     /**
107      * Get and load a properties file from the writable area or if that fails
108      * from the classpath (where a default ought to be stored)
109      * 
110      * @param subject
111      *            The name of the desired resource (without any extension)
112      * @return The found and loaded properties file
113      * @throws IOException
114      *             if the resource can not be loaded
115      * @throws MissingResourceException
116      *             if the resource can not be found
117      */
118     public static PropertyMap getProperties(String subject) throws IOException {
119         return getProperties(CallContext.getCallingClass(), subject);
120     }
121 
122     /**
123      * Get and load a properties file from the writable area or if that fails
124      * from the classpath (where a default ought to be stored)
125      * 
126      * @param clazz
127      *            The name of the desired resource
128      * @return The found and loaded properties file
129      * @throws IOException
130      *             if the resource can not be loaded
131      * @throws MissingResourceException
132      *             if the resource can not be found
133      */
134     public static <T> PropertyMap getProperties(Class<T> clazz) throws IOException {
135         return getProperties(clazz, ClassUtil.getShortClassName(clazz));
136     }
137 
138     /**
139      * Get and load a properties file from the writable area or if that fails
140      * from the classpath (where a default ought to be stored)
141      * 
142      * @param clazz
143      *            The name of the desired resource
144      * @return The found and loaded properties file
145      * @throws IOException
146      *             if the resource can not be loaded
147      * @throws MissingResourceException
148      *             if the resource can not be found
149      */
150     private static <T> PropertyMap getProperties(Class<T> clazz, String subject) throws IOException {
151         try {
152             String lookup = subject + FileUtil.EXTENSION_PROPERTIES;
153             InputStream in = getResourceAsStream(clazz, lookup);
154 
155             PropertyMap prop = new PropertyMap();
156             prop.load(in);
157             return prop;
158         } catch (MissingResourceException e) {
159             return new PropertyMap();
160         }
161     }
162 }
163