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)
120 {
121 throw new NullPointerException("context");
122 }
123 if (component == null)
124 {
125 throw new NullPointerException("component");
126 }
127
128 if (value == null)
129 {
130 return;
131 }
132 if (!(value instanceof String))
133 {
134 throw new ValidatorException(_MessageUtils.getErrorMessage(context, NOT_MATCHED_MESSAGE_ID, null));
135 }
136
137 String string = (String) value;
138
139 Pattern thePattern;
140 if (pattern == null
141 || pattern.equals(EMPTY_STRING))
142 {
143 throw new ValidatorException(_MessageUtils.getErrorMessage(context, PATTERN_NOT_SET_MESSAGE_ID, null));
144 }
145
146 try
147 {
148 thePattern = Pattern.compile(pattern);
149 }
150 catch (PatternSyntaxException pse)
151 {
152 throw new ValidatorException(_MessageUtils.getErrorMessage(context, MATCH_EXCEPTION_MESSAGE_ID, null));
153 }
154
155 if (!thePattern.matcher(string).matches())
156 {
157
158 Object[] args = {thePattern, _MessageUtils.getLabel(context, component)};
159 throw new ValidatorException(_MessageUtils.getErrorMessage(context, NOT_MATCHED_MESSAGE_ID, args));
160 }
161 }
162
163
164
165
166 public Object saveState(FacesContext context)
167 {
168 if (!initialStateMarked())
169 {
170 return pattern;
171 }
172 return null;
173 }
174
175
176 public void restoreState(FacesContext context, Object state)
177 {
178 if (state != null)
179 {
180
181
182 this.pattern = (String) state;
183 }
184 }
185
186
187
188
189 public boolean isTransient()
190 {
191 return isTransient;
192 }
193
194
195 public void setTransient(boolean isTransient)
196 {
197 this.isTransient = isTransient;
198 }
199
200
201
202
203
204
205
206 public void setPattern(String pattern)
207 {
208
209 this.pattern = pattern;
210 clearInitialState();
211 }
212
213
214
215
216
217
218 @JSFProperty(required = true)
219 public String getPattern()
220 {
221 return this.pattern;
222 }
223
224 private boolean _initialStateMarked = false;
225
226 public void clearInitialState()
227 {
228 _initialStateMarked = false;
229 }
230
231 public boolean initialStateMarked()
232 {
233 return _initialStateMarked;
234 }
235
236 public void markInitialState()
237 {
238 _initialStateMarked = true;
239 }
240
241 @JSFProperty(faceletsOnly=true)
242 private Boolean isDisabled()
243 {
244 return null;
245 }
246
247 @JSFProperty(faceletsOnly=true)
248 private String getFor()
249 {
250 return null;
251 }
252 }