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 DoubleRangeValidator implements Validator, StateHolder {
030: // FIELDS
031: public static final String VALIDATOR_ID = "javax.faces.DoubleRange";
032: public static final String MAXIMUM_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.MAXIMUM";
033: public static final String MINIMUM_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.MINIMUM";
034: public static final String TYPE_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.TYPE";
035: public static final String NOT_IN_RANGE_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.NOT_IN_RANGE";
036:
037: private Double _minimum = null;
038: private Double _maximum = null;
039: private boolean _transient = false;
040:
041: // CONSTRUCTORS
042: public DoubleRangeValidator() {
043: }
044:
045: public DoubleRangeValidator(double maximum) {
046: _maximum = new Double(maximum);
047: }
048:
049: public DoubleRangeValidator(double maximum, double minimum) {
050: _maximum = new Double(maximum);
051: _minimum = new Double(minimum);
052: }
053:
054: // VALIDATE
055: public void validate(FacesContext facesContext,
056: UIComponent uiComponent, Object value)
057: throws ValidatorException {
058: if (facesContext == null)
059: throw new NullPointerException("facesContext");
060: if (uiComponent == null)
061: throw new NullPointerException("uiComponent");
062:
063: if (value == null) {
064: return;
065: }
066:
067: double dvalue = parseDoubleValue(facesContext, uiComponent,
068: value);
069: if (_minimum != null && _maximum != null) {
070: if (dvalue < _minimum.doubleValue()
071: || dvalue > _maximum.doubleValue()) {
072: Object[] args = {
073: _minimum,
074: _maximum,
075: _MessageUtils.getLabel(facesContext,
076: uiComponent) };
077: throw new ValidatorException(_MessageUtils
078: .getErrorMessage(facesContext,
079: NOT_IN_RANGE_MESSAGE_ID, args));
080: }
081: } else if (_minimum != null) {
082: if (dvalue < _minimum.doubleValue()) {
083: Object[] args = {
084: _minimum,
085: _MessageUtils.getLabel(facesContext,
086: uiComponent) };
087: throw new ValidatorException(_MessageUtils
088: .getErrorMessage(facesContext,
089: MINIMUM_MESSAGE_ID, args));
090: }
091: } else if (_maximum != null) {
092: if (dvalue > _maximum.doubleValue()) {
093: Object[] args = {
094: _maximum,
095: _MessageUtils.getLabel(facesContext,
096: uiComponent) };
097: throw new ValidatorException(_MessageUtils
098: .getErrorMessage(facesContext,
099: MAXIMUM_MESSAGE_ID, args));
100: }
101: }
102: }
103:
104: private double parseDoubleValue(FacesContext facesContext,
105: UIComponent uiComponent, Object value)
106: throws ValidatorException {
107: if (value instanceof Number) {
108: return ((Number) value).doubleValue();
109: }
110:
111: try {
112: return Double.parseDouble(value.toString());
113: } catch (NumberFormatException e) {
114: Object[] args = { _MessageUtils.getLabel(facesContext,
115: uiComponent) };
116: throw new ValidatorException(_MessageUtils.getErrorMessage(
117: facesContext, TYPE_MESSAGE_ID, args));
118: }
119: }
120:
121: // GETTER & SETTER
122: public double getMaximum() {
123: return _maximum != null ? _maximum.doubleValue()
124: : Double.MAX_VALUE;
125: }
126:
127: public void setMaximum(double maximum) {
128: _maximum = new Double(maximum);
129: }
130:
131: public double getMinimum() {
132: return _minimum != null ? _minimum.doubleValue()
133: : Double.MIN_VALUE;
134: }
135:
136: public void setMinimum(double minimum) {
137: _minimum = new Double(minimum);
138: }
139:
140: // RESTORE/SAVE STATE
141: public Object saveState(FacesContext context) {
142: Object values[] = new Object[2];
143: values[0] = _maximum;
144: values[1] = _minimum;
145: return values;
146: }
147:
148: public void restoreState(FacesContext context, Object state) {
149: Object values[] = (Object[]) state;
150: _maximum = (Double) values[0];
151: _minimum = (Double) values[1];
152: }
153:
154: public boolean isTransient() {
155: return _transient;
156: }
157:
158: public void setTransient(boolean transientValue) {
159: _transient = transientValue;
160: }
161:
162: // MISC
163: public boolean equals(Object o) {
164: if (this == o)
165: return true;
166: if (!(o instanceof DoubleRangeValidator))
167: return false;
168:
169: final DoubleRangeValidator doubleRangeValidator = (DoubleRangeValidator) o;
170:
171: if (_maximum != null ? !_maximum
172: .equals(doubleRangeValidator._maximum)
173: : doubleRangeValidator._maximum != null)
174: return false;
175: if (_minimum != null ? !_minimum
176: .equals(doubleRangeValidator._minimum)
177: : doubleRangeValidator._minimum != null)
178: return false;
179:
180: return true;
181: }
182:
183: }
|