001: /*
002: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package de.odysseus.calyxo.base;
017:
018: import java.util.HashMap;
019: import java.util.Locale;
020: import java.util.Map;
021:
022: import javax.servlet.http.HttpServletRequest;
023: import javax.servlet.jsp.PageContext;
024:
025: /**
026: * Abstract internationalization support class. There is one instance
027: * per module, which may be retreived via the <code>getInstance()</code>
028: * methods.
029: *
030: * I18n support includes determination of a request's desired locale,
031: * resource lookup and message construction(a message is a resource
032: * with parameters).
033: *
034: * The basic principle here is, that internationalized content may be
035: * determined by specifying a locale, a bundle name and a resource key.
036: * Beyond that, there are no further requirements.
037: *
038: * Bundle aliases may be registered using the <code>addBundleAlias</code>
039: * method.
040: *
041: * @author Christoph Beck
042: */
043: public abstract class I18nSupport {
044:
045: /**
046: * Module scope key where the i18n support instance is stored.
047: */
048: public static final String I18N_SUPPORT_KEY = "de.odysseus.calyxo.base.i18n";
049:
050: /**
051: * Bundle aliases map
052: */
053: private Map aliases = new HashMap();
054:
055: /**
056: * Lookup i18n support for module determined by specified request
057: */
058: public static final I18nSupport getInstance(
059: HttpServletRequest request) {
060: ModuleContext context = ModuleSupport.getInstance(request)
061: .getModuleContext(request);
062: return (I18nSupport) context.getAttribute(I18N_SUPPORT_KEY);
063: }
064:
065: /**
066: * Convenience method.
067: * @see #getInstance(HttpServletRequest)
068: */
069: public static final I18nSupport getInstance(PageContext pageContext) {
070: return getInstance((HttpServletRequest) pageContext
071: .getRequest());
072: }
073:
074: /**
075: * Lookup i18n support for specified module
076: */
077: public static final I18nSupport getInstance(ModuleContext context) {
078: return (I18nSupport) context.getAttribute(I18N_SUPPORT_KEY);
079: }
080:
081: /**
082: * Set bundle alias.
083: * When resolving resources and messages, bundle names may be
084: * aliased. The alias will be removed if the specified bundle
085: * is <code>null</code> or equal to the alias name.
086: * Otherwise, bundle names passed to subsequent calls of
087: * {@link #getMessage(Locale, String, String, Object[])}
088: * and {@link #getResource(Locale, String, String)} will be
089: * aliased as specified here.
090: *
091: * @param alias the alias name
092: * @param bundle the bundle name
093: */
094: public void setBundleAlias(String alias, String bundle) {
095: if (bundle == null || bundle.equals(alias)) {
096: aliases.remove(alias);
097: } else {
098: aliases.put(alias, bundle);
099: }
100: }
101:
102: /**
103: * Get unaliased bundle name.
104: * @param bundle the bundle name or alias
105: * @return if the specified bundle name is an alias set with {@link #setBundleAlias(String, String)},
106: * the associated name is returned. Otherwise, the specified bundle name is returned.
107: */
108: public String resolveBundle(String bundle) {
109: return aliases.containsKey(bundle) ? (String) aliases
110: .get(bundle) : bundle;
111: }
112:
113: /**
114: * Get the desired locale for the specified request.
115: * An implementation may choose to cache the locale
116: * in session scope and answer that value here.
117: */
118: public abstract Locale getLocale(HttpServletRequest request);
119:
120: /**
121: * Convenience method.
122: * @see #getLocale(HttpServletRequest)
123: */
124: public final Locale getLocale(PageContext pageContext) {
125: return getLocale((HttpServletRequest) pageContext.getRequest());
126: }
127:
128: /**
129: * Set the desired locale for the specified request.
130: * An implementation may choose to cache the locale set here
131: * in session scope and answer that value in subsequent calls
132: * of <code>getLocale()</code> methods.
133: */
134: public abstract void setLocale(HttpServletRequest request,
135: Locale locale);
136:
137: /**
138: * Convenience method.
139: * @see #setLocale(HttpServletRequest, Locale)
140: */
141: public void setLocale(PageContext pageContext, Locale locale) {
142: setLocale((HttpServletRequest) pageContext.getRequest(), locale);
143: }
144:
145: /**
146: * Get a resource string.
147: * @param locale the desired locale
148: * @param bundle the bundle name or alias
149: * @param key the resource key
150: * @return resource string
151: */
152: public final String getResource(Locale locale, String bundle,
153: String key) {
154: return lookupResource(locale, resolveBundle(bundle), key);
155: }
156:
157: /**
158: * Lookup a resource string.
159: * @param locale the desired locale
160: * @param bundle the bundle name
161: * @param key the resource key
162: * @return resource string
163: */
164: protected abstract String lookupResource(Locale locale,
165: String bundle, String key);
166:
167: /**
168: * Get a message string
169: * @param locale the desired locale
170: * @param bundle the bundle name or alias
171: * @param key the resource key
172: * @param args message arguments
173: * @return message string
174: */
175: public final String getMessage(Locale locale, String bundle,
176: String key, Object[] args) {
177: return lookupMessage(locale, resolveBundle(bundle), key, args);
178: }
179:
180: /**
181: * Lookup a message string
182: * @param locale the desired locale
183: * @param bundle the bundle name
184: * @param key the resource key
185: * @param args message arguments
186: * @return message string
187: */
188: protected abstract String lookupMessage(Locale locale,
189: String bundle, String key, Object[] args);
190: }
|