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