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
17
18
19
20 public class ComponentUtils
21 {
22
23
24
25
26
27
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
68
69
70
71
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
97
98
99
100
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
126
127
128
129
130
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
147 }
148 }
149 return attribute;
150 }
151 catch (Exception exception)
152 {
153 throw new RuntimeException(exception);
154 }
155 }
156
157
158
159
160
161
162
163
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
179 }
180 }
181 }
182 catch (Exception exception)
183 {
184 throw new RuntimeException(exception);
185 }
186 }
187
188
189
190
191
192
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 }