1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package javax.faces.validator;
20
21 import javax.faces.component.StateHolder;
22 import javax.faces.component.UIComponent;
23 import javax.faces.context.FacesContext;
24
25 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFJspProperty;
26 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
27 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFValidator;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 @JSFValidator(
46 name="f:validateLength",
47 bodyContent="empty",
48 tagClass="org.apache.myfaces.taglib.core.ValidateLengthTag")
49 @JSFJspProperty(
50 name="binding",
51 returnType = "javax.faces.validator.LengthValidator",
52 longDesc = "A ValueExpression that evaluates to a LengthValidator.")
53 public class LengthValidator
54 implements Validator, StateHolder
55 {
56
57 public static final String MAXIMUM_MESSAGE_ID = "javax.faces.validator.LengthValidator.MAXIMUM";
58 public static final String MINIMUM_MESSAGE_ID = "javax.faces.validator.LengthValidator.MINIMUM";
59 public static final String VALIDATOR_ID = "javax.faces.Length";
60
61 private Integer _minimum = null;
62 private Integer _maximum = null;
63 private boolean _transient = false;
64
65
66 public LengthValidator()
67 {
68 }
69
70 public LengthValidator(int maximum)
71 {
72 _maximum = new Integer(maximum);
73 }
74
75 public LengthValidator(int maximum,
76 int minimum)
77 {
78 _maximum = new Integer(maximum);
79 _minimum = new Integer(minimum);
80 }
81
82
83 public void validate(FacesContext facesContext,
84 UIComponent uiComponent,
85 Object value)
86 throws ValidatorException
87 {
88 if (facesContext == null) throw new NullPointerException("facesContext");
89 if (uiComponent == null) throw new NullPointerException("uiComponent");
90
91 if (value == null)
92 {
93 return;
94 }
95
96 int length = value instanceof String ?
97 ((String)value).length() : value.toString().length();
98
99 if (_minimum != null)
100 {
101 if (length < _minimum.intValue())
102 {
103 Object[] args = {_minimum,_MessageUtils.getLabel(facesContext, uiComponent)};
104 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MINIMUM_MESSAGE_ID, args));
105 }
106 }
107
108 if (_maximum != null)
109 {
110 if (length > _maximum.intValue())
111 {
112 Object[] args = {_maximum,_MessageUtils.getLabel(facesContext, uiComponent)};
113 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MAXIMUM_MESSAGE_ID, args));
114 }
115 }
116 }
117
118
119
120
121
122
123
124 @JSFProperty
125 public int getMaximum()
126 {
127 return _maximum != null ? _maximum.intValue() : 0;
128 }
129
130 public void setMaximum(int maximum)
131 {
132 _maximum = new Integer(maximum);
133 }
134
135
136
137
138
139 @JSFProperty
140 public int getMinimum()
141 {
142 return _minimum != null ? _minimum.intValue() : 0;
143 }
144
145 public void setMinimum(int minimum)
146 {
147 _minimum = new Integer(minimum);
148 }
149
150 public boolean isTransient()
151 {
152 return _transient;
153 }
154
155 public void setTransient(boolean transientValue)
156 {
157 _transient = transientValue;
158 }
159
160
161 public Object saveState(FacesContext context)
162 {
163 Object values[] = new Object[2];
164 values[0] = _maximum;
165 values[1] = _minimum;
166 return values;
167 }
168
169 public void restoreState(FacesContext context,
170 Object state)
171 {
172 Object values[] = (Object[])state;
173 _maximum = (Integer)values[0];
174 _minimum = (Integer)values[1];
175 }
176
177
178 public boolean equals(Object o)
179 {
180 if (this == o) return true;
181 if (!(o instanceof LengthValidator)) return false;
182
183 final LengthValidator lengthValidator = (LengthValidator)o;
184
185 if (_maximum != null ? !_maximum.equals(lengthValidator._maximum) : lengthValidator._maximum != null) return false;
186 if (_minimum != null ? !_minimum.equals(lengthValidator._minimum) : lengthValidator._minimum != null) return false;
187
188 return true;
189 }
190
191 }