Coverage Report - org.mule.config.spring.editors.DatePropertyEditor
 
Classes in this File Line Coverage Branch Coverage Complexity
DatePropertyEditor
0%
0/26
0%
0/16
0
 
 1  
 /*
 2  
  * $Id: DatePropertyEditor.java 20320 2010-11-24 15:03:31Z dfeist $
 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.equals("now"))
 83  
         {
 84  0
             setValue(new Date());
 85  
         }
 86  0
                 else if (this.exactDateLength >= 0 && text.length() != this.exactDateLength) {
 87  0
                         throw new IllegalArgumentException(
 88  
                                         "Could not parse date: it is not exactly" + this.exactDateLength + "characters long");
 89  
                 }
 90  
                 else {
 91  
                         try {
 92  0
                 if(shortDateFormat!=null && text.length() <=10) {
 93  0
                                     setValue(this.shortDateFormat.parse(text));
 94  
                 } else {
 95  0
                     setValue(this.dateFormat.parse(text));
 96  
                 }
 97  
                         }
 98  0
                         catch (ParseException ex) {
 99  0
                                 throw new IllegalArgumentException("Could not parse date: " + ex.getMessage(), ex);
 100  0
                         }
 101  
                 }
 102  0
         }
 103  
 
 104  
         /**
 105  
          * Format the Date as String, using the specified DateFormat.
 106  
          */
 107  
         @Override
 108  
         public String getAsText() {
 109  0
                 Date value = (Date) getValue();
 110  0
                 return (value != null ? this.dateFormat.format(value) : "");
 111  
         }
 112  
 }