View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
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    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jelly.tags.fmt;
17  
18  import org.apache.commons.jelly.JellyContext;
19  import org.apache.commons.jelly.JellyTagException;
20  import org.apache.commons.jelly.XMLOutput;
21  import org.apache.commons.jelly.Tag;
22  import org.apache.commons.jelly.TagSupport;
23  import org.apache.commons.jelly.expression.Expression;
24  import java.util.Enumeration;
25  import java.util.Locale;
26  import java.util.ResourceBundle;
27  import java.util.MissingResourceException;
28  import java.util.TimeZone;
29  
30  /***
31   * Support for tag handlers for <timeZone>, the time zone loading
32   * tag in JSTL.
33   *
34   * @author <a href="mailto:willievu@yahoo.com">Willie Vu</a>
35   * @version $Revision: 155420 $
36   *
37   * @task decide how to implement setResponseLocale
38   */
39  public class TimeZoneTag extends TagSupport {
40  
41      private TimeZone timeZone;
42      private Expression value;                    // 'value' attribute
43  
44  
45      //**********************************************************************
46      // Constructor and initialization
47  
48      public TimeZoneTag() {
49      }
50  
51      //*********************************************************************
52      // Collaboration with subtags
53  
54      public TimeZone getTimeZone() {
55          return timeZone;
56      }
57  
58  
59      //*********************************************************************
60      // Tag logic
61  
62      /**
63       * Evaluates this tag after all the tags properties have been initialized.
64       *
65       */
66      public void doTag(XMLOutput output) throws JellyTagException {
67          Object valueInput = null;
68          if (this.value != null) {
69              valueInput = this.value.evaluate(context);
70          }
71  
72          if (valueInput == null) {
73              timeZone = TimeZone.getTimeZone("GMT");
74          }
75          else if (valueInput instanceof String) {
76              if (((String) valueInput).trim().equals("")) {
77                  timeZone = TimeZone.getTimeZone("GMT");
78              } else {
79                  timeZone = TimeZone.getTimeZone((String) valueInput);
80              }
81          } else {
82              timeZone = (TimeZone) valueInput;
83          }
84  
85          invokeBody(output);
86      }
87  
88  
89      //**********************************************************************
90      // Package-scoped utility methods
91  
92      /*
93       * Determines and returns the time zone to be used by the given action.
94       *
95       * <p> If the given action is nested inside a &lt;timeZone&gt; action,
96       * the time zone is taken from the enclosing &lt;timeZone&gt; action.
97       *
98       * <p> Otherwise, the time zone configuration setting
99       * <tt>javax.servlet.jsp.jstl.core.Config.FMT_TIME_ZONE</tt>
100      * is used.
101      *
102      * @param jc the page containing the action for which the
103      * time zone needs to be determined
104      * @param fromTag the action for which the time zone needs to be
105      * determined
106      *
107      * @return the time zone, or <tt>null</tt> if the given action is not
108      * nested inside a &lt;timeZone&gt; action and no time zone configuration
109      * setting exists
110      */
111     static TimeZone getTimeZone(JellyContext jc, Tag fromTag) {
112         TimeZone tz = null;
113 
114         Tag t = findAncestorWithClass(fromTag, TimeZoneTag.class);
115         if (t != null) {
116             // use time zone from parent <timeZone> tag
117             TimeZoneTag parent = (TimeZoneTag) t;
118             tz = parent.getTimeZone();
119         } else {
120             // get time zone from configuration setting
121             Object obj = jc.getVariable(Config.FMT_TIME_ZONE);
122             if (obj != null) {
123                 if (obj instanceof TimeZone) {
124                     tz = (TimeZone) obj;
125                 } else {
126                     tz = TimeZone.getTimeZone((String) obj);
127                 }
128             }
129         }
130 
131         return tz;
132     }
133 
134     /*** Setter for property value.
135      * @param value New value of property value.
136      *
137      */
138     public void setValue(Expression value) {
139         this.value = value;
140     }
141 }