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: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package javax.servlet.jsp.jstl.core;
038:
039: import java.io.Serializable;
040: import java.util.Iterator;
041: import java.util.Collection;
042: import java.util.Enumeration;
043: import java.util.Map;
044: import java.util.StringTokenizer;
045:
046: import javax.el.ELContext;
047: import javax.el.ELException;
048: import javax.el.ValueExpression;
049:
050: import javax.servlet.jsp.JspTagException;
051:
052: /**
053: * @author Kin-man Chung
054: * @version $Id: IteratedExpression.java,v 1.7 2007/05/06 02:17:13 tcfujii Exp $
055: */
056: public final class IteratedExpression /*implements Serializable*/{
057:
058: private static final long serialVersionUID = 1L;
059: protected final ValueExpression orig;
060: protected final String delims;
061:
062: private Object base;
063: private int index;
064: private Iterator iter;
065:
066: public IteratedExpression(ValueExpression orig, String delims) {
067: this .orig = orig;
068: this .delims = delims;
069: }
070:
071: /**
072: * Evaluates the stored ValueExpression and return the indexed item.
073: * @param context The ELContext used to evaluate the ValueExpression
074: * @param i The index of the item to be retrieved
075: */
076: public Object getItem(ELContext context, int i) {
077:
078: if (base == null) {
079: base = orig.getValue(context);
080: if (base == null) {
081: return null;
082: }
083: iter = toIterator(base);
084: index = 0;
085: }
086: if (index > i) {
087: // Restart from index 0
088: iter = toIterator(base);
089: index = 0;
090: }
091: while (iter.hasNext()) {
092: Object item = iter.next();
093: if (index++ == i) {
094: return item;
095: }
096: }
097: return null;
098: }
099:
100: public ValueExpression getValueExpression() {
101: return orig;
102: }
103:
104: private Iterator toIterator(final Object obj) {
105:
106: Iterator iter;
107: if (obj instanceof String) {
108: iter = toIterator(new StringTokenizer((String) obj, delims));
109: } else if (obj instanceof Iterator) {
110: iter = (Iterator) obj;
111: } else if (obj instanceof Collection) {
112: iter = toIterator(((Collection) obj).iterator());
113: } else if (obj instanceof Enumeration) {
114: iter = toIterator((Enumeration) obj);
115: } else if (obj instanceof Map) {
116: iter = ((Map) obj).entrySet().iterator();
117: } else {
118: throw new ELException(
119: "Don't know how to iterate over supplied "
120: + "items in forEach");
121: }
122: return iter;
123: }
124:
125: private Iterator toIterator(final Enumeration obj) {
126: return new Iterator() {
127: public boolean hasNext() {
128: return obj.hasMoreElements();
129: }
130:
131: public Object next() {
132: return obj.nextElement();
133: }
134:
135: public void remove() {
136: }
137: };
138: }
139: }
|