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.BodyTagSupport;
027
028 import org.joda.time.DateTime;
029 import org.joda.time.DateTimeZone;
030 import org.joda.time.format.DateTimeFormat;
031 import org.joda.time.format.DateTimeFormatter;
032
033 /**
034 * Support for tag handlers for <parseDate>, the date and time parsing tag
035 * in JSTL 1.0.
036 *
037 * @author Jan Luehe
038 * @author Jim Newsham
039 */
040 public abstract class ParseDateTimeSupport extends BodyTagSupport {
041
042 /** The value attribute. */
043 protected String value;
044 /** Status of the value. */
045 protected boolean valueSpecified;
046 /** The pattern attribute. */
047 protected String pattern;
048 /** The style attribute. */
049 protected String style;
050 /** The zone attribute. */
051 protected DateTimeZone dateTimeZone;
052 /** The locale attribute. */
053 protected Locale locale;
054 /** The var attribute. */
055 private String var;
056 /** The scope attribute. */
057 private int scope;
058
059 /**
060 * Constructor.
061 */
062 public ParseDateTimeSupport() {
063 super();
064 init();
065 }
066
067 private void init() {
068 value = null;
069 valueSpecified = false;
070 pattern = null;
071 style = null;
072 dateTimeZone = null;
073 locale = null;
074 scope = PageContext.PAGE_SCOPE;
075 }
076
077 public void setVar(String var) {
078 this.var = var;
079 }
080
081 public void setScope(String scope) {
082 this.scope = Util.getScope(scope);
083 }
084
085 public int doEndTag() throws JspException {
086 String input = null;
087
088 // determine the input by...
089 if (valueSpecified) {
090 // ... reading 'value' attribute
091 input = value;
092 } else {
093 // ... retrieving and trimming our body
094 if (bodyContent != null && bodyContent.getString() != null) {
095 input = bodyContent.getString().trim();
096 }
097 }
098
099 if ((input == null) || input.equals("")) {
100 if (var != null) {
101 pageContext.removeAttribute(var, scope);
102 }
103 return EVAL_PAGE;
104 }
105
106 // Create formatter
107 DateTimeFormatter formatter;
108 if (pattern != null) {
109 formatter = DateTimeFormat.forPattern(pattern);
110 } else if (style != null) {
111 formatter = DateTimeFormat.forStyle(style);
112 } else {
113 formatter = DateTimeFormat.fullDateTime();
114 }
115
116 // set formatter locale
117 Locale locale = this.locale;
118 if (locale == null) {
119 locale = Util.getFormattingLocale(pageContext, this, true,
120 DateFormat.getAvailableLocales());
121 }
122 if (locale != null) {
123 formatter = formatter.withLocale(locale);
124 }
125
126 // set formatter timezone
127 DateTimeZone tz = this.dateTimeZone;
128 if (tz == null) {
129 tz = DateTimeZoneSupport.getDateTimeZone(pageContext, this);
130 }
131 if (tz != null) {
132 formatter = formatter.withZone(tz);
133 }
134
135 // Parse date
136 DateTime parsed = null;
137 try {
138 parsed = formatter.parseDateTime(input);
139 } catch (IllegalArgumentException iae) {
140 throw new JspException(Resources.getMessage(
141 "PARSE_DATE_PARSE_ERROR", input), iae);
142 }
143
144 if (var != null) {
145 pageContext.setAttribute(var, parsed, scope);
146 } else {
147 try {
148 pageContext.getOut().print(parsed);
149 } catch (IOException ioe) {
150 throw new JspTagException(ioe.toString(), ioe);
151 }
152 }
153
154 return EVAL_PAGE;
155 }
156
157 // Releases any resources we may have (or inherit)
158 public void release() {
159 init();
160 }
161
162 }