KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > propertyeditors > CustomDateEditor


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.beans.propertyeditors;
18
19 import java.beans.PropertyEditorSupport JavaDoc;
20 import java.text.DateFormat JavaDoc;
21 import java.text.ParseException JavaDoc;
22 import java.util.Date JavaDoc;
23
24 import org.springframework.util.StringUtils;
25
26 /**
27  * PropertyEditor for <code>java.util.Date</code>, supporting a custom
28  * <code>java.text.DateFormat</code>.
29  *
30  * <p>This is not meant to be used as system PropertyEditor but rather as
31  * locale-specific date editor within custom controller code, to parse
32  * user-entered date strings into Date properties of beans, and render
33  * them in the UI form.
34  *
35  * <p>In web MVC code, this editor will typically be registered with
36  * <code>binder.registerCustomEditor</code> calls in an implementation
37  * of BaseCommandController's <code>initBinder</code> method.
38  *
39  * @author Juergen Hoeller
40  * @since 28.04.2003
41  * @see java.util.Date
42  * @see java.text.DateFormat
43  * @see org.springframework.validation.DataBinder#registerCustomEditor
44  * @see org.springframework.web.servlet.mvc.BaseCommandController#initBinder
45  */

46 public class CustomDateEditor extends PropertyEditorSupport JavaDoc {
47
48     private final DateFormat JavaDoc dateFormat;
49
50     private final boolean allowEmpty;
51
52     private final int exactDateLength;
53
54
55     /**
56      * Create a new CustomDateEditor instance, using the given DateFormat
57      * for parsing and rendering.
58      * <p>The "allowEmpty" parameter states if an empty String should
59      * be allowed for parsing, i.e. get interpreted as null value.
60      * Otherwise, an IllegalArgumentException gets thrown in that case.
61      * @param dateFormat DateFormat to use for parsing and rendering
62      * @param allowEmpty if empty strings should be allowed
63      */

64     public CustomDateEditor(DateFormat JavaDoc dateFormat, boolean allowEmpty) {
65         this.dateFormat = dateFormat;
66         this.allowEmpty = allowEmpty;
67         this.exactDateLength = -1;
68     }
69
70     /**
71      * Create a new CustomDateEditor instance, using the given DateFormat
72      * for parsing and rendering.
73      * <p>The "allowEmpty" parameter states if an empty String should
74      * be allowed for parsing, i.e. get interpreted as null value.
75      * Otherwise, an IllegalArgumentException gets thrown in that case.
76      * <p>The "exactDateLength" parameter states that IllegalArgumentException gets
77      * thrown if the String does not exactly match the length specified. This is useful
78      * because SimpleDateFormat does not enforce strict parsing of the year part,
79      * not even with <code>setLenient(false)</code>. Without an "exactDateLength"
80      * specified, the "01/01/05" would get parsed to "01/01/0005".
81      * @param dateFormat DateFormat to use for parsing and rendering
82      * @param allowEmpty if empty strings should be allowed
83      * @param exactDateLength the exact expected length of the date String
84      */

85     public CustomDateEditor(DateFormat JavaDoc dateFormat, boolean allowEmpty, int exactDateLength) {
86         this.dateFormat = dateFormat;
87         this.allowEmpty = allowEmpty;
88         this.exactDateLength = exactDateLength;
89     }
90
91
92     /**
93      * Parse the Date from the given text, using the specified DateFormat.
94      */

95     public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
96         if (this.allowEmpty && !StringUtils.hasText(text)) {
97             // Treat empty String as null value.
98
setValue(null);
99         }
100         else if (text != null && this.exactDateLength >= 0 && text.length() != this.exactDateLength) {
101             throw new IllegalArgumentException JavaDoc(
102                     "Could not parse date: it is not exactly" + this.exactDateLength + "characters long");
103         }
104         else {
105             try {
106                 setValue(this.dateFormat.parse(text));
107             }
108             catch (ParseException JavaDoc ex) {
109                 throw new IllegalArgumentException JavaDoc("Could not parse date: " + ex.getMessage());
110             }
111         }
112     }
113
114     /**
115      * Format the Date as String, using the specified DateFormat.
116      */

117     public String JavaDoc getAsText() {
118         Date JavaDoc value = (Date JavaDoc) getValue();
119         return (value != null ? this.dateFormat.format(value) : "");
120     }
121
122 }
123
Popular Tags