Coverage report

  %line %branch
org.apache.turbine.services.intake.model.DateStringField
0% 
0% 

 1  
 package org.apache.turbine.services.intake.model;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2005 The Apache Software Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License")
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import java.text.DateFormat;
 20  
 import java.text.ParseException;
 21  
 
 22  
 import java.util.Date;
 23  
 
 24  
 import org.apache.commons.lang.StringUtils;
 25  
 
 26  
 import org.apache.turbine.services.intake.IntakeException;
 27  
 import org.apache.turbine.services.intake.validator.DateStringValidator;
 28  
 import org.apache.turbine.services.intake.xmlmodel.XmlField;
 29  
 import org.apache.turbine.util.TurbineRuntimeException;
 30  
 
 31  
 /**
 32  
  * Field for date inputs as free form text.  The parsing of date strings
 33  
  * is dependent on any rules that are defined, so this field will expect that
 34  
  * any validator will be (or extend) DateStringValidator.
 35  
  *
 36  
  * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
 37  
  * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
 38  
  * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
 39  
  * @version $Id: DateStringField.java 264148 2005-08-29 14:21:04Z henning $
 40  
  */
 41  
 public class DateStringField
 42  
         extends Field
 43  
 {
 44  
     /** date format. Fallback if no validator is defined */
 45  
     private static DateFormat df;
 46  
 
 47  
     static
 48  
     {
 49  0
         df = DateFormat.getInstance();
 50  0
         df.setLenient(true);
 51  0
     }
 52  
 
 53  
     /**
 54  
      * Constructor.
 55  
      *
 56  
      * @param field xml field definition object
 57  
      * @param group xml group definition object
 58  
      * @throws IntakeException thrown by superclass
 59  
      */
 60  
     public DateStringField(XmlField field, Group group)
 61  
             throws IntakeException
 62  
     {
 63  0
         super(field, group);
 64  0
     }
 65  
 
 66  
     /**
 67  
      * Sets the default value for a DateString field
 68  
      *
 69  
      * @param prop Parameter for the default values
 70  
      */
 71  
     public void setDefaultValue(String prop)
 72  
     {
 73  0
         defaultValue = null;
 74  
 
 75  0
         if (prop == null)
 76  
         {
 77  0
             return;
 78  
         }
 79  
 
 80  
         try
 81  
         {
 82  0
             defaultValue = getDate(prop);
 83  
         }
 84  0
         catch (ParseException e)
 85  
         {
 86  0
             throw new TurbineRuntimeException("Could not parse " + prop
 87  
                     + " into a valid Date for the default value", e);
 88  0
         }
 89  0
     }
 90  
 
 91  
     /**
 92  
      * Set the empty Value. This value is used if Intake
 93  
      * maps a field to a parameter returned by the user and
 94  
      * the corresponding field is either empty (empty string)
 95  
      * or non-existant.
 96  
      *
 97  
      * @param prop The value to use if the field is empty.
 98  
      */
 99  
     public void setEmptyValue(String prop)
 100  
     {
 101  0
         emptyValue = null;
 102  
 
 103  0
         if (prop == null)
 104  
         {
 105  0
             return;
 106  
         }
 107  
 
 108  
         try
 109  
         {
 110  0
             emptyValue = getDate(prop);
 111  
         }
 112  0
         catch (ParseException e)
 113  
         {
 114  0
             throw new TurbineRuntimeException("Could not parse " + prop
 115  
                     + " into a valid Date for the empty value", e);
 116  0
         }
 117  0
     }
 118  
 
 119  
     /**
 120  
      * A suitable validator.
 121  
      *
 122  
      * @return "DateStringValidator"
 123  
      */
 124  
     protected String getDefaultValidator()
 125  
     {
 126  0
         return DateStringValidator.class.getName();
 127  
     }
 128  
 
 129  
     /**
 130  
      * Sets the value of the field from data in the parser.
 131  
      */
 132  
     protected void doSetValue()
 133  
     {
 134  0
         if (isMultiValued)
 135  
         {
 136  0
             String[] inputs = parser.getStrings(getKey());
 137  0
             Date[] values = new Date[inputs.length];
 138  0
             for (int i = 0; i < inputs.length; i++)
 139  
             {
 140  
                 try
 141  
                 {
 142  0
                     values[i] = StringUtils.isNotEmpty(inputs[i])
 143  
                             ? getDate(inputs[i]) : (Date) getEmptyValue();
 144  
                 }
 145  0
                 catch (ParseException e)
 146  
                 {
 147  0
                     values[i] = null;
 148  0
                 }
 149  
             }
 150  0
             setTestValue(values);
 151  
         }
 152  
         else
 153  
         {
 154  0
             String val = parser.getString(getKey());
 155  
             try
 156  
             {
 157  0
                 setTestValue(StringUtils.isNotEmpty(val) ? getDate(val) : (Date) getEmptyValue());
 158  
             }
 159  0
             catch (ParseException e)
 160  
             {
 161  0
                 setTestValue(null);
 162  0
             }
 163  
         }
 164  0
     }
 165  
 
 166  
     /**
 167  
      * Parses a test date string using the Validator if is exists and
 168  
      * is an instance of DateStringValidator.  Otherwise, DateFormat.parse()
 169  
      * is used.
 170  
      *
 171  
      * @param dateString The string date to parse
 172  
      * @return A <code>Date</code> object
 173  
      * @throws ParseException The date could not be parsed.
 174  
      */
 175  
     private Date getDate(String dateString)
 176  
             throws ParseException
 177  
     {
 178  0
         Date date = null;
 179  
         // FIXME: Canonicalize user-entered date strings.
 180  0
         if (validator != null && validator instanceof DateStringValidator)
 181  
         {
 182  0
             date = ((DateStringValidator) validator).parse(dateString);
 183  
         }
 184  
         else
 185  
         {
 186  0
             date = df.parse(dateString);
 187  
         }
 188  0
         return date;
 189  
     }
 190  
 
 191  
     /**
 192  
      * returns a String representation
 193  
      *
 194  
      * @return a String representation
 195  
      */
 196  
     public String toString()
 197  
     {
 198  0
         String s = null;
 199  0
         Object value = getValue();
 200  0
         if (value == null)
 201  
         {
 202  0
             s = "";
 203  
         }
 204  0
         else if (value instanceof String)
 205  
         {
 206  0
             s = (String) value;
 207  
         }
 208  0
         else if (validator != null && validator instanceof DateStringValidator)
 209  
         {
 210  0
             s = ((DateStringValidator) validator).format((Date) value);
 211  
         }
 212  
         else
 213  
         {
 214  0
             s = df.format((Date) value);
 215  
         }
 216  0
         return s;
 217  
     }
 218  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.