SetLocaleSupportpublic abstract class SetLocaleSupport extends javax.servlet.jsp.tagext.TagSupport Support for tag handlers for <setLocale>, the locale setting tag in
JSTL 1.0. |
Fields Summary |
---|
private static final char | HYPHEN | private static final char | UNDERSCORE | private static Locale[] | availableFormattingLocales | private static final Locale[] | dateLocales | private static final Locale[] | numberLocales | protected Object | value | protected String | variant | private int | scope |
Constructors Summary |
---|
public SetLocaleSupport()Sets up the available formatting locales that will be used
by getFormattingLocale(PageContext). // 'scope' attribute
//*********************************************************************
// Constructor and initialization
Vector vec = new Vector(dateLocales.length);
for (int i=0; i<dateLocales.length; i++) {
for (int j=0; j<numberLocales.length; j++) {
if (dateLocales[i].equals(numberLocales[j])) {
vec.add(dateLocales[i]);
break;
}
}
}
availableFormattingLocales = new Locale[vec.size()];
availableFormattingLocales = (Locale[])vec.toArray(availableFormattingLocales);
super();
init();
|
Methods Summary |
---|
public int | doEndTag()
Locale locale = null;
if (value == null) {
locale = Locale.getDefault();
} else if (value instanceof String) {
if (((String) value).trim().equals("")) {
locale = Locale.getDefault();
} else {
locale = parseLocale((String) value, variant);
}
} else {
locale = (Locale) value;
}
Config.set(pageContext, Config.FMT_LOCALE, locale, scope);
setResponseLocale(pageContext, locale);
return EVAL_PAGE;
| private static java.util.Locale | findFormattingMatch(javax.servlet.jsp.PageContext pageContext, java.util.Locale[] avail)
Locale match = null;
for (Enumeration enum_ = Util.getRequestLocales((HttpServletRequest)pageContext.getRequest());
enum_.hasMoreElements(); ) {
Locale locale = (Locale)enum_.nextElement();
match = findFormattingMatch(locale, avail);
if (match != null) {
break;
}
}
return match;
| private static java.util.Locale | findFormattingMatch(java.util.Locale pref, java.util.Locale[] avail)
Locale match = null;
boolean langAndCountryMatch = false;
for (int i=0; i<avail.length; i++) {
if (pref.equals(avail[i])) {
// Exact match
match = avail[i];
break;
} else if (
!"".equals(pref.getVariant()) &&
"".equals(avail[i].getVariant()) &&
pref.getLanguage().equals(avail[i].getLanguage()) &&
pref.getCountry().equals(avail[i].getCountry())) {
// Language and country match; different variant
match = avail[i];
langAndCountryMatch = true;
} else if (
!langAndCountryMatch &&
pref.getLanguage().equals(avail[i].getLanguage()) &&
("".equals(avail[i].getCountry()))) {
// Language match
if (match == null) {
match = avail[i];
}
}
}
return match;
| static java.util.Locale | getFormattingLocale(javax.servlet.jsp.PageContext pc)
/*
* Establish formatting locale by comparing the preferred locales
* (in order of preference) against the available formatting
* locales, and determining the best matching locale.
*/
Locale match = null;
Locale pref = getLocale(pc, Config.FMT_LOCALE);
if (pref != null) {
// Preferred locale is application-based
match = findFormattingMatch(pref, availableFormattingLocales);
} else {
// Preferred locales are browser-based
match = findFormattingMatch(pc, availableFormattingLocales);
}
if (match == null) {
//Use fallback locale.
pref = getLocale(pc, Config.FMT_FALLBACK_LOCALE);
if (pref != null) {
match = findFormattingMatch(pref, availableFormattingLocales);
}
}
if (match != null) {
setResponseLocale(pc, match);
}
return match;
| static java.util.Locale | getFormattingLocale(javax.servlet.jsp.PageContext pc, javax.servlet.jsp.tagext.Tag fromTag, boolean isDate, boolean format)
LocalizationContext locCtxt = null;
// Get formatting locale from enclosing <fmt:bundle>
Tag parent = findAncestorWithClass(fromTag, BundleSupport.class);
if (parent != null) {
/*
* use locale from localization context established by parent
* <fmt:bundle> action, unless that locale is null
*/
locCtxt = ((BundleSupport) parent).getLocalizationContext();
if (locCtxt.getLocale() != null) {
if (format) {
setResponseLocale(pc, locCtxt.getLocale());
}
return locCtxt.getLocale();
}
}
// Use locale from default I18N localization context, unless it is null
if ((locCtxt = BundleSupport.getLocalizationContext(pc)) != null) {
if (locCtxt.getLocale() != null) {
if (format) {
setResponseLocale(pc, locCtxt.getLocale());
}
return locCtxt.getLocale();
}
}
/*
* Establish formatting locale by comparing the preferred locales
* (in order of preference) against the available formatting
* locales, and determining the best matching locale.
*/
Locale match = null;
Locale pref = getLocale(pc, Config.FMT_LOCALE);
Locale[] avail = null;
if (isDate) {
avail = dateLocales;
} else {
avail = numberLocales;
}
if (pref != null) {
// Preferred locale is application-based
match = findFormattingMatch(pref, avail);
} else {
// Preferred locales are browser-based
match = findFormattingMatch(pc, avail);
}
if (match == null) {
//Use fallback locale.
pref = getLocale(pc, Config.FMT_FALLBACK_LOCALE);
if (pref != null) {
match = findFormattingMatch(pref, avail);
}
}
if (format && (match != null)) {
setResponseLocale(pc, match);
}
return match;
| static java.util.Locale | getLocale(javax.servlet.jsp.PageContext pageContext, java.lang.String name)
Locale loc = null;
Object obj = Config.find(pageContext, name);
if (obj != null) {
if (obj instanceof Locale) {
loc = (Locale) obj;
} else {
loc = parseLocale((String) obj);
}
}
return loc;
| private void | init()
value = variant = null;
scope = PageContext.PAGE_SCOPE;
| public static java.util.Locale | parseLocale(java.lang.String locale)See parseLocale(String, String) for details.
return parseLocale(locale, null);
| public static java.util.Locale | parseLocale(java.lang.String locale, java.lang.String variant)Parses the given locale string into its language and (optionally)
country components, and returns the corresponding
java.util.Locale object.
If the given locale string is null or empty, the runtime's default
locale is returned.
Locale ret = null;
String language = locale;
String country = null;
int index = -1;
if (((index = locale.indexOf(HYPHEN)) > -1)
|| ((index = locale.indexOf(UNDERSCORE)) > -1)) {
language = locale.substring(0, index);
country = locale.substring(index+1);
}
if ((language == null) || (language.length() == 0)) {
throw new IllegalArgumentException(
Resources.getMessage("LOCALE_NO_LANGUAGE"));
}
if (country == null) {
if (variant != null)
ret = new Locale(language, "", variant);
else
ret = new Locale(language, "");
} else if (country.length() > 0) {
if (variant != null)
ret = new Locale(language, country, variant);
else
ret = new Locale(language, country);
} else {
throw new IllegalArgumentException(
Resources.getMessage("LOCALE_EMPTY_COUNTRY"));
}
return ret;
| public void | release()
init();
| static void | setResponseLocale(javax.servlet.jsp.PageContext pc, java.util.Locale locale)
// set response locale
ServletResponse response = pc.getResponse();
response.setLocale(locale);
// get response character encoding and store it in session attribute
if (pc.getSession() != null) {
try {
pc.setAttribute(RequestEncodingSupport.REQUEST_CHAR_SET,
response.getCharacterEncoding(),
PageContext.SESSION_SCOPE);
} catch (IllegalStateException ex) {} // invalidated session ignored
}
| public void | setScope(java.lang.String scope)
this.scope = Util.getScope(scope);
|
|