001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Portions Copyright Apache Software Foundation.
007: *
008: * The contents of this file are subject to the terms of either the GNU
009: * General Public License Version 2 only ("GPL") or the Common Development
010: * and Distribution License("CDDL") (collectively, the "License"). You
011: * may not use this file except in compliance with the License. You can obtain
012: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
013: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
014: * language governing permissions and limitations under the License.
015: *
016: * When distributing the software, include this License Header Notice in each
017: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
018: * Sun designates this particular file as subject to the "Classpath" exception
019: * as provided by Sun in the GPL Version 2 section of the License file that
020: * accompanied this code. If applicable, add the following below the License
021: * Header, with the fields enclosed by brackets [] replaced by your own
022: * identifying information: "Portions Copyrighted [year]
023: * [name of copyright owner]"
024: *
025: * Contributor(s):
026: *
027: * If you wish your version of this file to be governed by only the CDDL or
028: * only the GPL Version 2, indicate your decision by adding "[Contributor]
029: * elects to include this software in this distribution under the [CDDL or GPL
030: * Version 2] license." If you don't indicate a single choice of license, a
031: * recipient has the option to distribute your version of this file under
032: * either the CDDL, the GPL Version 2 or to extend the choice of license to
033: * its licensees as provided above. However, if you add GPL Version 2 code
034: * and therefore, elected the GPL Version 2 license, then the option applies
035: * only if the new code is made subject to such option by the copyright
036: * holder.
037: */
038:
039: package org.apache.taglibs.standard.tag.common.fmt;
040:
041: import java.io.IOException;
042: import java.util.TimeZone;
043:
044: import javax.servlet.jsp.JspException;
045: import javax.servlet.jsp.JspTagException;
046: import javax.servlet.jsp.PageContext;
047: import javax.servlet.jsp.jstl.core.Config;
048: import javax.servlet.jsp.tagext.BodyTagSupport;
049: import javax.servlet.jsp.tagext.Tag;
050:
051: /**
052: * Support for tag handlers for <timeZone>, the time zone tag in
053: * JSTL 1.0.
054: *
055: * @author Jan Luehe
056: */
057:
058: public abstract class TimeZoneSupport extends BodyTagSupport {
059:
060: //*********************************************************************
061: // Protected state
062:
063: protected Object value; // 'value' attribute
064:
065: //*********************************************************************
066: // Private state
067:
068: private TimeZone timeZone;
069:
070: //*********************************************************************
071: // Constructor and initialization
072:
073: public TimeZoneSupport() {
074: super ();
075: init();
076: }
077:
078: private void init() {
079: value = null;
080: }
081:
082: //*********************************************************************
083: // Collaboration with subtags
084:
085: public TimeZone getTimeZone() {
086: return timeZone;
087: }
088:
089: //*********************************************************************
090: // Tag logic
091:
092: public int doStartTag() throws JspException {
093:
094: if (value == null) {
095: timeZone = TimeZone.getTimeZone("GMT");
096: } else if (value instanceof String) {
097: if (((String) value).trim().equals("")) {
098: timeZone = TimeZone.getTimeZone("GMT");
099: } else {
100: timeZone = TimeZone.getTimeZone((String) value);
101: }
102: } else {
103: timeZone = (TimeZone) value;
104: }
105:
106: return EVAL_BODY_BUFFERED;
107: }
108:
109: public int doEndTag() throws JspException {
110: try {
111: pageContext.getOut().print(bodyContent.getString());
112: } catch (IOException ioe) {
113: throw new JspTagException(ioe.toString(), ioe);
114: }
115:
116: return EVAL_PAGE;
117: }
118:
119: // Releases any resources we may have (or inherit)
120: public void release() {
121: init();
122: }
123:
124: //*********************************************************************
125: // Package-scoped utility methods
126:
127: /*
128: * Determines and returns the time zone to be used by the given action.
129: *
130: * <p> If the given action is nested inside a <timeZone> action,
131: * the time zone is taken from the enclosing <timeZone> action.
132: *
133: * <p> Otherwise, the time zone configuration setting
134: * <tt>javax.servlet.jsp.jstl.core.Config.FMT_TIME_ZONE</tt>
135: * is used.
136: *
137: * @param pageContext the page containing the action for which the
138: * time zone needs to be determined
139: * @param fromTag the action for which the time zone needs to be
140: * determined
141: *
142: * @return the time zone, or <tt>null</tt> if the given action is not
143: * nested inside a <timeZone> action and no time zone configuration
144: * setting exists
145: */
146: static TimeZone getTimeZone(PageContext pc, Tag fromTag) {
147: TimeZone tz = null;
148:
149: Tag t = findAncestorWithClass(fromTag, TimeZoneSupport.class);
150: if (t != null) {
151: // use time zone from parent <timeZone> tag
152: TimeZoneSupport parent = (TimeZoneSupport) t;
153: tz = parent.getTimeZone();
154: } else {
155: // get time zone from configuration setting
156: Object obj = Config.find(pc, Config.FMT_TIME_ZONE);
157: if (obj != null) {
158: if (obj instanceof TimeZone) {
159: tz = (TimeZone) obj;
160: } else {
161: tz = TimeZone.getTimeZone((String) obj);
162: }
163: }
164: }
165:
166: return tz;
167: }
168: }
|