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