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.PartialStateHolder;
22 import javax.faces.component.StateHolder;
23 import javax.faces.component.UIComponent;
24 import javax.faces.context.FacesContext;
25
26 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFJspProperty;
27 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
28 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFValidator;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 @JSFValidator(
47 name="f:validateLength",
48 bodyContent="empty",
49 tagClass="org.apache.myfaces.taglib.core.ValidateLengthTag")
50 @JSFJspProperty(
51 name="binding",
52 returnType = "javax.faces.validator.LengthValidator",
53 longDesc = "A ValueExpression that evaluates to a LengthValidator.")
54 public class LengthValidator
55 implements Validator, PartialStateHolder
56 {
57
58 public static final String MAXIMUM_MESSAGE_ID = "javax.faces.validator.LengthValidator.MAXIMUM";
59 public static final String MINIMUM_MESSAGE_ID = "javax.faces.validator.LengthValidator.MINIMUM";
60 public static final String VALIDATOR_ID = "javax.faces.Length";
61
62 private Integer _minimum = null;
63 private Integer _maximum = null;
64 private boolean _transient = false;
65
66
67 public LengthValidator()
68 {
69 }
70
71 public LengthValidator(int maximum)
72 {
73 _maximum = Integer.valueOf(maximum);
74 }
75
76 public LengthValidator(int maximum,
77 int minimum)
78 {
79 _maximum = Integer.valueOf(maximum);
80 _minimum = Integer.valueOf(minimum);
81 }
82
83
84 public void validate(FacesContext facesContext,
85 UIComponent uiComponent,
86 Object value)
87 throws ValidatorException
88 {
89 if (facesContext == null) throw new NullPointerException("facesContext");
90 if (uiComponent == null) throw new NullPointerException("uiComponent");
91
92 if (value == null)
93 {
94 return;
95 }
96
97 int length = value instanceof String ?
98 ((String)value).length() : value.toString().length();
99
100 if (_minimum != null)
101 {
102 if (length < _minimum.intValue())
103 {
104 Object[] args = {_minimum,_MessageUtils.getLabel(facesContext, uiComponent)};
105 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MINIMUM_MESSAGE_ID, args));
106 }
107 }
108
109 if (_maximum != null)
110 {
111 if (length > _maximum.intValue())
112 {
113 Object[] args = {_maximum,_MessageUtils.getLabel(facesContext, uiComponent)};
114 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MAXIMUM_MESSAGE_ID, args));
115 }
116 }
117 }
118
119
120
121
122
123
124
125 @JSFProperty(deferredValueType="java.lang.Integer")
126 public int getMaximum()
127 {
128 return _maximum != null ? _maximum.intValue() : 0;
129 }
130
131 public void setMaximum(int maximum)
132 {
133 _maximum = Integer.valueOf(maximum);
134 clearInitialState();
135 }
136
137
138
139
140
141 @JSFProperty(deferredValueType="java.lang.Integer")
142 public int getMinimum()
143 {
144 return _minimum != null ? _minimum.intValue() : 0;
145 }
146
147 public void setMinimum(int minimum)
148 {
149 _minimum = Integer.valueOf(minimum);
150 clearInitialState();
151 }
152
153 public boolean isTransient()
154 {
155 return _transient;
156 }
157
158 public void setTransient(boolean transientValue)
159 {
160 _transient = transientValue;
161 }
162
163
164 public Object saveState(FacesContext context)
165 {
166 if (!initialStateMarked())
167 {
168 Object values[] = new Object[2];
169 values[0] = _maximum;
170 values[1] = _minimum;
171 return values;
172 }
173 return null;
174 }
175
176 public void restoreState(FacesContext context,
177 Object state)
178 {
179 if (state != null)
180 {
181 Object values[] = (Object[])state;
182 _maximum = (Integer)values[0];
183 _minimum = (Integer)values[1];
184 }
185 }
186
187
188 @Override
189 public boolean equals(Object o)
190 {
191 if (this == o) return true;
192 if (!(o instanceof LengthValidator)) return false;
193
194 final LengthValidator lengthValidator = (LengthValidator)o;
195
196 if (_maximum != null ? !_maximum.equals(lengthValidator._maximum) : lengthValidator._maximum != null) return false;
197 if (_minimum != null ? !_minimum.equals(lengthValidator._minimum) : lengthValidator._minimum != null) return false;
198
199 return true;
200 }
201
202 private boolean _initialStateMarked = false;
203
204 public void clearInitialState()
205 {
206 _initialStateMarked = false;
207 }
208
209 public boolean initialStateMarked()
210 {
211 return _initialStateMarked;
212 }
213
214 public void markInitialState()
215 {
216 _initialStateMarked = true;
217 }
218
219 @JSFProperty(faceletsOnly=true)
220 private Boolean isDisabled()
221 {
222 return null;
223 }
224
225 @JSFProperty(faceletsOnly=true)
226 private String getFor()
227 {
228 return null;
229 }
230 }