FileDocCategorySizeDatePackage
BooleanValueTag.javaAPI DocExample2756Thu Jun 28 16:14:16 BST 2001com.ora.jsp.tags.sql.value

BooleanValueTag.java

package com.ora.jsp.tags.sql.value;

import java.lang.reflect.*;
import java.text.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import com.ora.jsp.sql.value.*;
import com.ora.jsp.tags.sql.ValueTagParent;
import com.ora.jsp.util.*;

/**
 * This class is a custom action intended to be used in the body of
 * a sqlQuery or an sqlUpdate action element. It adds the specified 
 * value to its parent's value list.
 *
 * @author Hans Bergsten, Gefion software <hans@gefionsoftware.com>
 * @version 1.0.1
 */
public class BooleanValueTag extends ValueTag {
    private boolean value;

    /**
     * Sets the value property.
     */
    public void setValue(boolean value) {
        this.value = value;
    }

    /**
     * Gets the value, specified by the value attribute, the stringValue
     * attribute, the param attribute, or the name and property attributes,
     * and adds it to the parent's value list. 
     */
    public int doEndTag() throws JspException {
        if (stringValue != null) {
            value = toBoolean(stringValue);
        }
        else if (param != null) {
            String paramValue = getParameter(param);
            value = toBoolean(paramValue);
        }
        else if (name != null) {
            value = getBoolean(name, property);
        }
        ValueTagParent parent = 
            (ValueTagParent) findAncestorWithClass(this, ValueTagParent.class);
        if (parent == null) {
            throw new JspException("The sqlBooleanValue action is not " +
                "enclosed by a supported action type");
        }
        parent.addValue(new BooleanValue(value));
        return EVAL_PAGE;
    }
    
    private boolean toBoolean(String stringValue) {
        return new Boolean(stringValue).booleanValue();
    }
    
    private boolean getBoolean(String beanName, String propertyName) 
        throws JspException {
        boolean booleanValue;
        Object bean = getBean(beanName);
        Method readMethod = getPropertyReadMethod(bean, propertyName);
        Class returnType = readMethod.getReturnType();
        Object value = getValue(bean, readMethod, propertyName);
        
        if (Boolean.TYPE.isAssignableFrom(returnType)) {
            booleanValue = ((Boolean) value).booleanValue();
        }
        else if (String.class.isAssignableFrom(returnType)) {
            booleanValue = toBoolean((String) value);
        }
        else {
            throw new JspException("Read method for the " + propertyName +
                " property in the bean named " + beanName + " is not of type " +
                " String or boolean");
        }
        return booleanValue;
    }
}