001: /*
002: * Copyright 2006 Dan Shellman
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.iscreen.validators;
017:
018: import java.text.ParseException;
019: import java.text.SimpleDateFormat;
020:
021: import org.iscreen.BaseValidator;
022: import org.iscreen.ValidatorContext;
023:
024: /**
025: * This validator checks the format of the input String to verify that
026: * it conforms to the proper format.
027: *
028: * @author Shellman, Dan
029: */
030: public class DateFormatValidator extends BaseValidator {
031: protected String format = "MM/dd/yyyy";
032: protected String displayFormat = "MM/DD/YYYY";
033: protected boolean lenientFlag = false;
034:
035: /**
036: * Default constructor.
037: */
038: public DateFormatValidator() {
039: } //end DateFormatValidator()
040:
041: public void validate(ValidatorContext context, Object beanToValidate) {
042: String date;
043: SimpleDateFormat simpleDate;
044:
045: date = getStringValue(beanToValidate);
046: simpleDate = new SimpleDateFormat(format);
047: simpleDate.setLenient(lenientFlag);
048:
049: try {
050: simpleDate.parse(date);
051: } catch (ParseException e) {
052: context.reportFailure(getDefaultFailure(), date);
053: }
054: } //end validate()
055:
056: /**
057: * The format of the date String. The format should be a valid
058: * SimpleDateFormat pattern.
059: *
060: * @param theFormat The SimpleDateFormat pattern.
061: */
062: public void setFormat(String theFormat) {
063: format = theFormat;
064: } //end setFormat()
065:
066: /**
067: * Retrieves the date format that the date is validated against.
068: *
069: * @return Returns the date format that the date is validated against.
070: */
071: public String getFormat() {
072: return format;
073: } //end getFormat()
074:
075: /**
076: * The display format, which is reported when a failure occurs. It should
077: * be a human-readable format (defaults to MM/DD/YYYY).
078: *
079: * @param theDisplayFormat The format to display to the user.
080: */
081: public void setDisplayFormat(String theDisplayFormat) {
082: displayFormat = theDisplayFormat;
083: } //end setDisplayFormat()
084:
085: /**
086: * Retrieves the format of the date for display purposes.
087: *
088: * @return Returns the format for the date for display purposes.
089: */
090: public String getDisplayFormat() {
091: return displayFormat;
092: } //end getDisplayFormat()
093:
094: /**
095: * The lenient flag. Passed directly to the SimpleDateFormat that is
096: * used to validate the date String, this property defaults to false.
097: *
098: * @param lenient The lenient flag for the SimpleDateFormat.
099: */
100: public void setLenient(boolean lenient) {
101: lenientFlag = lenient;
102: } //end setLenient()
103: } //end DateFormatValidator
|