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 org.apache.myfaces.tobago.compat;
21  
22  import org.apache.myfaces.tobago.event.TabChangeSource;
23  import org.apache.myfaces.tobago.event.ValueExpressionPopupActionListener;
24  import org.apache.myfaces.tobago.event.ValueExpressionResetInputActionListener;
25  import org.apache.myfaces.tobago.event.ValueExpressionTabChangeListener;
26  import org.apache.myfaces.tobago.util.ValueExpressionComparator;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  import javax.el.ExpressionFactory;
31  import javax.el.MethodExpression;
32  import javax.el.ValueExpression;
33  import javax.faces.component.ActionSource;
34  import javax.faces.component.ContextCallback;
35  import javax.faces.component.EditableValueHolder;
36  import javax.faces.component.NamingContainer;
37  import javax.faces.component.UIComponent;
38  import javax.faces.component.UINamingContainer;
39  import javax.faces.component.ValueHolder;
40  import javax.faces.context.FacesContext;
41  import javax.faces.el.EvaluationException;
42  import javax.faces.el.MethodBinding;
43  import javax.faces.event.AbortProcessingException;
44  import javax.faces.event.FacesEvent;
45  import javax.faces.validator.MethodExpressionValidator;
46  import java.util.Comparator;
47  
48  public final class FacesUtilsEL {
49  
50    private static final Logger LOG = LoggerFactory.getLogger(FacesUtilsEL.class);
51  
52    private FacesUtilsEL() {
53    }
54  
55    /**
56     * @deprecated since 2.0.0
57     */
58    public static boolean invokeOnComponent(
59        final FacesContext context, final UIComponent component, final String clientId, final ContextCallback callback) {
60      final String thisClientId = component.getClientId(context);
61  
62      if (clientId.equals(thisClientId)) {
63        callback.invokeContextCallback(context, component);
64        return true;
65      } else if (component instanceof NamingContainer) {
66        // This component is a naming container. If the client id shows it's inside this naming container,
67        // then process further.
68        // Otherwise we know the client id we're looking for is not in this naming container,
69        // so for improved performance short circuit and return false.
70        if (clientId.startsWith(thisClientId)
71            && (clientId.charAt(thisClientId.length()) == UINamingContainer.getSeparatorChar(context))) {
72          if (invokeOnComponentFacetsAndChildren(context, component, clientId, callback)) {
73            return true;
74          }
75        }
76      } else {
77        if (invokeOnComponentFacetsAndChildren(context, component, clientId, callback)) {
78          return true;
79        }
80      }
81  
82      return false;
83    }
84  
85    private static boolean invokeOnComponentFacetsAndChildren(
86        final FacesContext context, final UIComponent component, final String clientId, final ContextCallback callback) {
87      for (java.util.Iterator<UIComponent> it = component.getFacetsAndChildren(); it.hasNext();) {
88        final UIComponent child = it.next();
89  
90        if (child.invokeOnComponent(context, clientId, callback)) {
91          return true;
92        }
93      }
94      return false;
95    }
96  
97    /**
98     * @deprecated since 2.0.0
99     */
100   @Deprecated
101   public static void invokeMethodBinding(
102       final FacesContext facesContext, final MethodBinding methodBinding, final FacesEvent event) {
103     if (methodBinding != null && event != null) {
104       try {
105         methodBinding.invoke(facesContext, new Object[]{event});
106       } catch (EvaluationException e) {
107         final Throwable cause = e.getCause();
108         if (cause instanceof AbortProcessingException) {
109           throw (AbortProcessingException) cause;
110         } else {
111           throw e;
112         }
113       }
114     }
115   }
116 
117   public static void invokeMethodExpression(
118       FacesContext facesContext, MethodExpression methodExpression, FacesEvent event) {
119 
120     if (methodExpression != null && event != null) {
121       try {
122         methodExpression.invoke(facesContext.getELContext(), new Object[]{event});
123       } catch (Exception e) {
124         throw new AbortProcessingException(e);
125       }
126     }
127   }
128 
129   /**
130    * @deprecated since 2.0.0
131    */
132   @Deprecated
133   public static Object getValueFromValueBindingOrValueExpression(
134       FacesContext context, UIComponent component, String name) {
135     return component.getValueExpression(name).getValue(context.getELContext());
136   }
137 
138 
139   public static boolean hasValueBindingOrValueExpression(UIComponent component, String name) {
140     return component.getValueExpression(name) != null;
141   }
142 
143   public static boolean isReadonlyValueBindingOrValueExpression(
144       FacesContext context, UIComponent component, String name) {
145     return component.getValueExpression(name).isReadOnly(context.getELContext());
146   }
147 
148 
149   public static String getExpressionString(UIComponent component, String name) {
150     return component.getValueExpression(name).getExpressionString();
151   }
152 
153   public static void setValueOfBindingOrExpression(
154       FacesContext context, Object value, UIComponent component, String bindingName) {
155     ValueExpression ve = component.getValueExpression(bindingName);
156     if (ve != null) {
157       ve.setValue(context.getELContext(), value);
158     }
159   }
160 
161   public static void setValueOfBindingOrExpression(
162       FacesContext context, Object value, Object bindingOrExpression) {
163     if (bindingOrExpression instanceof ValueExpression) {
164       ValueExpression ve = (ValueExpression) bindingOrExpression;
165       ve.setValue(context.getELContext(), value);
166     }
167   }
168 
169   public static void copyValueBindingOrValueExpression(
170       UIComponent fromComponent, String fromName, UIComponent toComponent, String toName) {
171     ValueExpression ve = fromComponent.getValueExpression(fromName);
172     if (ve != null) {
173       toComponent.setValueExpression(toName, ve);
174     }
175   }
176 
177   public static Object getValueFromBindingOrExpression(Object obj) {
178     if (obj instanceof ValueExpression) {
179       ValueExpression expression = (ValueExpression) obj;
180       return expression.getValue(FacesContext.getCurrentInstance().getELContext());
181     }
182     return null;
183   }
184 
185   /**
186    * @deprecated Since 2.0.0, please use {@link FacesUtilsEL#createValueExpression(String string)}
187    */
188   @Deprecated
189   public static Object createExpressionOrBinding(String string) {
190     return createValueExpression(string);
191   }
192 
193   public static ValueExpression createValueExpression(String string) {
194     FacesContext facesContext = FacesContext.getCurrentInstance();
195     ExpressionFactory expressionFactory = facesContext.getApplication().getExpressionFactory();
196     return expressionFactory.createValueExpression(facesContext.getELContext(), string, Object.class);
197   }
198 
199   /**
200    * @deprecated since 2.0.0
201    */
202   public static void setValidator(EditableValueHolder editableValueHolder, Object validator) {
203     if (validator instanceof MethodExpression) {
204       editableValueHolder.addValidator(new MethodExpressionValidator((MethodExpression) validator));
205     } else {
206       LOG.error("Unknown instance for validator: " + (validator != null ? validator.getClass().getName() : "<null>"));
207     }
208   }
209 
210   /**
211    * @deprecated since 2.0.0
212    */
213   public static void setConverter(ValueHolder valueHolder, Object converterExpression) {
214     if (converterExpression instanceof ValueExpression) {
215       ValueExpression expression = (ValueExpression) converterExpression;
216       if (!expression.isLiteralText()) {
217         ((UIComponent) valueHolder).setValueExpression("converter", expression);
218       } else {
219         valueHolder.setConverter(FacesContext.getCurrentInstance()
220             .getApplication().createConverter(expression.getExpressionString()));
221       }
222     }
223   }
224 
225   /**
226    * @deprecated since 2.0.0
227    */
228   public static void setBindingOrExpression(UIComponent component, String name, Object valueBindingOrExpression) {
229     component.setValueExpression(name, (ValueExpression) valueBindingOrExpression);
230   }
231 
232   /**
233    * @deprecated since 2.0.0
234    */
235   public static void addBindingOrExpressionTabChangeListener(
236       TabChangeSource source, String type, Object bindingOrExpression) {
237     if (bindingOrExpression instanceof ValueExpression) {
238       source.addTabChangeListener(new ValueExpressionTabChangeListener(type, (ValueExpression) bindingOrExpression));
239     }
240   }
241 
242   /**
243    * @deprecated since 2.0.0
244    */
245   public static Comparator getBindingOrExpressionComparator(
246       FacesContext facesContext, UIComponent child, String var, boolean descending, Comparator comparator) {
247     ValueExpression valueBinding = child.getValueExpression("value");
248     return new ValueExpressionComparator(facesContext, var, valueBinding, descending, comparator);
249   }
250 
251   /**
252    * @deprecated since 2.0.0
253    */
254   public static void addBindingOrExpressionPopupActionListener(ActionSource actionSource, Object bindingOrExpression) {
255     actionSource.addActionListener(new ValueExpressionPopupActionListener((ValueExpression) bindingOrExpression));
256   }
257 
258   /**
259    * @deprecated since 2.0.0
260    */
261   public static void addBindingOrExpressionResetActionListener(ActionSource actionSource, Object bindingOrExpression) {
262     actionSource.addActionListener(new ValueExpressionResetInputActionListener((ValueExpression) bindingOrExpression));
263   }
264 }