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.component.StateHolder; |
25 | |
import javax.faces.component.UIComponent; |
26 | |
import javax.faces.context.FacesContext; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public class MethodExpressionValidator implements Validator, StateHolder |
32 | |
{ |
33 | |
|
34 | |
private MethodExpression methodExpression; |
35 | |
|
36 | 0 | private boolean isTransient = false; |
37 | |
|
38 | |
|
39 | |
public MethodExpressionValidator() |
40 | 0 | { |
41 | 0 | } |
42 | |
|
43 | |
public MethodExpressionValidator(MethodExpression methodExpression) |
44 | 0 | { |
45 | 0 | if (methodExpression == null) |
46 | |
{ |
47 | 0 | throw new NullPointerException("methodExpression can not be null."); |
48 | |
} |
49 | |
|
50 | 0 | this.methodExpression = methodExpression; |
51 | 0 | } |
52 | |
|
53 | |
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException |
54 | |
{ |
55 | 0 | Object[] params = new Object[3]; |
56 | 0 | params[0] = context; |
57 | 0 | params[1] = component; |
58 | 0 | params[2] = value; |
59 | |
|
60 | |
try |
61 | |
{ |
62 | 0 | methodExpression.invoke(context.getELContext(), params); |
63 | |
} |
64 | 0 | catch (ELException e) |
65 | |
{ |
66 | 0 | Throwable cause = e.getCause(); |
67 | 0 | ValidatorException vex = null; |
68 | 0 | if (cause != null) |
69 | |
{ |
70 | |
do |
71 | |
{ |
72 | 0 | if (cause != null && cause instanceof ValidatorException) |
73 | |
{ |
74 | 0 | vex = (ValidatorException) cause; |
75 | 0 | break; |
76 | |
} |
77 | 0 | cause = cause.getCause(); |
78 | |
} |
79 | 0 | while (cause != null); |
80 | |
} |
81 | 0 | if (vex != null) |
82 | |
{ |
83 | 0 | throw vex; |
84 | |
} |
85 | |
else |
86 | |
{ |
87 | 0 | throw e; |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | 0 | } |
99 | 0 | } |
100 | |
|
101 | |
public void restoreState(FacesContext context, Object state) |
102 | |
{ |
103 | 0 | methodExpression = (MethodExpression)state; |
104 | 0 | } |
105 | |
|
106 | |
public Object saveState(FacesContext context) |
107 | |
{ |
108 | 0 | return methodExpression; |
109 | |
} |
110 | |
|
111 | |
public void setTransient(boolean newTransientValue) |
112 | |
{ |
113 | 0 | isTransient = newTransientValue; |
114 | 0 | } |
115 | |
|
116 | |
public boolean isTransient() |
117 | |
{ |
118 | 0 | return isTransient; |
119 | |
} |
120 | |
|
121 | |
} |