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