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.shared.taglib;
20  
21  import org.apache.myfaces.shared.el.SimpleActionMethodBinding;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  import javax.faces.component.*;
27  import javax.faces.context.FacesContext;
28  import javax.faces.convert.Converter;
29  import javax.faces.el.MethodBinding;
30  import javax.faces.el.ValueBinding;
31  import javax.faces.event.ActionEvent;
32  import javax.faces.event.ValueChangeEvent;
33  import javax.faces.webapp.UIComponentTag;
34  
35  /***
36   * @author Manfred Geiler (latest modification by $Author: matzew $)
37   * @version $Revision: 557350 $ $Date: 2007-07-18 13:19:50 -0500 (Wed, 18 Jul 2007) $
38   *
39   * @deprecated replaced by @{link UIComponentELTagUtils}
40   */
41  public class UIComponentTagUtils
42  {
43      private static final Log log = LogFactory.getLog(UIComponentTagUtils.class);
44  
45      private static final Class[] VALIDATOR_ARGS = {FacesContext.class,
46                                                     UIComponent.class,
47                                                     Object.class};
48      private static final Class[] ACTION_LISTENER_ARGS = {ActionEvent.class};
49      private static final Class[] VALUE_LISTENER_ARGS = {ValueChangeEvent.class};
50  
51      private UIComponentTagUtils() {}    //util class, no instantiation allowed
52  
53  
54      public static boolean isValueReference(String v)
55      {
56          return UIComponentTag.isValueReference(v);
57      }
58  
59  
60      public static void setIntegerProperty(FacesContext context,
61                                            UIComponent component,
62                                            String propName,
63                                            String value)
64      {
65          if (value != null)
66          {
67              if (isValueReference(value))
68              {
69                  ValueBinding vb = context.getApplication().createValueBinding(value);
70                  component.setValueBinding(propName, vb);
71              }
72              else
73              {
74                  //FIXME: should use converter maybe?
75                  component.getAttributes().put(propName, Integer.valueOf(value));
76              }
77          }
78      }
79  
80      public static void setLongProperty(FacesContext context,
81                                         UIComponent component,
82                                         String propName,
83                                         String value)
84      {
85          if (value != null)
86          {
87              if (isValueReference(value))
88              {
89                  ValueBinding vb = context.getApplication().createValueBinding(value);
90                  component.setValueBinding(propName, vb);
91              }
92              else
93              {
94                  //FIXME: should use converter maybe?
95                  component.getAttributes().put(propName, Long.valueOf(value));
96              }
97          }
98      }
99  
100     public static void setStringProperty(FacesContext context,
101                                      UIComponent component,
102                                      String propName,
103                                      String value)
104     {
105         if (value != null)
106         {
107             if (isValueReference(value))
108             {
109                 ValueBinding vb = context.getApplication().createValueBinding(value);
110                 component.setValueBinding(propName, vb);
111             }
112             else
113             {
114                 //TODO: Warning if component has no such property (with reflection)
115                 component.getAttributes().put(propName, value);
116             }
117         }
118     }
119 
120 
121     public static void setBooleanProperty(FacesContext context,
122                                       UIComponent component,
123                                       String propName,
124                                       String value)
125     {
126         if (value != null)
127         {
128             if (isValueReference(value))
129             {
130                 ValueBinding vb = context.getApplication().createValueBinding(value);
131                 component.setValueBinding(propName, vb);
132             }
133             else
134             {
135                 //TODO: More sophisticated way to convert boolean value (yes/no, 1/0, on/off, etc.)
136                 component.getAttributes().put(propName, Boolean.valueOf(value));
137             }
138         }
139     }
140 
141 
142     public static void setValueProperty(FacesContext context,
143                                         UIComponent component,
144                                         String value)
145     {
146         if (value != null)
147         {
148             if (isValueReference(value))
149             {
150                 ValueBinding vb = context.getApplication().createValueBinding(value);
151                 component.setValueBinding(org.apache.myfaces.shared.renderkit.JSFAttr.VALUE_ATTR, vb);
152             }
153             else if (component instanceof UICommand)
154             {
155                 ((UICommand)component).setValue(value);
156             }
157             else if (component instanceof UIParameter)
158             {
159                 ((UIParameter)component).setValue(value);
160             }
161             else if (component instanceof UISelectBoolean)
162             {
163                 ((UISelectBoolean)component).setValue(Boolean.valueOf(value));
164             }
165             else if (component instanceof UIGraphic)
166             {
167                 ((UIGraphic)component).setValue(value);
168             }
169             //Since many input components are ValueHolders the special components
170             //must come first, ValueHolder is the last resort.
171             else if (component instanceof ValueHolder)
172             {
173                 ((ValueHolder)component).setValue(value);
174             }
175             else
176             {
177                 log.error("Component " + component.getClass().getName() + " is no ValueHolder, cannot set value.");
178             }
179         }
180     }
181 
182 
183     public static void setConverterProperty(FacesContext context,
184                                         UIComponent component,
185                                         String value)
186     {
187         if (value != null)
188         {
189             if (component instanceof ValueHolder)
190             {
191                 if (isValueReference(value))
192                 {
193                     ValueBinding vb = context.getApplication().createValueBinding(value);
194                     component.setValueBinding(org.apache.myfaces.shared.renderkit.JSFAttr.CONVERTER_ATTR, vb);
195                 }
196                 else
197                 {
198                     FacesContext facesContext = FacesContext.getCurrentInstance();
199                     Converter converter = facesContext.getApplication().createConverter(value);
200                     ((ValueHolder)component).setConverter(converter);
201                 }
202             }
203             else
204             {
205                 log.error("Component " + component.getClass().getName() + " is no ValueHolder, cannot set value.");
206             }
207         }
208     }
209 
210 
211     public static void setValidatorProperty(FacesContext context,
212                                             UIComponent component,
213                                             String validator)
214     {
215         if (validator != null)
216         {
217             if (!(component instanceof EditableValueHolder))
218             {
219                 throw new IllegalArgumentException("Component " + component.getClientId(context) + " is no EditableValueHolder");
220             }
221             if (isValueReference(validator))
222             {
223                 MethodBinding mb = context.getApplication().createMethodBinding(validator,
224                                                                                 VALIDATOR_ARGS);
225                 ((EditableValueHolder)component).setValidator(mb);
226             }
227             else
228             {
229                 log.error("Component " + component.getClientId(context) + " has invalid validation expression " + validator);
230             }
231         }
232     }
233 
234     public static void setValueBinding(FacesContext context,
235                                        UIComponent component,
236                                        String propName,
237                                        String value)
238     {
239         if (value != null)
240         {
241             if (isValueReference(value))
242             {
243                 ValueBinding vb = context.getApplication().createValueBinding(value);
244                 component.setValueBinding(propName, vb);
245             }
246             else
247             {
248                 throw new IllegalArgumentException("Component " + component.getClientId(context) + " attribute " + propName + " must be a value reference, was " + value);
249             }
250         }
251     }
252 
253     public static void setActionProperty(FacesContext context,
254                                          UIComponent component,
255                                          String action)
256     {
257         if (action != null)
258         {
259             if (!(component instanceof ActionSource))
260             {
261                 throw new IllegalArgumentException("Component " + component.getClientId(context) + " is no ActionSource");
262             }
263             MethodBinding mb;
264             if (isValueReference(action))
265             {
266                 mb = context.getApplication().createMethodBinding(action, null);
267             }
268             else
269             {
270                 mb = new SimpleActionMethodBinding(action);
271             }
272             ((ActionSource)component).setAction(mb);
273         }
274     }
275 
276     public static void setActionListenerProperty(FacesContext context,
277                                                  UIComponent component,
278                                                  String actionListener)
279     {
280         if (actionListener != null)
281         {
282             if (!(component instanceof ActionSource))
283             {
284                 throw new IllegalArgumentException("Component " + component.getClientId(context) + " is no ActionSource");
285             }
286             if (isValueReference(actionListener))
287             {
288                 MethodBinding mb = context.getApplication().createMethodBinding(actionListener,
289                                                                                 ACTION_LISTENER_ARGS);
290 
291                 /***
292                 if(! Void.class.equals(mb.getType(context)))
293                 {
294                     throw new IllegalArgumentException(
295                             actionListener +
296                             " : Return types for action listeners must be void, see JSF spec. 3.2.1.1");
297                 }
298                 */
299 
300                 ((ActionSource)component).setActionListener(mb);
301             }
302             else
303             {
304                 log.error("Component " + component.getClientId(context) + " has invalid actionListener value: " + actionListener);
305             }
306         }
307     }
308 
309     public static void setValueChangedListenerProperty(FacesContext context,
310                                                        UIComponent component,
311                                                        String valueChangedListener)
312     {
313         if (valueChangedListener != null)
314         {
315             if (!(component instanceof EditableValueHolder))
316             {
317                 throw new IllegalArgumentException("Component " + component.getClientId(context) + " is no EditableValueHolder");
318             }
319             if (isValueReference(valueChangedListener))
320             {
321                 MethodBinding mb = context.getApplication().createMethodBinding(valueChangedListener,
322                                                                                 VALUE_LISTENER_ARGS);
323                 /***
324                 if(! Void.class.equals(mb.getType(context)))
325                 {
326                     throw new IllegalArgumentException(
327                             valueChangedListener +
328                             " : Return types for value change listeners must be void, see JSF spec. 3.2.5.1");
329                 }
330                 */
331 
332                 ((EditableValueHolder)component).setValueChangeListener(mb);
333             }
334             else
335             {
336                 log.error("Component " + component.getClientId(context) + " has invalid valueChangedListener expression " + valueChangedListener);
337             }
338         }
339     }
340 
341 }