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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
/*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program 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.
*
* Copyright (c) 2007 - 2009 Pentaho Corporation and Contributors. All rights reserved.
*/
package org.pentaho.reporting.libraries.base.util;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
/**
* A helper class for a simplified resource-bundle access. This class simply ignores all resource-bundle related errors
* and prints place-holder strings if a localization key cannot be found.
*
* @author David Kincade
*/
public class Messages extends ResourceBundleSupport
{
/**
* Creates a new Messages-collection. The locale and baseName will be used to create the resource-bundle that backs up
* this implementation.
*
* @param locale the locale.
* @param baseName the baseName of the resource-bundle.
* @see ResourceBundle#getBundle(String, Locale)
*/
public Messages(final Locale locale, final String baseName)
{
super(locale, baseName);
}
/**
* Creates a new Messages-collection. The locale and baseName will be used to create the resource-bundle that backs up
* this implementation.
*
* @param locale the locale.
* @param baseName the baseName of the resource-bundle.
* @param resourceBundle a predefined resource-bundle.
*/
public Messages(final Locale locale, final ResourceBundle resourceBundle, final String baseName)
{
super(locale, resourceBundle, baseName);
}
/**
* Creates a new Messages-collection. The locale and baseName will be used to create the resource-bundle that backs up
* this implementation.
*
* @param locale the locale.
* @param resourceBundle a predefined resource-bundle.
*/
public Messages(final Locale locale, final ResourceBundle resourceBundle)
{
super(locale, resourceBundle);
}
/**
* Creates a new Messages-collection. The default locale and baseName will be used to create the resource-bundle that
* backs up this implementation.
*
* @param baseName the baseName of the resource-bundle.
*/
public Messages(final String baseName)
{
super(baseName);
}
/**
* Creates a new Messages-collection. The default locale and baseName will be used to create the resource-bundle that
* backs up this implementation.
*
* @param baseName the baseName of the resource-bundle.
* @param resourceBundle a predefined resource-bundle.
*/
public Messages(final ResourceBundle resourceBundle, final String baseName)
{
super(resourceBundle, baseName);
}
/**
* Creates a new Messages-collection. The default locale and baseName will be used to create the resource-bundle that
* backs up this implementation.
*
* @param resourceBundle a predefined resource-bundle.
*/
public Messages(final ResourceBundle resourceBundle)
{
super(resourceBundle);
}
/**
* Gets a string for the given key from this resource bundle or one of its parents. If the key is a link, the link is
* resolved and the referenced string is returned instead. If the given key cannot be resolved, no exception will be
* thrown and a generic placeholder is used instead.
*
* @param key the key for the desired string
* @return the string for the given key
* @throws NullPointerException if <code>key</code> is <code>null</code>
* @throws MissingResourceException if no object for the given key can be found
*/
public String getString(final String key)
{
try
{
return super.strictString(key);
}
catch (final MissingResourceException e)
{
return '!' + key + '!';
}
}
/**
* Formats the message stored in the resource bundle (using a MessageFormat).
*
* @param key the resourcebundle key
* @param param1 the parameter for the message
* @return the formated string
*/
public String getString(final String key, final String param1)
{
try
{
return formatMessage(key, param1);
}
catch (final MissingResourceException e)
{
return '!' + key + '!';
}
}
/**
* Formats the message stored in the resource bundle (using a MessageFormat).
*
* @param key the resourcebundle key
* @param param1 the parameter for the message
* @param param2 the parameter for the message
* @return the formated string
*/
public String getString(final String key, final String param1, final String param2)
{
try
{
return formatMessage(key, param1, param2);
}
catch (final MissingResourceException e)
{
return '!' + key + '!';
}
}
/**
* Formats the message stored in the resource bundle (using a MessageFormat).
*
* @param key the resourcebundle key
* @param param1 the parameter for the message
* @param param2 the parameter for the message
* @param param3 the parameter for the message
* @return the formated string
*/
public String getString(final String key, final String param1, final String param2, final String param3)
{
try
{
return formatMessage(key, new Object[]{param1, param2, param3});
}
catch (final MissingResourceException e)
{
return '!' + key + '!';
}
}
/**
* Formats the message stored in the resource bundle (using a MessageFormat).
*
* @param key the resourcebundle key
* @param param1 the parameter for the message
* @param param2 the parameter for the message
* @param param3 the parameter for the message
* @param param4 the parameter for the message
* @return the formated string
*/
public String getString(final String key,
final String param1,
final String param2,
final String param3,
final String param4)
{
try
{
return formatMessage(key, new Object[]{param1, param2, param3, param4});
}
catch (final MissingResourceException e)
{
return '!' + key + '!';
}
}
/**
* Get a formatted error message. The message consists of two parts. The first part is the
* error numeric Id associated with the key used to identify the message in the resource file.
* For instance, suppose the error key is MyClass.ERROR_0068_TEST_ERROR. The first
* part of the error msg would be "0068". The second part of the returned string
* is simply the <code>msg</code> parameter.
* <p/>
* Currently the format is:
* error key - error msg
* For instance:
* "0068 - A test error message."
* <p/>
* Currently the format is: error key - error msg For instance: "0069 - You were punched by the donkey."
*
* @param key String containing the key that was used to obtain the <code>msg</code> parameter from the resource
* file.
* @param msg String containing the message that was obtained from the resource file using the <code>key</code>
* parameter.
* @return String containing the formatted error message.
*/
public String formatErrorMessage(final String key, final String msg)
{
try
{
final int end;
final int errorMarker = key.indexOf(".ERROR_");
if (errorMarker < 0)
{
end = key.length();
}
else
{
end = Math.min(errorMarker + ".ERROR_0000".length(), key.length()); //$NON-NLS-1$
}
return getString("MESSUTIL.ERROR_FORMAT_MASK", key.substring(0, end), msg); //$NON-NLS-1$
}
catch (final Exception e)
{
return "!MESSUTIL.ERROR_FORMAT_MASK:" + key + '!';
}
}
/**
* Get a formatted error message from the resource-bundle. The message consists of two parts. The first part is the
* error numeric Id associated with the key used to identify the message in the resource file. For instance, suppose
* the error key is MyClass.ERROR_0069_DONKEY_PUNCH. The first part of the error msg would be "0069". The second part
* of the returned string is simply the <code>msg</code> parameter.
* <p/>
* Currently the format is: error key - error msg For instance: "0069 - You were punched by the donkey."
*
* @param key String containing the key that was used to obtain the <code>msg</code> parameter from the resource
* file.
* @return String containing the formatted error message.
*/
public String getErrorString(final String key)
{
return formatErrorMessage(key, getString(key));
}
/**
* Get a parametrized formatted error message from the resource-bundle. The message consists of two parts. The first
* part is the error numeric Id associated with the key used to identify the message in the resource file. For
* instance, suppose the error key is MyClass.ERROR_0069_DONKEY_PUNCH. The first part of the error msg would be
* "0069". The second part of the returned string is simply the <code>msg</code> parameter.
* <p/>
* Currently the format is: error key - error msg For instance: "0069 - You were punched by the donkey."
*
* @param key String containing the key that was used to obtain the <code>msg</code> parameter from the resource
* file.
* @param param1 the parameter for the message
* @return String containing the formatted error message.
*/
public String getErrorString(final String key, final String param1)
{
return formatErrorMessage(key, getString(key, param1));
}
/**
* Get a parametrized formatted error message from the resource-bundle. The message consists of two parts. The first
* part is the error numeric Id associated with the key used to identify the message in the resource file. For
* instance, suppose the error key is MyClass.ERROR_0069_DONKEY_PUNCH. The first part of the error msg would be
* "0069". The second part of the returned string is simply the <code>msg</code> parameter.
* <p/>
* Currently the format is: error key - error msg For instance: "0069 - You were punched by the donkey."
*
* @param key String containing the key that was used to obtain the <code>msg</code> parameter from the resource
* file.
* @param param1 the parameter for the message
* @param param2 the parameter for the message
* @return String containing the formatted error message.
*/
public String getErrorString(final String key, final String param1, final String param2)
{
return formatErrorMessage(key, getString(key, param1, param2));
}
/**
* Get a parametrized formatted error message from the resource-bundle. The message consists of two parts. The first
* part is the error numeric Id associated with the key used to identify the message in the resource file. For
* instance, suppose the error key is MyClass.ERROR_0069_DONKEY_PUNCH. The first part of the error msg would be
* "0069". The second part of the returned string is simply the <code>msg</code> parameter.
* <p/>
* Currently the format is: error key - error msg For instance: "0069 - You were punched by the donkey."
*
* @param key String containing the key that was used to obtain the <code>msg</code> parameter from the resource
* file.
* @param param1 the parameter for the message
* @param param2 the parameter for the message
* @param param3 the parameter for the message
* @return String containing the formatted error message.
*/
public String getErrorString(final String key, final String param1, final String param2, final String param3)
{
return formatErrorMessage(key, getString(key, param1, param2, param3));
}
}
|