org.joda.time.contrib.jsptag
Class Util

java.lang.Object
  extended by org.joda.time.contrib.jsptag.Util

public class Util
extends java.lang.Object

Utilities in support of tag-handler classes.

Author:
Jan Luehe, Jim Newsham

Field Summary
private static java.lang.String APPLICATION
           
(package private) static java.util.Locale[] availableFormattingLocales
          Setup the available formatting locales that will be used by getFormattingLocale(PageContext).
private static java.util.Locale EMPTY_LOCALE
           
private static char HYPHEN
           
private static java.lang.String REQUEST
           
(package private) static java.lang.String REQUEST_CHAR_SET
           
private static java.lang.String SESSION
           
private static char UNDERSCORE
           
 
Constructor Summary
Util()
           
 
Method Summary
private static java.util.Locale findFormattingMatch(java.util.Locale pref, java.util.Locale[] avail)
          Returns the best match between the given preferred locale and the given available locales.
private static java.util.Locale findFormattingMatch(javax.servlet.jsp.PageContext pageContext, java.util.Locale[] avail)
          Determines the client's preferred locales from the request, and compares each of the locales (in order of preference) against the available locales in order to determine the best matching locale.
private static javax.servlet.jsp.jstl.fmt.LocalizationContext findMatch(javax.servlet.jsp.PageContext pageContext, java.lang.String basename)
          Determines the client's preferred locales from the request, and compares each of the locales (in order of preference) against the available locales in order to determine the best matching locale.
private static java.util.ResourceBundle findMatch(java.lang.String basename, java.util.Locale pref)
          Gets the resource bundle with the given base name and preferred locale.
(package private) static java.util.Locale getFormattingLocale(javax.servlet.jsp.PageContext pc)
          Returns the formatting locale to use when is used with a locale-less localization context.
(package private) static java.util.Locale getFormattingLocale(javax.servlet.jsp.PageContext pc, javax.servlet.jsp.tagext.Tag fromTag, boolean format, java.util.Locale[] avail)
          Returns the formatting locale to use with the given formatting action in the given page.
(package private) static java.util.Locale getLocale(javax.servlet.jsp.PageContext pageContext, java.lang.String name)
          Returns the locale specified by the named scoped attribute or context configuration parameter.
static javax.servlet.jsp.jstl.fmt.LocalizationContext getLocalizationContext(javax.servlet.jsp.PageContext pc)
          Gets the default I18N localization context.
static javax.servlet.jsp.jstl.fmt.LocalizationContext getLocalizationContext(javax.servlet.jsp.PageContext pc, java.lang.String basename)
          Gets the resource bundle with the given base name, whose locale is determined as follows: Check if a match exists between the ordered set of preferred locales and the available locales, for the given base name.
static java.util.Enumeration getRequestLocales(javax.servlet.http.HttpServletRequest request)
          HttpServletRequest.getLocales() returns the server's default locale if the request did not specify a preferred language.
static int getScope(java.lang.String scope)
          Converts the given string description of a scope to the corresponding PageContext constant.
static java.util.Locale parseLocale(java.lang.String locale)
          See parseLocale(String, String) for details.
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.
(package private) static void setResponseLocale(javax.servlet.jsp.PageContext pc, java.util.Locale locale)
          Stores the given locale in the response object of the given page context, and stores the locale's associated charset in the javax.servlet.jsp.jstl.fmt.request.charset session attribute, which may be used by the action in a page invoked by a form included in the response to set the request charset to the same as the response charset (this makes it possible for the container to decode the form parameter values properly, since browsers typically encode form field values using the response's charset).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REQUEST

private static final java.lang.String REQUEST
See Also:
Constant Field Values

SESSION

private static final java.lang.String SESSION
See Also:
Constant Field Values

APPLICATION

private static final java.lang.String APPLICATION
See Also:
Constant Field Values

HYPHEN

private static final char HYPHEN
See Also:
Constant Field Values

UNDERSCORE

private static final char UNDERSCORE
See Also:
Constant Field Values

EMPTY_LOCALE

private static final java.util.Locale EMPTY_LOCALE

REQUEST_CHAR_SET

static final java.lang.String REQUEST_CHAR_SET
See Also:
Constant Field Values

availableFormattingLocales

static java.util.Locale[] availableFormattingLocales
Setup the available formatting locales that will be used by getFormattingLocale(PageContext).

Constructor Detail

Util

public Util()
Method Detail

getScope

public static int getScope(java.lang.String scope)
Converts the given string description of a scope to the corresponding PageContext constant. The validity of the given scope has already been checked by the appropriate TLV.

Parameters:
scope - String description of scope
Returns:
PageContext constant corresponding to given scope description

getRequestLocales

public static java.util.Enumeration getRequestLocales(javax.servlet.http.HttpServletRequest request)
HttpServletRequest.getLocales() returns the server's default locale if the request did not specify a preferred language. We do not want this behavior, because it prevents us from using the fallback locale. We therefore need to return an empty Enumeration if no preferred locale has been specified. This way, the logic for the fallback locale will be able to kick in.


parseLocale

public static java.util.Locale parseLocale(java.lang.String locale)
See parseLocale(String, String) for details.


parseLocale

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.

Parameters:
locale - the locale string to parse
variant - the variant
Returns:
java.util.Locale object corresponding to the given locale string, or the runtime's default locale if the locale string is null or empty
Throws:
java.lang.IllegalArgumentException - if the given locale does not have a language component or has an empty country component

setResponseLocale

static void setResponseLocale(javax.servlet.jsp.PageContext pc,
                              java.util.Locale locale)
Stores the given locale in the response object of the given page context, and stores the locale's associated charset in the javax.servlet.jsp.jstl.fmt.request.charset session attribute, which may be used by the action in a page invoked by a form included in the response to set the request charset to the same as the response charset (this makes it possible for the container to decode the form parameter values properly, since browsers typically encode form field values using the response's charset).

Parameters:
pc - the page context whose response object is assigned the given locale
locale - the response locale

getFormattingLocale

static java.util.Locale getFormattingLocale(javax.servlet.jsp.PageContext pc,
                                            javax.servlet.jsp.tagext.Tag fromTag,
                                            boolean format,
                                            java.util.Locale[] avail)
Returns the formatting locale to use with the given formatting action in the given page.

Parameters:
pc - The page context containing the formatting action @param fromTag The formatting action @param format true if the formatting action is of type (as opposed to ), and false otherwise (if set to true, the formatting locale that is returned by this method is used to set the response locale).
avail - the array of available locales
Returns:
the formatting locale to use

getFormattingLocale

static java.util.Locale getFormattingLocale(javax.servlet.jsp.PageContext pc)
Returns the formatting locale to use when is used with a locale-less localization context.

Parameters:
pc - The page context containing the formatting action @return the formatting locale to use

getLocale

static java.util.Locale getLocale(javax.servlet.jsp.PageContext pageContext,
                                  java.lang.String name)
Returns the locale specified by the named scoped attribute or context configuration parameter.

The named scoped attribute is searched in the page, request, session (if valid), and application scope(s) (in this order). If no such attribute exists in any of the scopes, the locale is taken from the named context configuration parameter.

Parameters:
pageContext - the page in which to search for the named scoped attribute or context configuration parameter @param name the name of the scoped attribute or context configuration parameter
Returns:
the locale specified by the named scoped attribute or context configuration parameter, or null if no scoped attribute or configuration parameter with the given name exists

findFormattingMatch

private static java.util.Locale findFormattingMatch(javax.servlet.jsp.PageContext pageContext,
                                                    java.util.Locale[] avail)
Determines the client's preferred locales from the request, and compares each of the locales (in order of preference) against the available locales in order to determine the best matching locale.

Parameters:
pageContext - Page containing the formatting action @param avail Available formatting locales
Returns:
Best matching locale, or null if no match was found

findFormattingMatch

private static java.util.Locale findFormattingMatch(java.util.Locale pref,
                                                    java.util.Locale[] avail)
Returns the best match between the given preferred locale and the given available locales. The best match is given as the first available locale that exactly matches the given preferred locale ("exact match"). If no exact match exists, the best match is given to an available locale that meets the following criteria (in order of priority): - available locale's variant is empty and exact match for both language and country - available locale's variant and country are empty, and exact match for language.

Parameters:
pref - the preferred locale @param avail the available formatting locales
Returns:
Available locale that best matches the given preferred locale, or null if no match exists

getLocalizationContext

public static javax.servlet.jsp.jstl.fmt.LocalizationContext getLocalizationContext(javax.servlet.jsp.PageContext pc)
Gets the default I18N localization context.

Parameters:
pc - Page in which to look up the default I18N localization context

getLocalizationContext

public static javax.servlet.jsp.jstl.fmt.LocalizationContext getLocalizationContext(javax.servlet.jsp.PageContext pc,
                                                                                    java.lang.String basename)
Gets the resource bundle with the given base name, whose locale is determined as follows: Check if a match exists between the ordered set of preferred locales and the available locales, for the given base name. The set of preferred locales consists of a single locale (if the javax.servlet.jsp.jstl.fmt.locale configuration setting is present) or is equal to the client's preferred locales determined from the client's browser settings.

If no match was found in the previous step, check if a match exists between the fallback locale (given by the javax.servlet.jsp.jstl.fmt.fallbackLocale configuration setting) and the available locales, for the given base name.

Parameters:
pc - Page in which the resource bundle with the given base name is requested
basename - Resource bundle base name
Returns:
Localization context containing the resource bundle with the given base name and the locale that led to the resource bundle match, or the empty localization context if no resource bundle match was found

findMatch

private static javax.servlet.jsp.jstl.fmt.LocalizationContext findMatch(javax.servlet.jsp.PageContext pageContext,
                                                                        java.lang.String basename)
Determines the client's preferred locales from the request, and compares each of the locales (in order of preference) against the available locales in order to determine the best matching locale.

Parameters:
pageContext - the page in which the resource bundle with the given base name is requested @param basename the resource bundle's base name
Returns:
the localization context containing the resource bundle with the given base name and best matching locale, or null if no resource bundle match was found

findMatch

private static java.util.ResourceBundle findMatch(java.lang.String basename,
                                                  java.util.Locale pref)
Gets the resource bundle with the given base name and preferred locale. This method calls java.util.ResourceBundle.getBundle(), but ignores its return value unless its locale represents an exact or language match with the given preferred locale.

Parameters:
basename - the resource bundle base name @param pref the preferred locale
Returns:
the requested resource bundle, or null if no resource bundle with the given base name exists or if there is no exact- or language-match between the preferred locale and the locale of the bundle returned by java.util.ResourceBundle.getBundle().