KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > tag > common > fmt > TimeZoneSupport


1 /*
2  * Copyright 1999-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
17 package org.apache.taglibs.standard.tag.common.fmt;
18
19 import java.io.IOException JavaDoc;
20 import java.util.TimeZone JavaDoc;
21
22 import javax.servlet.jsp.JspException JavaDoc;
23 import javax.servlet.jsp.JspTagException JavaDoc;
24 import javax.servlet.jsp.PageContext JavaDoc;
25 import javax.servlet.jsp.jstl.core.Config;
26 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
27 import javax.servlet.jsp.tagext.Tag JavaDoc;
28
29 /**
30  * Support for tag handlers for <timeZone>, the time zone tag in
31  * JSTL 1.0.
32  *
33  * @author Jan Luehe
34  */

35
36 public abstract class TimeZoneSupport extends BodyTagSupport JavaDoc {
37
38
39     //*********************************************************************
40
// Protected state
41

42     protected Object JavaDoc value; // 'value' attribute
43

44
45     //*********************************************************************
46
// Private state
47

48     private TimeZone JavaDoc timeZone;
49
50
51     //*********************************************************************
52
// Constructor and initialization
53

54     public TimeZoneSupport() {
55     super();
56     init();
57     }
58
59     private void init() {
60     value = null;
61     }
62
63
64     //*********************************************************************
65
// Collaboration with subtags
66

67     public TimeZone JavaDoc getTimeZone() {
68     return timeZone;
69     }
70
71
72     //*********************************************************************
73
// Tag logic
74

75     public int doStartTag() throws JspException JavaDoc {
76
77     if (value == null) {
78         timeZone = TimeZone.getTimeZone("GMT");
79     } else if (value instanceof String JavaDoc) {
80         if (((String JavaDoc) value).trim().equals("")) {
81         timeZone = TimeZone.getTimeZone("GMT");
82         } else {
83         timeZone = TimeZone.getTimeZone((String JavaDoc) value);
84         }
85     } else {
86         timeZone = (TimeZone JavaDoc) value;
87     }
88
89     return EVAL_BODY_BUFFERED;
90     }
91
92     public int doEndTag() throws JspException JavaDoc {
93     try {
94         pageContext.getOut().print(bodyContent.getString());
95     } catch (IOException JavaDoc ioe) {
96         throw new JspTagException JavaDoc(ioe.toString(), ioe);
97     }
98
99     return EVAL_PAGE;
100     }
101
102     // Releases any resources we may have (or inherit)
103
public void release() {
104     init();
105     }
106
107
108     //*********************************************************************
109
// Package-scoped utility methods
110

111     /*
112      * Determines and returns the time zone to be used by the given action.
113      *
114      * <p> If the given action is nested inside a &lt;timeZone&gt; action,
115      * the time zone is taken from the enclosing &lt;timeZone&gt; action.
116      *
117      * <p> Otherwise, the time zone configuration setting
118      * <tt>javax.servlet.jsp.jstl.core.Config.FMT_TIME_ZONE</tt>
119      * is used.
120      *
121      * @param pageContext the page containing the action for which the
122      * time zone needs to be determined
123      * @param fromTag the action for which the time zone needs to be
124      * determined
125      *
126      * @return the time zone, or <tt>null</tt> if the given action is not
127      * nested inside a &lt;timeZone&gt; action and no time zone configuration
128      * setting exists
129      */

130     static TimeZone JavaDoc getTimeZone(PageContext JavaDoc pc, Tag JavaDoc fromTag) {
131     TimeZone JavaDoc tz = null;
132
133     Tag JavaDoc t = findAncestorWithClass(fromTag, TimeZoneSupport.class);
134     if (t != null) {
135         // use time zone from parent <timeZone> tag
136
TimeZoneSupport parent = (TimeZoneSupport) t;
137         tz = parent.getTimeZone();
138     } else {
139         // get time zone from configuration setting
140
Object JavaDoc obj = Config.find(pc, Config.FMT_TIME_ZONE);
141         if (obj != null) {
142         if (obj instanceof TimeZone JavaDoc) {
143             tz = (TimeZone JavaDoc) obj;
144         } else {
145             tz = TimeZone.getTimeZone((String JavaDoc) obj);
146         }
147         }
148     }
149
150     return tz;
151     }
152 }
153
Popular Tags