View Javadoc
1   package org.andromda.cartridges.jsf.utils;
2   
3   import java.lang.reflect.Method;
4   import javax.faces.component.UICommand;
5   import javax.faces.component.UIComponent;
6   import javax.faces.component.UIGraphic;
7   import javax.faces.component.UIParameter;
8   import javax.faces.component.UISelectBoolean;
9   import javax.faces.component.ValueHolder;
10  import javax.faces.context.FacesContext;
11  import javax.faces.el.ValueBinding;
12  import javax.faces.webapp.UIComponentTag;
13  import org.apache.commons.lang.ObjectUtils;
14  
15  /**
16   * Utilities for dealing with the JSF components.
17   *
18   * @author Chad Brandon
19   */
20  public class ComponentUtils
21  {
22      /**
23       * Sets the value property of a component.
24       *
25       * @param context the current faces context.
26       * @param component the component.
27       * @param value the value to set.
28       */
29      public static void setValueProperty(
30          final FacesContext context,
31          final UIComponent component,
32          final String value)
33      {
34          if (value != null)
35          {
36              if (UIComponentTag.isValueReference(value))
37              {
38                  final ValueBinding binding = context.getApplication().createValueBinding(value);
39                  component.setValueBinding(
40                      "value",
41                      binding);
42              }
43              else if (component instanceof UICommand)
44              {
45                  ((UICommand)component).setValue(value);
46              }
47              else if (component instanceof UIParameter)
48              {
49                  ((UIParameter)component).setValue(value);
50              }
51              else if (component instanceof UISelectBoolean)
52              {
53                  ((UISelectBoolean)component).setValue(Boolean.valueOf(value));
54              }
55              else if (component instanceof UIGraphic)
56              {
57                  ((UIGraphic)component).setValue(value);
58              }
59              else if (component instanceof ValueHolder)
60              {
61                  ((ValueHolder)component).setValue(value);
62              }
63          }
64      }
65  
66      /**
67       * Sets the property with the given <code>name</code> of a component.
68       * @param name the name of the component to set.
69       * @param context the current faces context.
70       * @param component the component.
71       * @param value the value to set.
72       */
73      public static void setStringProperty(
74          final String name,
75          final FacesContext context,
76          final UIComponent component,
77          final String value)
78      {
79          if (value != null)
80          {
81              if (UIComponentTag.isValueReference(value))
82              {
83                  final ValueBinding binding = context.getApplication().createValueBinding(value);
84                  component.setValueBinding(
85                      name,
86                      binding);
87              }
88              else
89              {
90                  component.getAttributes().put(name, value);
91              }
92          }
93      }
94  
95      /**
96       * Sets the boolean value of property with the given <code>name</code> of a component.
97       * @param name the name of the component to set.
98       * @param context the current faces context.
99       * @param component the component.
100      * @param value the value to set.
101      */
102     public static void setBooleanProperty(
103         final String name,
104         final FacesContext context,
105         final UIComponent component,
106         final String value)
107     {
108         if (value != null)
109         {
110             if (UIComponentTag.isValueReference(value))
111             {
112                 final ValueBinding binding = context.getApplication().createValueBinding(value);
113                 component.setValueBinding(
114                     name,
115                     binding);
116             }
117             else
118             {
119                 component.getAttributes().put(name, Boolean.valueOf(value));
120             }
121         }
122     }
123 
124     /**
125      * Gets the attribute from the given object.  The object can be either a context, request
126      * or response (HttpServletContext/PortletContext, HttpServletRequest/PortletRequest, etc).
127      *
128      * @param object the object from which to retrieve the attribute.
129      * @param attributeName the attribute name.
130      * @return the value of the attribute if one is present.
131      */
132     public static Object getAttribute(final Object object, final String attributeName)
133     {
134         try
135         {
136             Object attribute = null;
137             if (object != null)
138             {
139                 try
140                 {
141                     final Method method = object.getClass().getMethod("getAttribute", new Class[]{String.class});
142                     attribute = method.invoke(object, attributeName);
143                 }
144                 catch (NoSuchMethodException exception)
145                 {
146                     // Swallow Exception
147                 }
148             }
149             return attribute;
150         }
151         catch (Exception exception)
152         {
153             throw new RuntimeException(exception);
154         }
155     }
156 
157     /**
158      * Sets the attribute on the given object.  The object can be either a context, request
159      * or response (HttpServletContext/PortletContext, HttpServletRequest/PortletRequest, etc).
160      *
161      * @param object the object on which to set the attribute.
162      * @param attributeName the attribute name.
163      * @param attributeValue the value of the attribute to set.
164      */
165     public static void setAttribute(final Object object, final String attributeName, final Object attributeValue)
166     {
167         try
168         {
169             if (object != null)
170             {
171                 try
172                 {
173                     final Method method = object.getClass().getMethod("setAttribute", new Class[]{String.class, Object.class});
174                     method.invoke(object, attributeName, attributeValue);
175                 }
176                 catch (NoSuchMethodException ignore)
177                 {
178                     // Swallow exception
179                 }
180             }
181         }
182         catch (Exception exception)
183         {
184             throw new RuntimeException(exception);
185         }
186     }
187 
188     /**
189      * Gets the context path from the given request object (PortletRequest/HttpServletRequest)
190      *
191      * @param request the request object from which to retrieve the context path.
192      * @return the context path.
193      */
194     public static String getContextPath(final Object request)
195     {
196         try
197         {
198             String contextPath = null;
199             if (request != null)
200             {
201                 contextPath = ObjectUtils.toString(request.getClass().getMethod("getContextPath", new Class[0]).invoke(request));
202             }
203             return contextPath;
204         }
205         catch (Exception exception)
206         {
207             throw new RuntimeException(exception);
208         }
209     }
210 }