KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lateralnz > common > util > ResourceUtils


1 /* ====================================================================
2  * The LateralNZ Software License, Version 1.0
3  *
4  * Copyright (c) 2003 LateralNZ. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by
21  * LateralNZ (http://www.lateralnz.org/) and other third parties."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "LateralNZ" must not be used to endorse or promote
26  * products derived from this software without prior written
27  * permission. For written permission, please
28  * contact oss@lateralnz.org.
29  *
30  * 5. Products derived from this software may not be called "Panther",
31  * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
32  * "LATERALNZ" appear in their name, without prior written
33  * permission of LateralNZ.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of LateralNZ. For more
51  * information on Lateral, please see http://www.lateralnz.com/ or
52  * http://www.lateralnz.org
53  *
54  */

55 package org.lateralnz.common.util;
56
57 import java.io.File JavaDoc;
58 import java.io.FileInputStream JavaDoc;
59 import java.io.FileNotFoundException JavaDoc;
60 import java.io.InputStream JavaDoc;
61 import java.io.IOException JavaDoc;
62
63 import java.util.Enumeration JavaDoc;
64 import java.util.HashMap JavaDoc;
65 import java.util.Iterator JavaDoc;
66 import java.util.Locale JavaDoc;
67 import java.util.Map JavaDoc;
68 import java.util.MissingResourceException JavaDoc;
69 import java.util.Properties JavaDoc;
70 import java.util.ResourceBundle JavaDoc;
71
72 /**
73  * a utility class for loading resource bundles and property files
74  *
75  * @author J R Briggs
76  */

77 public final class ResourceUtils implements Constants {
78   private ResourceUtils() {
79   }
80
81   public static final Properties JavaDoc getProperties(String JavaDoc filename) throws IOException JavaDoc, FileNotFoundException JavaDoc {
82     File JavaDoc f = new File JavaDoc(filename);
83     InputStream JavaDoc is = null;
84     try {
85       if (f.exists()) {
86         is = new FileInputStream JavaDoc(filename);
87       }
88       else {
89         // prepend a forward slash to make sure it doesn't put package names
90
// on the supplied filename
91
is = ResourceUtils.class.getResourceAsStream(FORWARD_SLASH + filename);
92       }
93       if (is == null) {
94         throw new FileNotFoundException JavaDoc(filename);
95       }
96       Properties JavaDoc p = new Properties JavaDoc();
97       p.load(is);
98
99       return p;
100     }
101     finally {
102       IOUtils.close(is);
103     }
104   }
105   
106  /**
107   * get a resource bundle using the specified class(loader) to load it
108   */

109   public static final ResourceBundle JavaDoc getStaticBundle(Class JavaDoc c) {
110     return getStaticBundle(c.getName(), Locale.getDefault(), c);
111   }
112
113  /**
114   * get a resource bundle with the specified name, using the specified class(loader) to load it
115   */

116   public static final ResourceBundle JavaDoc getStaticBundle(String JavaDoc name, Class JavaDoc c) {
117     return getStaticBundle(name, Locale.getDefault(), c);
118   }
119
120  /**
121   * get a resource bundle with the specified name and locale,
122   * using the specified class(loader) to load it
123   */

124   public static final ResourceBundle JavaDoc getStaticBundle(String JavaDoc name, Locale JavaDoc locale, Class JavaDoc c) {
125     ResourceBundle JavaDoc rb = null;
126
127     try {
128       rb = ResourceBundle.getBundle(name, locale);
129     }
130     catch (Exception JavaDoc e1) {
131       try {
132         rb = ResourceBundle.getBundle(name, locale, Thread.currentThread().getContextClassLoader());
133       }
134       catch (Exception JavaDoc e2) {
135         rb = ResourceBundle.getBundle(name, locale, c.getClassLoader());
136       }
137     }
138
139     return rb;
140   }
141   
142   public static final String JavaDoc getString(ResourceBundle JavaDoc res, String JavaDoc key, String JavaDoc def) {
143     try {
144       return res.getString(key);
145     }
146     catch (MissingResourceException JavaDoc mre) {
147       return def;
148     }
149   }
150   
151   public static final String JavaDoc getString(ResourceBundle JavaDoc res, String JavaDoc key) {
152     return getString(res, key, EMPTY);
153   }
154   
155   public static final boolean hasString(ResourceBundle JavaDoc resources, String JavaDoc key) {
156     try {
157       if (!StringUtils.isEmpty(resources.getString(key))) {
158         return true;
159       }
160     }
161     catch (MissingResourceException JavaDoc mre) { }
162     return false;
163   }
164   
165  /**
166   * load a properties object with the contents of a map
167   */

168   public static final void loadProperties(Properties JavaDoc props, Map JavaDoc map) {
169     Iterator JavaDoc iter = map.keySet().iterator();
170     String JavaDoc key;
171     while (iter.hasNext()) {
172       key = (String JavaDoc)iter.next();
173       props.setProperty(key, (String JavaDoc)map.get(key));
174     }
175   }
176   
177  /**
178   * turn a resource bundle into a map
179   */

180   public static final Map JavaDoc toMap(ResourceBundle JavaDoc res) {
181     HashMap JavaDoc map = new HashMap JavaDoc();
182     Enumeration JavaDoc en = res.getKeys();
183     while (en.hasMoreElements()) {
184       String JavaDoc key = (String JavaDoc)en.nextElement();
185       map.put(key, res.getObject(key));
186     }
187     return map;
188   }
189   
190  /**
191   * convert a resource bundle to a properties object
192   */

193   public static final Properties JavaDoc toProperties(ResourceBundle JavaDoc res) {
194     Properties JavaDoc props = new Properties JavaDoc();
195     Enumeration JavaDoc en = res.getKeys();
196     while (en.hasMoreElements()) {
197       String JavaDoc key = (String JavaDoc)en.nextElement();
198       props.setProperty(key, res.getString(key));
199     }
200     return props;
201   }
202 }
Popular Tags