You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beehive.apache.org by ek...@apache.org on 2004/07/17 18:37:40 UTC
svn commit: rev 23006 - in incubator/beehive/trunk/netui: src/util/org/apache/beehive/netui/util/type test/src/junitTests/org/apache/beehive/netui/test/util/type
Author: ekoneil
Date: Sat Jul 17 09:37:39 2004
New Revision: 23006
Added:
incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/BaseTypeConverter.java (contents, props changed)
incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/DelegatingTypeConverter.java (contents, props changed)
Modified:
incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/TypeConverter.java
incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/TypeUtils.java
incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/util/type/TypeUtilsTest.java
Log:
Add the ability to pass the Locale to the type conversion infrastructure.
This is needed to be able to convert Date objects (and anything else needing a Locle) given a particular a Locale instance.
It's implemented as a new API method on TypeUtils as:
convertToObject(String value, Class type, Locale locale)
Type converter implementations are free to ignore the locale.
Added a BaseTypeConverter so that the TypeConverter interface doesn't break and switched the TypeUtils to talk to this base class. TypeConverter implementations are called through the DelegatingTypeConverter extension of BaseTypeConverter.
Consider this a proposal for a way to handle Locales during conversion; this should just get the tests running for non-US locales in the short term.
BB: self
DRT: Controls / NetUI pass
BVT: NetUI pass
Added: incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/BaseTypeConverter.java
==============================================================================
--- (empty file)
+++ incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/BaseTypeConverter.java Sat Jul 17 09:37:39 2004
@@ -0,0 +1,51 @@
+/*
+ * B E A S Y S T E M S
+ * Copyright 2002-2004 BEA Systems, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * $Header:$
+ */
+package org.apache.beehive.netui.util.type;
+
+// java imports
+import java.util.Locale;
+
+// internal imports
+
+// external imports
+
+/**
+ * Default implementation of the {@link TypeConverter} interface.
+ */
+public class BaseTypeConverter
+ implements TypeConverter
+{
+ /**
+ * Convert the String to a type. The base implementation
+ * delegates to the {@link convertToObject(java.lang.String, java.util.Locale)}
+ * method with <code>null</code> for the locale.
+ */
+ public Object convertToObject(String value)
+ {
+ return convertToObject(value, null);
+ }
+
+ /**
+ * Convert the String to a type, optinally using the given locale.
+ */
+ public Object convertToObject(String value, Locale locale)
+ {
+ return value;
+ }
+}
\ No newline at end of file
Added: incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/DelegatingTypeConverter.java
==============================================================================
--- (empty file)
+++ incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/DelegatingTypeConverter.java Sat Jul 17 09:37:39 2004
@@ -0,0 +1,54 @@
+/*
+ * B E A S Y S T E M S
+ * Copyright 2002-2004 BEA Systems, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * $Header:$
+ */
+package org.apache.beehive.netui.util.type;
+
+// java imports
+import java.util.Locale;
+
+// internal imports
+
+// external imports
+
+/**
+ * Implementation of the {@link TypeConverter} interface that delegates
+ * to a {@link TypeConverter} implementation which doesn't extend
+ * {@link BaseTypeConverter}.
+ */
+public final class DelegatingTypeConverter
+ extends BaseTypeConverter
+{
+ private TypeConverter _converter = null;
+
+ public DelegatingTypeConverter(TypeConverter converter)
+ {
+ super();
+ assert converter != null;
+ _converter = converter;
+ }
+
+ public Object convertToObject(String value)
+ {
+ return _converter.convertToObject(value);
+ }
+
+ public Object convertToObject(String value, Locale locale)
+ {
+ return _converter.convertToObject(value);
+ }
+}
\ No newline at end of file
Modified: incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/TypeConverter.java
==============================================================================
--- incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/TypeConverter.java (original)
+++ incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/TypeConverter.java Sat Jul 17 09:37:39 2004
@@ -19,15 +19,23 @@
package org.apache.beehive.netui.util.type;
// java imports
+import java.util.Locale;
// internal imports
// external imports
/**
- *
+ * Interface used to define converters for converting Strings into various
+ * Object types.
*/
public interface TypeConverter
{
+ /**
+ * Convert a String to an Object type
+ *
+ * @param value the String value to convert
+ * @return the converted object
+ */
public Object convertToObject(String value);
}
Modified: incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/TypeUtils.java
==============================================================================
--- incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/TypeUtils.java (original)
+++ incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/TypeUtils.java Sat Jul 17 09:37:39 2004
@@ -31,6 +31,7 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
+import java.util.Locale;
import java.util.Map;
import java.util.Properties;
@@ -75,30 +76,37 @@
/**
* Convert an object from a String to the given type.
*
+ * @param value the String to convert
+ * @param type the type to which to convert the String
+ * @param locale the locale to use during conversion
* @throws TypeConverterNotFoundException if a TypeConverter for the target type can not be found.
* @return the Object result of converting the String to the type.
*/
public static final Object convertToObject(String value, Class type)
{
- TypeConverter converter = (TypeConverter)_converters.get(type);
- if(converter == null)
- {
- String msg = "Could not find a TypeConverter for converting a String to an object of type \"" +
- (type != null ? type.getName() : null) + "\"";
- TypeConverterNotFoundException tcn = new TypeConverterNotFoundException(msg);
-
- if(type == null)
- msg = Bundle.getErrorString("TypeUtils_nullType");
- else msg = Bundle.getErrorString("TypeUtils_noConverterForType", new Object[] {type.getName()});
- tcn.setLocalizedMessage(msg);
- if(logger.isInfoEnabled()) logger.info(msg);
-
- throw tcn;
- }
-
+ BaseTypeConverter converter = lookupTypeConverter(type);
+ assert converter != null;
return converter.convertToObject(value);
}
+ /**
+ * Convert an object from a String to the given type using the specified {@link java.util.Locale}.
+ *
+ * The locale is optionally used depending on the implementation of the {@link TypeConverter} that is used.
+ *
+ * @param value the String to convert
+ * @param type the type to which to convert the String
+ * @param locale the locale to use during conversion
+ * @throws TypeConverterNotFoundException if a TypeConverter for the target type can not be found.
+ * @return the Object result of converting the String to the type.
+ */
+ public static final Object convertToObject(String value, Class type, Locale locale)
+ {
+ BaseTypeConverter converter = lookupTypeConverter(type);
+ assert converter != null;
+ return converter.convertToObject(value, locale);
+ }
+
public static final byte convertToByte(String value)
{return ((Byte)convertToObject(value, byte.class)).byteValue();}
@@ -147,6 +155,35 @@
public static final Short convertToShortObject(String value)
{return (Short)convertToObject(value, Short.class);}
+ /**
+ * Internal method used to lookup a {@link BaseTypeConverter}.
+ *
+ * @param type the target conversion type
+ * @throws TypeConverterNotFoundException if a TypeConverter for the target type can not be found.
+ * @return a {@link BaseTypeConverter} to use for conversion
+ */
+ private static final BaseTypeConverter lookupTypeConverter(Class type)
+ {
+ BaseTypeConverter converter = (BaseTypeConverter)_converters.get(type);
+ if(converter == null)
+ {
+ String msg = "Could not find a TypeConverter for converting a String to an object of type \"" +
+ (type != null ? type.getName() : null) + "\"";
+ TypeConverterNotFoundException tcn = new TypeConverterNotFoundException(msg);
+
+ if(type == null)
+ msg = Bundle.getErrorString("TypeUtils_nullType");
+ else msg = Bundle.getErrorString("TypeUtils_noConverterForType", new Object[] {type.getName()});
+ tcn.setLocalizedMessage(msg);
+ if(logger.isInfoEnabled())
+ logger.info(msg);
+
+ throw tcn;
+ }
+
+ return converter;
+ }
+
private static String registeredConvertersToString()
{
if(_toString != null)
@@ -281,6 +318,12 @@
{
tcClazz = Class.forName(className);
tc = (TypeConverter)tcClazz.newInstance();
+
+ // this supports existing TypeConverter implementations
+ // but allows TypeUtils make calls against the BaseTypeConverter
+ // API, which supports Locale-based conversion
+ if(!(tc instanceof BaseTypeConverter))
+ tc = new DelegatingTypeConverter(tc);
}
catch(ClassNotFoundException cnf)
{
@@ -316,26 +359,26 @@
*/
private static void initialize()
{
- _converters.put(byte.class, new TypeConverter()
+ _converters.put(byte.class, new BaseTypeConverter()
{
- public Object convertToObject(String value)
+ public Object convertToObject(String value, Locale locale)
{
return (value == null || value.equals(EMPTY_STRING) ? new Byte((byte)0) : new Byte(value.trim()));
}
});
- _converters.put(Byte.class, new TypeConverter()
+ _converters.put(Byte.class, new BaseTypeConverter()
{
- public Object convertToObject(String value)
+ public Object convertToObject(String value, Locale locale)
{
if(value == null || value.equals(EMPTY_STRING)) return null;
else return TypeUtils.convertToObject(value, byte.class);
}
});
- _converters.put(boolean.class, new TypeConverter()
+ _converters.put(boolean.class, new BaseTypeConverter()
{
- public Object convertToObject(String value)
+ public Object convertToObject(String value, Locale locale)
{
if(value == null || value.equals(EMPTY_STRING))
return Boolean.FALSE;
@@ -346,126 +389,126 @@
else return Boolean.FALSE;
}
});
- _converters.put(Boolean.class, new TypeConverter()
+ _converters.put(Boolean.class, new BaseTypeConverter()
{
- public Object convertToObject(String value)
+ public Object convertToObject(String value, Locale locale)
{
if(value == null || value.equals(EMPTY_STRING)) return null;
else return TypeUtils.convertToObject(value, boolean.class);
}
});
- _converters.put(char.class, new TypeConverter()
+ _converters.put(char.class, new BaseTypeConverter()
{
- public Object convertToObject(String value)
+ public Object convertToObject(String value, Locale locale)
{
if(value == null || value.equals(EMPTY_STRING))
return new Character('\u0000');
else return new Character(value.charAt(0));
}
});
- _converters.put(Character.class, new TypeConverter()
+ _converters.put(Character.class, new BaseTypeConverter()
{
- public Object convertToObject(String value)
+ public Object convertToObject(String value, Locale locale)
{
if(value == null || value.equals(EMPTY_STRING)) return null;
else return TypeUtils.convertToObject(value, char.class);
}
});
- _converters.put(double.class, new TypeConverter()
+ _converters.put(double.class, new BaseTypeConverter()
{
- public Object convertToObject(String value)
+ public Object convertToObject(String value, Locale locale)
{
if(value == null || value.equals(EMPTY_STRING))
return new Double(0.0);
else return new Double(value.trim());
}
});
- _converters.put(Double.class, new TypeConverter()
+ _converters.put(Double.class, new BaseTypeConverter()
{
- public Object convertToObject(String value)
+ public Object convertToObject(String value, Locale locale)
{
if(value == null || value.equals(EMPTY_STRING)) return null;
else return TypeUtils.convertToObject(value, double.class);
}
});
- _converters.put(float.class, new TypeConverter()
+ _converters.put(float.class, new BaseTypeConverter()
{
- public Object convertToObject(String value)
+ public Object convertToObject(String value, Locale locale)
{
if(value == null || value.equals(EMPTY_STRING))
return new Float(0.0);
else return new Float(value.trim());
}
});
- _converters.put(Float.class, new TypeConverter()
+ _converters.put(Float.class, new BaseTypeConverter()
{
- public Object convertToObject(String value)
+ public Object convertToObject(String value, Locale locale)
{
if(value == null || value.equals(EMPTY_STRING)) return null;
else return TypeUtils.convertToObject(value, float.class);
}
});
- _converters.put(int.class, new TypeConverter()
+ _converters.put(int.class, new BaseTypeConverter()
{
- public Object convertToObject(String value)
+ public Object convertToObject(String value, Locale locale)
{
if(value == null || value.equals(EMPTY_STRING))
return new Integer(0);
else return new Integer(value.trim());
}
});
- _converters.put(Integer.class, new TypeConverter()
+ _converters.put(Integer.class, new BaseTypeConverter()
{
- public Object convertToObject(String value)
+ public Object convertToObject(String value, Locale locale)
{
if(value == null || value.equals(EMPTY_STRING)) return null;
else return TypeUtils.convertToObject(value, int.class);
}
});
- _converters.put(long.class, new TypeConverter()
+ _converters.put(long.class, new BaseTypeConverter()
{
- public Object convertToObject(String value)
+ public Object convertToObject(String value, Locale locale)
{
if(value == null || value.equals(EMPTY_STRING))
return new Long(0);
else return new Long(value.trim());
}
});
- _converters.put(Long.class, new TypeConverter()
+ _converters.put(Long.class, new BaseTypeConverter()
{
- public Object convertToObject(String value)
+ public Object convertToObject(String value, Locale locale)
{
if(value == null || value.equals(EMPTY_STRING)) return null;
else return TypeUtils.convertToObject(value, long.class);
}
});
- _converters.put(short.class, new TypeConverter()
+ _converters.put(short.class, new BaseTypeConverter()
{
- public Object convertToObject(String value)
+ public Object convertToObject(String value, Locale locale)
{
if(value == null || value.equals(EMPTY_STRING))
return new Short((short)0);
else return new Short(value.trim());
}
});
- _converters.put(Short.class, new TypeConverter()
+ _converters.put(Short.class, new BaseTypeConverter()
{
- public Object convertToObject(String value)
+ public Object convertToObject(String value, Locale locale)
{
if(value == null || value.equals(EMPTY_STRING)) return null;
else return TypeUtils.convertToObject(value, short.class);
}
});
- _converters.put(String.class, new TypeConverter()
+ _converters.put(String.class, new BaseTypeConverter()
{
- public Object convertToObject(String value)
+ public Object convertToObject(String value, Locale locale)
{
if(value == null)
return null;
@@ -473,9 +516,9 @@
}
});
- _converters.put(java.math.BigDecimal.class, new TypeConverter()
+ _converters.put(java.math.BigDecimal.class, new BaseTypeConverter()
{
- public Object convertToObject(String value)
+ public Object convertToObject(String value, Locale locale)
{
if(value == null || value.equals(EMPTY_STRING))
return null;
@@ -483,9 +526,9 @@
}
});
- _converters.put(java.math.BigInteger.class, new TypeConverter()
+ _converters.put(java.math.BigInteger.class, new BaseTypeConverter()
{
- public Object convertToObject(String value)
+ public Object convertToObject(String value, Locale locale)
{
if(value == null || value.equals(EMPTY_STRING))
return null;
@@ -493,9 +536,9 @@
}
});
- _converters.put(byte[].class, new TypeConverter()
+ _converters.put(byte[].class, new BaseTypeConverter()
{
- public Object convertToObject(String value)
+ public Object convertToObject(String value, Locale locale)
{
if(value == null || value.equals(EMPTY_STRING))
return null;
@@ -503,9 +546,9 @@
}
});
- _converters.put(Byte[].class, new TypeConverter()
+ _converters.put(Byte[].class, new BaseTypeConverter()
{
- public Object convertToObject(String value)
+ public Object convertToObject(String value, Locale locale)
{
if(value == null || value.equals(EMPTY_STRING))
return null;
@@ -523,15 +566,18 @@
}
});
- _converters.put(Date.class, new TypeConverter()
+ _converters.put(Date.class, new BaseTypeConverter()
{
- public Object convertToObject(String value)
+ public Object convertToObject(String value, Locale locale)
{
if(value == null || value.equals(EMPTY_STRING)) return null;
try
{
- DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
+ if(locale == null)
+ locale = Locale.getDefault();
+
+ DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
return df.parse(value);
}
catch(java.text.ParseException pe)
@@ -549,9 +595,9 @@
});
// http://java.sun.com/j2se/1.4.1/docs/api/java/sql/Date.html
- _converters.put(java.sql.Date.class, new TypeConverter()
+ _converters.put(java.sql.Date.class, new BaseTypeConverter()
{
- public Object convertToObject(String value)
+ public Object convertToObject(String value, Locale locale)
{
if(value == null || value.equals(EMPTY_STRING)) return null;
@@ -573,9 +619,9 @@
});
// http://java.sun.com/j2se/1.4.1/docs/api/java/sql/Timestamp.html
- _converters.put(java.sql.Timestamp.class, new TypeConverter()
+ _converters.put(java.sql.Timestamp.class, new BaseTypeConverter()
{
- public Object convertToObject(String value)
+ public Object convertToObject(String value, Locale locale)
{
if(value == null || value.equals(EMPTY_STRING)) return null;
@@ -597,9 +643,9 @@
});
// http://java.sun.com/j2se/1.4.1/docs/api/java/sql/Time.html
- _converters.put(java.sql.Time.class, new TypeConverter()
+ _converters.put(java.sql.Time.class, new BaseTypeConverter()
{
- public Object convertToObject(String value)
+ public Object convertToObject(String value, Locale locale)
{
if(value == null || value.equals(EMPTY_STRING)) return null;
Modified: incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/util/type/TypeUtilsTest.java
==============================================================================
--- incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/util/type/TypeUtilsTest.java (original)
+++ incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/util/type/TypeUtilsTest.java Sat Jul 17 09:37:39 2004
@@ -24,6 +24,7 @@
// import java.sql.Date; // there are tests for this below, but the name conflicts with java.util.Date
import java.sql.Timestamp;
import java.util.Date;
+import java.util.Locale;
// internal imports
import org.apache.beehive.netui.util.logging.Logger;
@@ -140,7 +141,7 @@
Object result = null;
try
{
- result = TypeUtils.convertToObject(value, type);
+ result = TypeUtils.convertToObject(value, type, Locale.US);
if(!valid)
throw new TestFailedException("The test case [" + i + "] failed because an illegal value \"" +
Re: svn commit: rev 23006 - in incubator/beehive/trunk/netui: src/util/org/apache/beehive/netui/util/type
test/src/junitTests/org/apache/beehive/netui/test/util/type
Posted by Eddie O'Neil <ek...@bea.com>.
Didn't mark the code review earlier:
Review: Daryl
ekoneil@apache.org wrote:
>Author: ekoneil
>Date: Sat Jul 17 09:37:39 2004
>New Revision: 23006
>
>Added:
> incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/BaseTypeConverter.java (contents, props changed)
> incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/DelegatingTypeConverter.java (contents, props changed)
>Modified:
> incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/TypeConverter.java
> incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/TypeUtils.java
> incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/util/type/TypeUtilsTest.java
>Log:
>Add the ability to pass the Locale to the type conversion infrastructure.
>
>This is needed to be able to convert Date objects (and anything else needing a Locle) given a particular a Locale instance.
>
>It's implemented as a new API method on TypeUtils as:
>
> convertToObject(String value, Class type, Locale locale)
>
>Type converter implementations are free to ignore the locale.
>
>Added a BaseTypeConverter so that the TypeConverter interface doesn't break and switched the TypeUtils to talk to this base class. TypeConverter implementations are called through the DelegatingTypeConverter extension of BaseTypeConverter.
>
>Consider this a proposal for a way to handle Locales during conversion; this should just get the tests running for non-US locales in the short term.
>
>BB: self
>DRT: Controls / NetUI pass
>BVT: NetUI pass
>
>
>
>Added: incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/BaseTypeConverter.java
>==============================================================================
>--- (empty file)
>+++ incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/BaseTypeConverter.java Sat Jul 17 09:37:39 2004
>@@ -0,0 +1,51 @@
>+/*
>+ * B E A S Y S T E M S
>+ * Copyright 2002-2004 BEA Systems, Inc.
>+ *
>+ * Licensed under the Apache License, Version 2.0 (the "License");
>+ * you may not use this file except in compliance with the License.
>+ * You may obtain a copy of the License at
>+ *
>+ * http://www.apache.org/licenses/LICENSE-2.0
>+ *
>+ * Unless required by applicable law or agreed to in writing, software
>+ * distributed under the License is distributed on an "AS IS" BASIS,
>+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>+ * See the License for the specific language governing permissions and
>+ * limitations under the License.
>+ *
>+ * $Header:$
>+ */
>+package org.apache.beehive.netui.util.type;
>+
>+// java imports
>+import java.util.Locale;
>+
>+// internal imports
>+
>+// external imports
>+
>+/**
>+ * Default implementation of the {@link TypeConverter} interface.
>+ */
>+public class BaseTypeConverter
>+ implements TypeConverter
>+{
>+ /**
>+ * Convert the String to a type. The base implementation
>+ * delegates to the {@link convertToObject(java.lang.String, java.util.Locale)}
>+ * method with <code>null</code> for the locale.
>+ */
>+ public Object convertToObject(String value)
>+ {
>+ return convertToObject(value, null);
>+ }
>+
>+ /**
>+ * Convert the String to a type, optinally using the given locale.
>+ */
>+ public Object convertToObject(String value, Locale locale)
>+ {
>+ return value;
>+ }
>+}
>\ No newline at end of file
>
>Added: incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/DelegatingTypeConverter.java
>==============================================================================
>--- (empty file)
>+++ incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/DelegatingTypeConverter.java Sat Jul 17 09:37:39 2004
>@@ -0,0 +1,54 @@
>+/*
>+ * B E A S Y S T E M S
>+ * Copyright 2002-2004 BEA Systems, Inc.
>+ *
>+ * Licensed under the Apache License, Version 2.0 (the "License");
>+ * you may not use this file except in compliance with the License.
>+ * You may obtain a copy of the License at
>+ *
>+ * http://www.apache.org/licenses/LICENSE-2.0
>+ *
>+ * Unless required by applicable law or agreed to in writing, software
>+ * distributed under the License is distributed on an "AS IS" BASIS,
>+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>+ * See the License for the specific language governing permissions and
>+ * limitations under the License.
>+ *
>+ * $Header:$
>+ */
>+package org.apache.beehive.netui.util.type;
>+
>+// java imports
>+import java.util.Locale;
>+
>+// internal imports
>+
>+// external imports
>+
>+/**
>+ * Implementation of the {@link TypeConverter} interface that delegates
>+ * to a {@link TypeConverter} implementation which doesn't extend
>+ * {@link BaseTypeConverter}.
>+ */
>+public final class DelegatingTypeConverter
>+ extends BaseTypeConverter
>+{
>+ private TypeConverter _converter = null;
>+
>+ public DelegatingTypeConverter(TypeConverter converter)
>+ {
>+ super();
>+ assert converter != null;
>+ _converter = converter;
>+ }
>+
>+ public Object convertToObject(String value)
>+ {
>+ return _converter.convertToObject(value);
>+ }
>+
>+ public Object convertToObject(String value, Locale locale)
>+ {
>+ return _converter.convertToObject(value);
>+ }
>+}
>\ No newline at end of file
>
>Modified: incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/TypeConverter.java
>==============================================================================
>--- incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/TypeConverter.java (original)
>+++ incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/TypeConverter.java Sat Jul 17 09:37:39 2004
>@@ -19,15 +19,23 @@
> package org.apache.beehive.netui.util.type;
>
> // java imports
>+import java.util.Locale;
>
> // internal imports
>
> // external imports
>
> /**
>- *
>+ * Interface used to define converters for converting Strings into various
>+ * Object types.
> */
> public interface TypeConverter
> {
>+ /**
>+ * Convert a String to an Object type
>+ *
>+ * @param value the String value to convert
>+ * @return the converted object
>+ */
> public Object convertToObject(String value);
> }
>
>Modified: incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/TypeUtils.java
>==============================================================================
>--- incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/TypeUtils.java (original)
>+++ incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/TypeUtils.java Sat Jul 17 09:37:39 2004
>@@ -31,6 +31,7 @@
> import java.util.HashMap;
> import java.util.Iterator;
> import java.util.LinkedHashMap;
>+import java.util.Locale;
> import java.util.Map;
> import java.util.Properties;
>
>@@ -75,30 +76,37 @@
> /**
> * Convert an object from a String to the given type.
> *
>+ * @param value the String to convert
>+ * @param type the type to which to convert the String
>+ * @param locale the locale to use during conversion
> * @throws TypeConverterNotFoundException if a TypeConverter for the target type can not be found.
> * @return the Object result of converting the String to the type.
> */
> public static final Object convertToObject(String value, Class type)
> {
>- TypeConverter converter = (TypeConverter)_converters.get(type);
>- if(converter == null)
>- {
>- String msg = "Could not find a TypeConverter for converting a String to an object of type \"" +
>- (type != null ? type.getName() : null) + "\"";
>- TypeConverterNotFoundException tcn = new TypeConverterNotFoundException(msg);
>-
>- if(type == null)
>- msg = Bundle.getErrorString("TypeUtils_nullType");
>- else msg = Bundle.getErrorString("TypeUtils_noConverterForType", new Object[] {type.getName()});
>- tcn.setLocalizedMessage(msg);
>- if(logger.isInfoEnabled()) logger.info(msg);
>-
>- throw tcn;
>- }
>-
>+ BaseTypeConverter converter = lookupTypeConverter(type);
>+ assert converter != null;
> return converter.convertToObject(value);
> }
>
>+ /**
>+ * Convert an object from a String to the given type using the specified {@link java.util.Locale}.
>+ *
>+ * The locale is optionally used depending on the implementation of the {@link TypeConverter} that is used.
>+ *
>+ * @param value the String to convert
>+ * @param type the type to which to convert the String
>+ * @param locale the locale to use during conversion
>+ * @throws TypeConverterNotFoundException if a TypeConverter for the target type can not be found.
>+ * @return the Object result of converting the String to the type.
>+ */
>+ public static final Object convertToObject(String value, Class type, Locale locale)
>+ {
>+ BaseTypeConverter converter = lookupTypeConverter(type);
>+ assert converter != null;
>+ return converter.convertToObject(value, locale);
>+ }
>+
> public static final byte convertToByte(String value)
> {return ((Byte)convertToObject(value, byte.class)).byteValue();}
>
>@@ -147,6 +155,35 @@
> public static final Short convertToShortObject(String value)
> {return (Short)convertToObject(value, Short.class);}
>
>+ /**
>+ * Internal method used to lookup a {@link BaseTypeConverter}.
>+ *
>+ * @param type the target conversion type
>+ * @throws TypeConverterNotFoundException if a TypeConverter for the target type can not be found.
>+ * @return a {@link BaseTypeConverter} to use for conversion
>+ */
>+ private static final BaseTypeConverter lookupTypeConverter(Class type)
>+ {
>+ BaseTypeConverter converter = (BaseTypeConverter)_converters.get(type);
>+ if(converter == null)
>+ {
>+ String msg = "Could not find a TypeConverter for converting a String to an object of type \"" +
>+ (type != null ? type.getName() : null) + "\"";
>+ TypeConverterNotFoundException tcn = new TypeConverterNotFoundException(msg);
>+
>+ if(type == null)
>+ msg = Bundle.getErrorString("TypeUtils_nullType");
>+ else msg = Bundle.getErrorString("TypeUtils_noConverterForType", new Object[] {type.getName()});
>+ tcn.setLocalizedMessage(msg);
>+ if(logger.isInfoEnabled())
>+ logger.info(msg);
>+
>+ throw tcn;
>+ }
>+
>+ return converter;
>+ }
>+
> private static String registeredConvertersToString()
> {
> if(_toString != null)
>@@ -281,6 +318,12 @@
> {
> tcClazz = Class.forName(className);
> tc = (TypeConverter)tcClazz.newInstance();
>+
>+ // this supports existing TypeConverter implementations
>+ // but allows TypeUtils make calls against the BaseTypeConverter
>+ // API, which supports Locale-based conversion
>+ if(!(tc instanceof BaseTypeConverter))
>+ tc = new DelegatingTypeConverter(tc);
> }
> catch(ClassNotFoundException cnf)
> {
>@@ -316,26 +359,26 @@
> */
> private static void initialize()
> {
>- _converters.put(byte.class, new TypeConverter()
>+ _converters.put(byte.class, new BaseTypeConverter()
> {
>- public Object convertToObject(String value)
>+ public Object convertToObject(String value, Locale locale)
> {
> return (value == null || value.equals(EMPTY_STRING) ? new Byte((byte)0) : new Byte(value.trim()));
> }
>
> });
>- _converters.put(Byte.class, new TypeConverter()
>+ _converters.put(Byte.class, new BaseTypeConverter()
> {
>- public Object convertToObject(String value)
>+ public Object convertToObject(String value, Locale locale)
> {
> if(value == null || value.equals(EMPTY_STRING)) return null;
> else return TypeUtils.convertToObject(value, byte.class);
> }
> });
>
>- _converters.put(boolean.class, new TypeConverter()
>+ _converters.put(boolean.class, new BaseTypeConverter()
> {
>- public Object convertToObject(String value)
>+ public Object convertToObject(String value, Locale locale)
> {
> if(value == null || value.equals(EMPTY_STRING))
> return Boolean.FALSE;
>@@ -346,126 +389,126 @@
> else return Boolean.FALSE;
> }
> });
>- _converters.put(Boolean.class, new TypeConverter()
>+ _converters.put(Boolean.class, new BaseTypeConverter()
> {
>- public Object convertToObject(String value)
>+ public Object convertToObject(String value, Locale locale)
> {
> if(value == null || value.equals(EMPTY_STRING)) return null;
> else return TypeUtils.convertToObject(value, boolean.class);
> }
> });
>
>- _converters.put(char.class, new TypeConverter()
>+ _converters.put(char.class, new BaseTypeConverter()
> {
>- public Object convertToObject(String value)
>+ public Object convertToObject(String value, Locale locale)
> {
> if(value == null || value.equals(EMPTY_STRING))
> return new Character('\u0000');
> else return new Character(value.charAt(0));
> }
> });
>- _converters.put(Character.class, new TypeConverter()
>+ _converters.put(Character.class, new BaseTypeConverter()
> {
>- public Object convertToObject(String value)
>+ public Object convertToObject(String value, Locale locale)
> {
> if(value == null || value.equals(EMPTY_STRING)) return null;
> else return TypeUtils.convertToObject(value, char.class);
> }
> });
>
>- _converters.put(double.class, new TypeConverter()
>+ _converters.put(double.class, new BaseTypeConverter()
> {
>- public Object convertToObject(String value)
>+ public Object convertToObject(String value, Locale locale)
> {
> if(value == null || value.equals(EMPTY_STRING))
> return new Double(0.0);
> else return new Double(value.trim());
> }
> });
>- _converters.put(Double.class, new TypeConverter()
>+ _converters.put(Double.class, new BaseTypeConverter()
> {
>- public Object convertToObject(String value)
>+ public Object convertToObject(String value, Locale locale)
> {
> if(value == null || value.equals(EMPTY_STRING)) return null;
> else return TypeUtils.convertToObject(value, double.class);
> }
> });
>
>- _converters.put(float.class, new TypeConverter()
>+ _converters.put(float.class, new BaseTypeConverter()
> {
>- public Object convertToObject(String value)
>+ public Object convertToObject(String value, Locale locale)
> {
> if(value == null || value.equals(EMPTY_STRING))
> return new Float(0.0);
> else return new Float(value.trim());
> }
> });
>- _converters.put(Float.class, new TypeConverter()
>+ _converters.put(Float.class, new BaseTypeConverter()
> {
>- public Object convertToObject(String value)
>+ public Object convertToObject(String value, Locale locale)
> {
> if(value == null || value.equals(EMPTY_STRING)) return null;
> else return TypeUtils.convertToObject(value, float.class);
> }
> });
>
>- _converters.put(int.class, new TypeConverter()
>+ _converters.put(int.class, new BaseTypeConverter()
> {
>- public Object convertToObject(String value)
>+ public Object convertToObject(String value, Locale locale)
> {
> if(value == null || value.equals(EMPTY_STRING))
> return new Integer(0);
> else return new Integer(value.trim());
> }
> });
>- _converters.put(Integer.class, new TypeConverter()
>+ _converters.put(Integer.class, new BaseTypeConverter()
> {
>- public Object convertToObject(String value)
>+ public Object convertToObject(String value, Locale locale)
> {
> if(value == null || value.equals(EMPTY_STRING)) return null;
> else return TypeUtils.convertToObject(value, int.class);
> }
> });
>
>- _converters.put(long.class, new TypeConverter()
>+ _converters.put(long.class, new BaseTypeConverter()
> {
>- public Object convertToObject(String value)
>+ public Object convertToObject(String value, Locale locale)
> {
> if(value == null || value.equals(EMPTY_STRING))
> return new Long(0);
> else return new Long(value.trim());
> }
> });
>- _converters.put(Long.class, new TypeConverter()
>+ _converters.put(Long.class, new BaseTypeConverter()
> {
>- public Object convertToObject(String value)
>+ public Object convertToObject(String value, Locale locale)
> {
> if(value == null || value.equals(EMPTY_STRING)) return null;
> else return TypeUtils.convertToObject(value, long.class);
> }
> });
>
>- _converters.put(short.class, new TypeConverter()
>+ _converters.put(short.class, new BaseTypeConverter()
> {
>- public Object convertToObject(String value)
>+ public Object convertToObject(String value, Locale locale)
> {
> if(value == null || value.equals(EMPTY_STRING))
> return new Short((short)0);
> else return new Short(value.trim());
> }
> });
>- _converters.put(Short.class, new TypeConverter()
>+ _converters.put(Short.class, new BaseTypeConverter()
> {
>- public Object convertToObject(String value)
>+ public Object convertToObject(String value, Locale locale)
> {
> if(value == null || value.equals(EMPTY_STRING)) return null;
> else return TypeUtils.convertToObject(value, short.class);
> }
> });
>
>- _converters.put(String.class, new TypeConverter()
>+ _converters.put(String.class, new BaseTypeConverter()
> {
>- public Object convertToObject(String value)
>+ public Object convertToObject(String value, Locale locale)
> {
> if(value == null)
> return null;
>@@ -473,9 +516,9 @@
> }
> });
>
>- _converters.put(java.math.BigDecimal.class, new TypeConverter()
>+ _converters.put(java.math.BigDecimal.class, new BaseTypeConverter()
> {
>- public Object convertToObject(String value)
>+ public Object convertToObject(String value, Locale locale)
> {
> if(value == null || value.equals(EMPTY_STRING))
> return null;
>@@ -483,9 +526,9 @@
> }
> });
>
>- _converters.put(java.math.BigInteger.class, new TypeConverter()
>+ _converters.put(java.math.BigInteger.class, new BaseTypeConverter()
> {
>- public Object convertToObject(String value)
>+ public Object convertToObject(String value, Locale locale)
> {
> if(value == null || value.equals(EMPTY_STRING))
> return null;
>@@ -493,9 +536,9 @@
> }
> });
>
>- _converters.put(byte[].class, new TypeConverter()
>+ _converters.put(byte[].class, new BaseTypeConverter()
> {
>- public Object convertToObject(String value)
>+ public Object convertToObject(String value, Locale locale)
> {
> if(value == null || value.equals(EMPTY_STRING))
> return null;
>@@ -503,9 +546,9 @@
> }
> });
>
>- _converters.put(Byte[].class, new TypeConverter()
>+ _converters.put(Byte[].class, new BaseTypeConverter()
> {
>- public Object convertToObject(String value)
>+ public Object convertToObject(String value, Locale locale)
> {
> if(value == null || value.equals(EMPTY_STRING))
> return null;
>@@ -523,15 +566,18 @@
> }
> });
>
>- _converters.put(Date.class, new TypeConverter()
>+ _converters.put(Date.class, new BaseTypeConverter()
> {
>- public Object convertToObject(String value)
>+ public Object convertToObject(String value, Locale locale)
> {
> if(value == null || value.equals(EMPTY_STRING)) return null;
>
> try
> {
>- DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
>+ if(locale == null)
>+ locale = Locale.getDefault();
>+
>+ DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
> return df.parse(value);
> }
> catch(java.text.ParseException pe)
>@@ -549,9 +595,9 @@
> });
>
> // http://java.sun.com/j2se/1.4.1/docs/api/java/sql/Date.html
>- _converters.put(java.sql.Date.class, new TypeConverter()
>+ _converters.put(java.sql.Date.class, new BaseTypeConverter()
> {
>- public Object convertToObject(String value)
>+ public Object convertToObject(String value, Locale locale)
> {
> if(value == null || value.equals(EMPTY_STRING)) return null;
>
>@@ -573,9 +619,9 @@
> });
>
> // http://java.sun.com/j2se/1.4.1/docs/api/java/sql/Timestamp.html
>- _converters.put(java.sql.Timestamp.class, new TypeConverter()
>+ _converters.put(java.sql.Timestamp.class, new BaseTypeConverter()
> {
>- public Object convertToObject(String value)
>+ public Object convertToObject(String value, Locale locale)
> {
> if(value == null || value.equals(EMPTY_STRING)) return null;
>
>@@ -597,9 +643,9 @@
> });
>
> // http://java.sun.com/j2se/1.4.1/docs/api/java/sql/Time.html
>- _converters.put(java.sql.Time.class, new TypeConverter()
>+ _converters.put(java.sql.Time.class, new BaseTypeConverter()
> {
>- public Object convertToObject(String value)
>+ public Object convertToObject(String value, Locale locale)
> {
> if(value == null || value.equals(EMPTY_STRING)) return null;
>
>
>Modified: incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/util/type/TypeUtilsTest.java
>==============================================================================
>--- incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/util/type/TypeUtilsTest.java (original)
>+++ incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/util/type/TypeUtilsTest.java Sat Jul 17 09:37:39 2004
>@@ -24,6 +24,7 @@
> // import java.sql.Date; // there are tests for this below, but the name conflicts with java.util.Date
> import java.sql.Timestamp;
> import java.util.Date;
>+import java.util.Locale;
>
> // internal imports
> import org.apache.beehive.netui.util.logging.Logger;
>@@ -140,7 +141,7 @@
> Object result = null;
> try
> {
>- result = TypeUtils.convertToObject(value, type);
>+ result = TypeUtils.convertToObject(value, type, Locale.US);
>
> if(!valid)
> throw new TestFailedException("The test case [" + i + "] failed because an illegal value \"" +
>
>
>