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 java.io.IOException;
020 import java.text.DateFormat;
021 import java.util.Locale;
022
023 import javax.servlet.jsp.JspException;
024 import javax.servlet.jsp.JspTagException;
025 import javax.servlet.jsp.PageContext;
026 import javax.servlet.jsp.tagext.TagSupport;
027
028 import org.joda.time.DateTimeZone;
029 import org.joda.time.ReadableInstant;
030 import org.joda.time.ReadablePartial;
031 import org.joda.time.format.DateTimeFormat;
032 import org.joda.time.format.DateTimeFormatter;
033
034 /**
035 * Support for tag handlers for <formatDate>, the date and time
036 * formatting tag in JSTL 1.0.
037 *
038 * @author Jan Luehe
039 * @author Jim Newsham
040 */
041 public abstract class FormatSupport extends TagSupport {
042
043 /** The value attribute. */
044 protected Object value;
045 /** The pattern attribute. */
046 protected String pattern;
047 /** The style attribute. */
048 protected String style;
049 /** The dateTimeZone attribute. */
050 protected DateTimeZone dateTimeZone;
051 /** The locale attribute. */
052 protected Locale locale;
053 /** The var attribute. */
054 private String var;
055 /** The scope attribute. */
056 private int scope;
057
058 /**
059 * Constructor.
060 */
061 public FormatSupport() {
062 super();
063 init();
064 }
065
066 private void init() {
067 var = null;
068 value = null;
069 pattern = null;
070 style = null;
071 dateTimeZone = null;
072 locale = null;
073 scope = PageContext.PAGE_SCOPE;
074 }
075
076 public void setVar(String var) {
077 this.var = var;
078 }
079
080 public void setScope(String scope) {
081 this.scope = Util.getScope(scope);
082 }
083
084 /*
085 * Formats the given instant or partial.
086 */
087 public int doEndTag() throws JspException {
088 if (value == null) {
089 if (var != null) {
090 pageContext.removeAttribute(var, scope);
091 }
092 return EVAL_PAGE;
093 }
094
095 // Create formatter
096 DateTimeFormatter formatter;
097 if (pattern != null) {
098 formatter = DateTimeFormat.forPattern(pattern);
099 } else if (style != null) {
100 formatter = DateTimeFormat.forStyle(style);
101 } else {
102 // use a medium date (no time) style by default; same as jstl
103 formatter = DateTimeFormat.mediumDate();
104 }
105
106 // set formatter locale
107 Locale locale = this.locale;
108 if (locale == null) {
109 locale = Util.getFormattingLocale(pageContext, this, true,
110 DateFormat.getAvailableLocales());
111 }
112 if (locale != null) {
113 formatter = formatter.withLocale(locale);
114 }
115
116 // set formatter timezone
117 DateTimeZone tz = this.dateTimeZone;
118 if (tz == null) {
119 tz = DateTimeZoneSupport.getDateTimeZone(pageContext, this);
120 }
121 if (tz != null) {
122 formatter = formatter.withZone(tz);
123 }
124
125 // format value
126 String formatted;
127 if (value instanceof ReadableInstant) {
128 formatted = formatter.print((ReadableInstant) value);
129 } else if (value instanceof ReadablePartial) {
130 formatted = formatter.print((ReadablePartial) value);
131 } else {
132 throw new JspException(
133 "value attribute of format tag must be a ReadableInstant or ReadablePartial," +
134 " was: " + value.getClass().getName());
135 }
136
137 if (var != null) {
138 pageContext.setAttribute(var, formatted, scope);
139 } else {
140 try {
141 pageContext.getOut().print(formatted);
142 } catch (IOException ioe) {
143 throw new JspTagException(ioe.toString(), ioe);
144 }
145 }
146
147 return EVAL_PAGE;
148 }
149
150 // Releases any resources we may have (or inherit)
151 public void release() {
152 init();
153 }
154
155 }