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.orchestra.dynaForm.lib;
20  
21  import java.lang.reflect.InvocationTargetException;
22  import java.lang.reflect.Method;
23  
24  import javax.faces.FacesException;
25  import javax.faces.application.Application;
26  import javax.faces.component.UIComponent;
27  import javax.faces.context.FacesContext;
28  import javax.faces.el.ValueBinding;
29  import javax.el.ELContext;
30  import javax.el.ExpressionFactory;
31  import javax.el.ValueExpression;
32  
33  /**
34   * Some JSF helpers to smooth over the differences between JSF1.1 and JSF1.2.
35   */
36  public final class _FacesUtils
37  {
38      private static final Class<?>[] ARGS_SET_VALUE_EXPR = new Class[]
39      {
40          String.class,
41          ValueExpression.class
42      };
43  
44      private static final Class<?>[] ARGS_GET_VALUE_EXPR = new Class[]
45      {
46          String.class
47      };
48  
49      private static final Method _METHOD_GELC = getMethod(FacesContext.class, "getELContext", (Class[]) null);
50      private static final Method _METHOD_GVE = getMethod(UIComponent.class, "getValueExpression", ARGS_GET_VALUE_EXPR);
51      private static final Method _METHOD_SVE = getMethod(UIComponent.class, "setValueExpression", ARGS_SET_VALUE_EXPR);
52      private static final Method _METHOD_EF = getMethod(Application.class, "getExpressionFactory", (Class[]) null);
53  
54      private _FacesUtils()
55      {
56      }
57  
58      private static Method getMethod(Class<?> clazz, String name, Class<?>[] args)
59      {
60          try
61          {
62              return clazz.getMethod(name, args);
63          }
64          catch(Exception e)
65          {
66              return null;
67          }
68      }
69  
70      private static <T> T invoke(Class<T> retType, Method method, Object target, Object[] args)
71      throws FacesException
72      {
73          try
74          {
75              @SuppressWarnings("unchecked")
76              T result = (T) method.invoke(target, args);
77              return result;
78          }
79          catch(IllegalAccessException e)
80          {
81              // should never happen
82              throw new FacesException("Unable to invoke standard JSF method", e);
83          }
84          catch(InvocationTargetException e)
85          {
86              // should never happen
87              throw new FacesException("Unable to invoke standard JSF method", e);
88          }
89      }
90  
91      public static void copyRendered(UIComponent source, UIComponent destination)
92      {
93          boolean renderedSet = false;
94          if (_METHOD_GVE != null)
95          {
96              ValueExpression ve = invoke(ValueExpression.class, _METHOD_GVE, source, new Object[] {"rendered"});
97              if (ve != null)
98              {
99                  invoke(Void.class, _METHOD_SVE, destination, new Object[]{"rendered", ve});
100                 renderedSet = true;
101             }
102         }
103         else
104         {
105             ValueBinding vb = source.getValueBinding("rendered");
106             if (vb != null)
107             {
108                 destination.setValueBinding("rendered", vb);
109                 renderedSet = true;
110             }
111         }
112         if (!renderedSet)
113         {
114             destination.setRendered(destination.isRendered());
115         }
116     }
117 
118     /**
119      * This associates an EL expression with the "value" attribute of the specified UIComponent.
120      * <p> 
121      * @param cmp is the component whose value attribute is to be updated.
122      * 
123      * @param elCtx is optional; when specified and this is a JSF1.2 environment then this is
124      * used when resolving the expression. When this is not a JSF1.2 environment then this
125      * parameter is ignored.
126      * 
127      * @param context is the JSF FacesContext to use.
128      * 
129      * @param vbString is the expression string, eg "#{someBean.someProperty}"
130      */
131     public static void setValueExpression(UIComponent cmp, ELContext elCtx, FacesContext context, String vbString)
132     {
133         Application app = context.getApplication();
134         if (_METHOD_GELC != null)
135         {
136             if (elCtx == null)
137             {
138                 elCtx = invoke(ELContext.class, _METHOD_GELC, context, (Object[]) null);
139             }
140 
141             ExpressionFactory expressionFactory = invoke(ExpressionFactory.class, _METHOD_EF, app, (Object[]) null);
142             ValueExpression ve = expressionFactory.createValueExpression(elCtx, vbString, Object.class);
143 
144             invoke(Void.class, _METHOD_SVE, cmp, new Object[] {"value", ve});
145         }
146         else
147         {
148             ValueBinding vb = app.createValueBinding(vbString);
149             cmp.setValueBinding("value", vb);
150         }
151     }
152 }