KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > i18n > I18nUtils


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.i18n;
17
18 import org.apache.avalon.framework.parameters.Parameters;
19
20 import org.apache.cocoon.environment.Cookie;
21 import org.apache.cocoon.environment.ObjectModelHelper;
22 import org.apache.cocoon.environment.Request;
23 import org.apache.cocoon.environment.Session;
24 import org.apache.cocoon.environment.Response;
25
26 import java.util.Enumeration JavaDoc;
27 import java.util.Locale JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30
31 /**
32  * A utility class for i18n formatting and parsing routing.
33  *
34  * @author <a HREF="mailto:kpiroumian@apache.org">Konstantin Piroumian</a>
35  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
36  * @version $Id: I18nUtils.java 312968 2005-10-11 22:28:46Z vgritsenko $
37  */

38 public class I18nUtils {
39
40     // Locale string delimiter
41
private static final String JavaDoc LOCALE_DELIMITER = "_-@.";
42
43     private I18nUtils() {
44         // Disable instantiation
45
}
46
47     /**
48      * Parses given locale string to Locale object. If the string is null
49      * then the given locale is returned.
50      *
51      * @param localeString a string containing locale in
52      * <code>language_country_variant</code> format.
53      * @param defaultLocale returned if localeString is <code>null</code>
54      */

55     public static Locale JavaDoc parseLocale(String JavaDoc localeString, Locale JavaDoc defaultLocale) {
56         if (localeString != null) {
57             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(localeString,
58                                                      LOCALE_DELIMITER);
59             String JavaDoc l = st.hasMoreElements() ? st.nextToken()
60                                             : defaultLocale.getLanguage();
61             String JavaDoc c = st.hasMoreElements() ? st.nextToken() : "";
62             String JavaDoc v = st.hasMoreElements() ? st.nextToken() : "";
63             return new Locale JavaDoc(l, c, v);
64         }
65
66         return defaultLocale;
67     }
68
69     /**
70      * Parses given locale string to Locale object. If the string is null
71      * then the VM default locale is returned.
72      *
73      * @param localeString a string containing locale in
74      * <code>language_country_variant</code> format.
75      *
76      * @see #parseLocale(String, Locale)
77      * @see java.util.Locale#getDefault()
78      */

79     public static Locale JavaDoc parseLocale(String JavaDoc localeString) {
80         return parseLocale(localeString, Locale.getDefault());
81     }
82
83
84     /**
85      * Callback interface for
86      * {@link I18nUtils#findLocale(Map, String, Parameters, Locale, boolean, boolean, boolean, I18nUtils.LocaleValidator)}
87      * @since 2.1.6
88      */

89     public interface LocaleValidator {
90
91         /**
92          * @param name of the locale (for debugging)
93          * @param locale to test
94          * @return true if locale satisfies validator's criteria
95          */

96         public boolean test(String JavaDoc name, Locale JavaDoc locale);
97     }
98
99     /**
100      * Find a suitable locale from an objectModel.
101      * @since 2.1.6
102      * @return locale found, or null if none found.
103      */

104     public static Locale JavaDoc findLocale(Map JavaDoc objectModel,
105                                     String JavaDoc attribute,
106                                     Parameters parameters,
107                                     Locale JavaDoc defaultLocale,
108                                     boolean useLocale,
109                                     boolean useLocales,
110                                     boolean useBlankLocale,
111                                     LocaleValidator test) {
112         String JavaDoc localeStr;
113         Locale JavaDoc locale;
114
115         Request request = ObjectModelHelper.getRequest(objectModel);
116
117         // 1. Request parameter 'locale'
118
localeStr = request.getParameter(attribute);
119         if (localeStr != null) {
120             locale = parseLocale(localeStr);
121             if (test == null || test.test("request", locale)) {
122                 return locale;
123             }
124         }
125
126         // 2. Session attribute 'locale'
127
Session session = request.getSession(false);
128         if (session != null &&
129                 ((localeStr = (String JavaDoc) session.getAttribute(attribute)) != null)) {
130             locale = parseLocale(localeStr);
131             if (test == null || test.test("session", locale)) {
132                 return locale;
133             }
134         }
135
136         // 3. First matching cookie parameter 'locale' within each cookie sent
137
Cookie[] cookies = request.getCookies();
138         if (cookies != null) {
139             for (int i = 0; i < cookies.length; i++) {
140                 Cookie cookie = cookies[i];
141                 if (cookie.getName().equals(attribute)) {
142                     localeStr = cookie.getValue();
143                     locale = parseLocale(localeStr);
144                     if (test == null || test.test("cookie", locale)) {
145                         return locale;
146                     }
147                     break;
148                 }
149             }
150         }
151
152         // 4. Sitemap parameter "locale"
153
if (parameters != null) {
154             localeStr = parameters.getParameter("locale", null);
155             if (localeStr != null) {
156                 locale = parseLocale(localeStr);
157                 if (test == null || test.test("sitemap", locale)) {
158                     return locale;
159                 }
160             }
161         }
162
163         // 5. Locale setting of the requesting browser or server default
164
if (useLocale && !useLocales) {
165             locale = request.getLocale();
166             if (test == null || test.test("request", locale)) {
167                 return locale;
168             }
169         }
170         if (useLocales) {
171             Enumeration JavaDoc locales = request.getLocales();
172             while (locales.hasMoreElements()) {
173                 locale = (Locale JavaDoc)locales.nextElement();
174                 if (test == null || test.test("request", locale)) {
175                     return locale;
176                 }
177             }
178         }
179
180         // 6. Default
181
if (defaultLocale != null) {
182             locale = defaultLocale;
183             if (test == null || test.test("default", locale)) {
184                 return locale;
185             }
186         }
187
188         // 7. Blank
189
if (useBlankLocale) {
190             locale = new Locale JavaDoc("", ""); // Use JDK1.3 constructor
191
if (test == null || test.test("blank", locale)) {
192                 return locale;
193             }
194         }
195
196         // 8. Fail
197
return null;
198     }
199
200     /**
201      * Find a suitable locale from an objectModel.
202      * @since 2.1.6
203      * @return locale found, or server default (never null).
204      */

205     public static Locale JavaDoc findLocale(Map JavaDoc objectModel,
206                                     String JavaDoc attribute,
207                                     Parameters parameters,
208                                     Locale JavaDoc defaultLocale,
209                                     boolean useLocale) {
210         return findLocale(objectModel, attribute, parameters, defaultLocale, useLocale, false, false, null);
211     }
212
213     /**
214      * Store locale in request, session, or cookie.
215      * @since 2.1.6
216      */

217     public static void storeLocale(Map JavaDoc objectModel,
218                                    String JavaDoc attribute,
219                                    String JavaDoc locale,
220                                    boolean storeInRequest,
221                                    boolean storeInSession,
222                                    boolean storeInCookie,
223                                    boolean createSession) {
224         // store in a request if so configured
225
if (storeInRequest) {
226             Request request = ObjectModelHelper.getRequest(objectModel);
227             request.setAttribute(attribute, locale);
228         }
229
230         // store in session if so configured
231
if (storeInSession) {
232             Request request = ObjectModelHelper.getRequest(objectModel);
233             Session session = request.getSession(createSession);
234             if (session != null) {
235                 session.setAttribute(attribute, locale);
236             }
237         }
238
239         // store in a cookie if so configured
240
if (storeInCookie) {
241             Response response = ObjectModelHelper.getResponse(objectModel);
242             response.addCookie(response.createCookie(attribute, locale));
243         }
244     }
245 }
246
Popular Tags