1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
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 * see Javadoc of <a href="http://java.sun.com/j2ee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
30 *
31 * @author Stan Silvert
32 */
33 public class MethodExpressionValidator implements Validator, StateHolder
34 {
35
36 private MethodExpression methodExpression;
37
38 private boolean isTransient = false;
39
40 /** Creates a new instance of MethodExpressionValidator */
41 public MethodExpressionValidator()
42 {
43 }
44
45 public MethodExpressionValidator(MethodExpression methodExpression)
46 {
47 if (methodExpression == null)
48 {
49 throw new NullPointerException("methodExpression can not be null.");
50 }
51
52 this.methodExpression = methodExpression;
53 }
54
55 public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException
56 {
57 Object[] params = new Object[3];
58 params[0] = context;
59 params[1] = component;
60 params[2] = value;
61
62 try
63 {
64 methodExpression.invoke(context.getELContext(), params);
65 }
66 catch (ELException e)
67 {
68 Throwable cause = e.getCause();
69 ValidatorException vex = null;
70 if (cause != null)
71 {
72 do
73 {
74 if (cause != null && cause instanceof ValidatorException)
75 {
76 vex = (ValidatorException) cause;
77 break;
78 }
79 cause = cause.getCause();
80 }
81 while (cause != null);
82 }
83 if (vex != null)
84 {
85 throw vex;
86 }
87 else
88 {
89 throw e;
90 }
91 //Throwable cause = e.getCause();
92 //if (cause instanceof ValidatorException)
93 //{
94 // throw (ValidatorException)cause;
95 //}
96 //else
97 //{
98 // throw e;
99 //}
100 }
101 }
102
103 public void restoreState(FacesContext context, Object state)
104 {
105 methodExpression = (MethodExpression)state;
106 }
107
108 public Object saveState(FacesContext context)
109 {
110 return methodExpression;
111 }
112
113 public void setTransient(boolean newTransientValue)
114 {
115 isTransient = newTransientValue;
116 }
117
118 public boolean isTransient()
119 {
120 return isTransient;
121 }
122
123 }