View Javadoc

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  import java.text.DateFormat;
21  import java.util.Locale;
22  
23  import javax.servlet.jsp.JspException;
24  import javax.servlet.jsp.JspTagException;
25  import javax.servlet.jsp.PageContext;
26  import javax.servlet.jsp.tagext.BodyTagSupport;
27  
28  import org.joda.time.DateTime;
29  import org.joda.time.DateTimeZone;
30  import org.joda.time.format.DateTimeFormat;
31  import org.joda.time.format.DateTimeFormatter;
32  
33  /**
34   * Support for tag handlers for <parseDate>, the date and time parsing tag
35   * in JSTL 1.0.
36   * 
37   * @author Jan Luehe
38   * @author Jim Newsham
39   */
40  public abstract class ParseDateTimeSupport extends BodyTagSupport {
41  
42      /** The value attribute. */
43      protected String value;
44      /** Status of the value. */
45      protected boolean valueSpecified;
46      /** The pattern attribute. */
47      protected String pattern;
48      /** The style attribute. */
49      protected String style;
50      /** The zone attribute. */
51      protected DateTimeZone dateTimeZone;
52      /** The locale attribute. */
53      protected Locale locale;
54      /** The var attribute. */
55      private String var;
56      /** The scope attribute. */
57      private int scope;
58  
59      /**
60       * Constructor.
61       */
62      public ParseDateTimeSupport() {
63          super();
64          init();
65      }
66  
67      private void init() {
68          value = null;
69          valueSpecified = false;
70          pattern = null;
71          style = null;
72          dateTimeZone = null;
73          locale = null;
74          scope = PageContext.PAGE_SCOPE;
75      }
76  
77      public void setVar(String var) {
78          this.var = var;
79      }
80  
81      public void setScope(String scope) {
82          this.scope = Util.getScope(scope);
83      }
84  
85      public int doEndTag() throws JspException {
86          String input = null;
87  
88          // determine the input by...
89          if (valueSpecified) {
90              // ... reading 'value' attribute
91              input = value;
92          } else {
93              // ... retrieving and trimming our body
94              if (bodyContent != null && bodyContent.getString() != null) {
95                  input = bodyContent.getString().trim();
96              }
97          }
98  
99          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 }