com.sun.faces.application.ApplicationResourceBundle Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jakarta.faces Show documentation
Show all versions of jakarta.faces Show documentation
EE4J Compatible Implementation for Jakarta Faces API
The newest version!
/*
* Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.faces.application;
import static com.sun.faces.util.Util.coalesce;
import static com.sun.faces.util.Util.getCurrentLoader;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
/**
*
* Contains an application level resource bundle name and its associated descriptions, if any.
*
*/
public class ApplicationResourceBundle {
public static final String DEFAULT_KEY = "DEFAULT";
private final String baseName;
private final Map displayNames;
private final Map descriptions;
private volatile Map resources;
// ------------------------------------------------------------ Constructors
/**
*
* Constructs a new ApplicationResourceBundle
*
*
* @param baseName the base name of the ResourceBundle
* @param displayNames any display names that were associated with the resource bundle definition in the configuration
* resource
* @param descriptions any descriptions that were associated with the resource bundle definition in the configuration
* resource
*/
public ApplicationResourceBundle(String baseName, Map displayNames, Map descriptions) {
if (baseName == null) {
throw new IllegalArgumentException();
}
this.baseName = baseName;
this.displayNames = displayNames;
this.descriptions = descriptions;
resources = new HashMap<>(4, 1.0f);
}
// ---------------------------------------------------------- Public Methods
/**
* @return the base name of the ResourceBundle
associated with this ApplicationResourceBundle
* instance
*/
public String getBaseName() {
return baseName;
}
/**
* @param locale a Locale
* @return return the ResourceBundle
associated with the specified locale
*/
public ResourceBundle getResourceBundle(Locale locale) {
if (locale == null) {
locale = Locale.getDefault();
}
ResourceBundle bundle = resources.get(locale);
if (bundle == null) {
ClassLoader loader = getCurrentLoader(this);
synchronized (this) {
bundle = resources.get(locale);
if (bundle == null) {
bundle = ResourceBundle.getBundle(baseName, locale, loader);
resources.put(locale, bundle);
}
}
}
return bundle;
}
/**
* @param locale a Locale
* @return a text of a display-name
element associated with the specified locale
*/
public String getDisplayName(Locale locale) {
String displayName = null;
if (displayNames != null) {
displayName = queryMap(locale, displayNames);
}
return coalesce(displayName, "");
}
/**
* @param locale a Locale
* @return a text of a description
element associated with the specified locale
*/
public String getDescription(Locale locale) {
String description = null;
if (descriptions != null) {
description = queryMap(locale, descriptions);
}
return coalesce(description, "");
}
// --------------------------------------------------------- Private Methods
/**
*
* Lookup and return the text for the specified Locale
from within the specified Map
.
*
*
* @param locale Locale
if interest
* @param map a map containing localized text keyed by Locale
* @return localized text, if any
*/
private String queryMap(Locale locale, Map map) {
if (locale == null) {
return map.get(DEFAULT_KEY);
}
String description = map.get(locale.toString());
if (description == null) {
return map.get(DEFAULT_KEY);
}
return null;
}
}