Clover coverage report - Code Coverage for tapestry release 3.1-alpha-1
Coverage timestamp: Mon Feb 21 2005 09:16:14 EST
file stats: LOC: 338   Methods: 24
NCLOC: 177   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
DateValidator.java 72.7% 71% 54.2% 67.6%
coverage coverage
 1   
 // Copyright 2004, 2005 The Apache Software Foundation
 2   
 //
 3   
 // Licensed under the Apache License, Version 2.0 (the "License");
 4   
 // you may not use this file except in compliance with the License.
 5   
 // You may obtain a copy of the License at
 6   
 //
 7   
 //     http://www.apache.org/licenses/LICENSE-2.0
 8   
 //
 9   
 // Unless required by applicable law or agreed to in writing, software
 10   
 // distributed under the License is distributed on an "AS IS" BASIS,
 11   
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12   
 // See the License for the specific language governing permissions and
 13   
 // limitations under the License.
 14   
 
 15   
 package org.apache.tapestry.valid;
 16   
 
 17   
 import java.text.DateFormat;
 18   
 import java.text.ParseException;
 19   
 import java.text.SimpleDateFormat;
 20   
 import java.util.Calendar;
 21   
 import java.util.Date;
 22   
 import java.util.GregorianCalendar;
 23   
 import java.util.HashMap;
 24   
 import java.util.Map;
 25   
 
 26   
 import org.apache.tapestry.IMarkupWriter;
 27   
 import org.apache.tapestry.IRequestCycle;
 28   
 import org.apache.tapestry.form.IFormComponent;
 29   
 
 30   
 /**
 31   
  * Provides input validation for strings treated as dates. In addition, allows a minimum and maximum
 32   
  * date to be set.
 33   
  * 
 34   
  * @author Howard Lewis Ship
 35   
  * @since 1.0.8
 36   
  */
 37   
 
 38   
 public class DateValidator extends BaseValidator
 39   
 {
 40   
     private DateFormat _format;
 41   
 
 42   
     private String _displayFormat;
 43   
 
 44   
     private Date _minimum;
 45   
 
 46   
     private Date _maximum;
 47   
 
 48   
     private Calendar _calendar;
 49   
 
 50   
     private String _scriptPath = "/org/apache/tapestry/valid/DateValidator.script";
 51   
 
 52   
     private static DateFormat defaultDateFormat = new SimpleDateFormat("MM/dd/yyyy");
 53   
 
 54   
     private static final String defaultDateDisplayFormat = "MM/DD/YYYY";
 55   
 
 56   
     private String _dateTooEarlyMessage;
 57   
 
 58   
     private String _dateTooLateMessage;
 59   
 
 60   
     private String _invalidDateFormatMessage;
 61   
 
 62  2
     public void setFormat(DateFormat value)
 63   
     {
 64  2
         _format = value;
 65   
     }
 66   
 
 67  0
     public DateFormat getFormat()
 68   
     {
 69  0
         return _format;
 70   
     }
 71   
 
 72   
     /**
 73   
      * @return the {@link DateFormat}the validator will use, returning the default if no other date
 74   
      *         format is specified via {@link #setFormat(DateFormat)}
 75   
      * @since 3.0
 76   
      */
 77  11
     public DateFormat getEffectiveFormat()
 78   
     {
 79  11
         if (_format == null)
 80  9
             return defaultDateFormat;
 81   
 
 82  2
         return _format;
 83   
     }
 84   
 
 85  0
     public String getDisplayFormat()
 86   
     {
 87  0
         return _displayFormat;
 88   
     }
 89   
 
 90  0
     public void setDisplayFormat(String value)
 91   
     {
 92  0
         _displayFormat = value;
 93   
     }
 94   
 
 95   
     /**
 96   
      * @return the display format message the validator will use, returning the default if no other
 97   
      *         display format message is specified. The default is the
 98   
      *         {@link SimpleDateFormat#toPattern()}for {@link SimpleDateFormat}s, or "MM/DD/YYYY"
 99   
      *         for unknown {@link DateFormat}subclasses.
 100   
      * @since 3.0
 101   
      */
 102  2
     public String getEffectiveDisplayFormat()
 103   
     {
 104  2
         if (_displayFormat == null)
 105   
         {
 106  2
             DateFormat format = getEffectiveFormat();
 107  2
             if (format instanceof SimpleDateFormat)
 108  2
                 return ((SimpleDateFormat) format).toPattern();
 109   
 
 110  0
             return defaultDateDisplayFormat;
 111   
         }
 112   
 
 113  0
         return _displayFormat;
 114   
     }
 115   
 
 116  3
     public String toString(IFormComponent file, Object value)
 117   
     {
 118  3
         if (value == null)
 119  1
             return null;
 120   
 
 121  2
         Date date = (Date) value;
 122   
 
 123  2
         DateFormat format = getEffectiveFormat();
 124   
 
 125   
         // DateFormat is not threadsafe, so guard access to it.
 126   
 
 127  2
         synchronized (format)
 128   
         {
 129  2
             return format.format(date);
 130   
         }
 131   
     }
 132   
 
 133  11
     public Object toObject(IFormComponent field, String value) throws ValidatorException
 134   
     {
 135  11
         if (checkRequired(field, value))
 136  4
             return null;
 137   
 
 138  7
         DateFormat format = getEffectiveFormat();
 139   
 
 140  7
         Date result;
 141   
 
 142  7
         try
 143   
         {
 144   
             // DateFormat is not threadsafe, so guard access
 145   
             // to it.
 146   
 
 147  7
             synchronized (format)
 148   
             {
 149  7
                 result = format.parse(value);
 150   
             }
 151   
 
 152  5
             if (_calendar == null)
 153  5
                 _calendar = new GregorianCalendar();
 154   
 
 155  5
             _calendar.setTime(result);
 156   
 
 157   
             // SimpleDateFormat allows two-digit dates to be
 158   
             // entered, i.e., 12/24/66 is Dec 24 0066 ... that's
 159   
             // probably not what is really wanted, so treat
 160   
             // it as an invalid date.
 161   
 
 162  5
             if (_calendar.get(Calendar.YEAR) < 1000)
 163  0
                 result = null;
 164   
 
 165   
         }
 166   
         catch (ParseException ex)
 167   
         {
 168   
             // ParseException does not include a useful error message
 169   
             // about what's wrong.
 170  2
             result = null;
 171   
         }
 172   
 
 173  7
         if (result == null)
 174  2
             throw new ValidatorException(buildInvalidDateFormatMessage(field),
 175   
                     ValidationConstraint.DATE_FORMAT);
 176   
 
 177   
         // OK, check that the date is in range.
 178   
 
 179  5
         if (_minimum != null && _minimum.compareTo(result) > 0)
 180  2
             throw new ValidatorException(buildDateTooEarlyMessage(field, format.format(_minimum)),
 181   
                     ValidationConstraint.TOO_SMALL);
 182   
 
 183  3
         if (_maximum != null && _maximum.compareTo(result) < 0)
 184  2
             throw new ValidatorException(buildDateTooLateMessage(field, format.format(_maximum)),
 185   
                     ValidationConstraint.TOO_LARGE);
 186   
 
 187  1
         return result;
 188   
 
 189   
     }
 190   
 
 191  0
     public Date getMaximum()
 192   
     {
 193  0
         return _maximum;
 194   
     }
 195   
 
 196  3
     public void setMaximum(Date maximum)
 197   
     {
 198  3
         _maximum = maximum;
 199   
     }
 200   
 
 201  0
     public Date getMinimum()
 202   
     {
 203  0
         return _minimum;
 204   
     }
 205   
 
 206  3
     public void setMinimum(Date minimum)
 207   
     {
 208  3
         _minimum = minimum;
 209   
     }
 210   
 
 211   
     /**
 212   
      * @since 2.2
 213   
      */
 214   
 
 215  0
     public void renderValidatorContribution(IFormComponent field, IMarkupWriter writer,
 216   
             IRequestCycle cycle)
 217   
     {
 218  0
         if (!(isClientScriptingEnabled() && isRequired()))
 219  0
             return;
 220   
 
 221  0
         Map symbols = new HashMap();
 222   
 
 223  0
         symbols.put("requiredMessage", buildRequiredMessage(field));
 224   
 
 225  0
         processValidatorScript(_scriptPath, cycle, field, symbols);
 226   
     }
 227   
 
 228   
     /**
 229   
      * @since 2.2
 230   
      */
 231   
 
 232  0
     public String getScriptPath()
 233   
     {
 234  0
         return _scriptPath;
 235   
     }
 236   
 
 237   
     /**
 238   
      * Allows a developer to use the existing validation logic with a different client-side script.
 239   
      * This is often sufficient to allow application-specific error presentation (perhaps by using
 240   
      * DHTML to update the content of a &lt;span&gt; tag, or to use a more sophisticated pop-up
 241   
      * window than <code>window.alert()</code>).
 242   
      * 
 243   
      * @since 2.2
 244   
      */
 245   
 
 246  0
     public void setScriptPath(String scriptPath)
 247   
     {
 248  0
         _scriptPath = scriptPath;
 249   
     }
 250   
 
 251   
     /** @since 3.0 */
 252   
 
 253  0
     public String getDateTooEarlyMessage()
 254   
     {
 255  0
         return _dateTooEarlyMessage;
 256   
     }
 257   
 
 258   
     /** @since 3.0 */
 259   
 
 260  0
     public String getDateTooLateMessage()
 261   
     {
 262  0
         return _dateTooLateMessage;
 263   
     }
 264   
 
 265   
     /** @since 3.0 */
 266   
 
 267  0
     public String getInvalidDateFormatMessage()
 268   
     {
 269  0
         return _invalidDateFormatMessage;
 270   
     }
 271   
 
 272   
     /** @since 3.0 */
 273   
 
 274  2
     protected String buildInvalidDateFormatMessage(IFormComponent field)
 275   
     {
 276  2
         String pattern = getPattern(_invalidDateFormatMessage, "invalid-date-format", field
 277   
                 .getPage().getLocale());
 278   
 
 279  2
         return formatString(pattern, field.getDisplayName(), getEffectiveDisplayFormat());
 280   
     }
 281   
 
 282   
     /** @since 3.0 * */
 283   
 
 284  2
     protected String buildDateTooEarlyMessage(IFormComponent field, String earliestDate)
 285   
     {
 286  2
         String pattern = getPattern(_dateTooEarlyMessage, "date-too-early", field.getPage()
 287   
                 .getLocale());
 288   
 
 289  2
         return formatString(pattern, field.getDisplayName(), earliestDate);
 290   
     }
 291   
 
 292   
     /** @since 3.0 */
 293   
 
 294  2
     protected String buildDateTooLateMessage(IFormComponent field, String latestDate)
 295   
     {
 296  2
         String pattern = getPattern(_dateTooLateMessage, "date-too-late", field.getPage()
 297   
                 .getLocale());
 298   
 
 299  2
         return formatString(pattern, field.getDisplayName(), latestDate);
 300   
     }
 301   
 
 302   
     /**
 303   
      * Overrides the bundle key <code>date-too-early</code>. Parameter {0} is the display name of
 304   
      * the field. Parameter {1} is the earliest allowed date.
 305   
      * 
 306   
      * @since 3.0
 307   
      */
 308   
 
 309  1
     public void setDateTooEarlyMessage(String string)
 310   
     {
 311  1
         _dateTooEarlyMessage = string;
 312   
     }
 313   
 
 314   
     /**
 315   
      * Overrides the bundle key <code>date-too-late</code>. Parameter {0} is the display name of
 316   
      * the field. Parameter {1} is the latest allowed date.
 317   
      * 
 318   
      * @since 3.0
 319   
      */
 320   
 
 321  1
     public void setDateTooLateMessage(String string)
 322   
     {
 323  1
         _dateTooLateMessage = string;
 324   
     }
 325   
 
 326   
     /**
 327   
      * Overrides the bundle key <code>invalid-date-format</code>. Parameter {0} is the display
 328   
      * name of the field. Parameter {1} is the allowed format.
 329   
      * 
 330   
      * @since 3.0
 331   
      */
 332   
 
 333  1
     public void setInvalidDateFormatMessage(String string)
 334   
     {
 335  1
         _invalidDateFormatMessage = string;
 336   
     }
 337   
 
 338   
 }