1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.commons.validator;
20
21 import java.io.Serializable;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Locale;
25 import java.util.Map;
26
27 import javax.el.ValueExpression;
28 import javax.faces.application.FacesMessage;
29 import javax.faces.component.PartialStateHolder;
30 import javax.faces.component.StateHelper;
31 import javax.faces.component.StateHolder;
32 import javax.faces.context.FacesContext;
33 import javax.faces.validator.Validator;
34
35 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
36 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFValidator;
37 import org.apache.myfaces.commons.util.MessageUtils;
38
39
40
41
42
43 @JSFValidator(
44 configExcluded = true,
45 evaluateELOnExecution = true,
46 tagClass = "org.apache.myfaces.commons.validator.ValidatorBaseTag",
47 tagHandler = "org.apache.myfaces.commons.validator.ValidatorBaseTagHandler")
48 public abstract class ValidatorBase implements PartialStateHolder, Validator {
49
50 private boolean _transient = false;
51
52 private transient FacesContext _facesContext;
53 private StateHelper _stateHelper = null;
54 private boolean _initialStateMarked = false;
55
56
57
58
59
60
61 @JSFProperty
62 public String getSummaryMessage()
63 {
64 return (String) getStateHelper().eval(PropertyKeys.summaryMessage);
65 }
66
67
68
69
70
71 public void setSummaryMessage(String message) {
72 getStateHelper().put(PropertyKeys.summaryMessage, message);
73 }
74
75
76
77
78
79
80
81
82 @JSFProperty
83 public String getMessage() {
84 return getDetailMessage();
85 }
86
87
88
89
90
91
92 public void setMessage(String message) {
93 setDetailMessage(message);
94 }
95
96
97
98
99
100
101
102
103 @JSFProperty
104 public String getDetailMessage() {
105 return (String) getStateHelper().eval(PropertyKeys.detailMessage);
106 }
107
108
109
110
111
112 public void setDetailMessage(String message) {
113 getStateHelper().put(PropertyKeys.detailMessage, message);
114 }
115
116
117
118
119
120 public Object saveState(FacesContext context)
121 {
122 if (context == null)
123 {
124 throw new NullPointerException ("context");
125 }
126
127 StateHelper stateHelper = getStateHelper(false);
128 if (stateHelper != null)
129 {
130 return stateHelper.saveState(context);
131 }
132 else
133 {
134 return null;
135 }
136 }
137
138 public void restoreState(FacesContext context, Object state)
139 {
140 getStateHelper().restoreState(context, state);
141 }
142
143 public boolean isTransient() {
144 return _transient;
145 }
146
147 public void setTransient(boolean newTransientValue) {
148 _transient = newTransientValue;
149 }
150
151
152
153
154
155
156
157
158 protected FacesMessage getFacesMessage(String defaultMessage, Object[] args) {
159 FacesMessage msg;
160
161 if (getSummaryMessage() == null && getDetailMessage() == null)
162 {
163 msg = MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR, defaultMessage, args);
164 } else {
165 Locale locale = MessageUtils.getCurrentLocale();
166 String summaryText = MessageUtils.substituteParams(locale, getSummaryMessage(), args);
167 String detailText = MessageUtils.substituteParams(locale, getDetailMessage(), args);
168 msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, summaryText, detailText);
169 }
170 return msg;
171 }
172
173
174
175 @SuppressWarnings("unchecked")
176 public ValueExpression getValueExpression(String name)
177 {
178 if (name == null) throw new NullPointerException("name");
179 StateHelper helper = getStateHelper(false);
180 if (helper == null)
181 {
182 return null;
183 }
184 Map<String,Object> bindings = (Map<String,Object>) helper.get(PropertyKeys.bindings);
185 if (bindings == null)
186 {
187 return null;
188 }
189 else
190 {
191 return (ValueExpression) bindings.get(name);
192 }
193 }
194
195 public void setValueExpression(String name, ValueExpression expression)
196 {
197 if (name == null) throw new NullPointerException("name");
198 if (expression == null) {
199 getStateHelper().remove(PropertyKeys.bindings, name);
200 } else {
201 getStateHelper().put(PropertyKeys.bindings, name, expression);
202 }
203 }
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230 public static Object saveAttachedState(FacesContext context, Object attachedObject)
231 {
232 if (context == null)
233 {
234 throw new NullPointerException ("context");
235 }
236
237 if (attachedObject == null)
238 return null;
239
240
241 if (attachedObject instanceof StateHolder)
242 {
243 StateHolder holder = (StateHolder) attachedObject;
244 if (holder.isTransient())
245 {
246 return null;
247 }
248
249 return new _AttachedStateWrapper(attachedObject.getClass(), holder.saveState(context));
250 }
251 else if (attachedObject instanceof List)
252 {
253 List<Object> lst = new ArrayList<Object>(((List<?>) attachedObject).size());
254 for (Object item : (List<?>) attachedObject)
255 {
256 if (item != null)
257 {
258 lst.add(saveAttachedState(context, item));
259 }
260 }
261
262 return new _AttachedListStateWrapper(lst);
263 }
264 else if (attachedObject instanceof Serializable)
265 {
266 return attachedObject;
267 }
268 else
269 {
270 return new _AttachedStateWrapper(attachedObject.getClass(), null);
271 }
272 }
273
274 @SuppressWarnings("unchecked")
275 public static Object restoreAttachedState(FacesContext context, Object stateObj) throws IllegalStateException
276 {
277 if (context == null)
278 throw new NullPointerException("context");
279 if (stateObj == null)
280 return null;
281 if (stateObj instanceof _AttachedListStateWrapper)
282 {
283 List<Object> lst = ((_AttachedListStateWrapper) stateObj).getWrappedStateList();
284 List<Object> restoredList = new ArrayList<Object>(lst.size());
285 for (Object item : lst)
286 {
287 restoredList.add(restoreAttachedState(context, item));
288 }
289 return restoredList;
290 }
291 else if (stateObj instanceof _AttachedStateWrapper)
292 {
293 Class<?> clazz = ((_AttachedStateWrapper) stateObj).getClazz();
294 Object restoredObject;
295 try
296 {
297 restoredObject = clazz.newInstance();
298 }
299 catch (InstantiationException e)
300 {
301 throw new RuntimeException("Could not restore StateHolder of type " + clazz.getName()
302 + " (missing no-args constructor?)", e);
303 }
304 catch (IllegalAccessException e)
305 {
306 throw new RuntimeException(e);
307 }
308 if (restoredObject instanceof StateHolder)
309 {
310 _AttachedStateWrapper wrapper = (_AttachedStateWrapper) stateObj;
311 Object wrappedState = wrapper.getWrappedStateObject();
312
313 StateHolder holder = (StateHolder) restoredObject;
314 holder.restoreState(context, wrappedState);
315 }
316 return restoredObject;
317 }
318 else
319 {
320 return stateObj;
321 }
322 }
323
324 protected FacesContext getFacesContext()
325 {
326 if (_facesContext == null)
327 {
328 return FacesContext.getCurrentInstance();
329 }
330 else
331 {
332 return _facesContext;
333 }
334 }
335
336 boolean isCachedFacesContext()
337 {
338 return _facesContext != null;
339 }
340
341 void setCachedFacesContext(FacesContext facesContext)
342 {
343 _facesContext = facesContext;
344 }
345
346 protected StateHelper getStateHelper() {
347 return getStateHelper(true);
348 }
349
350
351
352
353
354
355
356 protected StateHelper getStateHelper(boolean create) {
357 if(_stateHelper != null) {
358 return _stateHelper;
359 }
360 if(create) {
361 _stateHelper = new _DeltaStateHelper(this);
362 }
363 return _stateHelper;
364 }
365
366 public void clearInitialState()
367 {
368 _initialStateMarked = false;
369 }
370
371 public boolean initialStateMarked()
372 {
373 return _initialStateMarked;
374 }
375
376 public void markInitialState()
377 {
378 _initialStateMarked = true;
379 }
380
381 protected String getStringValue(FacesContext context, ValueExpression vb)
382 {
383 Object value = vb.getValue(context.getELContext());
384 if (value != null)
385 {
386 return value.toString();
387 }
388 return null;
389 }
390
391 enum PropertyKeys
392 {
393 bindings,
394 summaryMessage,
395 detailMessage
396 }
397 }