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 (final 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 (final 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       final FacesContext facesContext, final MethodExpression methodExpression, final FacesEvent event) {
119 
120     if (methodExpression != null && event != null) {
121       try {
122         methodExpression.invoke(facesContext.getELContext(), new Object[]{event});
123       } catch (final 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       final FacesContext context, final UIComponent component, final String name) {
135     return component.getValueExpression(name).getValue(context.getELContext());
136   }
137 
138 
139   public static boolean hasValueBindingOrValueExpression(final UIComponent component, final String name) {
140     return component.getValueExpression(name) != null;
141   }
142 
143   public static boolean isReadonlyValueBindingOrValueExpression(
144       final FacesContext context, final UIComponent component, final String name) {
145     return component.getValueExpression(name).isReadOnly(context.getELContext());
146   }
147 
148 
149   public static String getExpressionString(final UIComponent component, final String name) {
150     return component.getValueExpression(name).getExpressionString();
151   }
152 
153   public static void setValueOfBindingOrExpression(
154       final FacesContext context, final Object value, final UIComponent component, final String bindingName) {
155     final ValueExpression ve = component.getValueExpression(bindingName);
156     if (ve != null) {
157       ve.setValue(context.getELContext(), value);
158     }
159   }
160 
161   public static void setValueOfBindingOrExpression(
162       final FacesContext context, final Object value, final Object bindingOrExpression) {
163     if (bindingOrExpression instanceof ValueExpression) {
164       final ValueExpression ve = (ValueExpression) bindingOrExpression;
165       ve.setValue(context.getELContext(), value);
166     }
167   }
168 
169   public static void copyValueBindingOrValueExpression(
170       final UIComponent fromComponent, final String fromName, final UIComponent toComponent, final String toName) {
171     final ValueExpression ve = fromComponent.getValueExpression(fromName);
172     if (ve != null) {
173       toComponent.setValueExpression(toName, ve);
174     }
175   }
176 
177   public static Object getValueFromBindingOrExpression(final Object obj) {
178     if (obj instanceof ValueExpression) {
179       final 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(final String string) {
190     return createValueExpression(string);
191   }
192 
193   public static ValueExpression createValueExpression(final String string) {
194     final FacesContext facesContext = FacesContext.getCurrentInstance();
195     final 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(final EditableValueHolder editableValueHolder, final 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(final ValueHolder valueHolder, final Object converterExpression) {
214     if (converterExpression instanceof ValueExpression) {
215       final 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(
229       final UIComponent component, final String name, final Object valueBindingOrExpression) {
230     component.setValueExpression(name, (ValueExpression) valueBindingOrExpression);
231   }
232 
233   /**
234    * @deprecated since 2.0.0
235    */
236   public static void addBindingOrExpressionTabChangeListener(
237       final TabChangeSource source, final String type, final Object bindingOrExpression) {
238     if (bindingOrExpression instanceof ValueExpression) {
239       source.addTabChangeListener(new ValueExpressionTabChangeListener(type, (ValueExpression) bindingOrExpression));
240     }
241   }
242 
243   /**
244    * @deprecated since 2.0.0
245    */
246   public static Comparator getBindingOrExpressionComparator(
247       final FacesContext facesContext, final UIComponent child, final String var, final boolean descending,
248       final Comparator comparator) {
249     final ValueExpression valueBinding = child.getValueExpression("value");
250     return new ValueExpressionComparator(facesContext, var, valueBinding, descending, comparator);
251   }
252 
253   /**
254    * @deprecated since 2.0.0
255    */
256   public static void addBindingOrExpressionPopupActionListener(
257       final ActionSource actionSource, final Object bindingOrExpression) {
258     actionSource.addActionListener(new ValueExpressionPopupActionListener((ValueExpression) bindingOrExpression));
259   }
260 
261   /**
262    * @deprecated since 2.0.0
263    */
264   public static void addBindingOrExpressionResetActionListener(
265       final ActionSource actionSource, final Object bindingOrExpression) {
266     actionSource.addActionListener(new ValueExpressionResetInputActionListener((ValueExpression) bindingOrExpression));
267   }
268 }