1 package org.apache.struts.faces.util;
2
3 import java.util.Map;
4
5 import jakarta.el.ELException;
6 import jakarta.el.MethodExpression;
7 import jakarta.el.ValueExpression;
8 import jakarta.faces.component.ActionSource2;
9 import jakarta.faces.component.UIComponent;
10 import jakarta.faces.event.MethodExpressionActionListener;
11
12 /**
13 * This class has some static utils-methods.
14 *
15 * @author Stefan Graff
16 *
17 * @since Struts 1.4.1
18 */
19 public class Utils {
20
21 /**
22 * Class is not instantiable.
23 */
24 private Utils() {
25 }
26
27 /**
28 * If the specified attribute value is not {@code null} use it
29 * to either store a value binding expression for the specified
30 * attribute name, or store it as the literal value of the
31 * attribute.
32 *
33 * @param component {@code UIComponent} whose attribute is to
34 * be set
35 * @param propName Property name
36 * @param value Property value (or {@code null})
37 *
38 * @exception ELException if the expression has invalid syntax
39 *
40 * @since 1.4.1
41 */
42 public static void setBooleanProperty(UIComponent component,
43 String propName, ValueExpression value) {
44
45 setBooleanProperty(component, propName, value, null);
46 }
47
48 /**
49 * If the specified attribute value is not {@code null} use it
50 * to either store a value binding expression for the specified
51 * attribute name, or store it as the literal value of the
52 * attribute.
53 *
54 * @param component {@code UIComponent} whose attribute is to
55 * be set
56 * @param propName Property name
57 * @param value Property value (or {@code null})
58 * @param defaultValue When not {@code null} the default-value
59 *
60 * @exception ELException if the expression has invalid syntax
61 *
62 * @since 1.4.3
63 */
64 public static void setBooleanProperty(UIComponent component,
65 String propName, ValueExpression value, Boolean defaultValue) {
66
67 if (value == null) {
68 if (defaultValue != null) {
69 component.getAttributes().put(propName, defaultValue);
70 }
71
72 return;
73 }
74
75 if (value.isLiteralText()) {
76 component.getAttributes().put(propName, Boolean.valueOf(value.getExpressionString()));
77 } else {
78 component.setValueExpression(propName, value);
79 }
80 }
81
82 /**
83 * If the specified attribute value is not {@code null} use it
84 * to either store a value binding expression for the specified
85 * attribute name, or store it as the literal value of the
86 * attribute.
87 *
88 * @param component {@code UIComponent} whose attribute is to
89 * be set
90 * @param propName Property name
91 * @param value Property value (or {@code null})
92 *
93 * @exception ELException if the expression has invalid syntax
94 *
95 * @since 1.4.1
96 */
97 public static void setStringProperty(UIComponent component,
98 String propName, ValueExpression value) {
99
100 setStringProperty(component, propName, value, null);
101 }
102
103 /**
104 * If the specified attribute value is not {@code null} use it
105 * to either store a value binding expression for the specified
106 * attribute name, or store it as the literal value of the
107 * attribute.
108 *
109 * @param component {@code UIComponent} whose attribute is to
110 * be set
111 * @param propName Property name
112 * @param value Property value (or {@code null})
113 * @param defaultValue When not {@code null} the default-value
114 *
115 * @exception ELException if the expression has invalid syntax
116 *
117 * @since 1.4.3
118 */
119 public static void setStringProperty(UIComponent component,
120 String propName, ValueExpression value, String defaultValue) {
121
122 if (value == null) {
123 if (defaultValue != null) {
124 component.getAttributes().put(propName, defaultValue);
125 }
126
127 return;
128 }
129
130 if (value.isLiteralText()) {
131 component.getAttributes().put(propName, value.getExpressionString());
132 } else {
133 component.setValueExpression(propName, value);
134 }
135 }
136
137 /**
138 * If the specified attribute value is not {@code null} use it
139 * to either store a value binding expression for the specified
140 * attribute name, or store it as the literal value of the
141 * attribute.
142 *
143 * @param component {@code UIComponent} whose attribute is to
144 * be set
145 * @param propName Property name
146 * @param value Property value (or {@code null})
147 *
148 * @exception ELException if the expression has invalid syntax
149 *
150 * @since 1.4.1
151 */
152 public static void setIntegerProperty(UIComponent component,
153 String propName, ValueExpression value) {
154
155 setIntegerProperty(component, propName, value, null);
156 }
157
158 /**
159 * If the specified attribute value is not {@code null} use it
160 * to either store a value binding expression for the specified
161 * attribute name, or store it as the literal value of the
162 * attribute.
163 *
164 * @param component {@code UIComponent} whose attribute is to
165 * be set
166 * @param propName Property name
167 * @param value Property value (or {@code null})
168 * @param defaultValue When not {@code null} the default-value
169 *
170 * @exception ELException if the expression has invalid syntax
171 *
172 * @since 1.4.3
173 */
174 public static void setIntegerProperty(UIComponent component,
175 String propName, ValueExpression value, Integer defaultValue) {
176
177 if (value == null) {
178 if (defaultValue != null) {
179 component.getAttributes().put(propName, defaultValue);
180 }
181
182 return;
183 }
184
185 if (value.isLiteralText()) {
186 component.getAttributes().put(propName, Integer.valueOf(value.getExpressionString()));
187 } else {
188 component.setValueExpression(propName, value);
189 }
190 }
191
192 /**
193 * If the specified action is not {@code null} use it to
194 * set the action of the component.
195 *
196 * @param component {@code UIComponent} whose action is to
197 * be set
198 * @param action the Action
199 *
200 * @throws IllegalArgumentException if the component is not an
201 * instance of {@code ActionSource2}
202 *
203 * @since 1.4.1
204 */
205 public static void setActionProperty(UIComponent component, MethodExpression action) {
206 if (action != null) {
207 castActionSource2(component).setActionExpression(action);
208 }
209 }
210
211 /**
212 * If the specified action-listener is not {@code null} use
213 * it to add the action-listener to the component.
214 *
215 * @param component {@code UIComponent} whose action-listener
216 * is to be added
217 * @param actionListener the Action-Listener
218 *
219 * @throws IllegalArgumentException if the component is not an
220 * instance of {@code ActionSource2}
221 *
222 * @since 1.4.1
223 */
224 public static void setActionListenerProperty(UIComponent component, MethodExpression actionListener) {
225 if (actionListener != null) {
226 castActionSource2(component).addActionListener(new MethodExpressionActionListener(actionListener));
227 }
228 }
229
230 /**
231 * Test the component if it's an instance of {@code ActionSource2}
232 * and returns it.
233 *
234 * @param component {@code UIComponent} to test
235 *
236 * @return the component as {@code ActionSource2}
237 *
238 * @throws IllegalArgumentException if the component is not an
239 * instance of {@code ActionSource2}
240 *
241 * @since 1.4.1
242 */
243 private static ActionSource2 castActionSource2(UIComponent component) {
244 if (component instanceof ActionSource2) {
245 return (ActionSource2) component;
246 }
247 throw new IllegalArgumentException("Component "
248 + component.getClientId()
249 + " is no ActionSource2");
250 }
251
252 /**
253 * Returns the value of a map as expected class.
254 *
255 * @param <T> the expected class
256 * @param clazz the expected class
257 * @param map the map with the values
258 * @param key the key to access the map
259 *
260 * @return the value as expected class
261 *
262 * @throws ClassCastException if the key is of an
263 * inappropriate type for this map or if the
264 * value is not null and is not assignable to
265 * the type T.
266 * @throws NullPointerException if the specified key
267 * is null and this map does not permit null keys.
268 */
269 public static <T> T getMapValue(Class<T> clazz, Map<String, Object> map, String key) {
270 final Object ret = map.get(key);
271 return clazz.cast(ret);
272 }
273 }