001: package com.mockrunner.example.tag;
002:
003: import java.io.IOException;
004: import java.util.Collection;
005: import java.util.Iterator;
006:
007: import javax.servlet.jsp.JspException;
008: import javax.servlet.jsp.JspWriter;
009: import javax.servlet.jsp.PageContext;
010: import javax.servlet.jsp.tagext.BodyTagSupport;
011:
012: /**
013: * This example tag creates an HTML table with multiple rows. It takes
014: * a <code>Collection</code> from the session (attribute key is
015: * <i>currentCollection</i>) and iterates through the elements.
016: * The elements are stored in the request (attribute key is <i>currentObject</i>).
017: * The body of the tag will be evaluated multiple times, and if it's not empty,
018: * an HTML row will be created.
019: * A Struts <i>bean:write</i> or JSTL<i>c:out</i>tag can be used to write the
020: * elements from the <code>Collection</code> as a string to the page.
021: * If you store a <code>List</code> with one entry <i>Entry1</i> in the session
022: * and specify the following JSP code (<i>enumtag</i> is our tag)
023: *
024: * <pre>
025: * <mytags:enumtag label="myLabel">
026: * <bean:write scope="request" name="currentObject"/>
027: * </mytags:enumtag>
028: * </pre>
029: *
030: * then the html output will be
031: *
032: * <pre>
033: * <table>
034: * <tr>
035: * <td>
036: * myLabel
037: * </td>
038: * <td>
039: * Entry1
040: * </td>
041: * </tr>
042: * </table>
043: * </pre>
044: *
045: * Note: The <i>bean:write</i> tag is the common Struts tag.
046: * Check out {@link TableEnumTagTest} to see how to write a test for
047: * this tag.
048: */
049: public class TableEnumTag extends BodyTagSupport {
050: private String label;
051: private Iterator iterator;
052:
053: public void setLabel(String label) {
054: this .label = label;
055: }
056:
057: public void release() {
058: super .release();
059: label = null;
060: iterator = null;
061: }
062:
063: private void copyNextObjectToRequest() {
064: Object nextAttribute = iterator.next();
065: pageContext.setAttribute("currentObject", nextAttribute,
066: PageContext.REQUEST_SCOPE);
067: }
068:
069: public int doStartTag() throws JspException {
070: try {
071: Collection col = (Collection) pageContext.getAttribute(
072: "currentCollection", PageContext.SESSION_SCOPE);
073: iterator = col.iterator();
074: if (iterator.hasNext()) {
075: pageContext.getOut().println("<table>");
076: copyNextObjectToRequest();
077: return EVAL_BODY_BUFFERED;
078: }
079: return SKIP_BODY;
080: } catch (IOException exc) {
081: throw new JspException(exc.getMessage());
082: }
083: }
084:
085: public int doAfterBody() throws JspException {
086: String bodyString = getBodyContent().getString();
087: JspWriter out = getBodyContent().getEnclosingWriter();
088: try {
089: if (null != bodyString && bodyString.length() != 0) {
090: out.println("<tr>");
091: out.println("\t<td>");
092: out.println("\t\t" + label);
093: out.println("\t</td>");
094: out.println("\t<td>");
095: out.println("\t\t" + bodyString);
096: out.println("\t</td>");
097: out.println("</tr>");
098: }
099: if (iterator.hasNext()) {
100: copyNextObjectToRequest();
101: getBodyContent().clear();
102: return EVAL_BODY_AGAIN;
103: }
104: out.println("</table>");
105: return SKIP_BODY;
106: } catch (IOException exc) {
107: throw new JspException(exc.getMessage());
108: }
109: }
110:
111: public int doEndTag() throws JspException {
112: return EVAL_PAGE;
113: }
114: }
|