001: /**
002: * $Id: BeanGetTag.java,v 1.9 2005/09/21 10:50:11 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: import javax.servlet.jsp.tagext.*;
022:
023: /**
024: * BeanGetTag - get a bean property
025: *
026: * This class reads the specified bean property
027: * and either writes the value as a string to the
028: * page output or saves the value into the page context
029: * using the "id" attribute.
030: *
031: * Attributes:
032: *
033: * property
034: * name of the bean property to get
035: * name
036: * name to use to retrieve the bean from the page context
037: * id
038: * name to use to store the property onto the page context
039: * eval
040: * attribute to evaluate and write to the page output
041: *
042: * @author Robert O'Brien
043: * @version 1.0
044: * @see BeanSetTag
045: * @see BeanSupport
046: */
047: public class BeanGetTag extends BeanSupport {
048:
049: // Create a logger for this class
050: private static Logger debugLogger = PortalLogger
051: .getLogger(BeanGetTag.class);
052:
053: /**
054: * Get the bean property value
055: *
056: * If the "id" attribute is specified then store
057: * the value into the page context using the id.
058: * Else write the value as a string to page output.
059: * If the 'eval' attribute is specifed then write
060: * the evalated form to the page output.
061: *
062: * @return SKIP_BODY
063: * @exception JspException
064: */
065: public int doStartTag() throws JspException {
066:
067: if (eval != null) {
068: String rslt = eval;
069: if (edit != null) {
070: rslt = edit.editString(eval);
071: /**
072: * If rslt is null, output default.
073: */
074: if (rslt == null) {
075: rslt = def;
076: }
077: }
078: try {
079: pageContext.getOut().print(rslt);
080: return SKIP_BODY;
081: } catch (java.io.IOException e) {
082: debugLogger.log(Level.INFO, "PSMA_CSPWTB0001", e);
083: throw new JspException(this .getClass().getName()
084: + ".doStartTag(): IO Exception: "
085: + e.getMessage());
086: }
087: }
088:
089: try {
090: bean = findBean();
091:
092: } catch (Exception e) {
093: debugLogger.log(Level.FINE, "PSMA_CSPWTB0007", e);
094: throw new JspException(this .getClass().getName()
095: + ".doStartTag(): No bean found: "
096: + e.getMessage());
097: }
098:
099: try {
100: Object value;
101:
102: if (property != null) {
103: value = readProperty(bean);
104: } else {
105: value = bean;
106: }
107:
108: debugLogger.finest("id=" + id + ", bean=" + bean
109: + ", prop=" + property + ", value=" + value);
110:
111: if (id != null) {
112: pageContext.setAttribute(id, value);
113:
114: } else if (value != null) {
115: String valueStr = value.toString();
116: if (edit != null) {
117: pageContext.getOut().print(
118: edit.editString(valueStr));
119: } else {
120: pageContext.getOut().print(valueStr);
121: }
122: }
123:
124: } catch (IntrospectionException e) {
125: debugLogger.log(Level.FINE, "PSMA_CSPWTB0005", e);
126: throw new JspException(this .getClass().getName()
127: + ".doStartTag(): Couldn't read " + getProperty()
128: + " property");
129:
130: } catch (java.io.IOException e) {
131: debugLogger.log(Level.INFO, "PSMA_CSPWTB0001", e);
132: throw new JspException(this .getClass().getName()
133: + ".doStartTag(): IO Exception: " + e.getMessage());
134: }
135:
136: return SKIP_BODY;
137: }
138:
139: /**
140: * eval attribute
141: */
142: String eval = null;
143:
144: /**
145: * Set the eval attribute
146: */
147: public void setEval(String eval) {
148: this .eval = evalAttribute(eval);
149: }
150:
151: /**
152: * Edit bean...
153: */
154: private Edit edit = null;
155:
156: /**
157: * Set the edit bean
158: */
159: public void setEdit(String edit) {
160: this .edit = (Edit) pageContext.getAttribute(edit);
161: }
162:
163: /**
164: * Default to return if result is null
165: */
166: private String def = null;
167:
168: public String getDefault() {
169: return def;
170: }
171:
172: public void setDefault(String def) {
173: this .def = def;
174: }
175:
176: /**
177: * Cleanup
178: */
179: public void release() {
180: super.release();
181: eval = null;
182: edit = null;
183: }
184: }
|