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