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 LengthValidator implements Validator, StateHolder {
036: public static final String MAXIMUM_MESSAGE_ID = "javax.faces.validator.LengthValidator.MAXIMUM";
037: public static final String MINIMUM_MESSAGE_ID = "javax.faces.validator.LengthValidator.MINIMUM";
038: public static final String VALIDATOR_ID = "javax.faces.Length";
039:
040: private int _minimum = 0;
041: private int _maximum = Integer.MAX_VALUE;
042: private boolean _isTransient;
043:
044: public LengthValidator() {
045: }
046:
047: public LengthValidator(int maximum) {
048: _maximum = maximum;
049: }
050:
051: public LengthValidator(int maximum, int minimum) {
052: _maximum = maximum;
053: _minimum = minimum;
054: }
055:
056: public int getMinimum() {
057: return _minimum;
058: }
059:
060: public void setMinimum(int min) {
061: _minimum = min;
062: }
063:
064: public int getMaximum() {
065: return _maximum;
066: }
067:
068: public void setMaximum(int max) {
069: _maximum = max;
070: }
071:
072: public boolean isTransient() {
073: return _isTransient;
074: }
075:
076: public void setTransient(boolean isTransient) {
077: _isTransient = isTransient;
078: }
079:
080: public void validate(FacesContext context, UIComponent component,
081: Object value) throws ValidatorException {
082: if (context == null || component == null)
083: throw new NullPointerException();
084:
085: if (value == null)
086: return;
087:
088: String str = value.toString();
089:
090: int len = str.length();
091:
092: if (len < getMinimum()) {
093: String summary = Util
094: .l10n(
095: context,
096: MINIMUM_MESSAGE_ID,
097: "{1}: Validation Error: Value is less than allowable minimum of '{0}'.",
098: getMinimum(), Util.getLabel(context,
099: component));
100:
101: String detail = summary;
102:
103: FacesMessage msg = new FacesMessage(summary, detail);
104:
105: throw new ValidatorException(msg);
106: }
107:
108: if (getMaximum() < len) {
109: String summary = Util
110: .l10n(
111: context,
112: MAXIMUM_MESSAGE_ID,
113: "{1}: Validation Error: Value is greater than allowable maximum of '{0}'.",
114: getMaximum(), Util.getLabel(context,
115: component));
116:
117: String detail = summary;
118:
119: FacesMessage msg = new FacesMessage(summary, detail);
120:
121: throw new ValidatorException(msg);
122: }
123: }
124:
125: public Object saveState(FacesContext context) {
126: return new Object[] { _minimum, _maximum };
127: }
128:
129: public void restoreState(FacesContext context, Object state) {
130: Object[] stateV = (Object[]) state;
131:
132: _minimum = (Integer) stateV[0];
133: _maximum = (Integer) stateV[1];
134: }
135:
136: public int hashCode() {
137: return 65521 * (int) _minimum + (int) _maximum;
138: }
139:
140: public boolean equals(Object o) {
141: if (this == o)
142: return true;
143: else if (!(o instanceof LengthValidator))
144: return false;
145:
146: LengthValidator validator = (LengthValidator) o;
147:
148: return (_minimum == validator._minimum && _maximum == validator._maximum);
149: }
150: }
|