View Javadoc

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.application.FacesMessage;
25  import javax.faces.component.StateHolder;
26  import javax.faces.component.UIComponent;
27  import javax.faces.context.FacesContext;
28  
29  /**
30   * see Javadoc of <a href="http://java.sun.com/j2ee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
31   *
32   * @author Stan Silvert
33   */
34  public class MethodExpressionValidator implements Validator, StateHolder {
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      public MethodExpressionValidator(MethodExpression methodExpression) {
45          if (methodExpression == null) throw new NullPointerException("methodExpression can not be null.");
46          
47          this.methodExpression = methodExpression;
48      }
49  
50      public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
51          Object[] params = new Object[3];
52          params[0] = context;
53          params[1] = component;
54          params[2] = value;
55          
56          try {
57              methodExpression.invoke(context.getELContext(), params);
58          } 
59          catch (ELException e) {
60              Throwable cause = e.getCause();
61              if (cause instanceof ValidatorException) {
62                  throw (ValidatorException) cause;
63              }
64              else {
65                  throw e;
66              }
67          }
68      }
69  
70      public void restoreState(FacesContext context, Object state) {
71          methodExpression = (MethodExpression)state;
72      }
73  
74      public Object saveState(FacesContext context) {
75          return methodExpression;
76      }
77  
78      public void setTransient(boolean newTransientValue) {
79          isTransient = newTransientValue;
80      }
81  
82      public boolean isTransient() {
83          return isTransient;
84      }
85      
86  }