001: /**
002: * $Id: BeanTestTag.java,v 1.6 2005/09/21 10:50:27 dg154973 Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.wireless.taglibs.base;
014:
015: import com.sun.portal.log.common.PortalLogger;
016:
017: import java.beans.*;
018: import java.util.logging.Logger;
019: import java.util.logging.Level;
020: import javax.servlet.jsp.*;
021:
022: /**
023: * BeanTestTag - test a bean property
024: *
025: * Evaluates the body if the property is true; else skips the body.
026: *
027: * A bean property is evaluated as follows:
028: * - if the property value is null then return false
029: * - if the value is a boolean then return the boolean value
030: * - if the value is some other object then compare it to the specified value
031: *
032: * Attributes:
033: *
034: * property
035: * name of the bean property to test
036: * value
037: * string value of the bean property to test; optional
038: * id
039: * object value of the bean property to test; optional
040: * name
041: * name to use to find the bean on the page context
042: *
043: * @author Robert O'Brien
044: * @version 1.0
045: * @see BeanSetTag
046: * @see BeanSupport
047: */
048: public class BeanTestTag extends BeanSupport {
049:
050: // Create a logger for this class
051: private static Logger debugLogger = PortalLogger
052: .getLogger(BeanTestTag.class);
053:
054: /**
055: * The value of the bean property to test
056: */
057: public String value;
058:
059: /**
060: * Get the value of the bean property
061: *
062: * @return the value
063: */
064: public String getValue() {
065: return value;
066: }
067:
068: /**
069: * Set the value of the bean property
070: *
071: * @param value the value
072: */
073: public void setValue(String value) {
074: this .value = evalAttribute(value);
075: }
076:
077: /**
078: * Evaluate a bean property
079: *
080: * @param prop the bean property value to test
081: * @param test the test value to compare against
082: * @return true if the two values are considered equivalent
083: */
084: public boolean evaluate(Object prop, Object test) {
085:
086: debugLogger.finest("prop=" + prop + ", test=" + test);
087:
088: if (prop == null) {
089: return false;
090: }
091:
092: if (prop instanceof String && test == null) {
093: String pstring = (String) prop;
094: if (pstring.equals("")) {
095: return false;
096: }
097: }
098:
099: if (prop instanceof Boolean && test == null) {
100: return ((Boolean) prop).booleanValue();
101: }
102:
103: if (test == null) {
104: return true;
105: }
106:
107: try {
108: Class propclass = prop.getClass();
109: Class testclass = test.getClass();
110:
111: if (propclass != testclass) {
112: test = getInstanceOf(test, propclass);
113: }
114:
115: return prop.equals(test);
116:
117: } catch (InstantiationException e) {
118: }
119:
120: return false;
121: }
122:
123: /**
124: * Test a bean property
125: *
126: * @return SKIP_BODY
127: * @exception JspException
128: */
129: public int doStartTag() throws JspException {
130: Object value, propval;
131:
132: try {
133: bean = findBean();
134: } catch (Exception e) {
135: debugLogger.log(Level.FINE, "PSMA_CSPWTB0007", e);
136: throw new JspException(this .getClass().getName()
137: + ".doStartTag(): No bean found: "
138: + e.getMessage());
139: }
140:
141: if (property == null) {
142: debugLogger.fine("PSMA_CSPWTB0002");
143: throw new JspException(this .getClass().getName()
144: + ".doStartTag(): property attribute is required");
145: }
146:
147: if (id != null) {
148: value = pageContext.findAttribute(id);
149: } else {
150: value = getValue();
151: }
152:
153: try {
154: propval = readProperty(bean);
155: } catch (IntrospectionException e) {
156: debugLogger.log(Level.FINE, "PSMA_CSPWTB0005",
157: getProperty());
158: throw new JspException(this .getClass().getName()
159: + ".doStartTag(): Couldn't read " + getProperty()
160: + " property");
161: }
162:
163: return (evaluate(propval, value)) ? EVAL_BODY_INCLUDE
164: : SKIP_BODY;
165: }
166:
167: /**
168: * Cleanup
169: */
170: public void release() {
171: super.release();
172: value = null;
173: }
174: }
|