001: /**
002: *
003: */package clime.messadmin.providers.locale;
004:
005: import java.util.Locale;
006:
007: import javax.servlet.http.HttpSession;
008:
009: import clime.messadmin.providers.spi.LocaleProvider;
010:
011: /**
012: * @author Cédrik LIME
013: */
014: public class JSTLProvider implements LocaleProvider {
015: private static final char HYPHEN = '-';//$NON-NLS-1$
016: private static final char UNDERSCORE = '_';//$NON-NLS-1$
017:
018: //javax.servlet.jsp.jstl.core.Config.SESSION_SCOPE_SUFFIX
019: private static final String SESSION_SCOPE_SUFFIX = ".session";//$NON-NLS-1$
020:
021: /**
022: * Name of configuration setting for application- (as opposed to browser-)
023: * based preferred locale
024: */
025: //javax.servlet.jsp.jstl.core.Config.FMT_LOCALE
026: public static final String FMT_LOCALE = "javax.servlet.jsp.jstl.fmt.locale";//$NON-NLS-1$
027:
028: /**
029: * Name of configuration setting for fallback locale
030: */
031: //javax.servlet.jsp.jstl.core.Config.FMT_FALLBACK_LOCALE
032: public static final String FMT_FALLBACK_LOCALE = "javax.servlet.jsp.jstl.fmt.fallbackLocale";//$NON-NLS-1$
033:
034: /**
035: *
036: */
037: public JSTLProvider() {
038: super ();
039: }
040:
041: /**
042: * {@inheritDoc}
043: */
044: public int getPriority() {
045: return 20;
046: }
047:
048: /**
049: * {@inheritDoc}
050: */
051: public Locale guessLocaleFromSession(HttpSession httpSession) {
052: Locale loc = null;
053:
054: Object obj = httpSession.getAttribute(FMT_LOCALE
055: + SESSION_SCOPE_SUFFIX);
056: if (obj != null) {
057: if (obj instanceof Locale) {
058: loc = (Locale) obj;
059: } else if (obj instanceof String) {
060: try {
061: loc = parseLocale((String) obj, null);
062: } catch (IllegalArgumentException iae) {
063: }
064: }
065: }
066:
067: return loc;
068: }
069:
070: /**
071: * Parses the given locale string into its language and (optionally)
072: * country components, and returns the corresponding
073: * <tt>java.util.Locale</tt> object.
074: *
075: * If the given locale string is null or empty, null is returned.
076: *
077: * @param locale the locale string to parse
078: * @param variant the variant
079: *
080: * @return <tt>java.util.Locale</tt> object corresponding to the given
081: * locale string, null if the locale string is null or empty
082: *
083: * @throws IllegalArgumentException if the given locale does not have a
084: * language component or has an empty country component
085: */
086: public static Locale parseLocale(String locale, String variant) {
087:
088: Locale ret = null;
089: String language = locale;
090: String country = null;
091: int index = -1;
092:
093: if (((index = locale.indexOf(HYPHEN)) > -1)
094: || ((index = locale.indexOf(UNDERSCORE)) > -1)) {
095: language = locale.substring(0, index);
096: country = locale.substring(index + 1);
097: }
098:
099: if ((language == null) || (language.length() == 0)) {
100: throw new IllegalArgumentException(
101: "Missing language component in 'value' attribute in <setLocale>");
102: }
103:
104: if (country == null) {
105: if (variant != null)
106: ret = new Locale(language, "", variant);
107: else
108: ret = new Locale(language, "");
109: } else if (country.length() > 0) {
110: if (variant != null)
111: ret = new Locale(language, country, variant);
112: else
113: ret = new Locale(language, country);
114: } else {
115: throw new IllegalArgumentException(
116: "Empty country component in 'value' attribute in <setLocale>");
117: }
118:
119: return ret;
120: }
121: }
|