FileDocCategorySizeDatePackage
ForEachSupport.javaAPI DocGlassfish v2 API17776Sat May 05 19:17:52 BST 2007org.apache.taglibs.standard.tag.common.core

ForEachSupport

public abstract class ForEachSupport extends javax.servlet.jsp.jstl.core.LoopTagSupport

Support for tag handlers for <forEach>, the core iteration tag in JSTL 1.0. This class extends LoopTagSupport and provides ForEach-specific functionality. The rtexprvalue library and the expression-evaluating library each have handlers that extend this class.

Localized here is the logic for handling the veritable smorgasbord of types supported by <forEach>, including arrays, Collections, and others. To see how the actual iteration is controlled, review the javax.servlet.jsp.jstl.core.LoopTagSupport class instead.

see
javax.servlet.jsp.jstl.core.LoopTagSupport
author
Shawn Bayern

Fields Summary
protected ForEachIterator
items
protected Object
rawItems
Constructors Summary
Methods Summary
private org.apache.taglibs.standard.tag.common.core.ForEachSupport$ForEachIteratorbeginEndForEachIterator()

        /*
         * To plug into existing support, we need to keep 'begin', 'end',
         * and 'step' as they are.  So we'll simply create an Integer[]
         * from 0 to 'end', inclusive, and let the existing implementation
         * handle the subsetting and stepping operations.  (Other than
         * localizing the cost of creating this Integer[] to the start
         * of the operation instead of spreading it out over the lifetime
         * of the iteration, this implementation isn't worse than one that
         * created new Integers() as needed from next().  Such an adapter
         * to ForEachIterator could easily be written but, like I said,
         * wouldn't provide much benefit.)
         */
        Integer[] ia = new Integer[end + 1];
        for (int i = 0; i <= end; i++)
            ia[i] = Integer.valueOf(i);
        return new SimpleForEachIterator(Arrays.asList(ia).iterator());
    
protected booleanhasNext()

        return items.hasNext();
    
protected java.lang.Objectnext()

        return items.next();
    
protected voidprepare()

        // produce the right sort of ForEachIterator
        if (rawItems != null) {
            // If this is a deferred expression, make a note and get
            // the 'items' instance.
            if (rawItems instanceof ValueExpression) {
                deferredExpression = (ValueExpression) rawItems;
                rawItems = deferredExpression.getValue(
                               pageContext.getELContext());
                if (rawItems == null) {
                    // Simulate an empty list
                    rawItems = new ArrayList();
                }
            }
            // extract an iterator over the 'items' we've got
            items = supportedTypeForEachIterator(rawItems);
        } else {
            // no 'items', so use 'begin' and 'end'
            items = beginEndForEachIterator();
        }

        /* ResultSet no more supported in <c:forEach>
        // step must be 1 when ResultSet is passed in
        if (rawItems instanceof ResultSet && step != 1)
            throw new JspTagException(
		Resources.getMessage("FOREACH_STEP_NO_RESULTSET"));
        */
    
public voidrelease()

        super.release();
        items = null;
        rawItems = null;
        deferredExpression = null;
    
protected org.apache.taglibs.standard.tag.common.core.ForEachSupport$ForEachIteratorsupportedTypeForEachIterator(java.lang.Object o)


        /*
         * This is, of necessity, just a big, simple chain, matching in
         * order.  Since we are passed on Object because of all the
         * various types we support, we cannot rely on the language's
         * mechanism for resolving overloaded methods.  (Method overloading
         * resolves via early binding, so the type of the 'o' reference,
         * not the type of the eventual value that 'o' references, is
         * all that's available.)
         *
         * Currently, we 'match' on the object we have through an
         * if/else chain that picks the first interface (or class match)
         * found for an Object.
         */

        ForEachIterator items;

        if (o instanceof Object[])
            items = toForEachIterator((Object[]) o);
        else if (o instanceof boolean[])
            items = toForEachIterator((boolean[]) o);
        else if (o instanceof byte[])
            items = toForEachIterator((byte[]) o);
        else if (o instanceof char[])
            items = toForEachIterator((char[]) o);
        else if (o instanceof short[])
            items = toForEachIterator((short[]) o);
        else if (o instanceof int[])
            items = toForEachIterator((int[]) o);
        else if (o instanceof long[])
            items = toForEachIterator((long[]) o);
        else if (o instanceof float[])
            items = toForEachIterator((float[]) o);
        else if (o instanceof double[])
            items = toForEachIterator((double[]) o);
        else if (o instanceof Collection)
            items = toForEachIterator((Collection) o);
        else if (o instanceof Iterator)
            items = toForEachIterator((Iterator) o);
        else if (o instanceof Enumeration)
            items = toForEachIterator((Enumeration) o);
        else if (o instanceof Map)
            items = toForEachIterator((Map) o);
        /*
        else if (o instanceof ResultSet)
            items = toForEachIterator((ResultSet) o);
        */
        else if (o instanceof String)
            items = toForEachIterator((String) o);
        else
            items = toForEachIterator(o);

        return (items);
    
protected org.apache.taglibs.standard.tag.common.core.ForEachSupport$ForEachIteratortoForEachIterator(byte[] a)

        Byte[] wrapped = new Byte[a.length];
        for (int i = 0; i < a.length; i++)
            wrapped[i] = Byte.valueOf(a[i]);
        return new SimpleForEachIterator(Arrays.asList(wrapped).iterator());
    
protected org.apache.taglibs.standard.tag.common.core.ForEachSupport$ForEachIteratortoForEachIterator(char[] a)

        Character[] wrapped = new Character[a.length];
        for (int i = 0; i < a.length; i++)
            wrapped[i] = Character.valueOf(a[i]);
        return new SimpleForEachIterator(Arrays.asList(wrapped).iterator());
    
protected org.apache.taglibs.standard.tag.common.core.ForEachSupport$ForEachIteratortoForEachIterator(short[] a)

        Short[] wrapped = new Short[a.length];
        for (int i = 0; i < a.length; i++)
            wrapped[i] = Short.valueOf(a[i]);
        return new SimpleForEachIterator(Arrays.asList(wrapped).iterator());
    
protected org.apache.taglibs.standard.tag.common.core.ForEachSupport$ForEachIteratortoForEachIterator(int[] a)

        Integer[] wrapped = new Integer[a.length];
        for (int i = 0; i < a.length; i++)
            wrapped[i] = Integer.valueOf(a[i]);
        return new SimpleForEachIterator(Arrays.asList(wrapped).iterator());
    
protected org.apache.taglibs.standard.tag.common.core.ForEachSupport$ForEachIteratortoForEachIterator(long[] a)

        Long[] wrapped = new Long[a.length];
        for (int i = 0; i < a.length; i++)
            wrapped[i] = Long.valueOf(a[i]);
        return new SimpleForEachIterator(Arrays.asList(wrapped).iterator());
    
protected org.apache.taglibs.standard.tag.common.core.ForEachSupport$ForEachIteratortoForEachIterator(float[] a)

        Float[] wrapped = new Float[a.length];
        for (int i = 0; i < a.length; i++)
            wrapped[i] = new Float(a[i]);
        return new SimpleForEachIterator(Arrays.asList(wrapped).iterator());
    
protected org.apache.taglibs.standard.tag.common.core.ForEachSupport$ForEachIteratortoForEachIterator(double[] a)

        Double[] wrapped = new Double[a.length];
        for (int i = 0; i < a.length; i++)
            wrapped[i] = new Double(a[i]);
        return new SimpleForEachIterator(Arrays.asList(wrapped).iterator());
    
protected org.apache.taglibs.standard.tag.common.core.ForEachSupport$ForEachIteratortoForEachIterator(java.util.Collection c)

        return new SimpleForEachIterator(c.iterator());
    
protected org.apache.taglibs.standard.tag.common.core.ForEachSupport$ForEachIteratortoForEachIterator(java.util.Iterator i)

        return new SimpleForEachIterator(i);
    
protected org.apache.taglibs.standard.tag.common.core.ForEachSupport$ForEachIteratortoForEachIterator(java.util.Enumeration e)


        // local adapter
        class EnumerationAdapter implements ForEachIterator {
            private Enumeration e;
            public EnumerationAdapter(Enumeration e) {
                this.e = e;
            }
            public boolean hasNext() {
                return e.hasMoreElements();
            }
            public Object next() {
                return e.nextElement();
            }
        }

        return new EnumerationAdapter(e);
    
protected org.apache.taglibs.standard.tag.common.core.ForEachSupport$ForEachIteratortoForEachIterator(java.util.Map m)

        return new SimpleForEachIterator(m.entrySet().iterator());
    
protected org.apache.taglibs.standard.tag.common.core.ForEachSupport$ForEachIteratortoForEachIterator(java.lang.String s)

        StringTokenizer st = new StringTokenizer(s, ",");
        return toForEachIterator(st);           // convert from Enumeration
    
protected org.apache.taglibs.standard.tag.common.core.ForEachSupport$ForEachIteratortoForEachIterator(java.lang.Object o)

        throw new JspTagException(Resources.getMessage("FOREACH_BAD_ITEMS"));
    
protected org.apache.taglibs.standard.tag.common.core.ForEachSupport$ForEachIteratortoForEachIterator(java.lang.Object[] a)

        return new SimpleForEachIterator(Arrays.asList(a).iterator());
    
protected org.apache.taglibs.standard.tag.common.core.ForEachSupport$ForEachIteratortoForEachIterator(boolean[] a)

        Boolean[] wrapped = new Boolean[a.length];
        for (int i = 0; i < a.length; i++)
            wrapped[i] = Boolean.valueOf(a[i]);
        return new SimpleForEachIterator(Arrays.asList(wrapped).iterator());