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 java.io.IOException;
20
21 import javax.servlet.jsp.JspException;
22 import javax.servlet.jsp.JspTagException;
23 import javax.servlet.jsp.PageContext;
24 import javax.servlet.jsp.jstl.core.Config;
25 import javax.servlet.jsp.tagext.BodyTagSupport;
26 import javax.servlet.jsp.tagext.Tag;
27
28 import org.joda.time.DateTimeZone;
29
30 /**
31 * Support for tag handlers for <timeZone>.
32 *
33 * @author Jan Luehe
34 * @author Jim Newsham
35 */
36 public abstract class DateTimeZoneSupport extends BodyTagSupport {
37
38 /** The config key for the time zone. */
39 public static final String FMT_TIME_ZONE = "org.joda.time.dateTimeZone";
40
41 /** The value attribute. */
42 protected Object value;
43
44 /** The zone. */
45 private DateTimeZone dateTimeZone;
46
47 /**
48 * Constructor.
49 */
50 public DateTimeZoneSupport() {
51 super();
52 init();
53 }
54
55 private void init() {
56 value = null;
57 }
58
59 public DateTimeZone getDateTimeZone() {
60 return dateTimeZone;
61 }
62
63 public int doStartTag() throws JspException {
64 if (value == null) {
65 dateTimeZone = DateTimeZone.UTC;
66 } else if (value instanceof String) {
67 try {
68 dateTimeZone = DateTimeZone.forID((String) value);
69 } catch (IllegalArgumentException iae) {
70 dateTimeZone = DateTimeZone.UTC;
71 }
72 } else {
73 dateTimeZone = (DateTimeZone) value;
74 }
75 return EVAL_BODY_BUFFERED;
76 }
77
78 public int doEndTag() throws JspException {
79 try {
80 pageContext.getOut().print(bodyContent.getString());
81 } catch (IOException ioe) {
82 throw new JspTagException(ioe.toString(), ioe);
83 }
84 return EVAL_PAGE;
85 }
86
87 // Releases any resources we may have (or inherit)
88 public void release() {
89 init();
90 }
91
92 /**
93 * Determines and returns the time zone to be used by the given action.
94 * <p>
95 * If the given action is nested inside a <dateTimeZone> action,
96 * the time zone is taken from the enclosing <dateTimeZone> action.
97 * <p>
98 * Otherwise, the time zone configuration setting
99 * <tt>org.joda.time.FMT_TIME_ZONE</tt> is used.
100 *
101 * @param pc the page containing the action for which the time zone
102 * needs to be determined
103 * @param fromTag the action for which the time zone needs to be determined
104 *
105 * @return the time zone, or <tt> null </tt> if the given action is not
106 * nested inside a <dateTimeZone> action and no time zone configuration
107 * setting exists
108 */
109 static DateTimeZone getDateTimeZone(PageContext pc, Tag fromTag) {
110 DateTimeZone tz = null;
111
112 Tag t = findAncestorWithClass(fromTag, DateTimeZoneSupport.class);
113 if (t != null) {
114 // use time zone from parent <timeZone> tag
115 DateTimeZoneSupport parent = (DateTimeZoneSupport) t;
116 tz = parent.getDateTimeZone();
117 } else {
118 // get time zone from configuration setting
119 Object obj = Config.find(pc, FMT_TIME_ZONE);
120 if (obj != null) {
121 if (obj instanceof DateTimeZone) {
122 tz = (DateTimeZone) obj;
123 } else {
124 try {
125 tz = DateTimeZone.forID((String) obj);
126 } catch (IllegalArgumentException iae) {
127 tz = DateTimeZone.UTC;
128 }
129 }
130 }
131 }
132
133 return tz;
134 }
135
136 }