001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License version 2
011: * as published by the Free Software Foundation.
012: *
013: * Resin Open Source is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
016: * of NON-INFRINGEMENT. See the GNU General Public License for more
017: * details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with Resin Open Source; if not, write to the
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package javax.faces.validator;
030:
031: import javax.faces.application.*;
032: import javax.faces.component.*;
033: import javax.faces.context.*;
034:
035: public class DoubleRangeValidator implements Validator, StateHolder {
036: public static final String MAXIMUM_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.MAXIMUM";
037: public static final String MINIMUM_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.MINIMUM";
038: public static final String NOT_IN_RANGE_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.NOT_IN_RANGE";
039: public static final String TYPE_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.TYPE";
040: public static final String VALIDATOR_ID = "javax.faces.DoubleRange";
041:
042: private double _minimum = Double.MIN_VALUE;
043: private double _maximum = Double.MAX_VALUE;
044: private boolean _isTransient;
045:
046: public DoubleRangeValidator() {
047: }
048:
049: public DoubleRangeValidator(double maximum) {
050: _maximum = maximum;
051: }
052:
053: public DoubleRangeValidator(double maximum, double minimum) {
054: _maximum = maximum;
055: _minimum = minimum;
056: }
057:
058: public double getMinimum() {
059: return _minimum;
060: }
061:
062: public void setMinimum(double min) {
063: _minimum = min;
064: }
065:
066: public double getMaximum() {
067: return _maximum;
068: }
069:
070: public void setMaximum(double max) {
071: _maximum = max;
072: }
073:
074: public boolean isTransient() {
075: return _isTransient;
076: }
077:
078: public void setTransient(boolean isTransient) {
079: _isTransient = isTransient;
080: }
081:
082: public void validate(FacesContext context, UIComponent component,
083: Object value) throws ValidatorException {
084: if (context == null || component == null)
085: throw new NullPointerException();
086:
087: if (value == null)
088: return;
089:
090: if (value instanceof String) {
091: try {
092: value = Double.parseDouble((String) value);
093: } catch (Exception e) {
094: String summary = Util
095: .l10n(
096: context,
097: TYPE_MESSAGE_ID,
098: "{0}: Validation Error: Value is not of the correct type.",
099: Util.getLabel(context, component));
100:
101: String detail = summary;
102:
103: FacesMessage msg = new FacesMessage(summary, detail);
104:
105: throw new ValidatorException(msg, e);
106: }
107: }
108:
109: if (!(value instanceof Number)) {
110: String summary = Util
111: .l10n(
112: context,
113: TYPE_MESSAGE_ID,
114: "{0}: Validation Error: Value is not of the correct type.",
115: Util.getLabel(context, component));
116:
117: String detail = summary;
118:
119: FacesMessage msg = new FacesMessage(summary, detail);
120:
121: throw new ValidatorException(msg);
122: }
123:
124: Number v = (Number) value;
125:
126: if (v.doubleValue() < getMinimum()) {
127: String summary = Util
128: .l10n(
129: context,
130: MINIMUM_MESSAGE_ID,
131: "{1}: Validation Error: Value is less than allowable minimum of '{0}'.",
132: getMinimum(), Util.getLabel(context,
133: component));
134:
135: String detail = summary;
136:
137: FacesMessage msg = new FacesMessage(summary, detail);
138:
139: throw new ValidatorException(msg);
140: }
141:
142: if (getMaximum() < v.doubleValue()) {
143: String summary = Util
144: .l10n(
145: context,
146: MAXIMUM_MESSAGE_ID,
147: "{1}: Validation Error: Value is greater than allowable maximum of '{0}'.",
148: getMaximum(), Util.getLabel(context,
149: component));
150:
151: String detail = summary;
152:
153: FacesMessage msg = new FacesMessage(summary, detail);
154:
155: throw new ValidatorException(msg);
156: }
157: }
158:
159: public Object saveState(FacesContext context) {
160: return new Object[] { _minimum, _maximum };
161: }
162:
163: public void restoreState(FacesContext context, Object state) {
164: Object[] stateV = (Object[]) state;
165:
166: _minimum = (Double) stateV[0];
167: _maximum = (Double) stateV[1];
168: }
169:
170: public int hashCode() {
171: return 65521 * (int) _minimum + (int) _maximum;
172: }
173:
174: public boolean equals(Object o) {
175: if (this == o)
176: return true;
177: else if (!(o instanceof DoubleRangeValidator))
178: return false;
179:
180: DoubleRangeValidator validator = (DoubleRangeValidator) o;
181:
182: return (_minimum == validator._minimum && _maximum == validator._maximum);
183: }
184: }
|