001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Portions Copyright Apache Software Foundation.
007: *
008: * The contents of this file are subject to the terms of either the GNU
009: * General Public License Version 2 only ("GPL") or the Common Development
010: * and Distribution License("CDDL") (collectively, the "License"). You
011: * may not use this file except in compliance with the License. You can obtain
012: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
013: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
014: * language governing permissions and limitations under the License.
015: *
016: * When distributing the software, include this License Header Notice in each
017: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
018: * Sun designates this particular file as subject to the "Classpath" exception
019: * as provided by Sun in the GPL Version 2 section of the License file that
020: * accompanied this code. If applicable, add the following below the License
021: * Header, with the fields enclosed by brackets [] replaced by your own
022: * identifying information: "Portions Copyrighted [year]
023: * [name of copyright owner]"
024: *
025: * Contributor(s):
026: *
027: * If you wish your version of this file to be governed by only the CDDL or
028: * only the GPL Version 2, indicate your decision by adding "[Contributor]
029: * elects to include this software in this distribution under the [CDDL or GPL
030: * Version 2] license." If you don't indicate a single choice of license, a
031: * recipient has the option to distribute your version of this file under
032: * either the CDDL, the GPL Version 2 or to extend the choice of license to
033: * its licensees as provided above. However, if you add GPL Version 2 code
034: * and therefore, elected the GPL Version 2 license, then the option applies
035: * only if the new code is made subject to such option by the copyright
036: * holder.
037: */
038:
039: package org.apache.taglibs.standard.tag.el.core;
040:
041: import java.util.ArrayList;
042:
043: import javax.servlet.jsp.JspException;
044: import javax.servlet.jsp.jstl.core.LoopTag;
045: import javax.servlet.jsp.tagext.IterationTag;
046:
047: import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
048: import org.apache.taglibs.standard.tag.common.core.ForEachSupport;
049: import org.apache.taglibs.standard.tag.common.core.NullAttributeException;
050:
051: /**
052: * <p>A handler for <forEach> that accepts attributes as Strings
053: * and evaluates them as expressions at runtime.</p>
054: *
055: * @author Shawn Bayern
056: */
057:
058: public class ForEachTag extends ForEachSupport implements LoopTag,
059: IterationTag {
060:
061: //*********************************************************************
062: // 'Private' state (implementation details)
063:
064: private String begin_; // stores EL-based property
065: private String end_; // stores EL-based property
066: private String step_; // stores EL-based property
067: private String items_; // stores EL-based property
068:
069: //*********************************************************************
070: // Constructor
071:
072: public ForEachTag() {
073: super ();
074: init();
075: }
076:
077: //*********************************************************************
078: // Tag logic
079:
080: /* Begins iterating by processing the first item. */
081: public int doStartTag() throws JspException {
082:
083: // evaluate any expressions we were passed, once per invocation
084: evaluateExpressions();
085:
086: // chain to the parent implementation
087: return super .doStartTag();
088: }
089:
090: // Releases any resources we may have (or inherit)
091: public void release() {
092: super .release();
093: init();
094: }
095:
096: //*********************************************************************
097: // Accessor methods
098:
099: // for EL-based attribute
100: public void setBegin(String begin_) {
101: this .begin_ = begin_;
102: this .beginSpecified = true;
103: }
104:
105: // for EL-based attribute
106: public void setEnd(String end_) {
107: this .end_ = end_;
108: this .endSpecified = true;
109: }
110:
111: // for EL-based attribute
112: public void setStep(String step_) {
113: this .step_ = step_;
114: this .stepSpecified = true;
115: }
116:
117: public void setItems(String items_) {
118: this .items_ = items_;
119: }
120:
121: //*********************************************************************
122: // Private (utility) methods
123:
124: // (re)initializes state (during release() or construction)
125: private void init() {
126: // defaults for interface with page author
127: begin_ = null; // (no expression)
128: end_ = null; // (no expression)
129: step_ = null; // (no expression)
130: items_ = null; // (no expression)
131: }
132:
133: /* Evaluates expressions as necessary */
134: private void evaluateExpressions() throws JspException {
135: /*
136: * Note: we don't check for type mismatches here; we assume
137: * the expression evaluator will return the expected type
138: * (by virtue of knowledge we give it about what that type is).
139: * A ClassCastException here is truly unexpected, so we let it
140: * propagate up.
141: */
142:
143: if (begin_ != null) {
144: Object r = ExpressionEvaluatorManager.evaluate("begin",
145: begin_, Integer.class, this , pageContext);
146: if (r == null)
147: throw new NullAttributeException("forEach", "begin");
148: begin = ((Integer) r).intValue();
149: validateBegin();
150: }
151:
152: if (end_ != null) {
153: Object r = ExpressionEvaluatorManager.evaluate("end", end_,
154: Integer.class, this , pageContext);
155: if (r == null)
156: throw new NullAttributeException("forEach", "end");
157: end = ((Integer) r).intValue();
158: validateEnd();
159: }
160:
161: if (step_ != null) {
162: Object r = ExpressionEvaluatorManager.evaluate("step",
163: step_, Integer.class, this , pageContext);
164: if (r == null)
165: throw new NullAttributeException("forEach", "step");
166: step = ((Integer) r).intValue();
167: validateStep();
168: }
169:
170: if (items_ != null) {
171: rawItems = ExpressionEvaluatorManager.evaluate("items",
172: items_, Object.class, this , pageContext);
173: // use an empty list to indicate "no iteration", if relevant
174: if (rawItems == null)
175: rawItems = new ArrayList();
176: }
177: }
178: }
|