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 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * 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   * ID: $Id: ResourceUtil.java 2099 2011-03-07 17:13:00Z dmsmith $
21   */
22  package org.crosswire.common.util;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.net.URL;
27  import java.util.MissingResourceException;
28  
29  import org.crosswire.jsword.JSOtherMsg;
30  
31  /**
32   * Better implementations of the getResource methods with less ambiguity and
33   * that are less dependent on the specific classloader situation.
34   * 
35   * @see gnu.lgpl.License for license details.<br>
36   *      The copyright to this program is held by it's authors.
37   * @author Joe Walker [joe at eireneh dot com]
38   * @author DM Smith [ dmsmith555 at yahoo dot com ]
39   */
40  public final class ResourceUtil {
41      /**
42       * Prevent Instantiation
43       */
44      private ResourceUtil() {
45      }
46  
47      /**
48       * Generic resource URL fetcher. One way or the other we'll find it! Either
49       * as a relative or an absolute reference.
50       * 
51       * @param search
52       *            The name of the resource (without a leading /) to find
53       * @return The requested resource
54       * @throws MissingResourceException
55       *             if the resource can not be found
56       */
57      public static URL getResource(String search) throws MissingResourceException {
58          return getResource(CallContext.getCallingClass(), search);
59      }
60  
61      /**
62       * Generic resource URL fetcher. One way or the other we'll find it! Either
63       * as a relative or an absolute reference.
64       * 
65       * @param clazz
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       * @return The requested resource
85       * @throws IOException
86       *             if there is a problem reading the file
87       * @throws MissingResourceException
88       *             if the resource can not be found
89       */
90      public static InputStream getResourceAsStream(String search) throws IOException, MissingResourceException {
91          return getResourceAsStream(CallContext.getCallingClass(), search);
92      }
93  
94      /**
95       * Generic resource URL fetcher
96       * 
97       * @return The requested resource
98       * @throws IOException
99       *             if there is a problem reading the file
100      * @throws MissingResourceException
101      *             if the resource can not be found
102      */
103     public static <T> InputStream getResourceAsStream(Class<T> clazz, String search) throws IOException, MissingResourceException {
104         return ResourceUtil.getResource(clazz, search).openStream();
105     }
106 
107     /**
108      * Get and load a properties file from the writable area or if that fails
109      * from the classpath (where a default ought to be stored)
110      * 
111      * @param subject
112      *            The name of the desired resource (without any extension)
113      * @return The found and loaded properties file
114      * @throws IOException
115      *             if the resource can not be loaded
116      * @throws MissingResourceException
117      *             if the resource can not be found
118      */
119     public static PropertyMap getProperties(String subject) throws IOException {
120         return getProperties(CallContext.getCallingClass(), subject);
121     }
122 
123     /**
124      * Get and load a properties file from the writable area or if that fails
125      * from the classpath (where a default ought to be stored)
126      * 
127      * @param clazz
128      *            The name of the desired resource
129      * @return The found and loaded properties file
130      * @throws IOException
131      *             if the resource can not be loaded
132      * @throws MissingResourceException
133      *             if the resource can not be found
134      */
135     public static <T> PropertyMap getProperties(Class<T> clazz) throws IOException {
136         return getProperties(clazz, ClassUtil.getShortClassName(clazz));
137     }
138 
139     /**
140      * Get and load a properties file from the writable area or if that fails
141      * from the classpath (where a default ought to be stored)
142      * 
143      * @param clazz
144      *            The name of the desired resource
145      * @return The found and loaded properties file
146      * @throws IOException
147      *             if the resource can not be loaded
148      * @throws MissingResourceException
149      *             if the resource can not be found
150      */
151     private static <T> PropertyMap getProperties(Class<T> clazz, String subject) throws IOException {
152         try {
153             String lookup = subject + FileUtil.EXTENSION_PROPERTIES;
154             InputStream in = getResourceAsStream(clazz, lookup);
155 
156             PropertyMap prop = new PropertyMap();
157             prop.load(in);
158             return prop;
159         } catch (MissingResourceException e) {
160             return new PropertyMap();
161         }
162     }
163 }
164