01: /*
02: * Copyright 2006 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package javax.faces.validator;
18:
19: import javax.el.ELException;
20: import javax.el.MethodExpression;
21: import javax.faces.application.FacesMessage;
22: import javax.faces.component.StateHolder;
23: import javax.faces.component.UIComponent;
24: import javax.faces.context.FacesContext;
25:
26: /**
27: * see Javadoc of <a href="http://java.sun.com/j2ee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
28: *
29: * @author Stan Silvert
30: */
31: public class MethodExpressionValidator implements Validator,
32: StateHolder {
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: public MethodExpressionValidator(MethodExpression methodExpression) {
43: if (methodExpression == null)
44: throw new NullPointerException(
45: "methodExpression can not be null.");
46:
47: this .methodExpression = methodExpression;
48: }
49:
50: public void validate(FacesContext context, UIComponent component,
51: Object value) throws ValidatorException {
52: Object[] params = new Object[3];
53: params[0] = context;
54: params[1] = component;
55: params[2] = value;
56:
57: try {
58: methodExpression.invoke(context.getELContext(), params);
59: } catch (ELException e) {
60: throw new ValidatorException(new FacesMessage(), e);
61: }
62: }
63:
64: public void restoreState(FacesContext context, Object state) {
65: methodExpression = (MethodExpression) state;
66: }
67:
68: public Object saveState(FacesContext context) {
69: return methodExpression;
70: }
71:
72: public void setTransient(boolean newTransientValue) {
73: isTransient = newTransientValue;
74: }
75:
76: public boolean isTransient() {
77: return isTransient;
78: }
79:
80: }
|