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.internal.taglib;
21  
22  import org.apache.commons.beanutils.PropertyUtils;
23  import org.apache.myfaces.tobago.component.Attributes;
24  import org.apache.myfaces.tobago.component.InputSuggest;
25  import org.apache.myfaces.tobago.el.ConstantMethodBinding;
26  import org.apache.myfaces.tobago.event.SheetStateChangeSource;
27  import org.apache.myfaces.tobago.event.SortActionSource;
28  import org.apache.myfaces.tobago.event.TabChangeSource;
29  import org.apache.myfaces.tobago.internal.component.AbstractUIMessages;
30  import org.apache.myfaces.tobago.internal.component.AbstractUIPage;
31  import org.apache.myfaces.tobago.internal.component.AbstractUIPopup;
32  import org.apache.myfaces.tobago.util.ComponentUtils;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  import javax.faces.application.Application;
37  import javax.faces.component.ActionSource;
38  import javax.faces.component.EditableValueHolder;
39  import javax.faces.component.UIComponent;
40  import javax.faces.component.ValueHolder;
41  import javax.faces.context.FacesContext;
42  import javax.faces.convert.Converter;
43  import javax.faces.el.MethodBinding;
44  import javax.faces.el.ValueBinding;
45  import javax.faces.webapp.UIComponentTag;
46  import java.lang.reflect.InvocationTargetException;
47  
48  /**
49   * @deprecated since 2.0.0
50   */
51  @Deprecated
52  public class TagUtils {
53    private static final Logger LOG = LoggerFactory.getLogger(TagUtils.class);
54  
55    private TagUtils() {
56    }
57  
58    /**
59     * @deprecated since 2.0.0
60     */
61    @Deprecated
62    public static void setIntegerProperty(final UIComponent component, final String name, String value) {
63      if (value != null) {
64        if (UIComponentTag.isValueReference(value)) {
65          component.setValueBinding(name, createValueBinding(value));
66        } else {
67          if ((component instanceof AbstractUIPage
68              || component instanceof javax.faces.component.UIGraphic
69              || component instanceof AbstractUIPopup)
70              && (Attributes.WIDTH.equals(name) || Attributes.HEIGHT.equals(name))) {
71            if (value.endsWith("px")) {
72              value = value.substring(0, value.length() - 2);
73            }
74          }
75          component.getAttributes().put(name, new Integer(value));
76        }
77      }
78    }
79  
80    /**
81     * @deprecated since 2.0.0
82     */
83    @Deprecated
84    public static void setBooleanProperty(final UIComponent component, final String name, final String value) {
85      if (value != null) {
86        if (UIComponentTag.isValueReference(value)) {
87          component.setValueBinding(name, createValueBinding(value));
88        } else {
89          component.getAttributes().put(name, Boolean.valueOf(value));
90        }
91      }
92    }
93  
94    /**
95     * @deprecated since 2.0.0
96     */
97    @Deprecated
98    public static void setStringProperty(final UIComponent component, final String name, final String value) {
99      if (value != null) {
100       if (UIComponentTag.isValueReference(value)) {
101         component.setValueBinding(name, createValueBinding(value));
102       } else {
103         component.getAttributes().put(name, value);
104       }
105     }
106   }
107 
108   /**
109    * @deprecated since 2.0.0
110    */
111   @Deprecated
112   public static void setConverterProperty(final UIComponent component, final String name, final String value) {
113     if (value != null && component instanceof ValueHolder) {
114       final FacesContext facesContext = FacesContext.getCurrentInstance();
115       final Application application = facesContext.getApplication();
116       if (UIComponentTag.isValueReference(value)) {
117         final ValueBinding valueBinding = application.createValueBinding(value);
118         component.setValueBinding(name, valueBinding);
119       } else {
120         final Converter converter = application.createConverter(value);
121         ((ValueHolder) component).setConverter(converter);
122       }
123     }
124   }
125 
126   /**
127    * @deprecated since 2.0.0
128    */
129   @Deprecated
130   public static void setSeverityProperty(final UIComponent component, final String name, final String value) {
131     setStringProperty(component, name, value);
132   }
133 
134   /**
135    * @deprecated since 2.0.0
136    */
137   @Deprecated
138   public static void setObjectProperty(final UIComponent component, final String name, final String value) {
139     setStringProperty(component, name, value);
140   }
141 
142   /**
143    * @deprecated since 2.0.0
144    */
145   @Deprecated
146   public static void setCharacterProperty(final UIComponent component, final String name, final String value) {
147     setStringProperty(component, name, value);
148   }
149 
150   /**
151    * @deprecated since 2.0.0
152    */
153   @Deprecated
154   public static ValueBinding createValueBinding(final String value) {
155     return FacesContext.getCurrentInstance().getApplication().createValueBinding(value);
156   }
157 
158   /**
159    * @deprecated since 2.0.0
160    */
161   @Deprecated
162   public static void setStateChangeListenerMethodBinding(
163       final UIComponent component, final String value, final Class[] args) {
164     if (value != null && UIComponentTag.isValueReference(value)) {
165       final MethodBinding methodBinding = FacesContext.getCurrentInstance().getApplication()
166           .createMethodBinding(value, args);
167       ((SheetStateChangeSource) component).setStateChangeListener(methodBinding);
168     }
169   }
170 
171   /**
172    * @deprecated since 2.0.0
173    */
174   @Deprecated
175   public static void setSortActionListenerMethodBinding(
176       final UIComponent component, final String value, final Class[] args) {
177     if (value != null && UIComponentTag.isValueReference(value)) {
178       final MethodBinding methodBinding = FacesContext.getCurrentInstance().getApplication()
179           .createMethodBinding(value, args);
180       ((SortActionSource) component).setSortActionListener(methodBinding);
181     }
182   }
183 
184   /**
185    * @deprecated since 2.0.0
186    */
187   @Deprecated
188   public static void setSuggestMethodMethodBinding(
189       final UIComponent component, final String value, final Class[] args) {
190     if (value != null && UIComponentTag.isValueReference(value)) {
191       final MethodBinding methodBinding = FacesContext.getCurrentInstance().getApplication()
192           .createMethodBinding(value, args);
193       ((InputSuggest) component).setSuggestMethod(methodBinding);
194     }
195   }
196 
197   /**
198    * @deprecated since 2.0.0
199    */
200   @Deprecated
201   public static void setValueChangeListenerMethodBinding(
202       final UIComponent component, final String value, final Class[] args) {
203     if (value != null && UIComponentTag.isValueReference(value)) {
204       final MethodBinding methodBinding = FacesContext.getCurrentInstance().getApplication()
205           .createMethodBinding(value, args);
206       ((EditableValueHolder) component).setValueChangeListener(methodBinding);
207     }
208   }
209 
210   /**
211    * @deprecated since 2.0.0
212    */
213   @Deprecated
214   public static void setValidatorMethodBinding(final UIComponent component, final String value, final Class[] args) {
215     if (value != null && UIComponentTag.isValueReference(value)) {
216       final MethodBinding methodBinding = FacesContext.getCurrentInstance().getApplication()
217           .createMethodBinding(value, args);
218       ((EditableValueHolder) component).setValidator(methodBinding);
219     }
220   }
221 
222   /**
223    * @deprecated since 2.0.0
224    */
225   @Deprecated
226   public static void setActionListenerMethodBinding(
227       final UIComponent component, final String value, final Class[] args) {
228     if (value != null && UIComponentTag.isValueReference(value)) {
229       final MethodBinding methodBinding = FacesContext.getCurrentInstance().getApplication()
230           .createMethodBinding(value, args);
231       ((ActionSource) component).setActionListener(methodBinding);
232     }
233   }
234 
235   /**
236    * @deprecated since 2.0.0
237    */
238   @Deprecated
239   public static void setActionMethodBinding(final UIComponent component, final String value, final Class[] args) {
240     if (value != null) {
241       if (UIComponentTag.isValueReference(value)) {
242         final MethodBinding methodBinding =
243             FacesContext.getCurrentInstance().getApplication().createMethodBinding(value, args);
244         ((ActionSource) component).setAction(methodBinding);
245       } else {
246         ((ActionSource) component).setAction(new ConstantMethodBinding(value));
247       }
248     }
249   }
250 
251   /**
252    * @deprecated since 2.0.0
253    */
254   @Deprecated
255   public static void setTabChangeListenerMethodBinding(
256       final UIComponent component, final String value, final Class[] args) {
257     if (value != null && UIComponentTag.isValueReference(value)) {
258       final MethodBinding methodBinding = FacesContext.getCurrentInstance().getApplication()
259           .createMethodBinding(value, args);
260       ((TabChangeSource) component).setTabChangeListener(methodBinding);
261     }
262   }
263 
264   /**
265    * @deprecated since 2.0.0
266    */
267   @Deprecated
268   public static void setStringArrayProperty(final UIComponent component, final String name, final String value) {
269     if (value != null) {
270       if (UIComponentTag.isValueReference(value)) {
271         component.setValueBinding(name, createValueBinding(value));
272       } else {
273         final String[] components = ComponentUtils.splitList(value);
274         try {
275           PropertyUtils.setProperty(component, name, components);
276         } catch (final IllegalAccessException e) {
277           LOG.error("Ignoring Property", e);
278         } catch (final InvocationTargetException e) {
279           LOG.error("Ignoring Property", e);
280         } catch (final NoSuchMethodException e) {
281           LOG.error("Ignoring Property", e);
282         }
283       }
284     }
285   }
286 
287   /**
288    * @deprecated since 2.0.0
289    */
290   @Deprecated
291   public static void setValueBindingProperty(final UIComponent component, final String name, final String value) {
292     if (value != null && UIComponentTag.isValueReference(value)) {
293       final ValueBinding valueBinding = createValueBinding(value);
294       component.setValueBinding(name, valueBinding);
295     }
296   }
297 
298   /**
299    * @deprecated since 2.0.0
300    */
301   @Deprecated
302   public static void setOrderByProperty(final UIComponent component, final String name, final String value) {
303     if (value != null) {
304       if (UIComponentTag.isValueReference(value)) {
305         component.setValueBinding(name, createValueBinding(value));
306       } else {
307         component.getAttributes().put(name, AbstractUIMessages.OrderBy.parse(value));
308       }
309     }
310   }
311 
312   /**
313    * @deprecated since 2.0.0
314    */
315   @Deprecated
316   public static String getValueFromEl(String script) {
317     if (UIComponentTag.isValueReference(script)) {
318       final ValueBinding valueBinding = createValueBinding(script);
319       script = (String) valueBinding.getValue(FacesContext.getCurrentInstance());
320     }
321     return script;
322   }
323 }