More converter improvements. Better handling of strings supplied to formatting classes.

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@835197 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Adrian Crum
2009-11-12 02:31:59 +00:00
parent 172a135a56
commit 19f8bf0228
3 changed files with 42 additions and 25 deletions
@@ -283,7 +283,8 @@ public class DateTimeConverters implements ConverterLoader {
}
public Calendar convert(String obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
if (obj.length() == 0) {
String trimStr = obj.trim();
if (trimStr.length() == 0) {
return null;
}
DateFormat df = null;
@@ -293,7 +294,7 @@ public class DateTimeConverters implements ConverterLoader {
df = UtilDateTime.toDateTimeFormat(formatString, timeZone, locale);
}
try {
java.util.Date date = df.parse(obj);
java.util.Date date = df.parse(trimStr);
return UtilDateTime.toCalendar(date, timeZone, locale);
} catch (ParseException e) {
throw new ConversionException(e);
@@ -309,7 +310,8 @@ public class DateTimeConverters implements ConverterLoader {
}
public java.util.Date convert(String obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
if (obj.length() == 0) {
String trimStr = obj.trim();
if (trimStr.length() == 0) {
return null;
}
DateFormat df = null;
@@ -319,7 +321,7 @@ public class DateTimeConverters implements ConverterLoader {
df = UtilDateTime.toDateTimeFormat(formatString, timeZone, locale);
}
try {
return df.parse(obj);
return df.parse(trimStr);
} catch (ParseException e) {
throw new ConversionException(e);
}
@@ -346,7 +348,8 @@ public class DateTimeConverters implements ConverterLoader {
}
public java.sql.Date convert(String obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
if (obj.length() == 0) {
String trimStr = obj.trim();
if (trimStr.length() == 0) {
return null;
}
DateFormat df = null;
@@ -356,7 +359,7 @@ public class DateTimeConverters implements ConverterLoader {
df = UtilDateTime.toDateFormat(formatString, timeZone, locale);
}
try {
return new java.sql.Date(df.parse(obj).getTime());
return new java.sql.Date(df.parse(trimStr).getTime());
} catch (ParseException e) {
throw new ConversionException(e);
}
@@ -371,7 +374,8 @@ public class DateTimeConverters implements ConverterLoader {
}
public java.sql.Time convert(String obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
if (obj.length() == 0) {
String trimStr = obj.trim();
if (trimStr.length() == 0) {
return null;
}
DateFormat df = null;
@@ -381,7 +385,7 @@ public class DateTimeConverters implements ConverterLoader {
df = UtilDateTime.toTimeFormat(formatString, timeZone, locale);
}
try {
return new java.sql.Time(df.parse(obj).getTime());
return new java.sql.Time(df.parse(trimStr).getTime());
} catch (ParseException e) {
throw new ConversionException(e);
}
@@ -396,7 +400,8 @@ public class DateTimeConverters implements ConverterLoader {
}
public java.sql.Timestamp convert(String obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
if (obj.length() == 0) {
String trimStr = obj.trim();
if (trimStr.length() == 0) {
return null;
}
DateFormat df = null;
@@ -406,7 +411,7 @@ public class DateTimeConverters implements ConverterLoader {
df = UtilDateTime.toDateTimeFormat(formatString, timeZone, locale);
}
try {
return new java.sql.Timestamp(df.parse(obj).getTime());
return new java.sql.Timestamp(df.parse(trimStr).getTime());
} catch (ParseException e) {
throw new ConversionException(e);
}
@@ -26,6 +26,8 @@ import java.util.Locale;
import java.util.Set;
import java.util.TimeZone;
import org.ofbiz.base.util.StringUtil;
import javolution.util.FastList;
import javolution.util.FastSet;
@@ -520,7 +522,11 @@ public class NumberConverters implements ConverterLoader {
}
public BigDecimal convert(String obj, Locale locale, TimeZone timeZone) throws ConversionException {
return BigDecimal.valueOf(this.fromString(obj, locale).doubleValue());
String trimStr = StringUtil.removeSpaces(obj);
if (trimStr.length() == 0) {
return null;
}
return BigDecimal.valueOf(this.fromString(trimStr, locale).doubleValue());
}
}
@@ -532,7 +538,11 @@ public class NumberConverters implements ConverterLoader {
}
public Double convert(String obj, Locale locale, TimeZone timeZone) throws ConversionException {
return this.fromString(obj, locale).doubleValue();
String trimStr = StringUtil.removeSpaces(obj);
if (trimStr.length() == 0) {
return null;
}
return this.fromString(trimStr, locale).doubleValue();
}
}
@@ -544,7 +554,11 @@ public class NumberConverters implements ConverterLoader {
}
public Float convert(String obj, Locale locale, TimeZone timeZone) throws ConversionException {
return this.fromString(obj, locale).floatValue();
String trimStr = StringUtil.removeSpaces(obj);
if (trimStr.length() == 0) {
return null;
}
return this.fromString(trimStr, locale).floatValue();
}
}
@@ -556,7 +570,11 @@ public class NumberConverters implements ConverterLoader {
}
public Integer convert(String obj, Locale locale, TimeZone timeZone) throws ConversionException {
return this.fromString(obj, locale).intValue();
String trimStr = StringUtil.removeSpaces(obj);
if (trimStr.length() == 0) {
return null;
}
return this.fromString(trimStr, locale).intValue();
}
}
@@ -568,7 +586,11 @@ public class NumberConverters implements ConverterLoader {
}
public Long convert(String obj, Locale locale, TimeZone timeZone) throws ConversionException {
return this.fromString(obj, locale).longValue();
String trimStr = StringUtil.removeSpaces(obj);
if (trimStr.length() == 0) {
return null;
}
return this.fromString(trimStr, locale).longValue();
}
}
@@ -18,25 +18,15 @@
*******************************************************************************/
package org.ofbiz.base.util;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.nio.Buffer;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import javolution.util.FastList;
import javolution.util.FastMap;
import javolution.util.FastSet;
import org.ofbiz.base.conversion.ConversionException;
import org.ofbiz.base.conversion.Converter;