View | Details | Raw Unified | Return to bug 39331
Collapse All | Expand All

(-)src/org/apache/taglibs/input/Select.java (-19 / +105 lines)
Lines 48-54 Link Here
48
    private Map attributes; // attributes of the <select> element
48
    private Map attributes; // attributes of the <select> element
49
49
50
    private Map options; // what are our options? :)
50
    private Map options; // what are our options? :)
51
51
	
52
    private String attributesText; // attributes of the <input> element as text
52
    private String attributesText; // attributes of the <input> element as text
53
53
54
    private String beanId; // bean id to get default values from
54
    private String beanId; // bean id to get default values from
Lines 187-207 Link Here
187
                    outputOption(out, oLabel, oVal);
187
                    outputOption(out, oLabel, oVal);
188
                }
188
                }
189
            } else if (options != null) {
189
            } else if (options != null) {
190
                Iterator i = options.keySet().iterator();
190
                outputOptions(options, out);
191
                while (i.hasNext()) {
192
                    Object oLabel = i.next();
193
                    Object oVal = options.get(oLabel);
194
195
                    outputOption(out, oLabel, oVal);
196
                }
197
            }
191
            }
198
199
        } catch (Exception ex) {
192
        } catch (Exception ex) {
193
			pageContext.getServletContext().log("Error in doStartTag", ex);
200
            throw new JspTagException(ex.getMessage());
194
            throw new JspTagException(ex.getMessage());
201
        }
195
        }
202
        return EVAL_BODY_INCLUDE;
196
        return EVAL_BODY_INCLUDE;
203
    }
197
    }
204
198
199
    /**
200
     * Write the html option elements using the given Map and writer.
201
     * 
202
     * @param optionsMap
203
     * @param out
204
     * @throws java.io.IOException
205
     */
206
    private void outputOptions(Map optionsMap, JspWriter out)
207
    	throws java.io.IOException {
208
        Iterator i = optionsMap.keySet().iterator();
209
        while (i.hasNext()) {
210
            Object oLabel = i.next();
211
            Object oVal = optionsMap.get(oLabel);
212
213
            outputOption(out, oLabel, oVal);
214
        }
215
    }
216
205
    private void outputOption(JspWriter out, Object oLabel, Object oVal)
217
    private void outputOption(JspWriter out, Object oLabel, Object oVal)
206
            throws java.io.IOException {
218
            throws java.io.IOException {
207
        String label = oLabel.toString();
219
        String label = oLabel.toString();
Lines 300-317 Link Here
300
        }
312
        }
301
    }
313
    }
302
314
303
    public void setOptions(Map x) {
315
    public void setOptions(Object x) {
304
        options = x;
316
    	options = resolveToMap(x);
305
    }
317
    }
306
318
307
    public void setOptionLabels(List x) {
319
    public void setOptionLabels(Object x) {
308
        optionLabels = x;
320
        optionLabels = resolveToList(x);
309
    }
321
    }
310
322
311
    public void setOptionValues(List x) {
323
    public void setOptionValues(Object x) {
312
        optionValues = x;
324
        optionValues = resolveToList(x);
313
    }
325
    }
314
326
327
    /**
328
     * This method attempts to resolve the param to a Map.<br/>
329
     * If param is a Map, simply return it.<br/>
330
     * If param is a String, try to find a bean with param as the name.<br/>
331
     * If param is null or not a Map or String, a message will be logged
332
     * to the ServletContext log.<br/>
333
     * 
334
     * @param param
335
     * @return param, if it is a Map, or a bean found using param as the name, or null.
336
     */
337
    private Map resolveToMap(Object param) {
338
    	Map result = null;
339
    	
340
    	if (param == null) {
341
        	pageContext.getServletContext().log("attribute was null");
342
    	}
343
    	else if (param instanceof String) {
344
    		// user provided a string, so try to find an attribute with that name.
345
            result = (Map)pageContext.findAttribute((String)param);
346
            
347
            // no object was found with that name
348
            if (result == null) {
349
            	// log something to help the user
350
            	pageContext.getServletContext().log("no object found for : " + param);
351
            }
352
    	}
353
    	else if (param instanceof Map) {
354
    		result = (Map)param;
355
    	}
356
    	else {
357
        	pageContext.getServletContext().log("Unexpected type for param : " + 
358
        												param.getClass().getName());
359
    	}
360
    	
361
    	return result;
362
    }
363
364
    /**
365
     * This method attempts to resolve the param to a List.<br/>
366
     * If param is a List, simply return it.<br/>
367
     * If param is a String, try to find a bean with param as the name.<br/>
368
     * If param is null or not a List or String, a message will be logged
369
     * to the ServletContext log.<br/>
370
     * 
371
     * @param param
372
     * @return param, if it is a List, or a bean found using param as the name, or null.
373
     */
374
    private List resolveToList(Object param) {
375
    	List result = null;
376
    	
377
    	if (param == null) {
378
        	pageContext.getServletContext().log("attribute was null");
379
    	}
380
    	else if (param instanceof String) {
381
    		// user provided a string, so try to find an attribute with that name.
382
            result = (List)pageContext.findAttribute((String)param);
383
            
384
            // no object was found with that name
385
            if (result == null) {
386
            	// log something to help the user
387
            	pageContext.getServletContext().log("no object found for : " + param);
388
            }
389
    	}
390
    	else if (param instanceof List) {
391
    		result = (List)param;
392
    	}
393
    	else {
394
        	pageContext.getServletContext().log("Unexpected type for param : " + 
395
        												param.getClass().getName());
396
    	}
397
    	
398
    	return result;
399
    }
400
    
315
    public HashMap getChosen() {
401
    public HashMap getChosen() {
316
        return chosen;
402
        return chosen;
317
    }
403
    }
Lines 384-390 Link Here
384
     * 
470
     * 
385
     * @return Value of property optionLabels.
471
     * @return Value of property optionLabels.
386
     */
472
     */
387
    public List getOptionLabels() {
473
    public Object getOptionLabels() {
388
        return optionLabels;
474
        return optionLabels;
389
    }
475
    }
390
476
Lines 393-399 Link Here
393
     * 
479
     * 
394
     * @return Value of property optionValues.
480
     * @return Value of property optionValues.
395
     */
481
     */
396
    public List getOptionValues() {
482
    public Object getOptionValues() {
397
        return optionValues;
483
        return optionValues;
398
    }
484
    }
399
485
Lines 402-408 Link Here
402
     * 
488
     * 
403
     * @return Value of property options.
489
     * @return Value of property options.
404
     */
490
     */
405
    public Map getOptions() {
491
    public Object getOptions() {
406
        return options;
492
        return options;
407
    }
493
    }
408
494
Lines 415-418 Link Here
415
        return size;
501
        return size;
416
    }
502
    }
417
503
418
}
504
}
(-)src/org/apache/taglibs/input/Util.java (-20 / +87 lines)
Lines 15-21 Link Here
15
 */
15
 */
16
package org.apache.taglibs.input;
16
package org.apache.taglibs.input;
17
17
18
import java.beans.BeanInfo;
18
import java.beans.IntrospectionException;
19
import java.beans.IntrospectionException;
20
import java.beans.Introspector;
19
import java.beans.PropertyDescriptor;
21
import java.beans.PropertyDescriptor;
20
import java.io.IOException;
22
import java.io.IOException;
21
import java.lang.reflect.InvocationTargetException;
23
import java.lang.reflect.InvocationTargetException;
Lines 128-166 Link Here
128
    }
130
    }
129
131
130
    /**
132
    /**
131
     * Gets a named property from a JavaBean and returns its value as an Object,
133
     * 
132
     * possibly null.
134
     * @param propertyName
135
     * @return the javabean standard getter method name for the
136
     * given propertyName.
133
     */
137
     */
134
    public static Object beanPropertyValueObject(Object bean, String name)
138
    private static String getReadMethodName(String propertyName) {
139
        String result = "get" + 
140
				Character.toUpperCase(propertyName.charAt(0)) +
141
				propertyName.substring(1);
142
        
143
        return result;
144
    }
145
146
    /**
147
     * Internal method that does all the work of getting a single value
148
     * for the given property name, from the given bean.
149
     * <p/>
150
     * Here, the name parameter does NOT support nested objects.
151
     * <p/>
152
     * 
153
     * @see #beanPropertyValueObject(Object, String)
154
     * @param bean
155
     * @param name
156
     */
157
    private static Object beanPropertyValueObjectInternal(Object bean, String name)
135
            throws JspTagException {
158
            throws JspTagException {
159
        
136
        if (bean != null) {
160
        if (bean != null) {
137
            Method reader = null;
161
            Method reader = null;
138
            Object[] params = null;
162
            Object[] params = null;
139
163
140
            // Try to find a reader method for the named property
141
            try {
164
            try {
142
                PropertyDescriptor prop = new PropertyDescriptor(name, bean
165
                BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
143
                        .getClass());
166
                
144
                reader = prop.getReadMethod();
167
                PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
145
            } catch (IntrospectionException e) {
168
                
146
                // No property exists with that name, try a generic get method
169
                for(PropertyDescriptor prop: props) {
147
                // Object get( Object key )
170
                    if (prop.getName().equals(name)) {
148
                try {
171
                        reader = prop.getReadMethod();
149
                    reader = bean.getClass().getMethod("get",
172
                    }
150
                            new Class[] { Object.class });
173
                }
151
                    params = new Object[] { name };
174
                
152
                } catch (NoSuchMethodException f) {
175
                if (reader == null) {
153
                    // Try an Object get( String key) method
154
                    try {
176
                    try {
155
                        reader = bean.getClass().getMethod("get",
177
                        // try a generic get method: Object get( Object key )
156
                                new Class[] { String.class });
178
                        reader = bean.getClass().getMethod("get", new Class[] { Object.class });
157
                        params = new Object[] { name };
179
                        params = new Object[] { name };
158
                    } catch (NoSuchMethodException g) {
180
                    } catch(NoSuchMethodException e) {
159
                        // Give up
181
                        // Give up
160
                    }
182
                    }
161
                }
183
                }
184
            } catch(IntrospectionException e) {
185
                // couldn't get beaninfo
162
            }
186
            }
163
187
            
164
            // If a reader method has been found
188
            // If a reader method has been found
165
            if (reader != null) {
189
            if (reader != null) {
166
                try {
190
                try {
Lines 174-185 Link Here
174
                            + e.getTargetException());
198
                            + e.getTargetException());
175
                }
199
                }
176
            }
200
            }
201
            else {
202
                // shouldn't we at least log something for the user?
203
            }
177
        }
204
        }
178
205
179
        return null;
206
        return null;
180
    }
207
    }
181
208
182
    /**
209
    /**
210
     * Gets a named property from a JavaBean and returns its value as an Object,
211
     * possibly null.
212
     * <p/>
213
     * name supports nested objects.  For example, a name
214
     * of "foo.bar.myProperty" will return the value of
215
     * bean.getFoo().getBar().getMyProperty().
216
     * <p/>
217
     * bean is only required to have a getter for the property;
218
     * a setter is not required.
219
     * 
220
     * @param bean the bean to read the property from.
221
     * @param name name of property to read.
222
     */
223
    public static Object beanPropertyValueObject(Object bean, String name)
224
            throws JspTagException {
225
        if (bean != null) {
226
227
            // split the name by period
228
            String[] names = name.split("\\.");
229
            
230
            if (names.length == 1) {
231
            	// only 1, so call the non-nested method
232
            	return beanPropertyValueObjectInternal(bean, names[0]);
233
            }
234
            else
235
            {
236
            	Object obj = bean;
237
            	
238
            	for (int i = 0; i < names.length && obj != null; i++) {
239
            		obj = beanPropertyValueObjectInternal(obj, names[i]);
240
            	}
241
            	
242
            	return obj;
243
            }
244
        }
245
246
        return null;
247
    }
248
249
    /**
183
     * Gets a named property from a JavaBean and returns its value as a String,
250
     * Gets a named property from a JavaBean and returns its value as a String,
184
     * possibly null.
251
     * possibly null.
185
     */
252
     */

Return to bug 39331