InvoiceElse.java
package allaire.samples.invoice.taglib;
import java.io.IOException;
import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class InvoiceElse extends BodyTagSupport {
public int doStartTag() throws JspTagException {
int result = SKIP_BODY;
String message;
InvoiceIf parent = (InvoiceIf)
findAncestorWithClass( this, InvoiceIf.class );
if ( parent == null ) {
message = "The else tag must be inside an if tag.";
throw new JspTagException( message );
} else if ( !parent.hasCondition() ) {
message = "The else tag must FOLLOW a condition tag.";
throw new JspTagException( message );
}
if ( !parent.getCondition() ) {
result = EVAL_BODY_TAG;
}
return result;
}
public int doAfterBody() throws JspException {
InvoiceIf parent = (InvoiceIf)
findAncestorWithClass( this, InvoiceIf.class );
if ( !parent.getCondition() ) {
try {
BodyContent body = getBodyContent();
JspWriter out = body.getEnclosingWriter();
out.print( body.getString() );
} catch (IOException exc) {
throw new JspException( exc.getMessage() );
}
}
return SKIP_BODY;
}
}