* if the type cannot be represented as number.
*/
public Number convertToNumber(final Type type1, final Object value)
throws TypeConversionException
{
final LocalizationContext localizationContext = context.getLocalizationContext();
if (value == null)
{
// there's no point in digging deeper - there *is* no value ..
throw new TypeConversionException();
}
if (type1.isFlagSet(Type.NUMERIC_TYPE) || type1.isFlagSet(Type.ANY_TYPE))
{
if (type1.isFlagSet(Type.DATETIME_TYPE)
|| type1.isFlagSet(Type.TIME_TYPE) || type1.isFlagSet(Type.DATE_TYPE)
|| type1.isFlagSet(Type.ANY_TYPE))
{
if (value instanceof Date)
{
final Number serial = DateUtil.toSerialDate((Date) value, localizationContext);
// System.out.println(serial);
final Number ret = DateUtil.normalizeDate(serial, type1);
// System.out.println(ret);
return ret;
}
}
if (value instanceof Number)
{
return (Number) value;
}
}
if (type1.isFlagSet(Type.LOGICAL_TYPE) || type1.isFlagSet(Type.ANY_TYPE))
{
if (value instanceof Boolean)
{
if (Boolean.TRUE.equals(value))
{
return new Integer(1);
}
else
{
return new Integer(0);
}
}
}
if (type1.isFlagSet(Type.TEXT_TYPE) || type1.isFlagSet(Type.ANY_TYPE))
{
final String val = value.toString();
// first, try to parse the value as a big-decimal.
try
{
return new BigDecimal(val);
}
catch (NumberFormatException e)
{
// ignore ..
}
// then checking for datetimes
final Iterator datetimeIterator = localizationContext.getDateFormats(DateTimeType.DATETIME_TYPE).iterator();
while (datetimeIterator.hasNext())
{
final DateFormat df = (DateFormat) datetimeIterator.next();
try
{
final Date date = df.parse(val);
final Number serial = DateUtil.toSerialDate(date, localizationContext);
// return DateUtil.normalizeDate(serial, DateTimeType.TYPE);
return serial;
}
catch (ParseException e)
{
// ignore as well ..
}
}
// then checking for datetimes
final Iterator dateIterator = localizationContext.getDateFormats(DateTimeType.DATE_TYPE).iterator();
while (dateIterator.hasNext())
{
final DateFormat df = (DateFormat) dateIterator.next();
try
{
final Date date = df.parse(val);
final Number serial = DateUtil.toSerialDate(date, localizationContext);
// return DateUtil.normalizeDate(serial, DateType.TYPE);
return serial;
}
catch (ParseException e)
{
// ignore as well ..
}
}
// then checking for datetimes
final Iterator timeIterator = localizationContext
.getDateFormats(DateTimeType.TIME_TYPE).iterator();
while (timeIterator.hasNext())
{
final DateFormat df = (DateFormat) timeIterator.next();
try