Hi there,
Here's a patch which provides support for custom converters in
ConvertUtils. It allows registering an object implementing the Converter
interface as a converter for a certain class. It's pretty simple but can
be useful, so I hope it can be included in beanutils.
greets,
Klaasjan Brand
--- jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/ConvertUtils.java
Wed Jan 23 23:35:58 2002
+++
+eclipse/workspace/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/ConvertUtils.java
+ Tue Mar 12 16:40:47 2002
@@ -65,6 +65,8 @@
import java.lang.reflect.Array;
+import org.apache.commons.collections.FastHashMap;
+
/**
* Utility methods for converting String values to objects of the specified
@@ -206,9 +208,25 @@
*/
private static Class stringClass = String.class;
+ /**
+ * Custom Converters
+ */
+ private static FastHashMap customConverters;
// --------------------------------------------------------- Public Classes
+ /**
+ * Register a custom Converter object
+ *
+ * @param converter The Converter object to register
+ * @param clazz The class this Converter is able to convert to
+ */
+ public static void registerConverter( Converter converter, Class clazz) {
+ if ( ConvertUtils.customConverters == null)
+ ConvertUtils.customConverters = new FastHashMap();
+
+ ConvertUtils.customConverters.put( clazz, converter);
+ }
/**
* Convert the specified value into a String. If the specified value
@@ -284,6 +302,8 @@
return (convertFloat(value, null));
} else if (clazz == Short.class) {
return (convertShort(value, null));
+ } else if ( ConvertUtils.customConverters != null &&
+ConvertUtils.customConverters.containsKey( clazz)) {
+ return ( ((Converter) customConverters.get( clazz)).convert(
+clazz, value) );
} else {
if (value == null)
return ((String) null);
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>