package examples;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import oracle.jsp.jml.JmlNumber;

public class ExampleLoopTag 
    extends TagSupport
{
    
    String index;
    int count;
    int i;
    JmlNumber ib;

    public ExampleLoopTag() {
      resetAttr();
      resetInternal();
    }

    public void release() {
      resetAttr();
      resetInternal();
    }

    private void resetAttr() {
      index=null;
      count=0;
    }

    private void resetInternal() {
      i=0;
      ib=null;
    }

    public void setIndex(String index)
    {
      this.index=index;
    }

    public void setCount(String count)
    {
      this.count=Integer.parseInt(count);
    }

    public int doStartTag() throws JspException {
      resetInternal();
      ib=new JmlNumber();
      pageContext.setAttribute(index, ib);
      i++;
      ib.setValue(i);
      return EVAL_BODY_INCLUDE;
    }

    public int doAfterBody() throws JspException {
      if (i >= count) {
        return SKIP_BODY;
      } else
        pageContext.setAttribute(index, ib);
        i++;
        ib.setValue(i);
        return EVAL_BODY_AGAIN;
    }

}

