下面列出了java.util.ResourceBundle#containsKey ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
String[] getJavaTimeNames(String key) {
String[] names = null;
String cacheKey = CALENDAR_NAMES + key;
removeEmptyReferences();
ResourceReference data = cache.get(cacheKey);
if (data == null || ((names = (String[]) data.get()) == null)) {
ResourceBundle rb = getJavaTimeFormatData();
if (rb.containsKey(key)) {
names = rb.getStringArray(key);
cache.put(cacheKey,
new ResourceReference(cacheKey, (Object) names, referenceQueue));
}
}
return names;
}
int getCalendarData(String key) {
Integer caldata;
String cacheKey = CALENDAR_DATA + key;
removeEmptyReferences();
ResourceReference data = cache.get(cacheKey);
if (data == null || ((caldata = (Integer) data.get()) == null)) {
ResourceBundle rb = localeData.getCalendarData(locale);
if (rb.containsKey(key)) {
caldata = Integer.parseInt(rb.getString(key));
} else {
caldata = 0;
}
cache.put(cacheKey,
new ResourceReference(cacheKey, (Object) caldata, referenceQueue));
}
return caldata;
}
/**
* Tries to create an instance from a property message.
* <p>
* FIXME?: there must be a better way than current loop with exception on missing locale...
*/
public static LocalizedName getNormalizedFromProperties(String resource, String name, Locale defaultLocale) {
Map<String, String> localeValueMap = new HashMap<>();
for(Locale locale: UtilMisc.availableLocales()) {
ResourceBundle bundle = null;
try {
bundle = UtilProperties.getResourceBundle(resource, locale);
} catch(Exception e) {
continue; // when locale is not there, throws IllegalArgumentException
}
if (bundle == null || !bundle.containsKey(name)) continue;
String value = bundle.getString(name);
if (value == null) continue;
value = value.trim();
if (value.isEmpty()) continue;
localeValueMap.put(locale.toString(), value);
}
return new LocalizedName(localeValueMap, defaultLocale);
}
private void prepareNavigationDataFromExtensionPoints(ResourceBundle resourceBundle) throws Exception {
if (!resourceBundle.containsKey("navigation.plugin") || !resourceBundle.containsKey("navigation.extensionpoint")) {
return;
}
//getting data from navigation.property file for the extension point
String plugin = resourceBundle.getString("navigation.plugin");
String extensionPoint = resourceBundle.getString("navigation.extensionpoint");
ExtensionSystem extensionSystem = ExtensionUtils.getExtensionSystem(pageContext.getServletContext());
if(extensionSystem != null) {
Collection<Extension> extensions = extensionSystem.getActiveExtensions(plugin, extensionPoint);
for (Extension extensionItem : extensions) {
String resourceName = extensionItem.getParameter("navigation-bundle").valueAsString();
ResourceBundle extensionBundle = extensionSystem.getPluginResourceBundle(extensionItem.getDeclaringPluginDescriptor().getId(), resourceName);
prepareNavigationDataFromResourceBundle(extensionBundle, extensionItem.getDeclaringPluginDescriptor().getId(), extensionItem.getId());
}
} else {
logger.warn("No active Navigation extensions for plugin '" + plugin + "' defined");
}
}
int getCalendarData(String key) {
Integer caldata;
String cacheKey = CALENDAR_DATA + key;
removeEmptyReferences();
ResourceReference data = cache.get(cacheKey);
if (data == null || ((caldata = (Integer) data.get()) == null)) {
ResourceBundle rb = localeData.getCalendarData(locale);
if (rb.containsKey(key)) {
caldata = Integer.parseInt(rb.getString(key));
} else {
caldata = 0;
}
cache.put(cacheKey,
new ResourceReference(cacheKey, (Object) caldata, referenceQueue));
}
return caldata;
}
String[] getCalendarNames(String key) {
String[] names = null;
String cacheKey = CALENDAR_NAMES + key;
removeEmptyReferences();
ResourceReference data = cache.get(cacheKey);
if (data == null || ((names = (String[]) data.get()) == null)) {
ResourceBundle rb = localeData.getDateFormatData(locale);
if (rb.containsKey(key)) {
names = rb.getStringArray(key);
cache.put(cacheKey,
new ResourceReference(cacheKey, (Object) names, referenceQueue));
}
}
return names;
}
public String getCollationData() {
String key = "Rule";
String coldata = "";
removeEmptyReferences();
ResourceReference data = cache.get(COLLATION_DATA_CACHEKEY);
if (data == null || ((coldata = (String) data.get()) == null)) {
ResourceBundle rb = localeData.getCollationData(locale);
if (rb.containsKey(key)) {
coldata = rb.getString(key);
}
cache.put(COLLATION_DATA_CACHEKEY,
new ResourceReference(COLLATION_DATA_CACHEKEY, (Object) coldata, referenceQueue));
}
return coldata;
}
String[] getJavaTimeNames(String key) {
String[] names = null;
String cacheKey = CALENDAR_NAMES + key;
removeEmptyReferences();
ResourceReference data = cache.get(cacheKey);
if (data == null || ((names = (String[]) data.get()) == null)) {
ResourceBundle rb = getJavaTimeFormatData();
if (rb.containsKey(key)) {
names = rb.getStringArray(key);
cache.put(cacheKey,
new ResourceReference(cacheKey, (Object) names, referenceQueue));
}
}
return names;
}
String[] getCalendarNames(String key) {
String[] names = null;
String cacheKey = CALENDAR_NAMES + key;
removeEmptyReferences();
ResourceReference data = cache.get(cacheKey);
if (data == null || ((names = (String[]) data.get()) == null)) {
ResourceBundle rb = localeData.getDateFormatData(locale);
if (rb.containsKey(key)) {
names = rb.getStringArray(key);
cache.put(cacheKey,
new ResourceReference(cacheKey, (Object) names, referenceQueue));
}
}
return names;
}
public String getCollationData() {
String key = "Rule";
String coldata = "";
removeEmptyReferences();
ResourceReference data = cache.get(COLLATION_DATA_CACHEKEY);
if (data == null || ((coldata = (String) data.get()) == null)) {
ResourceBundle rb = localeData.getCollationData(locale);
if (rb.containsKey(key)) {
coldata = rb.getString(key);
}
cache.put(COLLATION_DATA_CACHEKEY,
new ResourceReference(COLLATION_DATA_CACHEKEY, (Object) coldata, referenceQueue));
}
return coldata;
}
@Override
public String getDisplayName(Locale locale) {
Objects.requireNonNull(locale, "locale");
if (rangeUnit == YEARS) { // only have values for week-of-year
LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()
.getLocaleResources(locale);
ResourceBundle rb = lr.getJavaTimeFormatData();
return rb.containsKey("field.week") ? rb.getString("field.week") : name;
}
return name;
}
@Override
public String getDisplayName(Locale locale) {
Objects.requireNonNull(locale, "locale");
if (rangeUnit == YEARS) { // only have values for week-of-year
LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()
.getLocaleResources(locale);
ResourceBundle rb = lr.getJavaTimeFormatData();
return rb.containsKey("field.week") ? rb.getString("field.week") : name;
}
return name;
}
public String getMessage(String key, Locale locale) throws Exception {
ResourceBundle languageBundle = getMessageBundle( locale.getLanguage() + "_" + locale.getCountry());
if( languageBundle != null && languageBundle.containsKey( key))
return languageBundle.getString( key);
languageBundle = getMessageBundle( locale.getLanguage());
if( languageBundle != null && languageBundle.containsKey( key))
return languageBundle.getString( key);
languageBundle = getMessageBundle( "");
if( languageBundle != null && languageBundle.containsKey( key))
return languageBundle.getString( key);
return null;
}
@Override
public String getDisplayName(Locale locale) {
Objects.requireNonNull(locale, "locale");
if (rangeUnit == YEARS) { // only have values for week-of-year
LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()
.getLocaleResources(locale);
ResourceBundle rb = lr.getJavaTimeFormatData();
return rb.containsKey("field.week") ? rb.getString("field.week") : name;
}
return name;
}
@Override
public String getDisplayName(Locale locale) {
Objects.requireNonNull(locale, "locale");
LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()
.getLocaleResources(locale);
ResourceBundle rb = lr.getJavaTimeFormatData();
return rb.containsKey("field.week") ? rb.getString("field.week") : toString();
}
/**
* Initializes this DateFormatSymbols with the locale data. This method uses
* a cached DateFormatSymbols instance for the given locale if available. If
* there's no cached one, this method creates an uninitialized instance and
* populates its fields from the resource bundle for the locale, and caches
* the instance. Note: zoneStrings isn't initialized in this method.
*/
private void initializeData(Locale locale) {
SoftReference<DateFormatSymbols> ref = cachedInstances.get(locale);
DateFormatSymbols dfs;
if (ref == null || (dfs = ref.get()) == null) {
if (ref != null) {
// Remove the empty SoftReference
cachedInstances.remove(locale, ref);
}
dfs = new DateFormatSymbols(false);
// Initialize the fields from the ResourceBundle for locale.
LocaleProviderAdapter adapter
= LocaleProviderAdapter.getAdapter(DateFormatSymbolsProvider.class, locale);
// Avoid any potential recursions
if (!(adapter instanceof ResourceBundleBasedAdapter)) {
adapter = LocaleProviderAdapter.getResourceBundleBased();
}
ResourceBundle resource
= ((ResourceBundleBasedAdapter)adapter).getLocaleData().getDateFormatData(locale);
dfs.locale = locale;
// JRE and CLDR use different keys
// JRE: Eras, short.Eras and narrow.Eras
// CLDR: long.Eras, Eras and narrow.Eras
if (resource.containsKey("Eras")) {
dfs.eras = resource.getStringArray("Eras");
} else if (resource.containsKey("long.Eras")) {
dfs.eras = resource.getStringArray("long.Eras");
} else if (resource.containsKey("short.Eras")) {
dfs.eras = resource.getStringArray("short.Eras");
}
dfs.months = resource.getStringArray("MonthNames");
dfs.shortMonths = resource.getStringArray("MonthAbbreviations");
dfs.ampms = resource.getStringArray("AmPmMarkers");
dfs.localPatternChars = resource.getString("DateTimePatternChars");
// Day of week names are stored in a 1-based array.
dfs.weekdays = toOneBasedArray(resource.getStringArray("DayNames"));
dfs.shortWeekdays = toOneBasedArray(resource.getStringArray("DayAbbreviations"));
// Put dfs in the cache
ref = new SoftReference<>(dfs);
SoftReference<DateFormatSymbols> x = cachedInstances.putIfAbsent(locale, ref);
if (x != null) {
DateFormatSymbols y = x.get();
if (y == null) {
// Replace the empty SoftReference with ref.
cachedInstances.replace(locale, x, ref);
} else {
ref = x;
dfs = y;
}
}
// If the bundle's locale isn't the target locale, put another cache
// entry for the bundle's locale.
Locale bundleLocale = resource.getLocale();
if (!bundleLocale.equals(locale)) {
SoftReference<DateFormatSymbols> z
= cachedInstances.putIfAbsent(bundleLocale, ref);
if (z != null && z.get() == null) {
cachedInstances.replace(bundleLocale, z, ref);
}
}
}
// Copy the field values from dfs to this instance.
copyMembers(dfs, this);
}
/**
* Initializes this DateFormatSymbols with the locale data. This method uses
* a cached DateFormatSymbols instance for the given locale if available. If
* there's no cached one, this method creates an uninitialized instance and
* populates its fields from the resource bundle for the locale, and caches
* the instance. Note: zoneStrings isn't initialized in this method.
*/
private void initializeData(Locale locale) {
SoftReference<DateFormatSymbols> ref = cachedInstances.get(locale);
DateFormatSymbols dfs;
if (ref == null || (dfs = ref.get()) == null) {
if (ref != null) {
// Remove the empty SoftReference
cachedInstances.remove(locale, ref);
}
dfs = new DateFormatSymbols(false);
// Initialize the fields from the ResourceBundle for locale.
LocaleProviderAdapter adapter
= LocaleProviderAdapter.getAdapter(DateFormatSymbolsProvider.class, locale);
// Avoid any potential recursions
if (!(adapter instanceof ResourceBundleBasedAdapter)) {
adapter = LocaleProviderAdapter.getResourceBundleBased();
}
ResourceBundle resource
= ((ResourceBundleBasedAdapter)adapter).getLocaleData().getDateFormatData(locale);
dfs.locale = locale;
// JRE and CLDR use different keys
// JRE: Eras, short.Eras and narrow.Eras
// CLDR: long.Eras, Eras and narrow.Eras
if (resource.containsKey("Eras")) {
dfs.eras = resource.getStringArray("Eras");
} else if (resource.containsKey("long.Eras")) {
dfs.eras = resource.getStringArray("long.Eras");
} else if (resource.containsKey("short.Eras")) {
dfs.eras = resource.getStringArray("short.Eras");
}
dfs.months = resource.getStringArray("MonthNames");
dfs.shortMonths = resource.getStringArray("MonthAbbreviations");
dfs.ampms = resource.getStringArray("AmPmMarkers");
dfs.localPatternChars = resource.getString("DateTimePatternChars");
// Day of week names are stored in a 1-based array.
dfs.weekdays = toOneBasedArray(resource.getStringArray("DayNames"));
dfs.shortWeekdays = toOneBasedArray(resource.getStringArray("DayAbbreviations"));
// Put dfs in the cache
ref = new SoftReference<>(dfs);
SoftReference<DateFormatSymbols> x = cachedInstances.putIfAbsent(locale, ref);
if (x != null) {
DateFormatSymbols y = x.get();
if (y == null) {
// Replace the empty SoftReference with ref.
cachedInstances.replace(locale, x, ref);
} else {
ref = x;
dfs = y;
}
}
// If the bundle's locale isn't the target locale, put another cache
// entry for the bundle's locale.
Locale bundleLocale = resource.getLocale();
if (!bundleLocale.equals(locale)) {
SoftReference<DateFormatSymbols> z
= cachedInstances.putIfAbsent(bundleLocale, ref);
if (z != null && z.get() == null) {
cachedInstances.replace(bundleLocale, z, ref);
}
}
}
// Copy the field values from dfs to this instance.
copyMembers(dfs, this);
}
/**
* Gets the string for the supplied class and key.
*
* @param clazz the class to get the strings for
* @param key the key
* @return the string
*/
public static String getString(Class<?> clazz, String key) {
String baseName = null;
String className;
if((key != null) && key.startsWith(COMMON_PREFIX))
{
baseName = DEFAULT_BASE_NAME;
className = "SLDEditor";
}
else
{
String fullPackageName = clazz.getPackage().getName();
String[] packages = fullPackageName.split("\\.");
if(packages.length == 2)
{
baseName = DEFAULT_BASE_NAME;
}
else
{
baseName = packages[2];
}
className = clazz.getSimpleName();
}
ResourceBundle resourceBundle = getInstance().getResourceBundle(String.format("%s/%s/%s", RESOURCE_FOLDER, baseName, className));
if((resourceBundle == null) || (key == null))
{
return key;
}
else
{
if(resourceBundle.containsKey(key))
{
return resourceBundle.getString(key);
}
else
{
return key;
}
}
}
/**
* Efficiently retrieve the String value for the specified key,
* or return {@code null} if not found.
* <p>As of 4.2, the default implementation checks {@code containsKey}
* before it attempts to call {@code getString} (which would require
* catching {@code MissingResourceException} for key not found).
* <p>Can be overridden in subclasses.
* @param bundle the ResourceBundle to perform the lookup in
* @param key the key to look up
* @return the associated value, or {@code null} if none
* @since 4.2
* @see ResourceBundle#getString(String)
* @see ResourceBundle#containsKey(String)
*/
@Nullable
protected String getStringOrNull(ResourceBundle bundle, String key) {
if (bundle.containsKey(key)) {
try {
return bundle.getString(key);
}
catch (MissingResourceException ex){
// Assume key not found for some other reason
// -> do NOT throw the exception to allow for checking parent message source.
}
}
return 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;
}