Java Code Examples for java.util.ResourceBundle#containsKey()

The following examples show how to use java.util.ResourceBundle#containsKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocaleResources.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: LocaleResources.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: LocaleResources.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: LocaleResources.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 5
Source File: LocaleResources.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 6
Source File: LocaleResources.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 7
Source File: ShowNavigationTag.java    From openemm with GNU Affero General Public License v3.0 6 votes vote down vote up
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");
	}
}
 
Example 8
Source File: SeoCatalogUrlWorker.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 9
Source File: LocaleResources.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 10
Source File: LocaleResources.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 11
Source File: I18NResourceBundle.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 12
Source File: WeekFields.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 13
Source File: WeekFields.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 14
Source File: WeekFields.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 15
Source File: IsoFields.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@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();
}
 
Example 16
Source File: DateFormatSymbols.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example 17
Source File: DateFormatSymbols.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example 18
Source File: Localisation.java    From sldeditor with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 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;
        }
    }
}
 
Example 19
Source File: ResourceBundleMessageSource.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * 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;
}
 
Example 20
Source File: DateTimeTextProvider.java    From openjdk-jdk8u with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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;
}