001    /*
002     * Copyright 1999-2004 The Apache Software Foundation.
003     * Modifications, Copyright 2005 Stephen Colebourne
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.joda.time.contrib.jsptag;
018    
019    import javax.servlet.jsp.JspException;
020    import javax.servlet.jsp.PageContext;
021    import javax.servlet.jsp.jstl.core.Config;
022    import javax.servlet.jsp.tagext.TagSupport;
023    
024    import org.joda.time.DateTimeZone;
025    
026    /**
027     * Support for tag handlers for <setDateTimeZone>.
028     * 
029     * @author Jan Luehe
030     * @author Jim Newsham
031     */
032    public abstract class SetDateTimeZoneSupport extends TagSupport {
033    
034        /** The value attribute. */
035        protected Object value;
036        /** The scope attribute. */
037        private int scope;
038        /** The var attribute. */
039        private String var;
040    
041        /**
042         * Constructor.
043         */
044        public SetDateTimeZoneSupport() {
045            super();
046            init();
047        }
048    
049        // resets local state
050        private void init() {
051            value = null;
052            var = null;
053            scope = PageContext.PAGE_SCOPE;
054        }
055    
056        public void setScope(String scope) {
057            this.scope = Util.getScope(scope);
058        }
059    
060        public void setVar(String var) {
061            this.var = var;
062        }
063    
064        public int doEndTag() throws JspException {
065            DateTimeZone dateTimeZone = null;
066            if (value == null) {
067                dateTimeZone = DateTimeZone.UTC;
068            } else if (value instanceof String) {
069                try {
070                    dateTimeZone = DateTimeZone.forID((String) value);
071                } catch (IllegalArgumentException iae) {
072                    dateTimeZone = DateTimeZone.UTC;
073                }
074            } else {
075                dateTimeZone = (DateTimeZone) value;
076            }
077    
078            if (var != null) {
079                pageContext.setAttribute(var, dateTimeZone, scope);
080            } else {
081                Config.set(pageContext, DateTimeZoneSupport.FMT_TIME_ZONE,
082                        dateTimeZone, scope);
083            }
084    
085            return EVAL_PAGE;
086        }
087    
088        // Releases any resources we may have (or inherit)
089        public void release() {
090            init();
091        }
092    
093    }