| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
package org.apache.commons.i18n; |
| 22 | |
|
| 23 | |
import java.text.MessageFormat; |
| 24 | |
import java.util.Enumeration; |
| 25 | |
import java.util.HashMap; |
| 26 | |
import java.util.Locale; |
| 27 | |
import java.util.Map; |
| 28 | |
import java.util.MissingResourceException; |
| 29 | |
import java.util.ResourceBundle; |
| 30 | |
import java.util.logging.Level; |
| 31 | |
import java.util.logging.Logger; |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
public class ResourceBundleMessageProvider implements MessageProvider { |
| 39 | 2 | private static Logger logger = Logger.getLogger(ResourceBundleMessageProvider.class.getName()); |
| 40 | |
|
| 41 | |
private final String baseName; |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | 9 | public ResourceBundleMessageProvider(String baseName) throws MessageNotFoundException { |
| 48 | 9 | this.baseName = baseName; |
| 49 | |
|
| 50 | |
try { |
| 51 | 9 | ResourceBundle.getBundle(baseName); |
| 52 | |
} |
| 53 | 2 | catch ( MissingResourceException e ) { |
| 54 | 2 | String msg = MessageFormat.format( |
| 55 | |
I18nUtils.INTERNAL_MESSAGES.getString(I18nUtils.RESOURCE_BUNDLE_NOT_FOUND), |
| 56 | |
new String[]{baseName}); |
| 57 | 2 | logger.log(Level.WARNING, msg); |
| 58 | 2 | throw new MessageNotFoundException(msg); |
| 59 | 7 | } |
| 60 | 7 | } |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
public String getText(String id, String entry, Locale locale) throws MessageNotFoundException { |
| 66 | |
try { |
| 67 | 24 | ResourceBundle resourceBundle = ResourceBundle.getBundle(baseName, locale); |
| 68 | 24 | return resourceBundle.getObject(id+"."+entry).toString(); |
| 69 | |
} |
| 70 | 2 | catch ( MissingResourceException e ) { |
| 71 | 2 | logger.log( |
| 72 | |
Level.WARNING, |
| 73 | |
MessageFormat.format( |
| 74 | |
I18nUtils.INTERNAL_MESSAGES.getString(I18nUtils.NO_MESSAGE_ENTRIES_FOUND), |
| 75 | |
new String[] { id })); |
| 76 | 2 | return null; |
| 77 | |
} |
| 78 | |
} |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
public Map getEntries(String id, Locale locale) { |
| 84 | 4 | String messageIdentifier = id+"."; |
| 85 | 4 | Map entries = null; |
| 86 | 4 | ResourceBundle resourceBundle = ResourceBundle.getBundle(baseName, locale); |
| 87 | 4 | Enumeration keys = resourceBundle.getKeys(); |
| 88 | 16 | while ( keys.hasMoreElements() ) { |
| 89 | 12 | String key = (String)keys.nextElement(); |
| 90 | 12 | if ( key.startsWith(messageIdentifier) ) { |
| 91 | 9 | if ( entries == null ) { |
| 92 | 3 | entries = new HashMap(); |
| 93 | |
} |
| 94 | 9 | entries.put(key.substring(messageIdentifier.length()), resourceBundle.getString(key)); |
| 95 | |
} |
| 96 | 12 | } |
| 97 | 4 | if ( entries == null ) { |
| 98 | 1 | throw new MessageNotFoundException(MessageFormat.format( |
| 99 | |
I18nUtils.INTERNAL_MESSAGES.getString(I18nUtils.NO_MESSAGE_ENTRIES_FOUND), |
| 100 | |
new String[] { id })); |
| 101 | |
} |
| 102 | 3 | return entries; |
| 103 | |
} |
| 104 | |
} |