1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package javax.faces.validator;
21
22 import javax.el.ELException;
23 import javax.el.MethodExpression;
24 import javax.faces.application.FacesMessage;
25 import javax.faces.component.StateHolder;
26 import javax.faces.component.UIComponent;
27 import javax.faces.context.FacesContext;
28
29
30
31
32
33
34 public class MethodExpressionValidator implements Validator, StateHolder {
35
36 private MethodExpression methodExpression;
37
38 private boolean isTransient = false;
39
40
41 public MethodExpressionValidator() {
42 }
43
44 public MethodExpressionValidator(MethodExpression methodExpression) {
45 if (methodExpression == null) throw new NullPointerException("methodExpression can not be null.");
46
47 this.methodExpression = methodExpression;
48 }
49
50 public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
51 Object[] params = new Object[3];
52 params[0] = context;
53 params[1] = component;
54 params[2] = value;
55
56 try {
57 methodExpression.invoke(context.getELContext(), params);
58 }
59 catch (ELException e) {
60 Throwable cause = e.getCause();
61 if (cause instanceof ValidatorException) {
62 throw (ValidatorException) cause;
63 }
64 else {
65 throw e;
66 }
67 }
68 }
69
70 public void restoreState(FacesContext context, Object state) {
71 methodExpression = (MethodExpression)state;
72 }
73
74 public Object saveState(FacesContext context) {
75 return methodExpression;
76 }
77
78 public void setTransient(boolean newTransientValue) {
79 isTransient = newTransientValue;
80 }
81
82 public boolean isTransient() {
83 return isTransient;
84 }
85
86 }