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: FormatDateTag.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.4  2004/07/13 19:41:15  colinmacleod
38   * Moved project to POJOs from EJBs.
39   * Applied PicoContainer to services layer (replacing session EJBs).
40   * Applied Hibernate to persistence layer (replacing entity EJBs).
41   *
42   * Revision 1.3  2004/03/21 21:16:09  colinmacleod
43   * Shortened name to ivata op.
44   *
45   * Revision 1.2  2004/02/01 22:00:34  colinmacleod
46   * Added full names to author tags
47   *
48   * Revision 1.1.1.1  2004/01/27 20:57:58  colinmacleod
49   * Moved ivata openportal to SourceForge..
50   * -----------------------------------------------------------------------------
51   */
52  package com.ivata.mask.web.tag.format;
53  
54  import java.io.IOException;
55  import java.util.Date;
56  
57  import javax.servlet.jsp.JspException;
58  import javax.servlet.jsp.JspWriter;
59  import javax.servlet.jsp.tagext.TagSupport;
60  
61  import com.ivata.mask.web.format.DateFormatter;
62  import com.ivata.mask.web.format.DateFormatterException;
63  
64  
65  /***
66   * <p>This class wraps the <code>DateFormatter</code> class, so that
67   * you can call {@link com.ivata.groupware.util.DateFormatter#format
68   * DateFormatter.format} as a JSP tag.</p>
69   * <p><b>Tag attributes:</b><br/>
70   * <table cellpadding='2' cellspacing='5' border='0' align='center'
71   * width='85%'>
72   *   <tr class='TableHeadingColor'>
73   *     <th>attribute</th>
74   *     <th>reqd.</th>
75   *     <th>param. class</th>
76   *     <th width='100%'>description</th>
77   *   </tr>
78   *   <tr class='TableRowColor'>
79   *     <td>date</td>
80   *     <td>true</td>
81   *     <td><code>com.ivata.util.DateFormatter</code></td>
82   *     <td>Sets the date to be formatted into a string.</td>
83   *   </tr>
84   *   <tr class='TableRowColor'>
85   *     <td>formatter</td>
86   *     <td>true</td>
87   *     <td><code>com.ivata.util.DateFormatter</code></td>
88   *     <td>Sets the formatter which actually does the formatting.</td>
89   *   </tr>
90   * </table>
91   * </p>
92   *
93   * @since ivata masks 0.5 (2002-06-22)
94   * @author Colin MacLeod
95   * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
96   * @version $Revision: 1.2 $
97   */
98  public class FormatDateTag extends TagSupport {
99      /***
100      * <p>This date will be formatted into a string when this tag is used.</p>
101      */
102     private Date date;
103 
104     /***
105      * <p>
106      * This date formatter will do all the hard work.
107      * </p>
108      */
109     private DateFormatter formatter;
110 
111     /***
112      * <p>This method is called when the JSP engine encounters the start tag,
113      * after the attributes are processed.<p>
114      *
115      * <p>Scripting variables (if any) have their values set here.</p>
116      *
117      * @exception JspException if any of the attributes have invalid values.
118      * @exception JspException if there is any excpetion calling
119      * <code>out.print</code>.
120      * @return <code>SKIP_BODY</code> since this tag has no body.
121      */
122     public int doStartTag() throws JspException {
123         // check what we got
124         assert (formatter != null);
125         assert (date != null);
126 
127         JspWriter out = pageContext.getOut();
128 
129         try {
130             out.print(formatter.format(date));
131         } catch (DateFormatterException e) {
132             throw new RuntimeException(e);
133         } catch (IOException e) {
134             throw new RuntimeException(e);
135         }
136 
137         // singin' >>I don't have no body...<<
138         return SKIP_BODY;
139     }
140 
141     /***
142      * <p>Get the date which will be formatted into a string when this tag is
143      * used.</p>
144      *
145      * @return the current value of the date to be formatted into a string.
146      */
147     public final Date getDate() {
148         return date;
149     }
150     /***
151      * <p>
152      * This date formatter will do all the hard work.
153      * </p>
154      *
155      * @return current value of dateFormatter.
156      */
157     public final DateFormatter getFormatter() {
158         return formatter;
159     }
160 
161     /***
162      * <p>Set the date which will be formatted into a string when this tag is
163      * used.</p>
164      *
165      * @param dateParam the new value of the date to be formatted into a string.
166      */
167     public final void setDate(final Date dateParam) {
168         this.date = dateParam;
169     }
170     /***
171      * <p>
172      * This date formatter will do all the hard work.
173      * </p>
174      *
175      * @param formatterParam new value of date formatter.
176      */
177     public final void setFormatter(final DateFormatter formatterParam) {
178         this.formatter = formatterParam;
179     }
180 }