package oracle.taglib;

import java.util.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

/**
 * IterateTag extends TagSupport. The TagSupport class in JSP 1.2 implements the IterationTag 
 */

public class IterateTag extends TagSupport 
{
  private Iterator iterator;
  private Collection _collection;

  public void setCollection(Collection collection) 
  {
        this._collection = collection;
  }

  public int doStartTag() throws JspTagException 
  {
    if (_collection == null) 
    {
      throw new JspTagException("No collection with name "
        + _collection
        + " found");
    }
   
    iterator = _collection.iterator();
    if (iterator.hasNext()) 
    {
      pageContext.setAttribute("item", iterator.next());
      return EVAL_BODY_INCLUDE;
    }
    else
    {
       return SKIP_BODY;
    } 
  }

  public int doAfterBody() 
  {
     
     if (iterator.hasNext()) 
     {
      pageContext.setAttribute("item", iterator.next());
      return EVAL_BODY_AGAIN;
     }
     else 
     {
      return SKIP_BODY;
     }
  }

}