Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DatePropertyEditor |
|
| 0.0;0 |
1 | /* | |
2 | * $Id$ | |
3 | * -------------------------------------------------------------------------------------- | |
4 | * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com | |
5 | * | |
6 | * The software in this package is published under the terms of the CPAL v1.0 | |
7 | * license, a copy of which has been included with this distribution in the | |
8 | * LICENSE.txt file. | |
9 | */ | |
10 | package org.mule.config.spring.editors; | |
11 | ||
12 | import java.beans.PropertyEditorSupport; | |
13 | import java.text.DateFormat; | |
14 | import java.text.ParseException; | |
15 | import java.util.Date; | |
16 | ||
17 | import org.springframework.util.StringUtils; | |
18 | ||
19 | /** | |
20 | * Handles the conversion of date strings in {@link java.util.Date} objects. | |
21 | */ | |
22 | public class DatePropertyEditor extends PropertyEditorSupport | |
23 | { | |
24 | ||
25 | private DateFormat dateFormat; | |
26 | ||
27 | private DateFormat shortDateFormat; | |
28 | ||
29 | private final boolean allowEmpty; | |
30 | ||
31 | private final int exactDateLength; | |
32 | ||
33 | ||
34 | /** | |
35 | * Create a new CustomDateEditor instance, using the given DateFormat | |
36 | * for parsing and rendering. | |
37 | * <p>The "allowEmpty" parameter states if an empty String should | |
38 | * be allowed for parsing, i.e. get interpreted as null value. | |
39 | * Otherwise, an IllegalArgumentException gets thrown in that case. | |
40 | * @param longDateFormat DateFormat to use for parsing and rendering | |
41 | * @param shortDateFormat a short form of DateFormat to use for parsing and rendering | |
42 | * @param allowEmpty if empty strings should be allowed | |
43 | */ | |
44 | 0 | public DatePropertyEditor(DateFormat longDateFormat, DateFormat shortDateFormat, boolean allowEmpty) { |
45 | 0 | this.dateFormat = longDateFormat; |
46 | 0 | this.shortDateFormat = shortDateFormat; |
47 | 0 | this.allowEmpty = allowEmpty; |
48 | 0 | this.exactDateLength = -1; |
49 | 0 | } |
50 | ||
51 | /** | |
52 | * Create a new CustomDateEditor instance, using the given DateFormat | |
53 | * for parsing and rendering. | |
54 | * <p>The "allowEmpty" parameter states if an empty String should | |
55 | * be allowed for parsing, i.e. get interpreted as null value. | |
56 | * Otherwise, an IllegalArgumentException gets thrown in that case. | |
57 | * <p>The "exactDateLength" parameter states that IllegalArgumentException gets | |
58 | * thrown if the String does not exactly match the length specified. This is useful | |
59 | * because SimpleDateFormat does not enforce strict parsing of the year part, | |
60 | * not even with <code>setLenient(false)</code>. Without an "exactDateLength" | |
61 | * specified, the "01/01/05" would get parsed to "01/01/0005". | |
62 | * @param longDateFormat DateFormat to use for parsing and rendering | |
63 | * @param allowEmpty if empty strings should be allowed | |
64 | * @param exactDateLength the exact expected length of the date String | |
65 | */ | |
66 | 0 | public DatePropertyEditor(DateFormat longDateFormat, boolean allowEmpty, int exactDateLength) { |
67 | 0 | this.dateFormat = longDateFormat; |
68 | 0 | this.allowEmpty = allowEmpty; |
69 | 0 | this.exactDateLength = exactDateLength; |
70 | 0 | } |
71 | ||
72 | ||
73 | /** | |
74 | * Parse the Date from the given text, using the specified DateFormat. | |
75 | */ | |
76 | @Override | |
77 | public void setAsText(String text) throws IllegalArgumentException { | |
78 | 0 | if (this.allowEmpty && !StringUtils.hasText(text)) { |
79 | // Treat empty String as null value. | |
80 | 0 | setValue(null); |
81 | } | |
82 | 0 | else if (text != null && this.exactDateLength >= 0 && text.length() != this.exactDateLength) { |
83 | 0 | throw new IllegalArgumentException( |
84 | "Could not parse date: it is not exactly" + this.exactDateLength + "characters long"); | |
85 | } | |
86 | else { | |
87 | try { | |
88 | 0 | if(shortDateFormat!=null && text!=null && text.length() <=10) { |
89 | 0 | setValue(this.shortDateFormat.parse(text)); |
90 | } else { | |
91 | 0 | setValue(this.dateFormat.parse(text)); |
92 | } | |
93 | } | |
94 | 0 | catch (ParseException ex) { |
95 | 0 | throw new IllegalArgumentException("Could not parse date: " + ex.getMessage(), ex); |
96 | 0 | } |
97 | } | |
98 | 0 | } |
99 | ||
100 | /** | |
101 | * Format the Date as String, using the specified DateFormat. | |
102 | */ | |
103 | @Override | |
104 | public String getAsText() { | |
105 | 0 | Date value = (Date) getValue(); |
106 | 0 | return (value != null ? this.dateFormat.format(value) : ""); |
107 | } | |
108 | } |