001: /* ====================================================================
002: * The LateralNZ Software License, Version 1.0
003: *
004: * Copyright (c) 2003 LateralNZ. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by
021: * LateralNZ (http://www.lateralnz.org/) and other third parties."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. The names "LateralNZ" must not be used to endorse or promote
026: * products derived from this software without prior written
027: * permission. For written permission, please
028: * contact oss@lateralnz.org.
029: *
030: * 5. Products derived from this software may not be called "Panther",
031: * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
032: * "LATERALNZ" appear in their name, without prior written
033: * permission of LateralNZ.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of LateralNZ. For more
051: * information on Lateral, please see http://www.lateralnz.com/ or
052: * http://www.lateralnz.org
053: *
054: */
055: package org.lateralnz.common.util;
056:
057: import java.io.File;
058: import java.io.FileInputStream;
059: import java.io.FileNotFoundException;
060: import java.io.InputStream;
061: import java.io.IOException;
062:
063: import java.util.Enumeration;
064: import java.util.HashMap;
065: import java.util.Iterator;
066: import java.util.Locale;
067: import java.util.Map;
068: import java.util.MissingResourceException;
069: import java.util.Properties;
070: import java.util.ResourceBundle;
071:
072: /**
073: * a utility class for loading resource bundles and property files
074: *
075: * @author J R Briggs
076: */
077: public final class ResourceUtils implements Constants {
078: private ResourceUtils() {
079: }
080:
081: public static final Properties getProperties(String filename)
082: throws IOException, FileNotFoundException {
083: File f = new File(filename);
084: InputStream is = null;
085: try {
086: if (f.exists()) {
087: is = new FileInputStream(filename);
088: } else {
089: // prepend a forward slash to make sure it doesn't put package names
090: // on the supplied filename
091: is = ResourceUtils.class
092: .getResourceAsStream(FORWARD_SLASH + filename);
093: }
094: if (is == null) {
095: throw new FileNotFoundException(filename);
096: }
097: Properties p = new Properties();
098: p.load(is);
099:
100: return p;
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 getStaticBundle(Class 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 getStaticBundle(String name,
117: Class c) {
118: return getStaticBundle(name, Locale.getDefault(), c);
119: }
120:
121: /**
122: * get a resource bundle with the specified name and locale,
123: * using the specified class(loader) to load it
124: */
125: public static final ResourceBundle getStaticBundle(String name,
126: Locale locale, Class c) {
127: ResourceBundle rb = null;
128:
129: try {
130: rb = ResourceBundle.getBundle(name, locale);
131: } catch (Exception e1) {
132: try {
133: rb = ResourceBundle.getBundle(name, locale, Thread
134: .currentThread().getContextClassLoader());
135: } catch (Exception e2) {
136: rb = ResourceBundle.getBundle(name, locale, c
137: .getClassLoader());
138: }
139: }
140:
141: return rb;
142: }
143:
144: public static final String getString(ResourceBundle res,
145: String key, String def) {
146: try {
147: return res.getString(key);
148: } catch (MissingResourceException mre) {
149: return def;
150: }
151: }
152:
153: public static final String getString(ResourceBundle res, String key) {
154: return getString(res, key, EMPTY);
155: }
156:
157: public static final boolean hasString(ResourceBundle resources,
158: String key) {
159: try {
160: if (!StringUtils.isEmpty(resources.getString(key))) {
161: return true;
162: }
163: } catch (MissingResourceException mre) {
164: }
165: return false;
166: }
167:
168: /**
169: * load a properties object with the contents of a map
170: */
171: public static final void loadProperties(Properties props, Map map) {
172: Iterator iter = map.keySet().iterator();
173: String key;
174: while (iter.hasNext()) {
175: key = (String) iter.next();
176: props.setProperty(key, (String) map.get(key));
177: }
178: }
179:
180: /**
181: * turn a resource bundle into a map
182: */
183: public static final Map toMap(ResourceBundle res) {
184: HashMap map = new HashMap();
185: Enumeration en = res.getKeys();
186: while (en.hasMoreElements()) {
187: String key = (String) en.nextElement();
188: map.put(key, res.getObject(key));
189: }
190: return map;
191: }
192:
193: /**
194: * convert a resource bundle to a properties object
195: */
196: public static final Properties toProperties(ResourceBundle res) {
197: Properties props = new Properties();
198: Enumeration en = res.getKeys();
199: while (en.hasMoreElements()) {
200: String key = (String) en.nextElement();
201: props.setProperty(key, res.getString(key));
202: }
203: return props;
204: }
205: }
|