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

FormatNumberSupport

public abstract class FormatNumberSupport extends javax.servlet.jsp.tagext.BodyTagSupport
Support for tag handlers for <formatNumber>, the number formatting tag in JSTL 1.0.
author
Jan Luehe

Fields Summary
private static final Class[]
GET_INSTANCE_PARAM_TYPES
private static final String
NUMBER
private static final String
CURRENCY
private static final String
PERCENT
protected Object
value
protected boolean
valueSpecified
protected String
type
protected String
pattern
protected String
currencyCode
protected String
currencySymbol
protected boolean
isGroupingUsed
protected boolean
groupingUsedSpecified
protected int
maxIntegerDigits
protected boolean
maxIntegerDigitsSpecified
protected int
minIntegerDigits
protected boolean
minIntegerDigitsSpecified
protected int
maxFractionDigits
protected boolean
maxFractionDigitsSpecified
protected int
minFractionDigits
protected boolean
minFractionDigitsSpecified
private String
var
private int
scope
private static Class
currencyClass
Constructors Summary
public FormatNumberSupport()



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

     
	try {
	    currencyClass = Class.forName("java.util.Currency");
	    // container's runtime is J2SE 1.4 or greater
	} catch (Exception cnfe) {
	}
    
	super();
	init();
    
Methods Summary
private voidconfigureFormatter(java.text.NumberFormat formatter)

	if (groupingUsedSpecified)
	    formatter.setGroupingUsed(isGroupingUsed);
	if (maxIntegerDigitsSpecified)
	    formatter.setMaximumIntegerDigits(maxIntegerDigits);
	if (minIntegerDigitsSpecified)
	    formatter.setMinimumIntegerDigits(minIntegerDigits);
	if (maxFractionDigitsSpecified)
	    formatter.setMaximumFractionDigits(maxFractionDigits);
	if (minFractionDigitsSpecified)
	    formatter.setMinimumFractionDigits(minFractionDigits);
    
private java.text.NumberFormatcreateFormatter(java.util.Locale loc)

	NumberFormat formatter = null;
	
	if ((type == null) || NUMBER.equalsIgnoreCase(type)) {
	    formatter = NumberFormat.getNumberInstance(loc);
	} else if (CURRENCY.equalsIgnoreCase(type)) {
	    formatter = NumberFormat.getCurrencyInstance(loc);
	} else if (PERCENT.equalsIgnoreCase(type)) {
	    formatter = NumberFormat.getPercentInstance(loc);
	} else {
	    throw new JspException(
	        Resources.getMessage("FORMAT_NUMBER_INVALID_TYPE", type));
	}
	
	return formatter;
    
public intdoEndTag()

	String formatted = null;
        Object input = null;

        // determine the input by...
        if (valueSpecified) {
	    // ... reading 'value' attribute
	    input = value;
	} else {
	    // ... retrieving and trimming our body
	    if (bodyContent != null && bodyContent.getString() != null)
	        input = bodyContent.getString().trim();
	}

	if ((input == null) || input.equals("")) {
	    // Spec says:
            // If value is null or empty, remove the scoped variable 
            // if it is specified (see attributes var and scope).
	    if (var != null) {
	        pageContext.removeAttribute(var, scope);
            }
	    return EVAL_PAGE;
	}

	/*
	 * If 'value' is a String, it is first parsed into an instance of
	 * java.lang.Number
	 */
	if (input instanceof String) {
	    try {
		if (((String) input).indexOf('.") != -1) {
		    input = Double.valueOf((String) input);
		} else {
		    input = Long.valueOf((String) input);
		}
	    } catch (NumberFormatException nfe) {
		throw new JspException(
                    Resources.getMessage("FORMAT_NUMBER_PARSE_ERROR", input),
		    nfe);
	    }
	}

	// Determine formatting locale
	Locale loc = SetLocaleSupport.getFormattingLocale(pageContext,
                                                          this,
                                                          false,
                                                          true);
	if (loc != null) {
	    // Create formatter 
	    NumberFormat formatter = null;
	    if ((pattern != null) && !pattern.equals("")) {
		// if 'pattern' is specified, 'type' is ignored
		DecimalFormatSymbols symbols = new DecimalFormatSymbols(loc);
		formatter = new DecimalFormat(pattern, symbols);
	    } else {
		formatter = createFormatter(loc);
	    }
	    if (((pattern != null) && !pattern.equals(""))
		    || CURRENCY.equalsIgnoreCase(type)) {
		try {
		    setCurrency(formatter);
		} catch (Exception e) {
		    throw new JspException(
                        Resources.getMessage("FORMAT_NUMBER_CURRENCY_ERROR"),
			e);
		}
	    }
	    configureFormatter(formatter);
	    formatted = formatter.format(input);
	} else {
	    // no formatting locale available, use toString()
	    formatted = input.toString();
	}

	if (var != null) {
	    pageContext.setAttribute(var, formatted, scope);	
	} else {
	    try {
		pageContext.getOut().print(formatted);
	    } catch (IOException ioe) {
		throw new JspTagException(ioe.toString(), ioe);
	    }
	}

	return EVAL_PAGE;
    
private voidinit()

	value = type = null;
	valueSpecified = false;
	pattern = var = currencyCode = currencySymbol = null;
	groupingUsedSpecified = false;
	maxIntegerDigitsSpecified = minIntegerDigitsSpecified = false;
	maxFractionDigitsSpecified = minFractionDigitsSpecified = false;
	scope = PageContext.PAGE_SCOPE;
    
public voidrelease()

	init();
    
private voidsetCurrency(java.text.NumberFormat formatter)

	String code = null;
	String symbol = null;

	if ((currencyCode == null) && (currencySymbol == null)) {
	    return;
	}

	if ((currencyCode != null) && (currencySymbol != null)) {
	    if (currencyClass != null)
		code = currencyCode;
	    else
		symbol = currencySymbol;
	} else if (currencyCode == null) {
	    symbol = currencySymbol;
	} else {
	    if (currencyClass != null)
		code = currencyCode;
	    else
		symbol = currencyCode;
	}

	if (code != null) {
	    Object[] methodArgs = new Object[1];

	    /*
	     * java.util.Currency.getInstance()
	     */
	    Method m = currencyClass.getMethod("getInstance",
					       GET_INSTANCE_PARAM_TYPES);
	    methodArgs[0] = code;
	    Object currency = m.invoke(null, methodArgs);

	    /*
	     * java.text.NumberFormat.setCurrency()
	     */
	    Class[] paramTypes = new Class[1];
	    paramTypes[0] = currencyClass;
	    Class numberFormatClass = Class.forName("java.text.NumberFormat");
	    m = numberFormatClass.getMethod("setCurrency", paramTypes);
	    methodArgs[0] = currency;
	    m.invoke(formatter, methodArgs);
	} else {
	    /*
	     * Let potential ClassCastException propagate up (will almost
	     * never happen)
	     */
	    DecimalFormat df = (DecimalFormat) formatter;
	    DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
	    dfs.setCurrencySymbol(symbol);
	    df.setDecimalFormatSymbols(dfs);
	}
    
public voidsetScope(java.lang.String scope)

	this.scope = Util.getScope(scope);
    
public voidsetVar(java.lang.String var)

        this.var = var;