View Javadoc

1   /*
2    * Copyright (c) 2001 - 2005 ivata limited.
3    * All rights reserved.
4    * -----------------------------------------------------------------------------
5    * ivata masks may be redistributed under the GNU General Public
6    * License as published by the Free Software Foundation;
7    * version 2 of the License.
8    *
9    * These programs are free software; you can redistribute them and/or
10   * modify them under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; version 2 of the License.
12   *
13   * These programs are distributed in the hope that they will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16   *
17   * See the GNU General Public License in the file LICENSE.txt for more
18   * details.
19   *
20   * If you would like a copy of the GNU General Public License write to
21   *
22   * Free Software Foundation, Inc.
23   * 59 Temple Place - Suite 330
24   * Boston, MA 02111-1307, USA.
25   *
26   *
27   * To arrange commercial support and licensing, contact ivata at
28   *                  http://www.ivata.com/contact.jsp
29   * -----------------------------------------------------------------------------
30   * $Log: BufferAppendTag.java,v $
31   * Revision 1.1  2005/04/26 15:05:32  colinmacleod
32   * Added string buffer append tag.
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 }