KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > servlet > jsp > jstl > core > IteratedExpression


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  *
21  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package javax.servlet.jsp.jstl.core;
25
26 import java.util.Iterator JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31
32 import javax.el.ELContext;
33 import javax.el.ELException;
34 import javax.el.ValueExpression;
35
36 import javax.servlet.jsp.JspTagException JavaDoc;
37
38 import org.apache.taglibs.standard.resources.Resources;
39
40 /**
41  * @author Kin-man Chung
42  * @version $Id: IteratedExpression.java,v 1.3 2005/12/08 01:20:43 kchung Exp $
43  */

44 public final class IteratedExpression {
45
46     private static final long serialVersionUID = 1L;
47     protected final ValueExpression orig;
48     protected final String JavaDoc delims;
49
50     private Object JavaDoc base;
51     private int index;
52     private Iterator JavaDoc iter;
53
54     public IteratedExpression(ValueExpression orig, String JavaDoc delims) {
55         this.orig = orig;
56         this.delims = delims;
57     }
58
59     /**
60      * Evaluates the stored ValueExpression and return the indexed item.
61      * @param context The ELContext used to evaluate the ValueExpression
62      * @param i The index of the item to be retrieved
63      */

64     public Object JavaDoc getItem(ELContext context, int i) {
65
66         if (base == null) {
67             base = orig.getValue(context);
68             if (base == null) {
69                 return null;
70             }
71             iter = toIterator(base);
72             index = 0;
73         }
74         if (index > i) {
75             // Restart from index 0
76
iter = toIterator(base);
77             index = 0;
78         }
79         while (iter.hasNext()) {
80             Object JavaDoc item = iter.next();
81             if (index++ == i) {
82                 return item;
83             }
84         }
85         return null;
86     }
87
88     public ValueExpression getValueExpression() {
89         return orig;
90     }
91
92     private Iterator JavaDoc toIterator(final Object JavaDoc obj) {
93
94         Iterator JavaDoc iter;
95         if (obj instanceof String JavaDoc) {
96             iter = toIterator(new StringTokenizer JavaDoc((String JavaDoc)obj, delims));
97         }
98         else if (obj instanceof Iterator JavaDoc) {
99             iter = (Iterator JavaDoc)obj;
100         }
101         else if (obj instanceof Collection JavaDoc) {
102             iter = toIterator(((Collection JavaDoc) obj).iterator());
103         }
104         else if (obj instanceof Enumeration JavaDoc) {
105             iter = toIterator((Enumeration JavaDoc)obj);
106         }
107         else if (obj instanceof Map JavaDoc) {
108             iter = ((Map JavaDoc)obj).entrySet().iterator();
109         } else {
110             throw new ELException(Resources.getMessage("FOREACH_BAD_ITEMS"));
111         }
112         return iter;
113     }
114
115     private Iterator JavaDoc toIterator(final Enumeration JavaDoc obj) {
116         return new Iterator JavaDoc() {
117             public boolean hasNext() {
118                 return obj.hasMoreElements();
119             }
120             public Object JavaDoc next() {
121                 return obj.nextElement();
122             }
123             public void remove() {}
124         };
125     }
126 }
127
128
Popular Tags