KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > beans > propertyeditors > SqlDateEditor


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.beans.propertyeditors;
25
26 import java.beans.PropertyEditorSupport JavaDoc;
27 import java.text.DateFormat JavaDoc;
28 import java.text.ParseException JavaDoc;
29 import java.text.SimpleDateFormat JavaDoc;
30 import java.util.Date JavaDoc;
31
32 import org.springframework.util.StringUtils;
33
34 /**
35  * PropertyEditor for <code>java.sql.Date</code>, supporting a custom
36  * <code>java.text.DateFormat</code>.
37  *
38  * Based on Spring's {@link org.springframework.beans.propertyeditors.CustomDateEditor}
39  */

40 public class SqlDateEditor extends PropertyEditorSupport JavaDoc {
41
42     private final DateFormat JavaDoc dateFormat;
43
44     private final boolean allowEmpty;
45
46     private final int exactDateLength;
47
48     /**
49      * Create a new SqlDateEditor for dates matching the <code>yyyy-MM-dd</code>
50      * pattern.
51      */

52     public SqlDateEditor() {
53         this(new SimpleDateFormat JavaDoc("yyyy-MM-dd"), true, 10);
54     }
55
56     /**
57      * Create a new SqlDateEditor instance, using the given DateFormat
58      * for parsing and rendering.
59      * <p>The "allowEmpty" parameter states if an empty String should
60      * be allowed for parsing, i.e. get interpreted as null value.
61      * Otherwise, an IllegalArgumentException gets thrown in that case.
62      * @param dateFormat DateFormat to use for parsing and rendering
63      * @param allowEmpty if empty strings should be allowed
64      */

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

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

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

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