下面列出了java.util.ResourceBundle#getObject ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns the compact number format patterns.
* @param formatStyle the style for formatting a number
* @return an array of compact number patterns
*/
@SuppressWarnings("unchecked")
public String[] getCNPatterns(NumberFormat.Style formatStyle) {
Objects.requireNonNull(formatStyle);
String[] compactNumberPatterns = null;
removeEmptyReferences();
String width = (formatStyle == NumberFormat.Style.LONG) ? "long" : "short";
String cacheKey = width + "." + COMPACT_NUMBER_PATTERNS_CACHEKEY;
ResourceReference data = cache.get(cacheKey);
if (data == null || ((compactNumberPatterns
= (String[]) data.get()) == null)) {
ResourceBundle resource = localeData.getNumberFormatData(locale);
compactNumberPatterns = (String[]) resource
.getObject(width + ".CompactNumberPatterns");
cache.put(cacheKey, new ResourceReference(cacheKey,
(Object) compactNumberPatterns, referenceQueue));
}
return compactNumberPatterns;
}
private static Object getResourceBundleObject(String messageKey, Locale locale) {
// slow resource checking
// need to loop thru all registered resource bundles
for (Iterator<String> it = bundles.keySet().iterator(); it.hasNext();) {
Class<? extends NLS> clazz = bundles.get(it.next());
ResourceBundle resourceBundle = ResourceBundle.getBundle(clazz.getName(),
locale);
if (resourceBundle != null) {
try {
Object obj = resourceBundle.getObject(messageKey);
if (obj != null)
return obj;
} catch (MissingResourceException e) {
// just continue it might be on the next resource bundle
}
}
}
// if resource is not found
return null;
}
/**
* Returns the orientation appropriate for the given ResourceBundle's
* localization. Three approaches are tried, in the following order:
* <ol>
* <li>Retrieve a ComponentOrientation object from the ResourceBundle
* using the string "Orientation" as the key.
* <li>Use the ResourceBundle.getLocale to determine the bundle's
* locale, then return the orientation for that locale.
* <li>Return the default locale's orientation.
* </ol>
*
* @deprecated As of J2SE 1.4, use {@link #getOrientation(java.util.Locale)}.
*/
@Deprecated
public static ComponentOrientation getOrientation(ResourceBundle bdl)
{
ComponentOrientation result = null;
try {
result = (ComponentOrientation)bdl.getObject("Orientation");
}
catch (Exception e) {
}
if (result == null) {
result = getOrientation(bdl.getLocale());
}
if (result == null) {
result = getOrientation(Locale.getDefault());
}
return result;
}
/**
* Returns the orientation appropriate for the given ResourceBundle's
* localization. Three approaches are tried, in the following order:
* <ol>
* <li>Retrieve a ComponentOrientation object from the ResourceBundle
* using the string "Orientation" as the key.
* <li>Use the ResourceBundle.getLocale to determine the bundle's
* locale, then return the orientation for that locale.
* <li>Return the default locale's orientation.
* </ol>
*
* @param bdl the bundle to use
* @return the orientation
* @deprecated As of J2SE 1.4, use {@link #getOrientation(java.util.Locale)}.
*/
@Deprecated
public static ComponentOrientation getOrientation(ResourceBundle bdl)
{
ComponentOrientation result = null;
try {
result = (ComponentOrientation)bdl.getObject("Orientation");
}
catch (Exception e) {
}
if (result == null) {
result = getOrientation(bdl.getLocale());
}
if (result == null) {
result = getOrientation(Locale.getDefault());
}
return result;
}
/**
* Returns a Map of the known resources for the given locale.
*/
private Map<String, Object> getResourceCache(Locale l) {
Map<String, Object> values = resourceCache.get(l);
if (values == null) {
values = new TextAndMnemonicHashMap();
for (int i=resourceBundles.size()-1; i >= 0; i--) {
String bundleName = resourceBundles.get(i);
try {
Control c = CoreResourceBundleControl.getRBControlInstance(bundleName);
ResourceBundle b;
if (c != null) {
b = ResourceBundle.getBundle(bundleName, l, c);
} else {
b = ResourceBundle.getBundle(bundleName, l,
ClassLoader.getSystemClassLoader());
}
Enumeration keys = b.getKeys();
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
if (values.get(key) == null) {
Object value = b.getObject(key);
values.put(key, value);
}
}
} catch( MissingResourceException mre ) {
// Keep looking
}
}
resourceCache.put(l, values);
}
return values;
}
/**
* Returns a Map of the known resources for the given locale.
*/
private Map<String, Object> getResourceCache(Locale l) {
Map<String, Object> values = resourceCache.get(l);
if (values == null) {
values = new TextAndMnemonicHashMap();
for (int i=resourceBundles.size()-1; i >= 0; i--) {
String bundleName = resourceBundles.get(i);
try {
Control c = CoreResourceBundleControl.getRBControlInstance(bundleName);
ResourceBundle b;
if (c != null) {
b = ResourceBundle.getBundle(bundleName, l, c);
} else {
b = ResourceBundle.getBundle(bundleName, l);
}
Enumeration keys = b.getKeys();
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
if (values.get(key) == null) {
Object value = b.getObject(key);
values.put(key, value);
}
}
} catch( MissingResourceException mre ) {
// Keep looking
}
}
resourceCache.put(l, values);
}
return values;
}
/**
* Returns a Map of the known resources for the given locale.
*/
private Map<String, Object> getResourceCache(Locale l) {
Map<String, Object> values = resourceCache.get(l);
if (values == null) {
values = new TextAndMnemonicHashMap();
for (int i=resourceBundles.size()-1; i >= 0; i--) {
String bundleName = resourceBundles.get(i);
try {
Control c = CoreResourceBundleControl.getRBControlInstance(bundleName);
ResourceBundle b;
if (c != null) {
b = ResourceBundle.getBundle(bundleName, l, c);
} else {
b = ResourceBundle.getBundle(bundleName, l,
ClassLoader.getSystemClassLoader());
}
Enumeration keys = b.getKeys();
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
if (values.get(key) == null) {
Object value = b.getObject(key);
values.put(key, value);
}
}
} catch( MissingResourceException mre ) {
// Keep looking
}
}
resourceCache.put(l, values);
}
return values;
}
/**
* Creates a new HashMap using data copied from a ResourceBundle.
*
* @param resourceBundle the resource bundle to convert, may not be null
* @return the hashmap containing the data
* @throws NullPointerException if the bundle is null
*/
public static Map toMap(final ResourceBundle resourceBundle) {
Enumeration enumeration = resourceBundle.getKeys();
Map map = new HashMap();
while (enumeration.hasMoreElements()) {
String key = (String) enumeration.nextElement();
Object value = resourceBundle.getObject(key);
map.put(key, value);
}
return map;
}
/**
* Get a resource given bundle name and key
* @param <T> type of the resource
* @param bundleName name of the resource bundle
* @param key to lookup the resource
* @param suffix for the key to lookup
* @param defaultValue of the resource
* @return the resource or the defaultValue
* @throws ClassCastException if the resource found doesn't match T
*/
@SuppressWarnings("unchecked")
public static synchronized <T> T getValue(String bundleName, String key,
String suffix, T defaultValue) {
T value;
try {
ResourceBundle bundle = getBundle(bundleName);
value = (T) bundle.getObject(getLookupKey(key, suffix));
}
catch (Exception e) {
return defaultValue;
}
return value;
}
/**
* Returns a Map of the known resources for the given locale.
*/
private Map<String, Object> getResourceCache(Locale l) {
Map<String, Object> values = resourceCache.get(l);
if (values == null) {
values = new TextAndMnemonicHashMap();
for (int i=resourceBundles.size()-1; i >= 0; i--) {
String bundleName = resourceBundles.get(i);
try {
ResourceBundle b;
if (isDesktopResourceBundle(bundleName)) {
// load resource bundle from java.desktop module
b = ResourceBundle.getBundle(bundleName, l, UIDefaults.class.getModule());
} else {
b = ResourceBundle.getBundle(bundleName, l, ClassLoader.getSystemClassLoader());
}
Enumeration<String> keys = b.getKeys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
if (values.get(key) == null) {
Object value = b.getObject(key);
values.put(key, value);
}
}
} catch( MissingResourceException mre ) {
// Keep looking
}
}
resourceCache.put(l, values);
}
return values;
}
/**
* Returns a Map of the known resources for the given locale.
*/
private Map<String, Object> getResourceCache(Locale l) {
Map<String, Object> values = resourceCache.get(l);
if (values == null) {
values = new TextAndMnemonicHashMap();
for (int i=resourceBundles.size()-1; i >= 0; i--) {
String bundleName = resourceBundles.get(i);
try {
Control c = CoreResourceBundleControl.getRBControlInstance(bundleName);
ResourceBundle b;
if (c != null) {
b = ResourceBundle.getBundle(bundleName, l, c);
} else {
b = ResourceBundle.getBundle(bundleName, l);
}
Enumeration keys = b.getKeys();
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
if (values.get(key) == null) {
Object value = b.getObject(key);
values.put(key, value);
}
}
} catch( MissingResourceException mre ) {
// Keep looking
}
}
resourceCache.put(l, values);
}
return values;
}
/**
* Returns a Map of the known resources for the given locale.
*/
private Map<String, Object> getResourceCache(Locale l) {
Map<String, Object> values = resourceCache.get(l);
if (values == null) {
values = new TextAndMnemonicHashMap();
for (int i=resourceBundles.size()-1; i >= 0; i--) {
String bundleName = resourceBundles.get(i);
try {
Control c = CoreResourceBundleControl.getRBControlInstance(bundleName);
ResourceBundle b;
if (c != null) {
b = ResourceBundle.getBundle(bundleName, l, c);
} else {
b = ResourceBundle.getBundle(bundleName, l);
}
Enumeration keys = b.getKeys();
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
if (values.get(key) == null) {
Object value = b.getObject(key);
values.put(key, value);
}
}
} catch( MissingResourceException mre ) {
// Keep looking
}
}
resourceCache.put(l, values);
}
return values;
}
/**
* Get a resource given bundle name and key
* @param <T> type of the resource
* @param bundleName name of the resource bundle
* @param key to lookup the resource
* @param suffix for the key to lookup
* @param defaultValue of the resource
* @return the resource or the defaultValue
* @throws ClassCastException if the resource found doesn't match T
*/
@SuppressWarnings("unchecked")
public static synchronized <T> T getValue(String bundleName, String key,
String suffix, T defaultValue) {
T value;
try {
ResourceBundle bundle = getBundle(bundleName);
value = (T) bundle.getObject(getLookupKey(key, suffix));
}
catch (Exception e) {
return defaultValue;
}
return value;
}
/**
* Returns the localized resource of the given key and locale, or null
* if no localized resource is available.
*
* @param key the key of the localized resource, not null
* @param locale the locale, not null
* @return the localized resource, or null if not available
* @throws NullPointerException if key or locale is null
*/
@SuppressWarnings("unchecked")
static <T> T getLocalizedResource(String key, Locale locale) {
LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()
.getLocaleResources(locale);
ResourceBundle rb = lr.getJavaTimeFormatData();
return rb.containsKey(key) ? (T) rb.getObject(key) : null;
}
/**
* Returns the localized resource of the given key and locale, or null
* if no localized resource is available.
*
* @param key the key of the localized resource, not null
* @param locale the locale, not null
* @return the localized resource, or null if not available
* @throws NullPointerException if key or locale is null
*/
@SuppressWarnings("unchecked")
static <T> T getLocalizedResource(String key, Locale locale) {
LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()
.getLocaleResources(locale);
ResourceBundle rb = lr.getJavaTimeFormatData();
return rb.containsKey(key) ? (T) rb.getObject(key) : null;
}
/**
* Returns the localized resource of the given key and locale, or null
* if no localized resource is available.
*
* @param key the key of the localized resource, not null
* @param locale the locale, not null
* @return the localized resource, or null if not available
* @throws NullPointerException if key or locale is null
*/
@SuppressWarnings("unchecked")
static <T> T getLocalizedResource(String key, Locale locale) {
LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()
.getLocaleResources(locale);
ResourceBundle rb = lr.getJavaTimeFormatData();
return rb.containsKey(key) ? (T) rb.getObject(key) : null;
}
/**
* Returns the localized resource of the given key and locale, or null
* if no localized resource is available.
*
* @param key the key of the localized resource, not null
* @param locale the locale, not null
* @return the localized resource, or null if not available
* @throws NullPointerException if key or locale is null
*/
@SuppressWarnings("unchecked")
static <T> T getLocalizedResource(String key, Locale locale) {
LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()
.getLocaleResources(locale);
ResourceBundle rb = lr.getJavaTimeFormatData();
return rb.containsKey(key) ? (T) rb.getObject(key) : null;
}
/**
* Returns the localized resource of the given key and locale, or null
* if no localized resource is available.
*
* @param key the key of the localized resource, not null
* @param locale the locale, not null
* @return the localized resource, or null if not available
* @throws NullPointerException if key or locale is null
*/
@SuppressWarnings("unchecked")
static <T> T getLocalizedResource(String key, Locale locale) {
LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()
.getLocaleResources(locale);
ResourceBundle rb = lr.getJavaTimeFormatData();
return rb.containsKey(key) ? (T) rb.getObject(key) : null;
}
/**
* Returns the localized resource of the given key and locale, or null
* if no localized resource is available.
*
* @param key the key of the localized resource, not null
* @param locale the locale, not null
* @return the localized resource, or null if not available
* @throws NullPointerException if key or locale is null
*/
@SuppressWarnings("unchecked")
static <T> T getLocalizedResource(String key, Locale locale) {
LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()
.getLocaleResources(locale);
ResourceBundle rb = lr.getJavaTimeFormatData();
return rb.containsKey(key) ? (T) rb.getObject(key) : null;
}
/**
* Convenience method for retrieving an Object resource.
*
* @param locale locale identifier
* @param key resource key
* @return resource value
*/
public static final Object getObject(Locale locale, String key)
{
ResourceBundle bundle = ResourceBundle.getBundle(LocaleData.class.getName(), locale);
return (bundle.getObject(key));
}