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 javax.servlet.jsp.JspException;
042: import javax.servlet.jsp.jstl.core.LoopTag;
043: import javax.servlet.jsp.tagext.IterationTag;
044:
045: import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
046: import org.apache.taglibs.standard.tag.common.core.ForTokensSupport;
047: import org.apache.taglibs.standard.tag.common.core.NullAttributeException;
048:
049: /**
050: * <p>A handler for <forTokens> that accepts attributes as Strings
051: * and evaluates them as expressions at runtime.
052: *
053: * @author Shawn Bayern
054: */
055: public class ForTokensTag extends ForTokensSupport implements LoopTag,
056: IterationTag {
057:
058: //*********************************************************************
059: // 'Private' state (implementation details)
060:
061: private String begin_; // raw EL-based property
062: private String end_; // raw EL-based property
063: private String step_; // raw EL-based property
064: private String items_; // raw EL-based property
065: private String delims_; // raw EL-based property
066:
067: //*********************************************************************
068: // Constructor
069:
070: public ForTokensTag() {
071: super ();
072: init();
073: }
074:
075: //*********************************************************************
076: // Tag logic
077:
078: /* Begins iterating by processing the first item. */
079: public int doStartTag() throws JspException {
080:
081: // evaluate any expressions we were passed, once per invocation
082: evaluateExpressions();
083:
084: // chain to the parent implementation
085: return super .doStartTag();
086: }
087:
088: // Releases any resources we may have (or inherit)
089: public void release() {
090: super .release();
091: init();
092: }
093:
094: //*********************************************************************
095: // Accessor methods
096:
097: // for EL-based attribute
098: public void setBegin(String begin_) {
099: this .begin_ = begin_;
100: this .beginSpecified = true;
101: }
102:
103: // for EL-based attribute
104: public void setEnd(String end_) {
105: this .end_ = end_;
106: this .endSpecified = true;
107: }
108:
109: // for EL-based attribute
110: public void setStep(String step_) {
111: this .step_ = step_;
112: this .stepSpecified = true;
113: }
114:
115: // for EL-based attribute
116: public void setItems(String items_) {
117: this .items_ = items_;
118: }
119:
120: // for EL-based attribute
121: public void setDelims(String delims_) {
122: this .delims_ = delims_;
123: }
124:
125: //*********************************************************************
126: // Private (utility) methods
127:
128: // (re)initializes state (during release() or construction)
129: private void init() {
130: // defaults for interface with page author
131: begin_ = null; // (no expression)
132: end_ = null; // (no expression)
133: step_ = null; // (no expression)
134: items_ = null; // (no expression)
135: delims_ = null; // (no expression)
136: }
137:
138: /* Evaluates expressions as necessary */
139: private void evaluateExpressions() throws JspException {
140: /*
141: * Note: we don't check for type mismatches here; we assume
142: * the expression evaluator will return the expected type
143: * (by virtue of knowledge we give it about what that type is).
144: * A ClassCastException here is truly unexpected, so we let it
145: * propagate up.
146: */
147:
148: if (begin_ != null) {
149: Object r = ExpressionEvaluatorManager.evaluate("begin",
150: begin_, Integer.class, this , pageContext);
151: if (r == null)
152: throw new NullAttributeException("forTokens", "begin");
153: begin = ((Integer) r).intValue();
154: validateBegin();
155: }
156:
157: if (end_ != null) {
158: Object r = ExpressionEvaluatorManager.evaluate("end", end_,
159: Integer.class, this , pageContext);
160: if (r == null)
161: throw new NullAttributeException("forTokens", "end");
162: end = ((Integer) r).intValue();
163: validateEnd();
164: }
165:
166: if (step_ != null) {
167: Object r = ExpressionEvaluatorManager.evaluate("step",
168: step_, Integer.class, this , pageContext);
169: if (r == null)
170: throw new NullAttributeException("forTokens", "step");
171: step = ((Integer) r).intValue();
172: validateStep();
173: }
174:
175: if (items_ != null) {
176: items = (String) ExpressionEvaluatorManager.evaluate(
177: "items", items_, String.class, this , pageContext);
178: // use the empty string to indicate "no iteration"
179: if (items == null)
180: items = "";
181: }
182:
183: if (delims_ != null) {
184: delims = (String) ExpressionEvaluatorManager.evaluate(
185: "delims", delims_, String.class, this , pageContext);
186: // use the empty string to cause monolithic tokenization
187: if (delims == null)
188: delims = "";
189: }
190: }
191: }
|