/** * Get Map of property values from a bean. * * @param obj Object to query for properties. * @param allowedProperties If array is NOT null, only the properties matching names * passed here shall be retrieved. * @return Map containing property-name (String) / property-value (Object) pairs. */ public final static Map getValues(Object obj, String[] allowedProperties) { Map result = new HashMap(); String[] propertyNames = getPropertyNames(obj); for (int i = 0; i < propertyNames.length; i++) { String propertyName = propertyNames[i]; Object propertyValue = getValue(obj, propertyName); if ((propertyName == null) || (propertyValue == null)) { continue; } if (allowed(propertyName, allowedProperties)) { result.put(propertyName, propertyValue); } } return result; }
/** * Set multiple properties of a bean at once using properties of another bean. * The beans may be of different types and any properties not common to both types * shall be ignored. * * @param obj The object to be manipulated. * @param src The object containing the properties to be copied. * @param allowedProperties If array is NOT null, only the properties matching names * passed here shall be set. */ public final static void setValues(Object obj, Object src, String[] allowedProperties) { setValues(obj, getValues(src, allowedProperties), allowedProperties); }
/** * Set multiple properties of a bean at once using a Map. Any unknown properties * shall be ignored. * * @param obj The object to be manipulated. * @param valueMap Map containing property-name (String) / property-value (Object) * pairs to set in the object. * @param allowedProperties If array is NOT null, only the properties matching names * passed here shall be set. */ public final static void setValues(Object obj, Map valueMap, String[] allowedProperties) { Iterator keys = valueMap.keySet().iterator(); while (keys.hasNext()) { String property = keys.next().toString(); Object value = valueMap.get(property); if (allowed(property, allowedProperties)) { setValue(obj, property, value); } } }
/** * Set multiple properties of a bean at once using the params passed across * from the ServletRequest (useful for mapping HTML forms to beans). Any properties * not known shall be ignored. * * @param obj The object to be manipulated. * @param request ServletRequest to get params from. * @param allowedProperties If array is NOT null, only the properties matching names * passed here shall be set. */ public final static void setValues(Object obj, ServletRequest request, String[] allowedProperties) { Map params = new HashMap(); Enumeration paramNames = request.getParameterNames(); while (paramNames.hasMoreElements()) { String paramName = (String) paramNames.nextElement(); String paramValue = request.getParameter(paramName); params.put(paramName, paramValue); } setValues(obj, params, allowedProperties); }
/** * Set multiple properties of a bean at once using a Map. Any unknown properties * shall be ignored. * * @param obj The object to be manipulated. * @param valueMap Map containing property-name (String) / property-value (Object) * pairs to set in the object. * @param allowedProperties If array is NOT null, only the properties matching names * passed here shall be set. */ public final static void setValues(Object obj, Map valueMap, String[] allowedProperties) { Iterator keys = valueMap.keySet().iterator(); while (keys.hasNext()) { String property = keys.next().toString(); Object value = valueMap.get(property); if (allowed(property, allowedProperties)) { setValue(obj, property, value); } } }
/** * Set multiple properties of a bean at once using the params passed across * from the ServletRequest (useful for mapping HTML forms to beans). Any properties * not known shall be ignored. * * @param obj The object to be manipulated. * @param request ServletRequest to get params from. * @param allowedProperties If array is NOT null, only the properties matching names * passed here shall be set. */ public final static void setValues(Object obj, ServletRequest request, String[] allowedProperties) { Map params = new HashMap(); Enumeration paramNames = request.getParameterNames(); while (paramNames.hasMoreElements()) { String paramName = (String) paramNames.nextElement(); String paramValue = request.getParameter(paramName); params.put(paramName, paramValue); } setValues(obj, params, allowedProperties); }
/** * Get Map of property values from a bean. * * @param obj Object to query for properties. * @param allowedProperties If array is NOT null, only the properties matching names * passed here shall be retrieved. * @return Map containing property-name (String) / property-value (Object) pairs. */ public final static Map getValues(Object obj, String[] allowedProperties) { Map result = new HashMap(); String[] propertyNames = getPropertyNames(obj); for (int i = 0; i < propertyNames.length; i++) { String propertyName = propertyNames[i]; Object propertyValue = getValue(obj, propertyName); if ((propertyName == null) || (propertyValue == null)) { continue; } if (allowed(propertyName, allowedProperties)) { result.put(propertyName, propertyValue); } } return result; }
/** * Set multiple properties of a bean at once using properties of another bean. * The beans may be of different types and any properties not common to both types * shall be ignored. * * @param obj The object to be manipulated. * @param src The object containing the properties to be copied. * @param allowedProperties If array is NOT null, only the properties matching names * passed here shall be set. */ public final static void setValues(Object obj, Object src, String[] allowedProperties) { setValues(obj, getValues(src, allowedProperties), allowedProperties); }