001: /*
002: * Copyright 2004 The Apache Software Foundation.
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 javax.faces.validator;
017:
018: import javax.faces.component.StateHolder;
019: import javax.faces.component.UIComponent;
020: import javax.faces.context.FacesContext;
021:
022: /**
023: * see Javadoc of <a href="http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
024: *
025: * @author Manfred Geiler (latest modification by $Author: cagatay $)
026: * @author Thomas Spiegl
027: * @version $Revision: 517405 $ $Date: 2007-03-12 22:23:37 +0100 (Mo, 12 Mrz 2007) $
028: */
029: public class LengthValidator implements Validator, StateHolder {
030: // FIELDS
031: public static final String MAXIMUM_MESSAGE_ID = "javax.faces.validator.LengthValidator.MAXIMUM";
032: public static final String MINIMUM_MESSAGE_ID = "javax.faces.validator.LengthValidator.MINIMUM";
033: public static final String VALIDATOR_ID = "javax.faces.Length";
034:
035: private Integer _minimum = null;
036: private Integer _maximum = null;
037: private boolean _transient = false;
038:
039: // CONSTRUCTORS
040: public LengthValidator() {
041: }
042:
043: public LengthValidator(int maximum) {
044: _maximum = new Integer(maximum);
045: }
046:
047: public LengthValidator(int maximum, int minimum) {
048: _maximum = new Integer(maximum);
049: _minimum = new Integer(minimum);
050: }
051:
052: // VALIDATE
053: public void validate(FacesContext facesContext,
054: UIComponent uiComponent, Object value)
055: throws ValidatorException {
056: if (facesContext == null)
057: throw new NullPointerException("facesContext");
058: if (uiComponent == null)
059: throw new NullPointerException("uiComponent");
060:
061: if (value == null) {
062: return;
063: }
064:
065: int length = value instanceof String ? ((String) value)
066: .length() : value.toString().length();
067:
068: if (_minimum != null) {
069: if (length < _minimum.intValue()) {
070: Object[] args = {
071: _minimum,
072: _MessageUtils.getLabel(facesContext,
073: uiComponent) };
074: throw new ValidatorException(_MessageUtils
075: .getErrorMessage(facesContext,
076: MINIMUM_MESSAGE_ID, args));
077: }
078: }
079:
080: if (_maximum != null) {
081: if (length > _maximum.intValue()) {
082: Object[] args = {
083: _maximum,
084: _MessageUtils.getLabel(facesContext,
085: uiComponent) };
086: throw new ValidatorException(_MessageUtils
087: .getErrorMessage(facesContext,
088: MAXIMUM_MESSAGE_ID, args));
089: }
090: }
091: }
092:
093: // SETTER & GETTER
094: public int getMaximum() {
095: return _maximum != null ? _maximum.intValue()
096: : Integer.MAX_VALUE;
097: }
098:
099: public void setMaximum(int maximum) {
100: _maximum = new Integer(maximum);
101: }
102:
103: public int getMinimum() {
104: return _minimum != null ? _minimum.intValue() : 0;
105: }
106:
107: public void setMinimum(int minimum) {
108: _minimum = new Integer(minimum);
109: }
110:
111: public boolean isTransient() {
112: return _transient;
113: }
114:
115: public void setTransient(boolean transientValue) {
116: _transient = transientValue;
117: }
118:
119: // RESTORE & SAVE STATE
120: public Object saveState(FacesContext context) {
121: Object values[] = new Object[2];
122: values[0] = _maximum;
123: values[1] = _minimum;
124: return values;
125: }
126:
127: public void restoreState(FacesContext context, Object state) {
128: Object values[] = (Object[]) state;
129: _maximum = (Integer) values[0];
130: _minimum = (Integer) values[1];
131: }
132:
133: // MISC
134: public boolean equals(Object o) {
135: if (this == o)
136: return true;
137: if (!(o instanceof LengthValidator))
138: return false;
139:
140: final LengthValidator lengthValidator = (LengthValidator) o;
141:
142: if (_maximum != null ? !_maximum
143: .equals(lengthValidator._maximum)
144: : lengthValidator._maximum != null)
145: return false;
146: if (_minimum != null ? !_minimum
147: .equals(lengthValidator._minimum)
148: : lengthValidator._minimum != null)
149: return false;
150:
151: return true;
152: }
153:
154: }
|