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  package org.apache.myfaces.view.facelets.el;
20  
21  import java.io.Externalizable;
22  import java.io.IOException;
23  import java.io.ObjectInput;
24  import java.io.ObjectOutput;
25  
26  import javax.el.ELContext;
27  import javax.el.MethodExpression;
28  import javax.el.ValueExpression;
29  import javax.faces.FacesWrapper;
30  import javax.faces.component.UIComponent;
31  import javax.faces.context.FacesContext;
32  import javax.faces.validator.Validator;
33  import javax.faces.validator.ValidatorException;
34  
35  /**
36   *
37   * @author Leonardo Uribe
38   */
39  public class RedirectMethodExpressionValueExpressionValidator
40         implements Validator, FacesWrapper<ValueExpression>, Externalizable
41  {
42      private ValueExpression valueExpression;
43  
44      public RedirectMethodExpressionValueExpressionValidator()
45      {
46      }
47      
48      public RedirectMethodExpressionValueExpressionValidator(ValueExpression valueExpression)
49      {
50          this.valueExpression = valueExpression;
51      }
52  
53      public void validate(FacesContext context, UIComponent component,
54              Object value) throws ValidatorException
55      {
56          getMethodExpression().invoke(FacesContext.getCurrentInstance().getELContext(), new Object[]{context,component,value});
57      }
58  
59      private MethodExpression getMethodExpression()
60      {
61          return getMethodExpression(FacesContext.getCurrentInstance().getELContext());
62      }
63      
64      private MethodExpression getMethodExpression(ELContext context)
65      {
66          Object meOrVe = valueExpression.getValue(context);
67          if (meOrVe instanceof MethodExpression)
68          {
69              return (MethodExpression) meOrVe;
70          }
71          else if (meOrVe instanceof ValueExpression)
72          {
73              while (meOrVe != null && meOrVe instanceof ValueExpression)
74              {
75                  meOrVe = ((ValueExpression)meOrVe).getValue(context);
76              }
77              return (MethodExpression) meOrVe;
78          }
79          else
80          {
81              return null;
82          }
83      }
84  
85      public ValueExpression getWrapped()
86      {
87          return valueExpression;
88      }
89      public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
90      {
91          this.valueExpression = (ValueExpression) in.readObject();
92      }
93  
94      public void writeExternal(ObjectOutput out) throws IOException
95      {
96          out.writeObject(this.valueExpression);
97      }
98  
99  }