View Javadoc

    1   //========================================================================
    2   //Copyright 2004-2008 Mort Bay Consulting Pty. Ltd.
    3   //------------------------------------------------------------------------
    4   //Licensed under the Apache License, Version 2.0 (the "License");
    5   //you may not use this file except in compliance with the License.
    6   //You may obtain a copy of the License at
    7   //http://www.apache.org/licenses/LICENSE-2.0
    8   //Unless required by applicable law or agreed to in writing, software
    9   //distributed under the License is distributed on an "AS IS" BASIS,
    10  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  //See the License for the specific language governing permissions and
    12  //limitations under the License.
    13  //========================================================================
    14
    15  package com.acme;
    16
    17  import java.text.SimpleDateFormat;
    18  import java.util.Date;
    19  import java.util.TimeZone;
    20
    21  import javax.servlet.jsp.JspException;
    22  import javax.servlet.jsp.JspTagException;
    23  import javax.servlet.jsp.PageContext;
    24  import javax.servlet.jsp.tagext.BodyContent;
    25  import javax.servlet.jsp.tagext.BodyTagSupport;
    26  import javax.servlet.jsp.tagext.Tag;
    27
    28  public class DateTag extends BodyTagSupport
    29  {
    30      Tag parent;
    31      BodyContent body;
    32      String tz="GMT";
    33
    34      public void setParent(Tag parent) {this.parent=parent;}
    35      public Tag getParent() {return parent;}
    36      public void setBodyContent(BodyContent content) {body=content;}
    37      public void setPageContext(PageContext pageContext) {}
    38
    39      public void setTz(String value) {tz=value;}
    40
    41      public int doStartTag() throws JspException {return EVAL_BODY_TAG;}
    42
    43      public int doEndTag() throws JspException {return EVAL_PAGE;}
    44
    45      public void doInitBody() throws JspException {}
    46
    47      public int doAfterBody() throws JspException {
    48  	try
    49  	{
    50              SimpleDateFormat format = new SimpleDateFormat(body.getString());
    51              format.setTimeZone(TimeZone.getTimeZone(tz));
    52  	    body.getEnclosingWriter().write(format.format(new Date()));
    53  	    return SKIP_BODY;
    54  	}
    55  	catch (Exception ex) {
    56              ex.printStackTrace();
    57              throw new JspTagException(ex.toString());
    58  	}
    59      }
    60
    61      public void release()
    62      {
    63  	body=null;
    64      }
    65  }
    66