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 | 0 | private boolean isTransient = false; |
39 | |
|
40 | |
|
41 | 0 | public MethodExpressionValidator() { |
42 | 0 | } |
43 | |
|
44 | 0 | public MethodExpressionValidator(MethodExpression methodExpression) { |
45 | 0 | if (methodExpression == null) throw new NullPointerException("methodExpression can not be null."); |
46 | |
|
47 | 0 | this.methodExpression = methodExpression; |
48 | 0 | } |
49 | |
|
50 | |
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { |
51 | 0 | Object[] params = new Object[3]; |
52 | 0 | params[0] = context; |
53 | 0 | params[1] = component; |
54 | 0 | params[2] = value; |
55 | |
|
56 | |
try { |
57 | 0 | methodExpression.invoke(context.getELContext(), params); |
58 | |
} |
59 | 0 | catch (ELException e) { |
60 | 0 | Throwable cause = e.getCause(); |
61 | 0 | if (cause instanceof ValidatorException) { |
62 | 0 | throw (ValidatorException) cause; |
63 | |
} |
64 | |
else { |
65 | 0 | throw e; |
66 | |
} |
67 | 0 | } |
68 | 0 | } |
69 | |
|
70 | |
public void restoreState(FacesContext context, Object state) { |
71 | 0 | methodExpression = (MethodExpression)state; |
72 | 0 | } |
73 | |
|
74 | |
public Object saveState(FacesContext context) { |
75 | 0 | return methodExpression; |
76 | |
} |
77 | |
|
78 | |
public void setTransient(boolean newTransientValue) { |
79 | 0 | isTransient = newTransientValue; |
80 | 0 | } |
81 | |
|
82 | |
public boolean isTransient() { |
83 | 0 | return isTransient; |
84 | |
} |
85 | |
|
86 | |
} |