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