commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dgra...@apache.org
Subject cvs commit: jakarta-commons/validator/src/share/org/apache/commons/validator/util ValidatorUtils.java
Date Fri, 15 Aug 2003 23:44:19 GMT
dgraham     2003/08/15 16:44:19

  Modified:    validator/src/share/org/apache/commons/validator/util
                        ValidatorUtils.java
  Log:
  Changed getValueAsString() to return "" for empty String[]s and Collections.
  This allows easier validation of lists.  PR# 22121.
  
  Revision  Changes    Path
  1.3       +39 -18    jakarta-commons/validator/src/share/org/apache/commons/validator/util/ValidatorUtils.java
  
  Index: ValidatorUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/util/ValidatorUtils.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ValidatorUtils.java	25 May 2003 18:18:31 -0000	1.2
  +++ ValidatorUtils.java	15 Aug 2003 23:44:19 -0000	1.3
  @@ -62,6 +62,7 @@
   package org.apache.commons.validator.util;
   
   import java.lang.reflect.InvocationTargetException;
  +import java.util.Collection;
   import java.util.Iterator;
   
   import org.apache.commons.beanutils.PropertyUtils;
  @@ -97,6 +98,7 @@
   		String value,
   		String key,
   		String replaceValue) {
  +            
   		if (value == null || key == null || replaceValue == null) {
   			return value;
   		}
  @@ -113,8 +115,10 @@
   
   		if (length == key.length()) {
   			value = replaceValue;
  +            
   		} else if (end == length) {
   			value = value.substring(0, start) + replaceValue;
  +            
   		} else {
   			value =
   				value.substring(0, start)
  @@ -127,24 +131,41 @@
   
   	/**
   	 * Convenience method for getting a value from a bean property as a 
  -	 * <code>String</code>.
  +	 * <code>String</code>.  If the property is a <code>String[]</code>
or 
  +     * <code>Collection</code> and it is empty, an empty <code>String</code>

  +     * "" is returned.  Otherwise, property.toString() is returned.  This method
  +     * may return <code>null</code> if there was an error retrieving the 
  +     * property.
   	 */
  -	public static String getValueAsString(Object bean, String property) {
  -		Object value = null;
  +    public static String getValueAsString(Object bean, String property) {
  +        Object value = null;
   
  -		try {
  -			value = PropertyUtils.getProperty(bean, property);
  +        try {
  +            value = PropertyUtils.getProperty(bean, property);
   
  -		} catch (IllegalAccessException e) {
  -			log.error(e.getMessage(), e);
  -		} catch (InvocationTargetException e) {
  -			log.error(e.getMessage(), e);
  -		} catch (NoSuchMethodException e) {
  -			log.error(e.getMessage(), e);
  -		}
  +        } catch (IllegalAccessException e) {
  +            log.error(e.getMessage(), e);
  +        } catch (InvocationTargetException e) {
  +            log.error(e.getMessage(), e);
  +        } catch (NoSuchMethodException e) {
  +            log.error(e.getMessage(), e);
  +        }
  +
  +        if (value == null) {
  +            return null;
  +        }
  +
  +        if (value instanceof String[]) {
  +            return ((String[]) value).length > 0 ? value.toString() : "";
  +
  +        } else if (value instanceof Collection) {
  +            return ((Collection) value).isEmpty() ? "" : value.toString();
  +
  +        } else {
  +            return value.toString();
  +        }
   
  -		return (value != null ? value.toString() : null);
  -	}
  +    }
   
   	/**
   	 * Makes a deep copy of a <code>FastHashMap</code> if the values 
  
  
  

Mime
View raw message