ASF Bugzilla – Attachment 18117 Details for
Bug 39331
support nested calls and Maps from request
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
A Patch to the input taglib to provide nested calls and request lookup for options map.
inputpatch_mgh.txt (text/plain), 10.94 KB, created by
Mike Hansen
on 2006-04-18 04:18:18 UTC
(
hide
)
Description:
A Patch to the input taglib to provide nested calls and request lookup for options map.
Filename:
MIME Type:
Creator:
Mike Hansen
Created:
2006-04-18 04:18:18 UTC
Size:
10.94 KB
patch
obsolete
>Index: src/org/apache/taglibs/input/Select.java >=================================================================== >--- src/org/apache/taglibs/input/Select.java (revision 394437) >+++ src/org/apache/taglibs/input/Select.java (working copy) >@@ -48,7 +48,7 @@ > private Map attributes; // attributes of the <select> element > > private Map options; // what are our options? :) >- >+ > private String attributesText; // attributes of the <input> element as text > > private String beanId; // bean id to get default values from >@@ -187,21 +187,33 @@ > outputOption(out, oLabel, oVal); > } > } else if (options != null) { >- Iterator i = options.keySet().iterator(); >- while (i.hasNext()) { >- Object oLabel = i.next(); >- Object oVal = options.get(oLabel); >- >- outputOption(out, oLabel, oVal); >- } >+ outputOptions(options, out); > } >- > } catch (Exception ex) { >+ pageContext.getServletContext().log("Error in doStartTag", ex); > throw new JspTagException(ex.getMessage()); > } > return EVAL_BODY_INCLUDE; > } > >+ /** >+ * Write the html option elements using the given Map and writer. >+ * >+ * @param optionsMap >+ * @param out >+ * @throws java.io.IOException >+ */ >+ private void outputOptions(Map optionsMap, JspWriter out) >+ throws java.io.IOException { >+ Iterator i = optionsMap.keySet().iterator(); >+ while (i.hasNext()) { >+ Object oLabel = i.next(); >+ Object oVal = optionsMap.get(oLabel); >+ >+ outputOption(out, oLabel, oVal); >+ } >+ } >+ > private void outputOption(JspWriter out, Object oLabel, Object oVal) > throws java.io.IOException { > String label = oLabel.toString(); >@@ -300,18 +312,92 @@ > } > } > >- public void setOptions(Map x) { >- options = x; >+ public void setOptions(Object x) { >+ options = resolveToMap(x); > } > >- public void setOptionLabels(List x) { >- optionLabels = x; >+ public void setOptionLabels(Object x) { >+ optionLabels = resolveToList(x); > } > >- public void setOptionValues(List x) { >- optionValues = x; >+ public void setOptionValues(Object x) { >+ optionValues = resolveToList(x); > } > >+ /** >+ * This method attempts to resolve the param to a Map.<br/> >+ * If param is a Map, simply return it.<br/> >+ * If param is a String, try to find a bean with param as the name.<br/> >+ * If param is null or not a Map or String, a message will be logged >+ * to the ServletContext log.<br/> >+ * >+ * @param param >+ * @return param, if it is a Map, or a bean found using param as the name, or null. >+ */ >+ private Map resolveToMap(Object param) { >+ Map result = null; >+ >+ if (param == null) { >+ pageContext.getServletContext().log("attribute was null"); >+ } >+ else if (param instanceof String) { >+ // user provided a string, so try to find an attribute with that name. >+ result = (Map)pageContext.findAttribute((String)param); >+ >+ // no object was found with that name >+ if (result == null) { >+ // log something to help the user >+ pageContext.getServletContext().log("no object found for : " + param); >+ } >+ } >+ else if (param instanceof Map) { >+ result = (Map)param; >+ } >+ else { >+ pageContext.getServletContext().log("Unexpected type for param : " + >+ param.getClass().getName()); >+ } >+ >+ return result; >+ } >+ >+ /** >+ * This method attempts to resolve the param to a List.<br/> >+ * If param is a List, simply return it.<br/> >+ * If param is a String, try to find a bean with param as the name.<br/> >+ * If param is null or not a List or String, a message will be logged >+ * to the ServletContext log.<br/> >+ * >+ * @param param >+ * @return param, if it is a List, or a bean found using param as the name, or null. >+ */ >+ private List resolveToList(Object param) { >+ List result = null; >+ >+ if (param == null) { >+ pageContext.getServletContext().log("attribute was null"); >+ } >+ else if (param instanceof String) { >+ // user provided a string, so try to find an attribute with that name. >+ result = (List)pageContext.findAttribute((String)param); >+ >+ // no object was found with that name >+ if (result == null) { >+ // log something to help the user >+ pageContext.getServletContext().log("no object found for : " + param); >+ } >+ } >+ else if (param instanceof List) { >+ result = (List)param; >+ } >+ else { >+ pageContext.getServletContext().log("Unexpected type for param : " + >+ param.getClass().getName()); >+ } >+ >+ return result; >+ } >+ > public HashMap getChosen() { > return chosen; > } >@@ -384,7 +470,7 @@ > * > * @return Value of property optionLabels. > */ >- public List getOptionLabels() { >+ public Object getOptionLabels() { > return optionLabels; > } > >@@ -393,7 +479,7 @@ > * > * @return Value of property optionValues. > */ >- public List getOptionValues() { >+ public Object getOptionValues() { > return optionValues; > } > >@@ -402,7 +488,7 @@ > * > * @return Value of property options. > */ >- public Map getOptions() { >+ public Object getOptions() { > return options; > } > >@@ -415,4 +501,4 @@ > return size; > } > >-} >\ No newline at end of file >+} >Index: src/org/apache/taglibs/input/Util.java >=================================================================== >--- src/org/apache/taglibs/input/Util.java (revision 394437) >+++ src/org/apache/taglibs/input/Util.java (working copy) >@@ -15,7 +15,9 @@ > */ > package org.apache.taglibs.input; > >+import java.beans.BeanInfo; > import java.beans.IntrospectionException; >+import java.beans.Introspector; > import java.beans.PropertyDescriptor; > import java.io.IOException; > import java.lang.reflect.InvocationTargetException; >@@ -128,39 +130,61 @@ > } > > /** >- * Gets a named property from a JavaBean and returns its value as an Object, >- * possibly null. >+ * >+ * @param propertyName >+ * @return the javabean standard getter method name for the >+ * given propertyName. > */ >- public static Object beanPropertyValueObject(Object bean, String name) >+ private static String getReadMethodName(String propertyName) { >+ String result = "get" + >+ Character.toUpperCase(propertyName.charAt(0)) + >+ propertyName.substring(1); >+ >+ return result; >+ } >+ >+ /** >+ * Internal method that does all the work of getting a single value >+ * for the given property name, from the given bean. >+ * <p/> >+ * Here, the name parameter does NOT support nested objects. >+ * <p/> >+ * >+ * @see #beanPropertyValueObject(Object, String) >+ * @param bean >+ * @param name >+ */ >+ private static Object beanPropertyValueObjectInternal(Object bean, String name) > throws JspTagException { >+ > if (bean != null) { > Method reader = null; > Object[] params = null; > >- // Try to find a reader method for the named property > try { >- PropertyDescriptor prop = new PropertyDescriptor(name, bean >- .getClass()); >- reader = prop.getReadMethod(); >- } catch (IntrospectionException e) { >- // No property exists with that name, try a generic get method >- // Object get( Object key ) >- try { >- reader = bean.getClass().getMethod("get", >- new Class[] { Object.class }); >- params = new Object[] { name }; >- } catch (NoSuchMethodException f) { >- // Try an Object get( String key) method >+ BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass()); >+ >+ PropertyDescriptor[] props = beanInfo.getPropertyDescriptors(); >+ >+ for(PropertyDescriptor prop: props) { >+ if (prop.getName().equals(name)) { >+ reader = prop.getReadMethod(); >+ } >+ } >+ >+ if (reader == null) { > try { >- reader = bean.getClass().getMethod("get", >- new Class[] { String.class }); >+ // try a generic get method: Object get( Object key ) >+ reader = bean.getClass().getMethod("get", new Class[] { Object.class }); > params = new Object[] { name }; >- } catch (NoSuchMethodException g) { >+ } catch(NoSuchMethodException e) { > // Give up > } > } >+ } catch(IntrospectionException e) { >+ // couldn't get beaninfo > } >- >+ > // If a reader method has been found > if (reader != null) { > try { >@@ -174,12 +198,55 @@ > + e.getTargetException()); > } > } >+ else { >+ // shouldn't we at least log something for the user? >+ } > } > > return null; > } > > /** >+ * Gets a named property from a JavaBean and returns its value as an Object, >+ * possibly null. >+ * <p/> >+ * name supports nested objects. For example, a name >+ * of "foo.bar.myProperty" will return the value of >+ * bean.getFoo().getBar().getMyProperty(). >+ * <p/> >+ * bean is only required to have a getter for the property; >+ * a setter is not required. >+ * >+ * @param bean the bean to read the property from. >+ * @param name name of property to read. >+ */ >+ public static Object beanPropertyValueObject(Object bean, String name) >+ throws JspTagException { >+ if (bean != null) { >+ >+ // split the name by period >+ String[] names = name.split("\\."); >+ >+ if (names.length == 1) { >+ // only 1, so call the non-nested method >+ return beanPropertyValueObjectInternal(bean, names[0]); >+ } >+ else >+ { >+ Object obj = bean; >+ >+ for (int i = 0; i < names.length && obj != null; i++) { >+ obj = beanPropertyValueObjectInternal(obj, names[i]); >+ } >+ >+ return obj; >+ } >+ } >+ >+ return null; >+ } >+ >+ /** > * Gets a named property from a JavaBean and returns its value as a String, > * possibly null. > */
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 39331
: 18117