View Javadoc

1   /*
2    * $Id: DatePropertyEditor.java 21943 2011-05-18 14:23:26Z aperepel $
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      public DatePropertyEditor(DateFormat longDateFormat, DateFormat shortDateFormat, boolean allowEmpty) {
45          this.dateFormat = longDateFormat;
46          this.shortDateFormat = shortDateFormat;
47          this.allowEmpty = allowEmpty;
48          this.exactDateLength = -1;
49      }
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      public DatePropertyEditor(DateFormat longDateFormat, boolean allowEmpty, int exactDateLength) {
67          this.dateFormat = longDateFormat;
68          this.allowEmpty = allowEmpty;
69          this.exactDateLength = exactDateLength;
70      }
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          if (this.allowEmpty && !StringUtils.hasText(text)) {
79              // Treat empty String as null value.
80              setValue(null);
81          }
82          else if(text.equals("now"))
83          {
84              setValue(new Date());
85          }
86          else if (this.exactDateLength >= 0 && text.length() != this.exactDateLength) {
87              throw new IllegalArgumentException(
88                      "Could not parse date: it is not exactly" + this.exactDateLength + "characters long");
89          }
90          else {
91              try {
92                  if(shortDateFormat!=null && text.length() <=10) {
93                      setValue(this.shortDateFormat.parse(text));
94                  } else {
95                      setValue(this.dateFormat.parse(text));
96                  }
97              }
98              catch (ParseException ex) {
99                  throw new IllegalArgumentException("Could not parse date: " + ex.getMessage(), ex);
100             }
101         }
102     }
103 
104     /**
105      * Format the Date as String, using the specified DateFormat.
106      */
107     @Override
108     public String getAsText() {
109         Date value = (Date) getValue();
110         return (value != null ? this.dateFormat.format(value) : "");
111     }
112 }