KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > java > plugin > util > ResourceManager


1 /*****************************************************************************
2  * Java Plug-in Framework (JPF)
3  * Copyright (C) 2004-2005 Dmitry Olshansky
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *****************************************************************************/

19 package org.java.plugin.util;
20
21 import java.util.Collection JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Locale JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.MissingResourceException JavaDoc;
28 import java.util.ResourceBundle JavaDoc;
29
30 /**
31  * Utility class to manage localization resources. This class is not for public
32  * usage but mainly for custom implementations developers to provide them
33  * uniform access and organization of locale specific data.
34  * <br>
35  * Class usage is very simple. Put your locale sensible data into
36  * <code>Resources.properties</code> files and save them near classes that you
37  * are going to get localized. For {@link java.util.Locale} to file mapping
38  * details see {@link ResourceBundle} documentation.
39  *
40  * @version $Id: ResourceManager.java,v 1.2 2005/10/22 15:15:59 ddimon Exp $
41  */

42 public final class ResourceManager {
43     private static final Object JavaDoc FAKE_BUNDLE = new Object JavaDoc();
44     private static final Map JavaDoc bundles =
45         Collections.synchronizedMap(new HashMap JavaDoc());
46     
47     /**
48      * @param packageName package name, used for
49      * <code>Resources.properties</code> file look-up
50      * @param messageKey message key
51      * @return message for {@link Locale#getDefault() default locale}
52      */

53     public static String JavaDoc getMessage(final String JavaDoc packageName,
54             final String JavaDoc messageKey) {
55         return getMessage(packageName, messageKey, Locale.getDefault(), null);
56     }
57     
58     /**
59      * @param packageName package name, used for
60      * <code>Resources.properties</code> file look-up
61      * @param messageKey message key
62      * @param data data for parameter placeholders substitution, may be
63      * <code>Object</code>, <code>array</code> or
64      * <code>Collection</code>.
65      * @return message for {@link Locale#getDefault() default locale}
66      */

67     public static String JavaDoc getMessage(final String JavaDoc packageName,
68             final String JavaDoc messageKey, final Object JavaDoc data) {
69         return getMessage(packageName, messageKey, Locale.getDefault(), data);
70     }
71
72     /**
73      * @param packageName package name, used for
74      * <code>Resources.properties</code> file look-up
75      * @param messageKey message key
76      * @param locale locale to get message for
77      * @return message for given locale
78      */

79     public static String JavaDoc getMessage(final String JavaDoc packageName,
80             final String JavaDoc messageKey, final Locale JavaDoc locale) {
81         return getMessage(packageName, messageKey, locale, null);
82     }
83
84     /**
85      * @param packageName package name, used for
86      * <code>Resources.properties</code> file look-up
87      * @param messageKey message key
88      * @param locale locale to get message for
89      * @param data data for parameter placeholders substitution, may be
90      * <code>Object</code>, <code>array</code> or
91      * <code>Collection</code>.
92      * @return message for given locale
93      */

94     public static String JavaDoc getMessage(final String JavaDoc packageName,
95             final String JavaDoc messageKey, final Locale JavaDoc locale, final Object JavaDoc data) {
96         Object JavaDoc obj = bundles.get(packageName + '|' + locale);
97         if (obj == null) {
98             try {
99                 obj = ResourceBundle.getBundle(packageName + ".Resources", //$NON-NLS-1$
100
locale);
101             } catch (MissingResourceException JavaDoc mre) {
102                 obj = FAKE_BUNDLE;
103             }
104             bundles.put(packageName + '|' + locale, obj);
105         }
106         if (obj == FAKE_BUNDLE) {
107             return "resource " + packageName + '.' + messageKey //$NON-NLS-1$
108
+ " not found for locale " + locale; //$NON-NLS-1$
109
}
110         try {
111             String JavaDoc result = ((ResourceBundle JavaDoc) obj).getString(messageKey);
112             return (data == null) ? result : processParams(result, data);
113         } catch (MissingResourceException JavaDoc mre) {
114             return "resource " + packageName + '.' + messageKey //$NON-NLS-1$
115
+ " not found for locale " + locale; //$NON-NLS-1$
116
}
117     }
118
119     private static String JavaDoc processParams(final String JavaDoc str, final Object JavaDoc data) {
120         String JavaDoc result = str;
121         if ((data != null) && data.getClass().isArray()) {
122             Object JavaDoc[] params = (Object JavaDoc[])data;
123             for (int i = 0; i < params.length; i++) {
124                 result = replaceAll(result, "{" + i + "}", "" + params[i]); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
125
}
126         } else if (data instanceof Collection JavaDoc) {
127             int i = 0;
128             for (Iterator JavaDoc it = ((Collection JavaDoc)data).iterator(); it.hasNext();
129                     i++) {
130                 result = replaceAll(result, "{" + i + "}", "" + it.next()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
131
}
132         } else {
133             result = replaceAll(result, "{0}", "" + data); //$NON-NLS-1$ //$NON-NLS-2$
134
}
135         return result;
136     }
137
138     private static String JavaDoc replaceAll(final String JavaDoc str, final String JavaDoc from,
139             final String JavaDoc to) {
140         String JavaDoc result = str;
141         int p = 0;
142         while (true) {
143             p = result.indexOf(from, p);
144             if (p == -1) {
145                 break;
146             }
147             result = result.substring(0, p) + to
148                 + result.substring(p + from.length());
149             p += to.length();
150         }
151         return result;
152     }
153
154     private ResourceManager() {
155         // no-op
156
}
157
158 }
159
Popular Tags