001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1999 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution, if
020: * any, must include the following acknowlegement:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowlegement may appear in the software itself,
024: * if and wherever such third-party acknowlegements normally appear.
025: *
026: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
027: * Foundation" must not be used to endorse or promote products derived
028: * from this software without prior written permission. For written
029: * permission, please contact apache@apache.org.
030: *
031: * 5. Products derived from this software may not be called "Apache"
032: * nor may "Apache" appear in their names without prior written
033: * permission of the Apache Group.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of the Apache Software Foundation. For more
051: * information on the Apache Software Foundation, please see
052: * <http://www.apache.org/>.
053: *
054: */
055:
056: package org.apache.taglibs.jsptl.tag.jx;
057:
058: import java.io.*;
059: import javax.servlet.jsp.*;
060: import javax.servlet.jsptl.*;
061: import javax.servlet.jsp.tagext.*;
062: import org.apache.taglibs.jsptl.tag.common.*;
063: import org.apache.taglibs.jsptl.lang.support.ExpressionEvaluatorManager;
064:
065: /**
066: * <p>A handler for the <expr> tag, which simply evalutes and
067: * prints the result of the expression it's passed. If the expression
068: * fails to complete evaluation successfully because of a
069: * javax.servlet.jsptl.ExpressionException, or if the result is
070: * null, we print the value of the 'default' attribute's expression or
071: * our body (which two are mutually exclusive, although this constraint
072: * is enforced outside this handler, in our TagLibraryValidator).</p>
073: *
074: * @author Shawn Bayern
075: */
076: public class ExprTag extends BodyTagSupport {
077:
078: /*
079: * (One almost wishes XML and JSP could support "anonymous tags,"
080: * given the amount of trouble we had naming this one!) :-) - sb
081: */
082:
083: //*********************************************************************
084: // Internal state
085: private String value; // tag attribute
086: private String def; // tag attribute
087: private boolean needBody; // non-space body needed?
088:
089: //*********************************************************************
090: // Construction and initialization
091:
092: /**
093: * Constructs a new handler. As with TagSupport, subclasses should
094: * not provide other constructors and are expected to call the
095: * superclass constructor.
096: */
097: public ExprTag() {
098: super ();
099: init();
100: }
101:
102: // resets local state
103: private void init() {
104: value = def = null;
105: needBody = false;
106: }
107:
108: // Releases any resources we may have (or inherit)
109: public void release() {
110: super .release();
111: init();
112: }
113:
114: //*********************************************************************
115: // Tag logic
116:
117: // evaluates 'value' and determines if the body should be evaluted
118: public int doStartTag() throws JspException {
119: try {
120: Object result;
121:
122: // try to evaluate 'value'
123: try {
124: result = ExpressionEvaluatorManager.evaluate("value",
125: value, Object.class, this , pageContext);
126: } catch (ExpressionException ex) {
127: result = null; // expression-specific error; no result
128: }
129:
130: // print value if available; otherwise, try 'default'
131: if (result != null) {
132: pageContext.getOut().print(result);
133: return SKIP_BODY;
134: } else {
135: // if we don't have a 'default' attribute, just go to the body
136: if (def == null) {
137: needBody = true;
138: return EVAL_BODY_TAG;
139: }
140:
141: // if we do have 'default', try to evaluate it
142: result = ExpressionEvaluatorManager.evaluate("default",
143: def, Object.class, this , pageContext);
144: if (result != null) {
145: // good 'default'
146: pageContext.getOut().print(result);
147: return SKIP_BODY;
148: } else {
149: // bad 'default'
150: throw new NullAttributeException("expr", "default");
151: }
152: }
153: } catch (IOException ex) {
154: throw new JspException(ex.getMessage());
155: }
156: }
157:
158: // prints the body if necessary; reports errors
159: public int doEndTag() throws JspException {
160: try {
161: if (!needBody)
162: return EVAL_PAGE; // nothing more to do
163:
164: // verify and print out the body
165: String bcs = getBodyContent().getString();
166: if (bcs == null || bcs.trim().equals(""))
167: throw new JspTagException(
168: "In <expr>, attribute "
169: + "value='"
170: + value
171: + "' "
172: + "didn't evaluate successfully, but there was no 'default' "
173: + "and no non-whitespace content inside the tag.");
174: getBodyContent().writeOut(pageContext.getOut());
175: return EVAL_PAGE;
176: } catch (IOException ex) {
177: throw new JspException(ex.getMessage());
178: }
179: }
180:
181: //*********************************************************************
182: // Accessor methods
183:
184: // for tag attribute
185: public void setValue(String value) {
186: this .value = value;
187: }
188:
189: // for tag attribute
190: public void setDefault(String def) {
191: this.def = def;
192: }
193: }
|