KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > util > resources > ResourceBundleResourceManager


1 /**
2  * Copyright (c) 2003-2006, David A. Czarnecki
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the
9  * following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11  * following disclaimer in the documentation and/or other materials provided with the distribution.
12  * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
13  * endorse or promote products derived from this software without specific prior written permission.
14  * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
15  * without prior written permission of David A. Czarnecki.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */

31 package org.blojsom.util.resources;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 import java.text.MessageFormat JavaDoc;
37 import java.util.Locale JavaDoc;
38 import java.util.MissingResourceException JavaDoc;
39 import java.util.ResourceBundle JavaDoc;
40
41 /**
42  * ResourceBundleResourceManager
43  *
44  * @author David Czarnecki
45  * @version $Id: ResourceBundleResourceManager.java,v 1.2 2006/03/21 03:11:09 czarneckid Exp $
46  * @since blojsom 3.0
47  */

48 public class ResourceBundleResourceManager implements ResourceManager {
49
50     private Log _logger = LogFactory.getLog(ResourceBundleResourceManager.class);
51     private String JavaDoc[] _resourceBundles;
52
53     /**
54      * Default constructor;
55      */

56     public ResourceBundleResourceManager() {
57     }
58
59     /**
60      * Set the list of resource bundles to load
61      *
62      * @param resourceBundles List of resource bundles
63      */

64     public void setResourceBundlesToLoad(String JavaDoc[] resourceBundles) {
65         _resourceBundles = resourceBundles;
66     }
67
68     /**
69      * Initialize the resource bundle manager.
70      * <p/>
71      * Resource bundles to pre-load are specified in a comma-separated list under the key
72      * <code>blojsom-resource-manager-bundles</code>.
73      */

74     public void init() throws org.blojsom.BlojsomException {
75         if (_resourceBundles != null && _resourceBundles.length > 0) {
76             for (int i = 0; i < _resourceBundles.length; i++) {
77                 String JavaDoc resourceBundle = _resourceBundles[i];
78                 try {
79                     ResourceBundle.getBundle(resourceBundle);
80                     if (_logger.isDebugEnabled()) {
81                         _logger.debug("Loaded resource bundle: " + resourceBundle);
82                     }
83                 } catch (Exception JavaDoc e) {
84                     if (_logger.isErrorEnabled()) {
85                         _logger.error(e);
86                     }
87                 }
88             }
89         }
90
91         if (_logger.isDebugEnabled()) {
92             _logger.debug("Initialized resource bundle resource manager");
93         }
94     }
95
96     /**
97      * Retrieve a string from a given resource bundle for the default locale.
98      *
99      * @param resourceID Resource ID to retrieve from the resource bundle
100      * @param resource Full-qualified resource bundle from which to retrieve the resource ID
101      * @param fallback Fallback string to use if the given resource ID cannot be found
102      * @return <code>resourceID</code> from resource bundle <code>resource</code> or <code>fallback</code> if the given resource ID cannot be found
103      */

104     public String JavaDoc getString(String JavaDoc resourceID, String JavaDoc resource, String JavaDoc fallback) {
105         try {
106             ResourceBundle JavaDoc resourceBundle = ResourceBundle.getBundle(resource);
107
108             return resourceBundle.getString(resourceID);
109         } catch (MissingResourceException JavaDoc e) {
110         }
111
112         return fallback;
113     }
114
115     /**
116      * Retrieve a string from a given resource bundle for the particular language and country locale.
117      *
118      * @param resourceID Resource ID to retrieve from the resource bundle
119      * @param resource Full-qualified resource bundle from which to retrieve the resource ID
120      * @param fallback Fallback string to use if the given resource ID cannot be found
121      * @param language Language code
122      * @return <code>resourceID</code> from resource bundle <code>resource</code> or <code>fallback</code> if the given resource ID cannot be found
123      */

124     public String JavaDoc getString(String JavaDoc resourceID, String JavaDoc resource, String JavaDoc fallback, String JavaDoc language) {
125         return getString(resourceID, resource, fallback, new Locale JavaDoc(language));
126     }
127
128     /**
129      * Retrieve a string from a given resource bundle for the particular language and country locale.
130      *
131      * @param resourceID Resource ID to retrieve from the resource bundle
132      * @param resource Full-qualified resource bundle from which to retrieve the resource ID
133      * @param fallback Fallback string to use if the given resource ID cannot be found
134      * @param language Language code
135      * @param country Country code
136      * @return <code>resourceID</code> from resource bundle <code>resource</code> or <code>fallback</code> if the given resource ID cannot be found
137      */

138     public String JavaDoc getString(String JavaDoc resourceID, String JavaDoc resource, String JavaDoc fallback, String JavaDoc language, String JavaDoc country) {
139         return getString(resourceID, resource, fallback, new Locale JavaDoc(language, country));
140     }
141
142     /**
143      * Retrieve a string from a given resource bundle for the particular language and country locale.
144      *
145      * @param resourceID Resource ID to retrieve from the resource bundle
146      * @param resource Full-qualified resource bundle from which to retrieve the resource ID
147      * @param fallback Fallback string to use if the given resource ID cannot be found
148      * @param locale Locale object to use when retrieving the resource bundle
149      * @return <code>resourceID</code> from resource bundle <code>resource</code> or <code>fallback</code> if the given resource ID cannot be found
150      */

151     public String JavaDoc getString(String JavaDoc resourceID, String JavaDoc resource, String JavaDoc fallback, Locale JavaDoc locale) {
152         try {
153             ResourceBundle JavaDoc resourceBundle = ResourceBundle.getBundle(resource, locale);
154
155             return resourceBundle.getString(resourceID);
156         } catch (MissingResourceException JavaDoc e) {
157         }
158
159         return fallback;
160     }
161
162     /**
163      * Wrapper for {@link java.text.MessageFormat#format(String, Object[])}
164      *
165      * @param pattern Pattern
166      * @param arguments Arguments to apply to pattern
167      * @return String where {@link java.text.MessageFormat#format(String, Object[])} has been applied or <code>null</code>
168      * if there is an error applying the arguments to the pattern
169      */

170     public String JavaDoc format(String JavaDoc pattern, Object JavaDoc[] arguments) {
171         String JavaDoc value = null;
172
173         try {
174             value = MessageFormat.format(pattern, arguments);
175         } catch (Exception JavaDoc e) {
176         }
177
178         return value;
179     }
180 }
Popular Tags