FileDocCategorySizeDatePackage
UseLocaleBundleTag.javaAPI DocExample4234Thu Jun 28 16:14:16 BST 2001com.ora.jsp.tags.generic

UseLocaleBundleTag

public class UseLocaleBundleTag extends TagSupport
This class implements a custom action that creates a com.ora.jsp.beans.locale.LocaleBean and saves it in the session scope.
author
Hans Bergsten, Gefion software
version
1.0

Fields Summary
private String
id
private String
bundleName
private String
supportedLangs
Constructors Summary
Methods Summary
public intdoEndTag()
Creates and initializes a LocaleBean and saves it in the session scope using the name specified by the "id" property.

        LocaleBean bean = (LocaleBean)
            pageContext.getAttribute(id, PageContext.SESSION_SCOPE);
        if (bean == null) {
            bean = new LocaleBean();
            pageContext.setAttribute(id, bean, PageContext.SESSION_SCOPE);
        }
        // Tomcat bug: Only looks for bean in page scope
        pageContext.setAttribute(id, bean);
        HttpServletRequest request = 
            (HttpServletRequest) pageContext.getRequest();
        HttpServletResponse response = 
            (HttpServletResponse) pageContext.getResponse();
        bean.setRequestLocales(toArray(request.getLocales()));
        bean.setSupportedLangs(supportedLangs);
        String language = request.getParameter("language");
        if (language != null) {
            bean.setLanguage(language);
        }
        String charset = request.getParameter("charset");
        if (charset != null) {
            bean.setCharset(charset);
            bean.setParameters(getParameters(request));
        }
        bean.setBundleName(bundleName);

        response.setHeader("Content-Language", bean.getLanguage());
        
        return EVAL_PAGE;
    
private java.util.HashtablegetParameters(javax.servlet.http.HttpServletRequest request)
Returns a Hashtable with all HTTP parameters from an HttpServetRequest. The key in the Hashtable is the parameter name and the value is a String[].

param
request the HttpServletRequest
return
a Hashtable with all parameters

        Hashtable parameters = new Hashtable();
        Enumeration names = request.getParameterNames();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            parameters.put(name, request.getParameterValues(name));
        }
        return parameters;
    
public voidrelease()
Releases all instance variables.

        id = null;
        bundleName = null;
        supportedLangs = null;
        super.release();
    
public voidsetBundleName(java.lang.String bundleName)
Sets the bundleName property, i.e. the base name for the ResourceBundle used for localized text.

param
bundleName the ResourceBundle base name

        this.bundleName = bundleName;
    
public voidsetId(java.lang.String id)
Sets the id property, i.e. the name to use for the LocaleBean in the session scope.

param
id the id for the LocaleBean

        this.id = id;
    
public voidsetSupportedLangs(java.lang.String supportedLangs)
Sets the supportedLangs property, i.e. a comma separated list of language/country codes.

param
supportedLangs the list of language/country codes

        this.supportedLangs = supportedLangs;
    
private java.util.Locale[]toArray(java.util.Enumeration locales)
Converts an Enumeration of Locale objects to a Locale array.

param
locales Enumeration of Locale objects
return
a Locale[]

        Vector v = new Vector();
        while (locales.hasMoreElements()) {
            v.addElement(locales.nextElement());
        }
        Locale[] localeArr = new Locale[v.size()];
        v.copyInto(localeArr);
        return localeArr;