001: /**
002: * Copyright (c) 2003-2007, David A. Czarnecki
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009: * following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
011: * following disclaimer in the documentation and/or other materials provided with the distribution.
012: * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
013: * endorse or promote products derived from this software without specific prior written permission.
014: * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
015: * without prior written permission of David A. Czarnecki.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
018: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
019: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
022: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
025: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */package org.blojsom.util.resources;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034:
035: import java.text.MessageFormat;
036: import java.util.Locale;
037: import java.util.MissingResourceException;
038: import java.util.ResourceBundle;
039:
040: /**
041: * ResourceBundleResourceManager
042: *
043: * @author David Czarnecki
044: * @version $Id: ResourceBundleResourceManager.java,v 1.3 2007/01/17 02:35:19 czarneckid Exp $
045: * @since blojsom 3.0
046: */
047: public class ResourceBundleResourceManager implements ResourceManager {
048:
049: private Log _logger = LogFactory
050: .getLog(ResourceBundleResourceManager.class);
051: private String[] _resourceBundles;
052:
053: /**
054: * Default constructor;
055: */
056: public ResourceBundleResourceManager() {
057: }
058:
059: /**
060: * Set the list of resource bundles to load
061: *
062: * @param resourceBundles List of resource bundles
063: */
064: public void setResourceBundlesToLoad(String[] resourceBundles) {
065: _resourceBundles = resourceBundles;
066: }
067:
068: /**
069: * Initialize the resource bundle manager.
070: * <p/>
071: * Resource bundles to pre-load are specified in a comma-separated list under the key
072: * <code>blojsom-resource-manager-bundles</code>.
073: */
074: public void init() throws org.blojsom.BlojsomException {
075: if (_resourceBundles != null && _resourceBundles.length > 0) {
076: for (int i = 0; i < _resourceBundles.length; i++) {
077: String resourceBundle = _resourceBundles[i];
078: try {
079: ResourceBundle.getBundle(resourceBundle);
080: if (_logger.isDebugEnabled()) {
081: _logger.debug("Loaded resource bundle: "
082: + resourceBundle);
083: }
084: } catch (Exception e) {
085: if (_logger.isErrorEnabled()) {
086: _logger.error(e);
087: }
088: }
089: }
090: }
091:
092: if (_logger.isDebugEnabled()) {
093: _logger
094: .debug("Initialized resource bundle resource manager");
095: }
096: }
097:
098: /**
099: * Retrieve a string from a given resource bundle for the default locale.
100: *
101: * @param resourceID Resource ID to retrieve from the resource bundle
102: * @param resource Full-qualified resource bundle from which to retrieve the resource ID
103: * @param fallback Fallback string to use if the given resource ID cannot be found
104: * @return <code>resourceID</code> from resource bundle <code>resource</code> or <code>fallback</code> if the given resource ID cannot be found
105: */
106: public String getString(String resourceID, String resource,
107: String fallback) {
108: try {
109: ResourceBundle resourceBundle = ResourceBundle
110: .getBundle(resource);
111:
112: return resourceBundle.getString(resourceID);
113: } catch (MissingResourceException e) {
114: }
115:
116: return fallback;
117: }
118:
119: /**
120: * Retrieve a string from a given resource bundle for the particular language and country locale.
121: *
122: * @param resourceID Resource ID to retrieve from the resource bundle
123: * @param resource Full-qualified resource bundle from which to retrieve the resource ID
124: * @param fallback Fallback string to use if the given resource ID cannot be found
125: * @param language Language code
126: * @return <code>resourceID</code> from resource bundle <code>resource</code> or <code>fallback</code> if the given resource ID cannot be found
127: */
128: public String getString(String resourceID, String resource,
129: String fallback, String language) {
130: return getString(resourceID, resource, fallback, new Locale(
131: language));
132: }
133:
134: /**
135: * Retrieve a string from a given resource bundle for the particular language and country locale.
136: *
137: * @param resourceID Resource ID to retrieve from the resource bundle
138: * @param resource Full-qualified resource bundle from which to retrieve the resource ID
139: * @param fallback Fallback string to use if the given resource ID cannot be found
140: * @param language Language code
141: * @param country Country code
142: * @return <code>resourceID</code> from resource bundle <code>resource</code> or <code>fallback</code> if the given resource ID cannot be found
143: */
144: public String getString(String resourceID, String resource,
145: String fallback, String language, String country) {
146: return getString(resourceID, resource, fallback, new Locale(
147: language, country));
148: }
149:
150: /**
151: * Retrieve a string from a given resource bundle for the particular language and country locale.
152: *
153: * @param resourceID Resource ID to retrieve from the resource bundle
154: * @param resource Full-qualified resource bundle from which to retrieve the resource ID
155: * @param fallback Fallback string to use if the given resource ID cannot be found
156: * @param locale Locale object to use when retrieving the resource bundle
157: * @return <code>resourceID</code> from resource bundle <code>resource</code> or <code>fallback</code> if the given resource ID cannot be found
158: */
159: public String getString(String resourceID, String resource,
160: String fallback, Locale locale) {
161: try {
162: ResourceBundle resourceBundle = ResourceBundle.getBundle(
163: resource, locale);
164:
165: return resourceBundle.getString(resourceID);
166: } catch (MissingResourceException e) {
167: }
168:
169: return fallback;
170: }
171:
172: /**
173: * Wrapper for {@link java.text.MessageFormat#format(String, Object[])}
174: *
175: * @param pattern Pattern
176: * @param arguments Arguments to apply to pattern
177: * @return String where {@link java.text.MessageFormat#format(String, Object[])} has been applied or <code>null</code>
178: * if there is an error applying the arguments to the pattern
179: */
180: public String format(String pattern, Object[] arguments) {
181: String value = null;
182:
183: try {
184: value = MessageFormat.format(pattern, arguments);
185: } catch (Exception e) {
186: }
187:
188: return value;
189: }
190: }
|