View Javadoc

1   /*
2    * Copyright 1999-2004 The Apache Software Foundation.
3    * Modifications, Copyright 2005 Stephen Colebourne
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.joda.time.contrib.jsptag;
18  
19  import javax.servlet.jsp.JspException;
20  import javax.servlet.jsp.PageContext;
21  import javax.servlet.jsp.jstl.core.Config;
22  import javax.servlet.jsp.tagext.TagSupport;
23  
24  import org.joda.time.DateTimeZone;
25  
26  /**
27   * Support for tag handlers for <setDateTimeZone>.
28   * 
29   * @author Jan Luehe
30   * @author Jim Newsham
31   */
32  public abstract class SetDateTimeZoneSupport extends TagSupport {
33  
34      /** The value attribute. */
35      protected Object value;
36      /** The scope attribute. */
37      private int scope;
38      /** The var attribute. */
39      private String var;
40  
41      /**
42       * Constructor.
43       */
44      public SetDateTimeZoneSupport() {
45          super();
46          init();
47      }
48  
49      // resets local state
50      private void init() {
51          value = null;
52          var = null;
53          scope = PageContext.PAGE_SCOPE;
54      }
55  
56      public void setScope(String scope) {
57          this.scope = Util.getScope(scope);
58      }
59  
60      public void setVar(String var) {
61          this.var = var;
62      }
63  
64      public int doEndTag() throws JspException {
65          DateTimeZone dateTimeZone = null;
66          if (value == null) {
67              dateTimeZone = DateTimeZone.UTC;
68          } else if (value instanceof String) {
69              try {
70                  dateTimeZone = DateTimeZone.forID((String) value);
71              } catch (IllegalArgumentException iae) {
72                  dateTimeZone = DateTimeZone.UTC;
73              }
74          } else {
75              dateTimeZone = (DateTimeZone) value;
76          }
77  
78          if (var != null) {
79              pageContext.setAttribute(var, dateTimeZone, scope);
80          } else {
81              Config.set(pageContext, DateTimeZoneSupport.FMT_TIME_ZONE,
82                      dateTimeZone, scope);
83          }
84  
85          return EVAL_PAGE;
86      }
87  
88      // Releases any resources we may have (or inherit)
89      public void release() {
90          init();
91      }
92  
93  }