View Javadoc

1   //========================================================================
2   //Copyright 2011-2012 Mort Bay Consulting Pty. Ltd.
3   //------------------------------------------------------------------------
4   //All rights reserved. This program and the accompanying materials
5   //are made available under the terms of the Eclipse Public License v1.0
6   //and Apache License v2.0 which accompanies this distribution.
7   //The Eclipse Public License is available at
8   //http://www.eclipse.org/legal/epl-v10.html
9   //The Apache License v2.0 is available at
10  //http://www.opensource.org/licenses/apache2.0.php
11  //You may elect to redistribute this code under either of these licenses.
12  //========================================================================
13  
14  package com.acme;
15  
16  import java.text.SimpleDateFormat;
17  import java.util.Date;
18  import java.util.TimeZone;
19  
20  import javax.servlet.jsp.JspException;
21  import javax.servlet.jsp.JspTagException;
22  import javax.servlet.jsp.PageContext;
23  import javax.servlet.jsp.tagext.BodyContent;
24  import javax.servlet.jsp.tagext.BodyTagSupport;
25  import javax.servlet.jsp.tagext.Tag;
26  
27  public class DateTag extends BodyTagSupport
28  {
29      Tag parent;
30      BodyContent body;
31      String tz="GMT";
32  
33      public void setParent(Tag parent) {this.parent=parent;}
34      public Tag getParent() {return parent;}
35      public void setBodyContent(BodyContent content) {body=content;}
36      public void setPageContext(PageContext pageContext) {}
37  
38      public void setTz(String value) {tz=value;}
39  
40      public int doStartTag() throws JspException {return EVAL_BODY_TAG;}
41      
42      public int doEndTag() throws JspException {return EVAL_PAGE;}
43  
44      public void doInitBody() throws JspException {}
45  
46      public int doAfterBody() throws JspException {
47  	try
48  	{
49              SimpleDateFormat format = new SimpleDateFormat(body.getString());
50              format.setTimeZone(TimeZone.getTimeZone(tz));
51  	    body.getEnclosingWriter().write(format.format(new Date()));
52  	    return SKIP_BODY;
53  	}
54  	catch (Exception ex) {
55              ex.printStackTrace();
56              throw new JspTagException(ex.toString());
57  	}
58      }
59  
60      public void release()
61      {
62  	body=null;
63      }
64  }
65