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(UIComponent component, 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(UIComponent component, String name, 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(UIComponent component, String name, 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(UIComponent component, String name, 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         ValueBinding valueBinding = application.createValueBinding(value);
118         component.setValueBinding(name, valueBinding);
119       } else {
120         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(UIComponent component, String name, String value) {
131     setStringProperty(component, name, value);
132   }
133 
134   /**
135    * @deprecated since 2.0.0
136    */
137   @Deprecated
138   public static void setObjectProperty(UIComponent component, String name, String value) {
139     setStringProperty(component, name, value);
140   }
141 
142   /**
143    * @deprecated since 2.0.0
144    */
145   @Deprecated
146   public static void setCharacterProperty(UIComponent component, String name, String value) {
147     setStringProperty(component, name, value);
148   }
149 
150   /**
151    * @deprecated since 2.0.0
152    */
153   @Deprecated
154   public static ValueBinding createValueBinding(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(UIComponent component, String value, Class[] args) {
163     if (value != null && UIComponentTag.isValueReference(value)) {
164       MethodBinding methodBinding = FacesContext.getCurrentInstance().getApplication().createMethodBinding(value, args);
165       ((SheetStateChangeSource) component).setStateChangeListener(methodBinding);
166     }
167   }
168 
169   /**
170    * @deprecated since 2.0.0
171    */
172   @Deprecated
173   public static void setSortActionListenerMethodBinding(UIComponent component, String value, Class[] args) {
174     if (value != null && UIComponentTag.isValueReference(value)) {
175       MethodBinding methodBinding = FacesContext.getCurrentInstance().getApplication().createMethodBinding(value, args);
176       ((SortActionSource) component).setSortActionListener(methodBinding);
177     }
178   }
179 
180   /**
181    * @deprecated since 2.0.0
182    */
183   @Deprecated
184   public static void setSuggestMethodMethodBinding(UIComponent component, String value, Class[] args) {
185     if (value != null && UIComponentTag.isValueReference(value)) {
186       MethodBinding methodBinding = FacesContext.getCurrentInstance().getApplication().createMethodBinding(value, args);
187       ((InputSuggest) component).setSuggestMethod(methodBinding);
188     }
189   }
190 
191   /**
192    * @deprecated since 2.0.0
193    */
194   @Deprecated
195   public static void setValueChangeListenerMethodBinding(UIComponent component, String value, Class[] args) {
196     if (value != null && UIComponentTag.isValueReference(value)) {
197       MethodBinding methodBinding = FacesContext.getCurrentInstance().getApplication().createMethodBinding(value, args);
198       ((EditableValueHolder) component).setValueChangeListener(methodBinding);
199     }
200   }
201 
202   /**
203    * @deprecated since 2.0.0
204    */
205   @Deprecated
206   public static void setValidatorMethodBinding(UIComponent component, String value, Class[] args) {
207     if (value != null && UIComponentTag.isValueReference(value)) {
208       MethodBinding methodBinding = FacesContext.getCurrentInstance().getApplication().createMethodBinding(value, args);
209       ((EditableValueHolder) component).setValidator(methodBinding);
210     }
211   }
212 
213   /**
214    * @deprecated since 2.0.0
215    */
216   @Deprecated
217   public static void setActionListenerMethodBinding(UIComponent component, String value, Class[] args) {
218     if (value != null && UIComponentTag.isValueReference(value)) {
219       MethodBinding methodBinding = FacesContext.getCurrentInstance().getApplication().createMethodBinding(value, args);
220       ((ActionSource) component).setActionListener(methodBinding);
221     }
222   }
223 
224   /**
225    * @deprecated since 2.0.0
226    */
227   @Deprecated
228   public static void setActionMethodBinding(UIComponent component, String value, Class[] args) {
229     if (value != null) {
230       if (UIComponentTag.isValueReference(value)) {
231         MethodBinding methodBinding =
232             FacesContext.getCurrentInstance().getApplication().createMethodBinding(value, args);
233         ((ActionSource) component).setAction(methodBinding);
234       } else {
235         ((ActionSource) component).setAction(new ConstantMethodBinding(value));
236       }
237     }  
238   }
239 
240   /**
241    * @deprecated since 2.0.0
242    */
243   @Deprecated
244   public static void setTabChangeListenerMethodBinding(UIComponent component, String value, Class[] args) {
245     if (value != null && UIComponentTag.isValueReference(value)) {
246       MethodBinding methodBinding = FacesContext.getCurrentInstance().getApplication().createMethodBinding(value, args);
247       ((TabChangeSource) component).setTabChangeListener(methodBinding);
248     }
249   }
250 
251   /**
252    * @deprecated since 2.0.0
253    */
254   @Deprecated
255   public static void setStringArrayProperty(UIComponent component, String name, String value) {
256     if (value != null) {
257       if (UIComponentTag.isValueReference(value)) {
258         component.setValueBinding(name, createValueBinding(value));
259       } else {
260         String[] components = ComponentUtils.splitList(value);
261         try {
262           PropertyUtils.setProperty(component, name, components);
263         } catch (IllegalAccessException e) {
264           LOG.error("Ignoring Property", e);
265         } catch (InvocationTargetException e) {
266           LOG.error("Ignoring Property", e);
267         } catch (NoSuchMethodException e) {
268           LOG.error("Ignoring Property", e);
269         }
270       }
271     }
272   }
273 
274   /**
275    * @deprecated since 2.0.0
276    */
277   @Deprecated
278   public static void setValueBindingProperty(UIComponent component, String name, String value) {
279     if (value != null && UIComponentTag.isValueReference(value)) {
280       ValueBinding valueBinding = createValueBinding(value);
281       component.setValueBinding(name, valueBinding);
282     }
283   }
284 
285   /**
286    * @deprecated since 2.0.0
287    */
288   @Deprecated
289   public static void setOrderByProperty(UIComponent component, String name, String value) {
290     if (value != null) {
291       if (UIComponentTag.isValueReference(value)) {
292         component.setValueBinding(name, createValueBinding(value));
293       } else {
294         component.getAttributes().put(name, AbstractUIMessages.OrderBy.parse(value));
295       }
296     }
297   }
298 
299   /**
300    * @deprecated since 2.0.0
301    */
302   @Deprecated
303   public static String getValueFromEl(String script) {
304     if (UIComponentTag.isValueReference(script)) {
305       ValueBinding valueBinding = createValueBinding(script);
306       script = (String) valueBinding.getValue(FacesContext.getCurrentInstance());
307     }
308     return script;
309   }
310 }