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 | 0 | private Integer _minimum = null; |
62 | 0 | private Integer _maximum = null; |
63 | 0 | private boolean _transient = false; |
64 | |
|
65 | |
|
66 | |
public LengthValidator() |
67 | 0 | { |
68 | 0 | } |
69 | |
|
70 | |
public LengthValidator(int maximum) |
71 | 0 | { |
72 | 0 | _maximum = new Integer(maximum); |
73 | 0 | } |
74 | |
|
75 | |
public LengthValidator(int maximum, |
76 | |
int minimum) |
77 | 0 | { |
78 | 0 | _maximum = new Integer(maximum); |
79 | 0 | _minimum = new Integer(minimum); |
80 | 0 | } |
81 | |
|
82 | |
|
83 | |
public void validate(FacesContext facesContext, |
84 | |
UIComponent uiComponent, |
85 | |
Object value) |
86 | |
throws ValidatorException |
87 | |
{ |
88 | 0 | if (facesContext == null) throw new NullPointerException("facesContext"); |
89 | 0 | if (uiComponent == null) throw new NullPointerException("uiComponent"); |
90 | |
|
91 | 0 | if (value == null) |
92 | |
{ |
93 | 0 | return; |
94 | |
} |
95 | |
|
96 | 0 | int length = value instanceof String ? |
97 | |
((String)value).length() : value.toString().length(); |
98 | |
|
99 | 0 | if (_minimum != null) |
100 | |
{ |
101 | 0 | if (length < _minimum.intValue()) |
102 | |
{ |
103 | 0 | Object[] args = {_minimum,_MessageUtils.getLabel(facesContext, uiComponent)}; |
104 | 0 | throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MINIMUM_MESSAGE_ID, args)); |
105 | |
} |
106 | |
} |
107 | |
|
108 | 0 | if (_maximum != null) |
109 | |
{ |
110 | 0 | if (length > _maximum.intValue()) |
111 | |
{ |
112 | 0 | Object[] args = {_maximum,_MessageUtils.getLabel(facesContext, uiComponent)}; |
113 | 0 | throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MAXIMUM_MESSAGE_ID, args)); |
114 | |
} |
115 | |
} |
116 | 0 | } |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
@JSFProperty |
125 | |
public int getMaximum() |
126 | |
{ |
127 | 0 | return _maximum != null ? _maximum.intValue() : 0; |
128 | |
} |
129 | |
|
130 | |
public void setMaximum(int maximum) |
131 | |
{ |
132 | 0 | _maximum = new Integer(maximum); |
133 | 0 | } |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
@JSFProperty |
140 | |
public int getMinimum() |
141 | |
{ |
142 | 0 | return _minimum != null ? _minimum.intValue() : 0; |
143 | |
} |
144 | |
|
145 | |
public void setMinimum(int minimum) |
146 | |
{ |
147 | 0 | _minimum = new Integer(minimum); |
148 | 0 | } |
149 | |
|
150 | |
public boolean isTransient() |
151 | |
{ |
152 | 0 | return _transient; |
153 | |
} |
154 | |
|
155 | |
public void setTransient(boolean transientValue) |
156 | |
{ |
157 | 0 | _transient = transientValue; |
158 | 0 | } |
159 | |
|
160 | |
|
161 | |
public Object saveState(FacesContext context) |
162 | |
{ |
163 | 0 | Object values[] = new Object[2]; |
164 | 0 | values[0] = _maximum; |
165 | 0 | values[1] = _minimum; |
166 | 0 | return values; |
167 | |
} |
168 | |
|
169 | |
public void restoreState(FacesContext context, |
170 | |
Object state) |
171 | |
{ |
172 | 0 | Object values[] = (Object[])state; |
173 | 0 | _maximum = (Integer)values[0]; |
174 | 0 | _minimum = (Integer)values[1]; |
175 | 0 | } |
176 | |
|
177 | |
|
178 | |
public boolean equals(Object o) |
179 | |
{ |
180 | 0 | if (this == o) return true; |
181 | 0 | if (!(o instanceof LengthValidator)) return false; |
182 | |
|
183 | 0 | final LengthValidator lengthValidator = (LengthValidator)o; |
184 | |
|
185 | 0 | if (_maximum != null ? !_maximum.equals(lengthValidator._maximum) : lengthValidator._maximum != null) return false; |
186 | 0 | if (_minimum != null ? !_minimum.equals(lengthValidator._minimum) : lengthValidator._minimum != null) return false; |
187 | |
|
188 | 0 | return true; |
189 | |
} |
190 | |
|
191 | |
} |