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