001 // Copyright 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry.form.validator;
016
017 import java.util.Date;
018
019 import org.apache.tapestry.form.IFormComponent;
020 import org.apache.tapestry.form.ValidationMessages;
021 import org.apache.tapestry.valid.ValidationConstraint;
022 import org.apache.tapestry.valid.ValidationStrings;
023 import org.apache.tapestry.valid.ValidatorException;
024
025 /**
026 * Validates that the object, a Date, is not after a set maximum.
027 *
028 * @author Howard Lewis Ship
029 * @since 4.0
030 */
031 public class MaxDate extends BaseValidator
032 {
033 private Date _maxDate;
034
035 public MaxDate()
036 {
037 }
038
039 public MaxDate(String initializer)
040 {
041 super(initializer);
042 }
043
044 public void validate(IFormComponent field, ValidationMessages messages, Object object)
045 throws ValidatorException
046 {
047 Date date = (Date) object;
048
049 if (date.after(_maxDate))
050 throw new ValidatorException(buildMessage(messages, field),
051 ValidationConstraint.TOO_LARGE);
052
053 }
054
055 private String buildMessage(ValidationMessages messages, IFormComponent field)
056 {
057 return messages.formatValidationMessage(
058 getMessage(),
059 ValidationStrings.DATE_TOO_LATE,
060 new Object[]
061 { field.getDisplayName(), _maxDate });
062 }
063
064 public void setMaxDate(Date minDate)
065 {
066 _maxDate = minDate;
067 }
068
069 }