Author: niallp
Date: Sat May 19 23:39:21 2007
New Revision: 539832

URL: http://svn.apache.org/viewvc?view=rev&rev=539832
Log:
BEANUTILS-258 - add new convert() and lookup() methods to ConvertUtils that 
utilize the capabilities of the improved Converter implementations. Also modify 
BeanUtils to use the new methods.

Modified:
    
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean.java
    
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ConvertUtils.java
    
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ConvertUtilsBean.java
    
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java
    
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/DynaBeanUtilsTestCase.java

Modified: 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean.java?view=diff&rev=539832&r1=539831&r2=539832
==============================================================================
--- 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean.java
 (original)
+++ 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean.java
 Sat May 19 23:39:21 2007
@@ -394,11 +394,7 @@
 
         // Convert the specified value to the required type and store it
         if (index >= 0) {                    // Destination must be indexed
-            Converter converter = 
getConvertUtils().lookup(type.getComponentType());
-            if (converter != null) {
-                log.trace("        USING CONVERTER " + converter);
-                value = converter.convert(type.getComponentType(), value);
-            }
+            value = getConvertUtils().convert(value, type.getComponentType());
             try {
                 getPropertyUtils().setIndexedProperty(target, propName,
                                                  index, value);
@@ -418,11 +414,7 @@
                     (e, "Cannot set " + propName);
             }
         } else {                             // Destination must be simple
-            Converter converter = getConvertUtils().lookup(type);
-            if (converter != null) {
-                log.trace("        USING CONVERTER " + converter);
-                value = converter.convert(type, value);
-            }
+            value = getConvertUtils().convert(value, type);
             try {
                 getPropertyUtils().setSimpleProperty(target, propName, value);
             } catch (NoSuchMethodException e) {
@@ -961,7 +953,13 @@
 
         // Convert the specified value to the required type
         Object newValue = null;
-        if (type.isArray() && (index < 0)) { // Scalar value into array
+        Class sourceType = value == null ? null : value.getClass();
+        Class targetType = (type.isArray() && (index >= 0) ? 
type.getComponentType() : type);
+        Converter converter = getConvertUtils().lookup(sourceType, targetType);
+        if (converter != null) {
+            newValue = converter.convert(targetType, value);
+            newValue = (targetType == String.class && newValue != null ? 
newValue.toString() : newValue);
+        } else if (type.isArray() && (index < 0)) { // Scalar value into array
             if (value == null) {
                 String values[] = new String[1];
                 values[0] = (String) value;

Modified: 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ConvertUtils.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ConvertUtils.java?view=diff&rev=539832&r1=539831&r2=539832
==============================================================================
--- 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ConvertUtils.java
 (original)
+++ 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ConvertUtils.java
 Sat May 19 23:39:21 2007
@@ -261,6 +261,21 @@
 
     }
 
+    /**
+     * <p>Convert the value to an object of the specified class (if
+     * possible).</p>
+     *
+     * @param value Value to be converted (may be null)
+     * @param targetType Class of the value to be converted to
+     * @return The converted value
+     *
+     * @exception ConversionException if thrown by an underlying Converter
+     */
+    public static Object convert(Object value, Class targetType) {
+
+        return ConvertUtilsBean.getInstance().convert(value, targetType);
+
+    }
 
     /**
      * <p>Remove all registered [EMAIL PROTECTED] Converter}s, and 
re-establish the
@@ -310,6 +325,20 @@
 
     }
 
+    /**
+     * Look up and return any registered [EMAIL PROTECTED] Converter} for the 
specified
+     * source and destination class; if there is no registered Converter,
+     * return <code>null</code>.
+     *
+     * @param sourceType Class of the value being converted
+     * @param targetType Class of the value to be converted to
+     * @return The registered [EMAIL PROTECTED] Converter} or 
<code>null</code> if not found
+     */
+    public static Converter lookup(Class sourceType, Class targetType) {
+
+        return ConvertUtilsBean.getInstance().lookup(sourceType, targetType);
+
+    }
 
     /**
      * <p>Register a custom [EMAIL PROTECTED] Converter} for the specified 
destination

Modified: 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ConvertUtilsBean.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ConvertUtilsBean.java?view=diff&rev=539832&r1=539831&r2=539832
==============================================================================
--- 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ConvertUtilsBean.java
 (original)
+++ 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ConvertUtilsBean.java
 Sat May 19 23:39:21 2007
@@ -27,6 +27,7 @@
 import java.sql.Date;
 import java.sql.Time;
 import java.sql.Timestamp;
+import java.util.Collection;
 import java.util.Map;
 import java.util.HashMap;
 import org.apache.commons.beanutils.converters.BigDecimalConverter;
@@ -434,12 +435,10 @@
             if (value == null) {
                 return ((String) null);
             } else {
-                Converter converter = lookup(String.class);
-                return ((String) converter.convert(String.class, value));
+                return ((String)convert(value, String.class));
             }
         } else {
-            Converter converter = lookup(String.class);
-            return ((String) converter.convert(String.class, value));
+            return ((String)convert(value, String.class));
         }
 
     }
@@ -490,6 +489,10 @@
 
         Class type = clazz;
         if (clazz.isArray()) {
+            Converter converter = lookup(clazz);
+            if (converter != null) {
+                return converter.convert(clazz, values);
+            }
             type = clazz.getComponentType();
         }
         if (log.isDebugEnabled()) {
@@ -513,6 +516,45 @@
 
 
     /**
+     * <p>Convert the value to an object of the specified class (if
+     * possible).</p>
+     *
+     * @param value Value to be converted (may be null)
+     * @param targetType Class of the value to be converted to
+     * @return The converted value
+     *
+     * @exception ConversionException if thrown by an underlying Converter
+     */
+    public Object convert(Object value, Class targetType) {
+
+        Class sourceType = value == null ? null : value.getClass();
+
+        if (log.isDebugEnabled()) {
+            if (value == null) {
+                log.debug("Convert null value to type '" +
+                        targetType.getName() + "'");
+            } else {
+                log.debug("Convert type '" + sourceType.getName() + "' value 
'" + value +
+                      "' to type '" + targetType.getName() + "'");
+            }
+        }
+
+        Object converted = value;
+        Converter converter = lookup(sourceType, targetType);
+        if (converter != null) {
+            if (log.isTraceEnabled()) {
+                log.trace("  Using converter " + converter);
+            }
+            converted = converter.convert(targetType, value);
+        }
+        if (targetType == String.class && value != null) {
+            converted = value.toString();
+        }
+        return converted;
+
+    }
+
+    /**
      * Remove all registered [EMAIL PROTECTED] Converter}s, and re-establish 
the
      * standard Converters.
      */
@@ -614,6 +656,51 @@
 
     }
 
+    /**
+     * Look up and return any registered [EMAIL PROTECTED] Converter} for the 
specified
+     * source and destination class; if there is no registered Converter,
+     * return <code>null</code>.
+     *
+     * @param sourceType Class of the value being converted
+     * @param targetType Class of the value to be converted to
+     * @return The registered [EMAIL PROTECTED] Converter} or 
<code>null</code> if not found
+     */
+    public Converter lookup(Class sourceType, Class targetType) {
+
+        if (targetType == null) {
+            throw new IllegalArgumentException("Target type is missing");
+        }
+
+        Converter converter = null;
+        // Convert --> String 
+        if (targetType == String.class) {
+            if (sourceType != null) {
+                converter = lookup(sourceType);
+                if (converter == null && (sourceType.isArray() ||
+                        Collection.class.isAssignableFrom(sourceType))) {
+                    converter = lookup(String[].class);
+                }
+            }
+            if (converter == null) {
+                converter = lookup(String.class);
+            }
+            return converter;
+        }
+
+        // Convert --> String array 
+        if (targetType == String[].class) {
+            if (sourceType != null && sourceType.isArray()) {
+                converter = lookup(sourceType);
+            }
+            if (converter == null) {
+                converter = lookup(String[].class);
+            }
+        } else {
+            converter = lookup(targetType);
+        }
+        return converter;
+
+    }
 
     /**
      * Register a custom [EMAIL PROTECTED] Converter} for the specified 
destination

Modified: 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java?view=diff&rev=539832&r1=539831&r2=539832
==============================================================================
--- 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java
 (original)
+++ 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java
 Sat May 19 23:39:21 2007
@@ -794,9 +794,7 @@
         assertTrue("stringArray of correct type",
                    newValue instanceof String[]);
         assertEquals("stringArray length",
-                     1, ((String[]) newValue).length);
-        assertTrue("stringArray[0] is null",
-                   ((String[]) newValue)[0] == null);
+                     0, ((String[]) newValue).length);
         PropertyUtils.setProperty(bean, "stringArray", oldValue);
 
         // Indexed value into array
@@ -1298,13 +1296,6 @@
         BeanUtilsBean beanUtils = new BeanUtilsBean(   
                                                     new ConvertUtilsBean(), 
                                                     new PropertyUtilsBean());
-        beanUtils.getConvertUtils().register(
-            new Converter () {
-                public Object convert(Class type, Object value) {
-                    return "Spam, spam, spam, spam!";
-                }
-            },
-            String.class);
             
         TestBean bean = new TestBean();
         String [] results = beanUtils.getArrayProperty(bean, "intArray");
@@ -1317,7 +1308,7 @@
         for (int i=0, size=values.length ;  i<size; i++) {
             assertEquals(
                     "Value " + i + " incorrectly converted ", 
-                    "Spam, spam, spam, spam!",
+                    values[i] + "",
                     results[i]);
         }
     }

Modified: 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/DynaBeanUtilsTestCase.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/DynaBeanUtilsTestCase.java?view=diff&rev=539832&r1=539831&r2=539832
==============================================================================
--- 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/DynaBeanUtilsTestCase.java
 (original)
+++ 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/DynaBeanUtilsTestCase.java
 Sat May 19 23:39:21 2007
@@ -907,9 +907,7 @@
         assertTrue("stringArray of correct type",
                    newValue instanceof String[]);
         assertEquals("stringArray length",
-                     1, ((String[]) newValue).length);
-        assertTrue("stringArray[0] is null",
-                   ((String[]) newValue)[0] == null);
+                     0, ((String[]) newValue).length);
         PropertyUtils.setProperty(bean, "stringArray", oldValue);
 
         // Indexed value into array



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to