1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 package com.ivata.mask.web.tag.util;
37
38 import javax.servlet.jsp.JspException;
39 import javax.servlet.jsp.JspWriter;
40 import javax.servlet.jsp.tagext.BodyContent;
41 import javax.servlet.jsp.tagext.BodyTagSupport;
42
43 import org.apache.log4j.Logger;
44
45
46 /***
47 * Appends the JSP contents to a <code>StringBuffer</code>.
48 *
49 * @since ivata masks 0.6 (2005-04-26)
50 * @author Colin MacLeod
51 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
52 * @version $Revision: 1.1 $
53 */
54 public class BufferAppendTag extends BodyTagSupport {
55 /***
56 * Logger for this class.
57 */
58 private static final Logger logger = Logger
59 .getLogger(BufferAppendTag.class);
60
61 /***
62 * Refer to {@link #getBufferName}.
63 */
64 private String bufferName = null;
65 /***
66 * Stores the value of the current page.
67 */
68 private String value = null;
69
70 /***
71 * <p>This method is called after the JSP engine processes the body
72 * content of the tag.</p>
73 * @exception JspException encapsulates any exception when calling
74 * {@link #writeTagBodyContent writeTagBodyContent}.
75 * @return <code>SKIP_BODY</code> since we only ever want to evaluate
76 * this tag once.
77 * @see #writeTagBodyContent( JspWriter out, BodyContent bodyContent
78 * )
79 *
80 */
81 public int doAfterBody() throws JspException {
82 if (getBodyContent() != null) {
83 value = getBodyContent().getString();
84 } else {
85 value = null;
86 }
87 return SKIP_BODY;
88 }
89
90 /***
91 * <p>This method is called after the JSP engine finished processing
92 * the tag.</p>
93 *
94 * @exception JspException encapsulates any exception when calling
95 * <code>out.println</code>
96 * @return <code>EVAL_PAGE</code> since we always want to evaluate
97 * the page after this tag.
98 */
99 public int doEndTag() throws JspException {
100 StringBuffer buffer = (StringBuffer) pageContext
101 .findAttribute(bufferName);
102 if (buffer == null) {
103 throw new JspException("Cannot find buffer called '"
104 + bufferName
105 + "' at any scope.");
106 }
107
108 if (value != null) {
109 buffer.append(value);
110 }
111 return EVAL_PAGE;
112 }
113
114 /***
115 * <p>
116 * Use this attribute to specify a <code>java.lang.StringBuffer</code>
117 * instance in the current page context.
118 * </p>
119 *
120 * @return the current value of mapName.
121 */
122 public final String getBufferName() {
123 return bufferName;
124 }
125 /***
126 * Refer to {@link #getBufferName}.
127 * @param bufferNameParam Refer to {@link #getBufferName}.
128 */
129 public void setBufferName(String bufferNameParam) {
130 if (logger.isDebugEnabled()) {
131 logger.debug("Setting bufferName. Before '" + bufferName
132 + "', after '" + bufferNameParam + "'");
133 }
134 bufferName = bufferNameParam;
135 }
136 }