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: FormatTag.java,v $
31   * Revision 1.2  2005/04/09 18:04:19  colinmacleod
32   * Changed copyright text to GPL v2 explicitly.
33   *
34   * Revision 1.1  2005/01/19 12:58:20  colinmacleod
35   * Moved from ivata groupware.
36   *
37   * Revision 1.3  2004/03/21 21:16:09  colinmacleod
38   * Shortened name to ivata op.
39   *
40   * Revision 1.2  2004/02/01 22:00:34  colinmacleod
41   * Added full names to author tags
42   *
43   * Revision 1.1.1.1  2004/01/27 20:57:58  colinmacleod
44   * Moved ivata openportal to SourceForge..
45   *
46   * Revision 1.1.1.1  2003/10/13 20:50:13  colin
47   * Restructured portal into subprojects
48   *
49   * Revision 1.1  2003/02/25 08:16:44  colin
50   * moved to jsp category
51   *
52   * Revision 1.4  2003/02/04 17:43:51  colin
53   * copyright notice
54   *
55   * Revision 1.3  2002/06/21 12:03:55  colin
56   * new format tag library, interface to the com.ivata.groupware.web.format.*
57   * classes
58   * -----------------------------------------------------------------------------
59   */
60  package com.ivata.mask.web.tag.format;
61  
62  import java.io.IOException;
63  
64  import javax.servlet.jsp.JspException;
65  import javax.servlet.jsp.JspWriter;
66  import javax.servlet.jsp.tagext.BodyContent;
67  import javax.servlet.jsp.tagext.BodyTagSupport;
68  
69  import com.ivata.mask.web.format.HTMLFormatter;
70  
71  
72  /***
73   * <p>This class wraps the <code>HTMLFormatter</code> class, so that
74   * you can call {@link com.ivata.groupware.web.format.HTMLFormatter#format
75   * HTMLFormatter.format} as a JSP tag.</p>
76   * <p><b>Tag attributes:</b><br/>
77   * <table cellpadding='2' cellspacing='5' border='0' align='center'
78   * width='85%'>
79   *   <tr class='TableHeadingColor'>
80   *     <th>attribute</th>
81   *     <th>reqd.</th>
82   *     <th>param. class</th>
83   *     <th width='100%'>description</th>
84   *   </tr>
85   *   <tr class='TableRowColor'>
86   *     <td>formatter</td>
87   *     <td>false</td>
88   *     <td><code>com.ivata.html.format.HTMLFormatter</code></td>
89   *     <td>Sets the formatter which actually does the formatting.</td>
90   *   </tr>
91   * </table>
92   * </p>
93   *
94   * @since 2002-06-20
95   * @author Colin MacLeod
96   * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
97   * @version $Revision: 1.2 $
98   */
99  public class FormatTag extends BodyTagSupport {
100     /***
101      * <p>This is the object which actually does all the hard work. We just wrap
102      * <code>HTMLFormatter.format</code> in a JSP tag.</p>
103      */
104     private HTMLFormatter formatter = null;
105 
106     /***
107      * <p>Here the contents of the tag are formatted using the
108      * <code>HTMLFormatter</code> provided.</p>
109      * <p>This method is called after the JSP engine processes the body content
110      * of the tag.</p>
111      *
112      * @return <code>SKIP_BODY</code> since the JSP engine should not evaluate
113      * the tag body again.
114      * @throws JspException encapsulates any exception when calling
115      * <code>out.println</code>.
116      * writeTagBodyContent}.
117      *
118      */
119     public int doAfterBody() throws JspException {
120         try {
121             JspWriter out = getPreviousOut();
122             BodyContent bodyContent = getBodyContent();
123             out.print(formatter.format(bodyContent.getString()));
124         } catch (IOException ex) {
125             throw new JspException("ERROR in FormatTag: " + ex);
126         }
127 
128         // for this tag, we only want to evluate the body one time
129         return SKIP_BODY;
130     }
131 
132     /***
133      * <p>This method is called when the JSP engine encounters the start tag,
134      * after the attributes are processed.<p>
135      *
136      * <p>Scripting variables (if any) have their values set here.</p>
137      *
138      * @return <code>EVAL_BODY_BUFFERED</code> since the body is always
139      * evaluated once.
140      */
141     public int doStartTag() {
142         // nothing to do here, except note that we have a body...
143         return EVAL_BODY_BUFFERED;
144     }
145 
146     /***
147      * <p>Set the {@link com.ivata.groupware.web.format.HTMLFormatter
148      * HTMLFormatter}
149      * object which actually does all the hard work. We just wrap
150      * <code>HTMLFormatter.format</code> in a JSP tag.</p>
151      * @param formatterParam <code>HTMLFormatter</code> used to format the tag
152      * body.
153      */
154     public void setFormatter(final HTMLFormatter formatterParam) {
155         this.formatter = formatterParam;
156     }
157 }