Return-Path: Delivered-To: apmail-cocoon-cvs-archive@www.apache.org Received: (qmail 74262 invoked from network); 17 Aug 2004 14:50:21 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 17 Aug 2004 14:50:21 -0000 Received: (qmail 81186 invoked by uid 500); 17 Aug 2004 14:50:19 -0000 Delivered-To: apmail-cocoon-cvs-archive@cocoon.apache.org Received: (qmail 81123 invoked by uid 500); 17 Aug 2004 14:50:18 -0000 Mailing-List: contact cvs-help@cocoon.apache.org; run by ezmlm Precedence: bulk Reply-To: dev@cocoon.apache.org list-help: list-unsubscribe: list-post: Delivered-To: mailing list cvs@cocoon.apache.org Received: (qmail 81108 invoked by uid 99); 17 Aug 2004 14:50:18 -0000 X-ASF-Spam-Status: No, hits=-2.8 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.27.1) with SMTP; Tue, 17 Aug 2004 07:50:14 -0700 Received: (qmail 74178 invoked by uid 65534); 17 Aug 2004 14:50:13 -0000 Date: 17 Aug 2004 14:50:13 -0000 Message-ID: <20040817145013.74175.qmail@minotaur.apache.org> From: vgritsenko@apache.org To: cvs@cocoon.apache.org Subject: svn commit: rev 36530 - cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/acting X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N Author: vgritsenko Date: Tue Aug 17 07:50:12 2004 New Revision: 36530 Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/acting/LocaleAction.java Log: Sync LocaleAction with 2.2 Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/acting/LocaleAction.java ============================================================================== --- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/acting/LocaleAction.java (original) +++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/acting/LocaleAction.java Tue Aug 17 07:50:12 2004 @@ -1,12 +1,12 @@ /* * Copyright 1999-2004 The Apache Software Foundation. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -15,246 +15,213 @@ */ package org.apache.cocoon.acting; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; - import org.apache.avalon.framework.configuration.Configurable; import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.configuration.ConfigurationException; import org.apache.avalon.framework.parameters.Parameters; import org.apache.avalon.framework.thread.ThreadSafe; -import org.apache.cocoon.environment.Cookie; -import org.apache.cocoon.environment.ObjectModelHelper; import org.apache.cocoon.environment.Redirector; -import org.apache.cocoon.environment.Request; -import org.apache.cocoon.environment.Response; -import org.apache.cocoon.environment.Session; import org.apache.cocoon.environment.SourceResolver; import org.apache.cocoon.i18n.I18nUtils; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + /** - * LocaleAction is a class which obtains the request's locale information - * (language, country, variant) and makes it available to the - * sitemap/pipeline. - * - * Definition in sitemap: - *
- * <map:actions>
- *  <map:action name="locale" src="org.apache.cocoon.acting.LocaleAction"/>
- * </map:actions>
- * 
- * - * Examples: + * An action that locates and provides to the pipeline locale information + * looked up in a range of ways. * + *

Configuration

+ *

A sample configuration (given in the <map:matchers> section of the + * sitemap) is given below. This configuration shows default values. + *

*
- * <map:match pattern="file">
- *  <map:act type="locale">
- *   <map:generate src="file_{lang}{country}{variant}.xml"/>
- *  </map:act>
- * </map:match>
+ *   <map:action name="locale" src="org.apache.cocoon.acting.LocaleAction">
+ *     <locale-attribute>locale</locale-attribute>
+ *     <use-locale>true</use-locale>
+ *     <default-locale language="en" country="US"/>
+ *     <store-in-request>false<store-in-request>
+ *     <create-session>false<create-session>
+ *     <store-in-session>false<store-in-session>
+ *     <store-in-cookie>false<store-in-cookie>
+ *   </map:action>
  * 
* - * or + *

Above configuration parameters mean: + *

    + *
  • locale-attribute specifies the name of the request + * parameter / session attribute / cookie that is to be used as a locale + * (defaults to locale)
  • + *
  • use-locale specifies whether the primary locale provided + * by the user agent (or server default, is no locale passed by the agent) + * is to be used
  • + *
  • default-locale specifies the default locale to be used when + * none found.
  • + *
  • store-in-request specifies whether found locale should be + * stored as request attribute.
  • + *
  • create-session specifies whether session should be created + * when storing found locale as session attribute.
  • + *
  • store-in-session specifies whether found locale should be + * stored as session attribute.
  • + *
  • store-in-cookie specifies whether found locale should be + * stored as cookie.
  • + *
+ *

* + *

Usage

+ *

This action will be used in a pipeline like so:

*
- * <map:match pattern="file">
- *  <map:act type="locale">
- *   <map:generate src="file.xml?locale={locale}"/>
- *  </map:act>
- * </map:match>
+ *   <map:act type="locale">
+ *     <map:generate src="file_{language}_{country}_{variant}.xml"/>
+ *     ...
+ *   </map:match>
  * 
- * - *
- * - * The variables lang, country, - * variant, and locale are all available. Note that - * country and variant can be empty, however - * lang and locale will always contain a valid value. - * - *
- * - * The following search criteria are used in order when ascertaining locale - * values: - * - *
    - *
  1. Request CGI parameter locale
  2. - *
  3. Session attribute locale
  4. - *
  5. First matching Cookie parameter locale - * within each cookie sent with the current request
  6. - *
  7. Locale setting of the requesting object
  8. - *
- * - * (in the case of language, if the above cases do not yield a valid value - * the locale value of the server is used) - * - *
- * - * The attribute names can be configured/customized at action definition - * using the configuration paramters - * {language,country,variant,locale}-attribute. - * - * eg. - * + *

or

*
- * <map:action name="locale" src="org.apache.cocoon.acting.LocaleAction">
- *  <language-attribute>lg</language-attribute>
- * </map:action>
+ *   <map:act type="locale">
+ *     <map:generate src="file_{locale}.xml"/>
+ *     ...
+ *   </map:match>
  * 
* - * or: - * - *
Code originated from org.apache.cocoon.acting.LangSelect
+ *

Locale Identification

+ *

Locales will be tested in following order:

+ *
    + *
  • Locale provided as a request parameter
  • + *
  • Locale provided as a session attribute
  • + *
  • Locale provided as a cookie
  • + *
  • Locale provided using a sitemap parameter
    + * (<map:parameter name="locale" value="{1}"/> style parameter within + * the <map:match> node)
  • + *
  • Locale provided by the user agent, or server default, + * if use-locale is set to true
  • + *
  • The default locale, if specified in the matcher's configuration
  • + *
+ *

First found locale will be returned.

+ * + *

Sitemap Variables

+ *

Once locale has been found, the following sitemap variables + * will be available to sitemap elements contained within the action:

+ *
    + *
  • {locale}: The locale string
  • + *
  • {language}: The language of the found locale
  • + *
  • {country}: The country of the found locale
  • + *
  • {variant}: The variant of the found locale
  • + *
* * @author Marcus Crafter * @author Konstantin Piroumian * @author Lassi Immonen - * @version CVS $Id: LocaleAction.java,v 1.3 2004/03/05 13:02:43 bdelacretaz Exp $ + * @author Vadim Gritsenko + * @version CVS $Id$ */ public class LocaleAction extends ServiceableAction implements ThreadSafe, Configurable { + private static final String DEFAULT_DEFAULT_LANG = "en"; + private static final String DEFAULT_DEFAULT_COUNTRY = "US"; + private static final String DEFAULT_DEFAULT_VARIANT = ""; + /** - * Constant representing the language parameter + * Default locale attribute name. */ - public static final String LANG = "lang"; + public static final String LOCALE = "locale"; /** - * Constant representing the country parameter + * Configuration element name for locale attribute name. */ - public static final String COUNTRY = "country"; + public static final String LOCALE_ATTR = "locale-attribute"; + /** - * Constant representing the variant parameter + * Constant representing the request storage configuration attribute */ - public static final String VARIANT = "variant"; + public static final String STORE_REQUEST = "store-in-request"; /** - * Constant representing the locale parameter + * Constant representing the session creation configuration attribute */ - public static final String LOCALE = "locale"; + public static final String CREATE_SESSION = "create-session"; /** - * Constant representing the language configuration attribute + * Constant representing the session storage configuration attribute */ - public static final String LANG_ATTR = "language-attribute"; + public static final String STORE_SESSION = "store-in-session"; /** - * Constant representing the country configuration attribute + * Constant representing the cookie storage configuration attribute */ - public static final String COUNTRY_ATTR = "country-attribute"; + public static final String STORE_COOKIE = "store-in-cookie"; + /** - * Constant representing the variant configuration attribute + * Name of the locale request parameter, session attribute, cookie. */ - public static final String VARIANT_ATTR = "variant-attribute"; + private String localeAttribute; /** - * Constant representing the locale configuration attribute + * Whether to query locale provided by the user agent or not. */ - public static final String LOCALE_ATTR = "locale-attribute"; + private boolean useLocale; /** - * Constant representing the request storage configuration attribute + * Default locale if no other found and {@link #useLocale} is false. */ - public static final String STORE_REQUEST = "store-in-request"; + private Locale defaultLocale; /** - * Constant representing the session creation configuration attribute + * Store the locale in request. Default is not to do this. */ - public static final String CREATE_SESSION = "create-session"; + private boolean storeInRequest; /** - * Constant representing the session storage configuration attribute + * Store the locale in session, if available. Default is not to do this. */ - public static final String STORE_SESSION = "store-in-session"; + private boolean storeInSession; /** - * Constant representing the cookie storage configuration attribute + * Should we create a session if needed. Default is not to do this. */ - public static final String STORE_COOKIE = "store-in-cookie"; - - - // Store the lang in request. Default is not to do this. - private boolean storeInRequest = false; - - // Store the lang in session, if available. Default is not to do this. - private boolean storeInSession = false; - - // Should we create a session if needed. Default is not to do this. - private boolean createSession = false; - - // Should we add a cookie with the lang. Default is not to do this. - private boolean storeInCookie = false; - - // Configuration attributes. - private String langAttr; - private String countryAttr; - private String variantAttr; - private String localeAttr; + private boolean createSession; + /** + * Should we add a cookie with the locale. Default is not to do this. + */ + private boolean storeInCookie; /** * Configure this action. * - * @param conf configuration information (if any) + * @param config configuration information (if any) */ - public void configure(Configuration conf) + public void configure(Configuration config) throws ConfigurationException { - Configuration child = conf.getChild(STORE_REQUEST); - storeInRequest = child.getValueAsBoolean(false); - - if (getLogger().isDebugEnabled()) { - getLogger().debug((storeInRequest ? "will" : "won't") + " set values in request"); - } - - child = conf.getChild(CREATE_SESSION); - createSession = child.getValueAsBoolean(false); - - if (getLogger().isDebugEnabled()) { - getLogger().debug((createSession ? "will" : "won't") + " create session"); - } - - child = conf.getChild(STORE_SESSION); - storeInSession = child.getValueAsBoolean(false); - + this.storeInRequest = config.getChild(STORE_REQUEST).getValueAsBoolean(false); + this.createSession = config.getChild(CREATE_SESSION).getValueAsBoolean(false); + this.storeInSession = config.getChild(STORE_SESSION).getValueAsBoolean(false); + this.storeInCookie = config.getChild(STORE_COOKIE).getValueAsBoolean(false); if (getLogger().isDebugEnabled()) { - getLogger().debug((storeInSession ? "will" : "won't") + " set values in session"); + getLogger().debug((this.storeInRequest ? "will" : "won't") + " set values in request"); + getLogger().debug((this.createSession ? "will" : "won't") + " create session"); + getLogger().debug((this.storeInSession ? "will" : "won't") + " set values in session"); + getLogger().debug((this.storeInCookie ? "will" : "won't") + " set values in cookies"); } - child = conf.getChild(STORE_COOKIE); - storeInCookie = child.getValueAsBoolean(false); - - if (getLogger().isDebugEnabled()) { - getLogger().debug((storeInCookie ? "will" : "won't") + " set values in cookies"); + this.localeAttribute = config.getChild(LOCALE_ATTR).getValue(LOCALE); + this.useLocale = config.getChild("use-locale").getValueAsBoolean(true); + + Configuration child = config.getChild("default-locale", false); + if (child != null) { + this.defaultLocale = new Locale(child.getAttribute("language", DEFAULT_DEFAULT_LANG), + child.getAttribute("country", DEFAULT_DEFAULT_COUNTRY), + child.getAttribute("variant", DEFAULT_DEFAULT_VARIANT)); } - child = conf.getChild(LANG_ATTR); - langAttr = child.getValue(LANG); - - if (getLogger().isDebugEnabled()) { - getLogger().debug("global language attribute name is " + langAttr); - } - - child = conf.getChild(COUNTRY_ATTR); - countryAttr = child.getValue(COUNTRY); - if (getLogger().isDebugEnabled()) { - getLogger().debug("global country attribute name is " + countryAttr); - } - - child = conf.getChild(VARIANT_ATTR); - variantAttr = child.getValue(VARIANT); - - if (getLogger().isDebugEnabled()) { - getLogger().debug("global variant attribute name is " + variantAttr); - } - - child = conf.getChild(LOCALE_ATTR); - localeAttr = child.getValue(LOCALE); - - if (getLogger().isDebugEnabled()) { - getLogger().debug("global locale attribute name is " + localeAttr); + getLogger().debug("Locale attribute name is " + this.localeAttribute); + getLogger().debug((this.useLocale ? "will" : "won't") + " use request locale"); + getLogger().debug("default locale " + this.defaultLocale); } } @@ -262,62 +229,46 @@ * Action which obtains the current environments locale information, and * places it in the objectModel (and optionally in a session/cookie). */ - public Map act( - Redirector redirector, - SourceResolver resolver, - Map objectModel, - String source, - Parameters par - ) throws Exception { - - // obtain locale information from params, session or cookies - String lc = getLocaleAttribute(objectModel, localeAttr); - Locale locale = I18nUtils.parseLocale(lc); + public Map act(Redirector redirector, + SourceResolver resolver, + Map objectModel, + String source, + Parameters params) + throws Exception { + // Obtain locale information from request, session, cookies, or params + Locale locale = I18nUtils.findLocale(objectModel, + localeAttribute, + params, + defaultLocale, + useLocale); - if (getLogger().isDebugEnabled()) { - getLogger().debug("obtained locale information, locale = " + lc); - checkParams(par); - } - - if (storeInRequest) { - Request request = ObjectModelHelper.getRequest(objectModel); - - request.setAttribute(localeAttr, lc); + if (locale == null) { if (getLogger().isDebugEnabled()) { - getLogger().debug("updated request"); + getLogger().debug("No locale found."); } - } - // store in session if so configured - if (storeInSession) { - Request request = ObjectModelHelper.getRequest(objectModel); - Session session = request.getSession(createSession); - - if (session != null) { - session.setAttribute(localeAttr, lc); - if (getLogger().isDebugEnabled()) { - getLogger().debug("updated session"); - } - } + return null; } - // store in a cookie if so configured - if (storeInCookie) { - Response response = ObjectModelHelper.getResponse(objectModel); - - response.addCookie(response.createCookie(localeAttr, lc)); - if (getLogger().isDebugEnabled()) { - getLogger().debug("created cookies"); - } + String localeStr = locale.toString(); + if (getLogger().isDebugEnabled()) { + getLogger().debug("Found locale: " + localeStr); } - // set up a map for sitemap parameters - Map map = new HashMap(); - map.put(langAttr, locale.getLanguage()); - map.put(countryAttr, locale.getCountry()); - map.put(variantAttr, locale.getVariant()); - map.put(localeAttr, lc); + I18nUtils.storeLocale(objectModel, + localeAttribute, + localeStr, + storeInRequest, + storeInSession, + storeInCookie, + createSession); + // Set up a map for sitemap parameters + Map map = new HashMap(); + map.put("language", locale.getLanguage()); + map.put("country", locale.getCountry()); + map.put("variant", locale.getVariant()); + map.put("locale", localeStr); return map; } @@ -325,73 +276,17 @@ * Helper method to retreive the attribute value containing locale * information. See class documentation for locale determination algorythm. * + * @deprecated See I18nUtils.findLocale * @param objectModel requesting object's environment * @return locale value or null if no locale was found */ public static String getLocaleAttribute(Map objectModel, - String localeAttrName) { - - String ret_val; - - Request request = ObjectModelHelper.getRequest(objectModel); - - // 1. Request CGI parameter 'locale' - if ((ret_val = request.getParameter(localeAttrName)) != null) - return ret_val; - - // 2. Session attribute 'locale' - Session session = request.getSession(false); - if (session != null && - ((ret_val = (String) session.getAttribute(localeAttrName)) != null)) - return ret_val; - - // 3. First matching cookie parameter 'locale' within each cookie sent - Cookie[] cookies = request.getCookies(); - if (cookies != null) { - for (int i = 0; i < cookies.length; ++i) { - Cookie cookie = cookies[i]; - if (cookie.getName().equals(localeAttrName)) - return cookie.getValue(); - } - } - - // 4. Locale setting of the requesting object/server - return request.getLocale().toString(); - } - - /** - * Method to check invocations for local - * customisation. - * - * eg. - * - *
-     * <map:act type="locale">
-     *     <map:parameter name="language-attribute" value="lg"/>
-     * </map:act>
-     * 
- */ - private void checkParams(Parameters par) { - - langAttr = par.getParameter(LANG_ATTR, langAttr); - countryAttr = par.getParameter(COUNTRY_ATTR, countryAttr); - variantAttr = par.getParameter(VARIANT_ATTR, variantAttr); - localeAttr = par.getParameter(LOCALE_ATTR, localeAttr); - - storeInRequest = par.getParameterAsBoolean(STORE_REQUEST, storeInRequest); - storeInSession = par.getParameterAsBoolean(STORE_SESSION, storeInSession); - createSession = par.getParameterAsBoolean(CREATE_SESSION, createSession); - storeInCookie = par.getParameterAsBoolean(STORE_COOKIE, storeInCookie); - - getLogger().debug("checking for local overrides:\n" + - " " + LANG_ATTR + " = " + langAttr + ",\n" + - " " + COUNTRY_ATTR + " = " + countryAttr + ",\n" + - " " + VARIANT_ATTR + " = " + variantAttr + ",\n" + - " " + LOCALE_ATTR + " = " + localeAttr + ",\n" + - " " + STORE_REQUEST + " = " + storeInRequest + ",\n" + - " " + STORE_SESSION + " = " + storeInSession + ",\n" + - " " + CREATE_SESSION + " = " + createSession + ",\n" + - " " + STORE_COOKIE + " = " + storeInCookie + "\n" - ); + String localeAttrName) { + Locale locale = I18nUtils.findLocale(objectModel, + localeAttrName, + null, + null, + true); + return locale.toString(); } }