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 org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFJspProperty;
22 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
23 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFValidator;
24
25 import javax.faces.component.PartialStateHolder;
26 import javax.faces.component.UIComponent;
27 import javax.faces.context.FacesContext;
28 import java.util.regex.Pattern;
29 import java.util.regex.PatternSyntaxException;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 @JSFValidator(
74 name="f:validateRegex",
75 bodyContent="empty",
76 tagClass="org.apache.myfaces.taglib.core.ValidateRegexTag")
77 @JSFJspProperty(
78 name="binding",
79 returnType = "javax.faces.validator.RegexValidator",
80 longDesc = "A ValueExpression that evaluates to a RegexValidator.")
81 public class RegexValidator implements Validator, PartialStateHolder
82 {
83
84
85
86
87 public static final String VALIDATOR_ID = "javax.faces.RegularExpression";
88
89
90
91
92 public static final String PATTERN_NOT_SET_MESSAGE_ID = "javax.faces.validator.RegexValidator.PATTERN_NOT_SET";
93
94
95
96
97
98 public static final String NOT_MATCHED_MESSAGE_ID = "javax.faces.validator.RegexValidator.NOT_MATCHED";
99
100
101
102
103
104 public static final String MATCH_EXCEPTION_MESSAGE_ID = "javax.faces.validator.RegexValidator.MATCH_EXCEPTION";
105
106
107 private static final String EMPTY_STRING = "";
108
109 private String pattern;
110
111 private boolean isTransient = false;
112
113
114
115 public void validate(FacesContext context,
116 UIComponent component,
117 Object value)
118 {
119 if (context == null) throw new NullPointerException("context");
120 if (component == null) throw new NullPointerException("component");
121
122 if (value == null)
123 {
124 return;
125 }
126 if (!(value instanceof String))
127 {
128 throw new ValidatorException(_MessageUtils.getErrorMessage(context, NOT_MATCHED_MESSAGE_ID, null));
129 }
130
131 String string = (String) value;
132
133 Pattern thePattern;
134 if (pattern == null
135 || pattern.equals(EMPTY_STRING))
136 {
137 throw new ValidatorException(_MessageUtils.getErrorMessage(context, PATTERN_NOT_SET_MESSAGE_ID, null));
138 }
139
140 try
141 {
142 thePattern = Pattern.compile(pattern);
143 }
144 catch (PatternSyntaxException pse)
145 {
146 throw new ValidatorException(_MessageUtils.getErrorMessage(context, MATCH_EXCEPTION_MESSAGE_ID, null));
147 }
148
149 if (!thePattern.matcher(string).matches())
150 {
151
152 Object[] args = {thePattern, _MessageUtils.getLabel(context, component)};
153 throw new ValidatorException(_MessageUtils.getErrorMessage(context, NOT_MATCHED_MESSAGE_ID, args));
154 }
155 }
156
157
158
159
160 public Object saveState(FacesContext context)
161 {
162 if (!initialStateMarked())
163 {
164 return pattern;
165 }
166 return null;
167 }
168
169
170 public void restoreState(FacesContext context, Object state)
171 {
172 if (state != null)
173 {
174
175
176 this.pattern = (String) state;
177 }
178 }
179
180
181
182
183 public boolean isTransient()
184 {
185 return isTransient;
186 }
187
188
189 public void setTransient(boolean isTransient)
190 {
191 this.isTransient = isTransient;
192 }
193
194
195
196
197
198
199
200 public void setPattern(String pattern)
201 {
202
203 this.pattern = pattern;
204 clearInitialState();
205 }
206
207
208
209
210
211
212 @JSFProperty(required = true)
213 public String getPattern()
214 {
215 return this.pattern;
216 }
217
218 private boolean _initialStateMarked = false;
219
220 public void clearInitialState()
221 {
222 _initialStateMarked = false;
223 }
224
225 public boolean initialStateMarked()
226 {
227 return _initialStateMarked;
228 }
229
230 public void markInitialState()
231 {
232 _initialStateMarked = true;
233 }
234
235 @JSFProperty(faceletsOnly=true)
236 private Boolean isDisabled()
237 {
238 return null;
239 }
240
241 @JSFProperty(faceletsOnly=true)
242 private String getFor()
243 {
244 return null;
245 }
246 }