InvoiceCondition.java

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

package allaire.samples.invoice.taglib;

import java.io.IOException;
import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;

/**
 * An <inv:condition> tag will evaluate its body and store the result
 * as a boolean in the paretn tag.  The parent tag must be a <inv:if>
 * tag.
 */

public class InvoiceCondition extends BodyTagSupport {

    /**
     * Simply validate that the required tag structure is being
     * employed.
     */
    public int doStartTag() throws JspTagException {

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

	    // It is not.  Throw an invoice exception.
	    String message = 
		"The condition tag must be inside an if tag.";
	    throw new JspTagException( message );
	}

	return EVAL_BODY_TAG;
    }

    /**
     * Get the condition from the body expression and update the
     * parent's condition state.
     */
    public int doAfterBody() {

	// Validate that the parent is in <inv:if> tag and get the
	// body content.
	InvoiceIf parent = (InvoiceIf)
	    findAncestorWithClass( this, InvoiceIf.class );
	String bodyString = getBodyContent().getString();

	// Set the condition attribute in the parent tag.
	if ( bodyString.trim().equals( "true" ) ) {
	    parent.setCondition( true );
	} else {
	    parent.setCondition( false );
	}

	return SKIP_BODY;
    }
}