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 org.iscreen.BaseValidator;
019: import org.iscreen.FailureMessage;
020: import org.iscreen.SimpleBean;
021: import org.iscreen.ValidatorContext;
022:
023: /**
024: * The StringValidator checks the length of the given value.
025: *
026: * @author Shellman, Dan
027: */
028: public class StringValidator extends BaseValidator {
029: protected Integer minLength;
030: protected Integer maxLength;
031: protected FailureMessage minLengthFailure;
032: protected FailureMessage maxLengthFailure;
033: protected FailureMessage nullFailure;
034:
035: /**
036: * Default constructor.
037: */
038: public StringValidator() {
039: } //end StringValidator()
040:
041: public void validate(ValidatorContext context, Object beanToValidate) {
042: String value;
043: int valueLength;
044:
045: value = getStringValue(beanToValidate);
046: if (value == null) {
047: if (minLength != null && minLength.intValue() > 0) {
048: context.reportFailure(nullFailure);
049: }
050:
051: return;
052: }
053:
054: valueLength = value.length();
055: if (minLength != null && valueLength < minLength.intValue()) {
056: context.reportFailure(minLengthFailure, new Integer(
057: valueLength));
058: } else if (maxLength != null
059: && valueLength > maxLength.intValue()) {
060: context.reportFailure(maxLengthFailure, new Integer(
061: valueLength));
062: }
063: } //end validate()
064:
065: // ***
066: // Constraints
067: // ***
068:
069: /**
070: * Sets the minimum required length for the string being validated.
071: *
072: * @param length The minimum required length
073: */
074: public void setMinLength(Integer length) {
075: minLength = length;
076: } //end setMinLength()
077:
078: /**
079: * Retrieves the minimum required length for the string being validated.
080: *
081: * @return Returns the minimum required length.
082: */
083: public Integer getMinLength() {
084: return minLength;
085: } //end getMinLength()
086:
087: /**
088: * Sets the maximum required length for the string being validated.
089: *
090: * @param length The maximum required length
091: */
092: public void setMaxLength(Integer length) {
093: maxLength = length;
094: } //end setMaxLength()
095:
096: /**
097: * Retrieves the maximum required length for the string being validated.
098: *
099: * @return Returns the maximum required length.
100: */
101: public Integer getMaxLength() {
102: return maxLength;
103: } //end getMaxLength()
104:
105: public void setMinLengthFailure(FailureMessage failure) {
106: minLengthFailure = failure;
107: } //end setMinLengthFailure()
108:
109: public void setMaxLengthFailure(FailureMessage failure) {
110: maxLengthFailure = failure;
111: } //end setMaxLengthFailure()
112:
113: public void setNullFailure(FailureMessage failure) {
114: nullFailure = failure;
115: } //end setNullFailure()
116: } //end StringValidator
|