1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
/*****************************************************************************
* Java Plug-in Framework (JPF)
* Copyright (C) 2004-2005 Dmitry Olshansky
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*****************************************************************************/
package org.java.plugin.util;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
/**
* Utility class to manage localization resources. This class is not for public
* usage but mainly for custom implementations developers to provide them
* uniform access and organization of locale specific data.
* <br>
* Class usage is very simple. Put your locale sensible data into
* <code>Resources.properties</code> files and save them near classes that you
* are going to get localized. For {@link java.util.Locale} to file mapping
* details see {@link ResourceBundle} documentation.
*
* @version $Id$
*/
public final class ResourceManager {
private static final Object FAKE_BUNDLE = new Object();
private static final Map<String, Object> bundles =
Collections.synchronizedMap(new HashMap<String, Object>());
/**
* @param packageName package name, used for
* <code>Resources.properties</code> file look-up
* @param messageKey message key
* @return message for {@link Locale#getDefault() default locale}
*/
public static String getMessage(final String packageName,
final String messageKey) {
return getMessage(packageName, messageKey, Locale.getDefault(), null);
}
/**
* @param packageName package name, used for
* <code>Resources.properties</code> file look-up
* @param messageKey message key
* @param data data for parameter placeholders substitution, may be
* <code>Object</code>, <code>array</code> or
* <code>Collection</code>.
* @return message for {@link Locale#getDefault() default locale}
*/
public static String getMessage(final String packageName,
final String messageKey, final Object data) {
return getMessage(packageName, messageKey, Locale.getDefault(), data);
}
/**
* @param packageName package name, used for
* <code>Resources.properties</code> file look-up
* @param messageKey message key
* @param locale locale to get message for
* @return message for given locale
*/
public static String getMessage(final String packageName,
final String messageKey, final Locale locale) {
return getMessage(packageName, messageKey, locale, null);
}
/**
* @param packageName package name, used for
* <code>Resources.properties</code> file look-up
* @param messageKey message key
* @param locale locale to get message for
* @param data data for parameter placeholders substitution, may be
* <code>Object</code>, <code>array</code> or
* <code>Collection</code>.
* @return message for given locale
*/
public static String getMessage(final String packageName,
final String messageKey, final Locale locale, final Object data) {
Object obj = bundles.get(packageName + '|' + locale);
if (obj == null) {
try {
obj = ResourceBundle.getBundle(packageName + ".Resources", //$NON-NLS-1$
locale);
} catch (MissingResourceException mre) {
obj = FAKE_BUNDLE;
}
bundles.put(packageName + '|' + locale, obj);
}
if (obj == FAKE_BUNDLE) {
return "resource " + packageName + '.' + messageKey //$NON-NLS-1$
+ " not found for locale " + locale; //$NON-NLS-1$
}
try {
String result = ((ResourceBundle) obj).getString(messageKey);
return (data == null) ? result : processParams(result, data);
} catch (MissingResourceException mre) {
return "resource " + packageName + '.' + messageKey //$NON-NLS-1$
+ " not found for locale " + locale; //$NON-NLS-1$
}
}
private static String processParams(final String str, final Object data) {
String result = str;
if ((data != null) && data.getClass().isArray()) {
Object[] params = (Object[])data;
for (int i = 0; i < params.length; i++) {
result = replaceAll(result, "{" + i + "}", "" + params[i]); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
} else if (data instanceof Collection) {
int i = 0;
for (Object object : (Collection) data) {
result = replaceAll(result, "{" + i++ + "}", "" + object); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
} else {
result = replaceAll(result, "{0}", "" + data); //$NON-NLS-1$ //$NON-NLS-2$
}
return result;
}
private static String replaceAll(final String str, final String from,
final String to) {
String result = str;
int p = 0;
while (true) {
p = result.indexOf(from, p);
if (p == -1) {
break;
}
result = result.substring(0, p) + to
+ result.substring(p + from.length());
p += to.length();
}
return result;
}
private ResourceManager() {
// no-op
}
}
|