FileDocCategorySizeDatePackage
BundleSupport.javaAPI DocGlassfish v2 API12215Sat May 05 19:17:52 BST 2007org.apache.taglibs.standard.tag.common.fmt

BundleSupport

public abstract class BundleSupport extends javax.servlet.jsp.tagext.BodyTagSupport
Support for tag handlers for <bundle>, the resource bundle loading tag in JSTL 1.0.
author
Jan Luehe

Fields Summary
private static final Locale
EMPTY_LOCALE
protected String
basename
protected String
prefix
private javax.servlet.jsp.jstl.fmt.LocalizationContext
locCtxt
Constructors Summary
public BundleSupport()



    //*********************************************************************
    // Constructor and initialization

      
	super();
	init();
    
Methods Summary
public intdoEndTag()

	if (bodyContent != null) {
	    try {
		pageContext.getOut().print(bodyContent.getString());
	    } catch (IOException ioe) {
		throw new JspTagException(ioe.toString(), ioe);
	    }
	}

	return EVAL_PAGE;
    
public intdoStartTag()

	locCtxt = getLocalizationContext(pageContext, basename);
	return EVAL_BODY_BUFFERED;
    
private static javax.servlet.jsp.jstl.fmt.LocalizationContextfindMatch(javax.servlet.jsp.PageContext pageContext, java.lang.String basename)

	LocalizationContext locCtxt = null;
	
	// Determine locale from client's browser settings.
        
	for (Enumeration enum_ = Util.getRequestLocales((HttpServletRequest)pageContext.getRequest());
	     enum_.hasMoreElements(); ) {
	    Locale pref = (Locale) enum_.nextElement();
	    ResourceBundle match = findMatch(basename, pref);
	    if (match != null) {
		locCtxt = new LocalizationContext(match, pref);
		break;
	    }
	}
        	
	return locCtxt;
    
private static java.util.ResourceBundlefindMatch(java.lang.String basename, java.util.Locale pref)

	ResourceBundle match = null;

	try {
	    ResourceBundle bundle =
		ResourceBundle.getBundle(basename, pref,
					 Thread.currentThread().getContextClassLoader());
	    Locale avail = bundle.getLocale();
	    if (pref.equals(avail)) {
		// Exact match
		match = bundle;
	    } else {
                /*
                 * We have to make sure that the match we got is for
                 * the specified locale. The way ResourceBundle.getBundle()
                 * works, if a match is not found with (1) the specified locale,
                 * it tries to match with (2) the current default locale as 
                 * returned by Locale.getDefault() or (3) the root resource 
                 * bundle (basename).
                 * We must ignore any match that could have worked with (2) or (3).
                 * So if an exact match is not found, we make the following extra
                 * tests:
                 *     - avail locale must be equal to preferred locale
                 *     - avail country must be empty or equal to preferred country
                 *       (the equality match might have failed on the variant)
		 */
                if (pref.getLanguage().equals(avail.getLanguage())
		    && ("".equals(avail.getCountry()) || pref.getCountry().equals(avail.getCountry()))) {
		    /*
		     * Language match.
		     * By making sure the available locale does not have a 
		     * country and matches the preferred locale's language, we
		     * rule out "matches" based on the container's default
		     * locale. For example, if the preferred locale is 
		     * "en-US", the container's default locale is "en-UK", and
		     * there is a resource bundle (with the requested base
		     * name) available for "en-UK", ResourceBundle.getBundle()
		     * will return it, but even though its language matches
		     * that of the preferred locale, we must ignore it,
		     * because matches based on the container's default locale
		     * are not portable across different containers with
		     * different default locales.
		     */
		    match = bundle;
		}
	    }
	} catch (MissingResourceException mre) {
	}

	return match;
    
public javax.servlet.jsp.jstl.fmt.LocalizationContextgetLocalizationContext()

	return locCtxt;
    
public static javax.servlet.jsp.jstl.fmt.LocalizationContextgetLocalizationContext(javax.servlet.jsp.PageContext pc)
Gets the default I18N localization context.

param
pc Page in which to look up the default I18N localization context

	LocalizationContext locCtxt = null;

	Object obj = Config.find(pc, Config.FMT_LOCALIZATION_CONTEXT);
	if (obj == null) {
	    return null;
	}

	if (obj instanceof LocalizationContext) {
	    locCtxt = (LocalizationContext) obj;
	} else {
	    // localization context is a bundle basename
	    locCtxt = getLocalizationContext(pc, (String) obj);
	}

	return locCtxt;
    
public static javax.servlet.jsp.jstl.fmt.LocalizationContextgetLocalizationContext(javax.servlet.jsp.PageContext pc, java.lang.String basename)
Gets the resource bundle with the given base name, whose locale is determined as follows: Check if a match exists between the ordered set of preferred locales and the available locales, for the given base name. The set of preferred locales consists of a single locale (if the javax.servlet.jsp.jstl.fmt.locale configuration setting is present) or is equal to the client's preferred locales determined from the client's browser settings.

If no match was found in the previous step, check if a match exists between the fallback locale (given by the javax.servlet.jsp.jstl.fmt.fallbackLocale configuration setting) and the available locales, for the given base name.

param
pageContext Page in which the resource bundle with the given base name is requested
param
basename Resource bundle base name
return
Localization context containing the resource bundle with the given base name and the locale that led to the resource bundle match, or the empty localization context if no resource bundle match was found

	LocalizationContext locCtxt = null;
	ResourceBundle bundle = null;

	if ((basename == null) || basename.equals("")) {
	    return new LocalizationContext();
	}

	// Try preferred locales
	Locale pref = SetLocaleSupport.getLocale(pc, Config.FMT_LOCALE);
	if (pref != null) {
	    // Preferred locale is application-based
	    bundle = findMatch(basename, pref);
	    if (bundle != null) {
		locCtxt = new LocalizationContext(bundle, pref);
	    }
	} else {
	    // Preferred locales are browser-based
	    locCtxt = findMatch(pc, basename);
	}
	
	if (locCtxt == null) {
	    // No match found with preferred locales, try using fallback locale
	    pref = SetLocaleSupport.getLocale(pc, Config.FMT_FALLBACK_LOCALE);
	    if (pref != null) {
		bundle = findMatch(basename, pref);
		if (bundle != null) {
		    locCtxt = new LocalizationContext(bundle, pref);
		}
	    }
	}

	if (locCtxt == null) {
	    // try using the root resource bundle with the given basename
	    try {
		bundle = ResourceBundle.getBundle(basename, EMPTY_LOCALE,
						  Thread.currentThread().getContextClassLoader());
		if (bundle != null) {
		    locCtxt = new LocalizationContext(bundle, null);
		}
	    } catch (MissingResourceException mre) {
		// do nothing
	    }
	}
		 
	if (locCtxt != null) {
	    // set response locale
	    if (locCtxt.getLocale() != null) {
		SetLocaleSupport.setResponseLocale(pc, locCtxt.getLocale());
	    }
	} else {
	    // create empty localization context
	    locCtxt = new LocalizationContext();
	}

	return locCtxt;
    
public java.lang.StringgetPrefix()

	return prefix;
    
private voidinit()

	basename = prefix = null;
	locCtxt = null;
    
public voidrelease()

	init();