/* * based off of WOF's WORepetition, but only handles loops. * since the http://jakarta.apache.org/taglibs's FOR tag is, right now, not released. */ package net.toodarkpark.taglib.html; import java.util.*; // import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.TagSupport; import javax.servlet.jsp.tagext.BodyTagSupport; import org.apache.struts.util.PropertyUtils; import org.apache.struts.util.RequestUtils; import org.apache.struts.util.ResponseUtils; public class HSRepetitionTag extends BodyTagSupport { protected String endValueString = null; protected String startValueString = null; protected int currentValue = -1; protected String indexId = null; // protected int startValue = -1; protected int endValue = -1; public String getIndexId() { return indexId; } public void setIndexId( String value ) { indexId = value; } public String getEndValue() { return endValueString; } public void setEndValue( String value ) { endValueString = value; } public String getStartValue() { return startValueString; } public void setStartValue( String value ) { startValueString = value; } /** methods that actually do something important **/ public int doStartTag() throws JspException { // make sure that startValueString and endValueString are ints, not bean names try { startValue = Integer.parseInt( startValueString ); } catch( NumberFormatException nfe1 ) { // startValueString is a bean Integer startValueInt = (Integer)pageContext.findAttribute( startValueString ); if( startValueInt != null ) { startValue = startValueInt.intValue(); } } try { endValue = Integer.parseInt( endValueString ); } catch( NumberFormatException nfe2 ) { // endValueString is a bean Integer endValueInt = (Integer)pageContext.findAttribute( endValueString ); if( endValueInt != null ) { endValue = endValueInt.intValue(); } } // sanity checks -- make sure we've got valid start and end values. if( startValue == -1 ) { throw new JspException( "HSRepetitionTag: Start value cannot be unassigned." ); } if( endValue == -1 ) { throw new JspException( "HSRepetitionTag: End value cannot be unassigned." ); } if( startValue > endValue ) { throw new JspException( "HSRepetitionTag: Start value cannot be greater than the end value." ); } // set our currentValue to be our startValue currentValue = startValue; // we need to set this here so the FIRST body has access to indexId's value. if( this.getIndexId() != null ) { pageContext.setAttribute( this.getIndexId(), new Integer( currentValue ) ); } return( EVAL_BODY_TAG ); } // this method is called after the body of this tag has been processed public int doAfterBody() throws JspException { if( bodyContent != null ) { ResponseUtils.writePrevious( pageContext, bodyContent.getString() ); bodyContent.clearBody(); } currentValue++; // since we're incrementing currentValue, we have to wait until its PAST endValue to exit. if( currentValue > endValue ) { if( this.getIndexId() != null ) { pageContext.removeAttribute( this.getIndexId() ); } return( SKIP_BODY ); } else { if( this.getIndexId() != null ) { pageContext.setAttribute( this.getIndexId(), new Integer( currentValue ) ); } return( EVAL_BODY_TAG ); } } public int doEndTag() throws JspException { return EVAL_PAGE; } }