InvoiceItem.java

/*
  Copyright 2001, Pajato Systems Group
  Copyright 2001, Allaire Corporation
*/

package allaire.samples.invoice.taglib;

import java.io.IOException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * The <inv:getItem> tag accesses an item provided by an Iterator
 * object.  The tag must be located inside an <inv:foreach> tag.
 */

public class InvoiceItem extends TagSupport {

    public int doStartTag() throws JspException, JspTagException {

	// Ignore any body content.
	int result = SKIP_BODY;

	// Validate that the tag is found in an <inv:foreach> tag.
	InvoiceIterator parent = (InvoiceIterator)
	    findAncestorWithClass( this, InvoiceIterator.class );
        if ( parent == null ) {

	    // It is not.  Throw an invoice exception.
	    String message =
		"The <inv:getItem> tag must be inside an <inv:foreach> tag.";
	    throw new JspTagException( message );
	}

	// Get the current item initialized by the <foreach> tag.
	String item = parent.getItem();

	// Output the item.
	try {
	    pageContext.getOut().print( item );
	} catch ( IOException exc ) {
	    exc.printStackTrace();
	    throw new JspException( exc.getMessage() );
	}

	return result;
    }
}