View Javadoc

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