Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
2113 posts
|
Author: adrianc
Date: Tue Nov 10 17:18:34 2009 New Revision: 834552 URL: http://svn.apache.org/viewvc?rev=834552&view=rev Log: Modified ObjectType.java to use only the new conversion framework. This change can be reverted if any major issues come up. I will monitor the dev mailing list and fix any issues that are reported. Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ObjectType.java Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ObjectType.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ObjectType.java?rev=834552&r1=834551&r2=834552&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ObjectType.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ObjectType.java Tue Nov 10 17:18:34 2009 @@ -490,8 +490,6 @@ type = type.substring(0, genericsStart); } - // ----- Try out new conversion code ----- - Class<?> sourceClass = obj.getClass(); Class<?> targetClass = null; try { @@ -500,611 +498,56 @@ Debug.logError(e, module); return obj; } - + if (sourceClass.equals(targetClass)) { + return obj; + } Converter<Object,Object> converter = null; try { converter = (Converter<Object, Object>) Converters.getConverter(sourceClass, targetClass); } catch (ClassNotFoundException e) {} if (converter != null) { + LocalizedConverter<Object, Object> localizedConverter = null; try { - LocalizedConverter<Object, Object> localizedConverter = (LocalizedConverter) converter; + localizedConverter = (LocalizedConverter) converter; + } catch (Exception e) {} + if (localizedConverter != null) { if (timeZone == null) { timeZone = TimeZone.getDefault(); } if (locale == null) { locale = Locale.getDefault(); } - return localizedConverter.convert(obj, locale, timeZone, format); - } catch (Exception e) {} - try { - return converter.convert(obj); - } catch (ConversionException e) {} - } - - // --------------------------------------- - - if (obj.getClass().getName().equals(type)) { - return obj; - } - if ("PlainString".equals(type)) { - return obj.toString(); - } - if ("Object".equals(type) || "java.lang.Object".equals(type)) { - return obj; - } - - if (timeZone == null) { - timeZone = TimeZone.getDefault(); - } - - if (locale == null) { - locale = Locale.getDefault(); - } - - String fromType = null; - - if ((type.equals("List") || type.equals("java.util.List")) && obj.getClass().isArray()) { - List<Object> newObj = FastList.newInstance(); - int len = Array.getLength(obj); - for (int i = 0; i < len; i++) { - newObj.add(Array.get(obj, i)); - } - return newObj; - } else if (obj instanceof java.lang.String) { - fromType = "String"; - String str = (String) obj; - if ("String".equals(type) || "java.lang.String".equals(type)) { - return obj; - } - if (str.length() == 0) { - return null; - } - - if ("Boolean".equals(type) || "java.lang.Boolean".equals(type)) { - str = StringUtil.removeSpaces(str); - return str.equalsIgnoreCase("TRUE") ? Boolean.TRUE : Boolean.FALSE; - } else if ("Locale".equals(type) || "java.util.Locale".equals(type)) { - Locale loc = UtilMisc.parseLocale(str); - if (loc != null) { - return loc; - } else { - throw new GeneralException("Could not convert " + str + " to " + type + ": "); - } - } else if ("TimeZone".equals(type) || "java.util.TimeZone".equals(type)) { - TimeZone tz = UtilDateTime.toTimeZone(str); - if (tz != null) { - return tz; - } else { - throw new GeneralException("Could not convert " + str + " to " + type + ": "); - } - } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) { - str = StringUtil.removeSpaces(str); - try { - NumberFormat nf = NumberFormat.getNumberInstance(locale); - Number tempNum = nf.parse(str); - return new BigDecimal(tempNum.toString()); - } catch (ParseException e) { - throw new GeneralException("Could not convert " + str + " to " + type + ": ", e); - } - } else if ("Double".equals(type) || "java.lang.Double".equals(type)) { - str = StringUtil.removeSpaces(str); - try { - NumberFormat nf = NumberFormat.getNumberInstance(locale); - Number tempNum = nf.parse(str); - - return Double.valueOf(tempNum.doubleValue()); - } catch (ParseException e) { - throw new GeneralException("Could not convert " + str + " to " + type + ": ", e); - } - } else if ("Float".equals(type) || "java.lang.Float".equals(type)) { - str = StringUtil.removeSpaces(str); - try { - NumberFormat nf = NumberFormat.getNumberInstance(locale); - Number tempNum = nf.parse(str); - - return Float.valueOf(tempNum.floatValue()); - } catch (ParseException e) { - throw new GeneralException("Could not convert " + str + " to " + type + ": ", e); - } - } else if ("Long".equals(type) || "java.lang.Long".equals(type)) { - str = StringUtil.removeSpaces(str); - try { - NumberFormat nf = NumberFormat.getNumberInstance(locale); - nf.setMaximumFractionDigits(0); - Number tempNum = nf.parse(str); - - return Long.valueOf(tempNum.longValue()); - } catch (ParseException e) { - throw new GeneralException("Could not convert " + str + " to " + type + ": ", e); - } - } else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) { - str = StringUtil.removeSpaces(str); try { - NumberFormat nf = NumberFormat.getNumberInstance(locale); - nf.setMaximumFractionDigits(0); - Number tempNum = nf.parse(str); - - return Integer.valueOf(tempNum.intValue()); - } catch (ParseException e) { - throw new GeneralException("Could not convert " + str + " to " + type + ": ", e); - } - } else if ("Date".equals(type) || "java.sql.Date".equals(type)) { - DateFormat df = null; - if (format == null || format.length() == 0) { - df = UtilDateTime.toDateFormat(UtilDateTime.DATE_FORMAT, timeZone, locale); - } else { - df = UtilDateTime.toDateFormat(format, timeZone, locale); - } - try { - Date fieldDate = df.parse(str); - return new java.sql.Date(fieldDate.getTime()); - } catch (ParseException e1) { - throw new GeneralException("Could not convert " + str + " to " + type + ": ", e1); - } - } else if ("Time".equals(type) || "java.sql.Time".equals(type)) { - DateFormat df = null; - if (format == null || format.length() == 0) { - df = UtilDateTime.toTimeFormat(UtilDateTime.TIME_FORMAT, timeZone, locale); - } else { - df = UtilDateTime.toTimeFormat(format, timeZone, locale); - } - try { - Date fieldDate = df.parse(str); - return new java.sql.Time(fieldDate.getTime()); - } catch (ParseException e1) { - throw new GeneralException("Could not convert " + str + " to " + type + ": ", e1); - } - } else if ("Timestamp".equals(type) || "java.sql.Timestamp".equals(type)) { - DateFormat df = null; - if (format == null || format.length() == 0) { - df = UtilDateTime.toDateTimeFormat(UtilDateTime.DATE_TIME_FORMAT, timeZone, locale); - // if time is missing add zeros - if (str.length() > 0 && !str.contains(":")) { - str = str + " 00:00:00.00"; - } - // hack to mimic Timestamp.valueOf() method - if (str.length() > 0 && !str.contains(".")) { - str = str + ".0"; - } else { - // DateFormat has a funny way of parsing milliseconds: - // 00:00:00.2 parses to 00:00:00.002 - // so we'll add zeros to the end to get 00:00:00.200 - String[] timeSplit = str.split("[.]"); - if (timeSplit.length > 1 && timeSplit[1].length() < 3) { - str = str + "000".substring(timeSplit[1].length()); - } - } - } else { - df = UtilDateTime.toDateTimeFormat(format, timeZone, locale); - } - try { - Date fieldDate = df.parse(str); - return new java.sql.Timestamp(fieldDate.getTime()); - } catch (ParseException e1) { - throw new GeneralException("Could not convert " + str + " to " + type + ": ", e1); - } - } else if ("List".equals(type) || "java.util.List".equals(type)) { - if (str.startsWith("[") && str.endsWith("]")) { - return StringUtil.toList(str); - } else { - List<String> tempList = FastList.newInstance(); - tempList.add(str); - return tempList; + return localizedConverter.convert(obj, locale, timeZone, format); + } catch (ConversionException e) { + Debug.logError(e, module); } - } else if ("Set".equals(type) || "java.util.Set".equals(type)) { - if (str.startsWith("[") && str.endsWith("]")) { - return StringUtil.toSet(str); - } else { - Set<String> tempSet = FastSet.newInstance(); - tempSet.add(str); - return tempSet; - } - } else if (("Map".equals(type) || "java.util.Map".equals(type)) && - (str.startsWith("{") && str.endsWith("}"))) { - return StringUtil.toMap(str); - } else if ("TimeDuration".equals(type) || "org.ofbiz.base.util.TimeDuration".equals(type)) { - if (!str.contains(":")) { - // Encoded duration - try { - NumberFormat nf = NumberFormat.getNumberInstance(locale); - nf.setMaximumFractionDigits(0); - Number number = nf.parse(str); - return TimeDuration.fromNumber(number); - } catch (ParseException e) { - throw new GeneralException("Could not convert " + str + " to " + type + ": ", e); - } - } - return TimeDuration.parseDuration(str); - } else { - throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); - } - } else if (obj instanceof Double) { - fromType = "Double"; - Double dbl = (Double) obj; - - if ("String".equals(type) || "java.lang.String".equals(type)) { - NumberFormat nf = NumberFormat.getNumberInstance(locale); - return nf.format(dbl.doubleValue()); - } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) { - return new BigDecimal(dbl.doubleValue()); - } else if ("Double".equals(type) || "java.lang.Double".equals(type)) { - return obj; - } else if ("Float".equals(type) || "java.lang.Float".equals(type)) { - return Float.valueOf(dbl.floatValue()); - } else if ("Long".equals(type) || "java.lang.Long".equals(type)) { - return Long.valueOf(Math.round(dbl.doubleValue())); - } else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) { - return Integer.valueOf((int) Math.round(dbl.doubleValue())); - } else if ("List".equals(type) || "java.util.List".equals(type)) { - List<Double> tempList = FastList.newInstance(); - tempList.add(dbl); - return tempList; - } else if ("Set".equals(type) || "java.util.Set".equals(type)) { - Set<Double> tempSet = FastSet.newInstance(); - tempSet.add(dbl); - return tempSet; - } else if ("TimeDuration".equals(type) || "org.ofbiz.base.util.TimeDuration".equals(type)) { - return TimeDuration.fromNumber(dbl); - } else { - throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); - } - } else if (obj instanceof Float) { - fromType = "Float"; - Float flt = (Float) obj; - - if ("String".equals(type)) { - NumberFormat nf = NumberFormat.getNumberInstance(locale); - return nf.format(flt.doubleValue()); - } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) { - return new BigDecimal(flt.doubleValue()); - } else if ("Double".equals(type)) { - return Double.valueOf(flt.doubleValue()); - } else if ("Float".equals(type)) { - return obj; - } else if ("Long".equals(type)) { - return Long.valueOf(Math.round(flt.doubleValue())); - } else if ("Integer".equals(type)) { - return Integer.valueOf((int) Math.round(flt.doubleValue())); - } else if ("List".equals(type) || "java.util.List".equals(type)) { - List<Float> tempList = FastList.newInstance(); - tempList.add(flt); - return tempList; - } else if ("Set".equals(type) || "java.util.Set".equals(type)) { - Set<Float> tempSet = FastSet.newInstance(); - tempSet.add(flt); - return tempSet; - } else if ("TimeDuration".equals(type) || "org.ofbiz.base.util.TimeDuration".equals(type)) { - return TimeDuration.fromNumber(flt); - } else { - throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); - } - } else if (obj instanceof Long) { - fromType = "Long"; - Long lng = (Long) obj; - - if ("String".equals(type) || "java.lang.String".equals(type)) { - NumberFormat nf = NumberFormat.getNumberInstance(locale); - return nf.format(lng.longValue()); - } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) { - return BigDecimal.valueOf(lng.longValue()); - } else if ("Double".equals(type) || "java.lang.Double".equals(type)) { - return Double.valueOf(lng.doubleValue()); - } else if ("Float".equals(type) || "java.lang.Float".equals(type)) { - return Float.valueOf(lng.floatValue()); - } else if ("Long".equals(type) || "java.lang.Long".equals(type)) { - return obj; - } else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) { - return Integer.valueOf(lng.intValue()); - } else if ("List".equals(type) || "java.util.List".equals(type)) { - List<Long> tempList = FastList.newInstance(); - tempList.add(lng); - return tempList; - } else if ("Set".equals(type) || "java.util.Set".equals(type)) { - Set<Long> tempSet = FastSet.newInstance(); - tempSet.add(lng); - return tempSet; - } else if ("Date".equals(type) || "java.util.Date".equals(type)) { - return new Date(lng.longValue()); - } else if ("Timestamp".equals(type) || "java.sql.Timestamp".equals(type)) { - return new java.sql.Timestamp(lng.longValue()); - } else if ("TimeDuration".equals(type) || "org.ofbiz.base.util.TimeDuration".equals(type)) { - return TimeDuration.fromNumber(lng); - } else { - throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); - } - } else if (obj instanceof Integer) { - fromType = "Integer"; - Integer intgr = (Integer) obj; - if ("String".equals(type) || "java.lang.String".equals(type)) { - NumberFormat nf = NumberFormat.getNumberInstance(locale); - return nf.format(intgr.longValue()); - } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) { - return BigDecimal.valueOf(intgr.longValue()); - } else if ("Double".equals(type) || "java.lang.Double".equals(type)) { - return Double.valueOf(intgr.doubleValue()); - } else if ("Float".equals(type) || "java.lang.Float".equals(type)) { - return Float.valueOf(intgr.floatValue()); - } else if ("Long".equals(type) || "java.lang.Long".equals(type)) { - return Long.valueOf(intgr.longValue()); - } else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) { - return obj; - } else if ("List".equals(type) || "java.util.List".equals(type)) { - List<Integer> tempList = FastList.newInstance(); - tempList.add(intgr); - return tempList; - } else if ("Set".equals(type) || "java.util.Set".equals(type)) { - Set<Integer> tempSet = FastSet.newInstance(); - tempSet.add(intgr); - return tempSet; - } else if ("TimeDuration".equals(type) || "org.ofbiz.base.util.TimeDuration".equals(type)) { - return TimeDuration.fromNumber(intgr); - } else { - throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); - } - } else if (obj instanceof BigDecimal) { - fromType = "BigDecimal"; - BigDecimal bigDec = (BigDecimal) obj; - if ("String".equals(type) || "java.lang.String".equals(type)) { - NumberFormat nf = NumberFormat.getNumberInstance(locale); - return nf.format(bigDec.doubleValue()); - } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) { - return obj; - } else if ("Double".equals(type) || "java.lang.Double".equals(type)) { - return Double.valueOf(bigDec.doubleValue()); - } else if ("Float".equals(type) || "java.lang.Float".equals(type)) { - return Float.valueOf(bigDec.floatValue()); - } else if ("Long".equals(type) || "java.lang.Long".equals(type)) { - return Long.valueOf(bigDec.longValue()); - } else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) { - return Integer.valueOf(bigDec.intValue()); - } else if ("List".equals(type) || "java.util.List".equals(type)) { - List<BigDecimal> tempList = FastList.newInstance(); - tempList.add(bigDec); - return tempList; - } else if ("Set".equals(type) || "java.util.Set".equals(type)) { - Set<BigDecimal> tempSet = FastSet.newInstance(); - tempSet.add(bigDec); - return tempSet; - } else if ("TimeDuration".equals(type) || "org.ofbiz.base.util.TimeDuration".equals(type)) { - return TimeDuration.fromNumber(bigDec); - } else { - throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); } - } else if (obj instanceof java.sql.Date) { - fromType = "Date"; - java.sql.Date dte = (java.sql.Date) obj; - if ("String".equals(type) || "java.lang.String".equals(type)) { - DateFormat df = null; - if (format == null || format.length() == 0) { - df = UtilDateTime.toDateFormat(UtilDateTime.DATE_FORMAT, timeZone, locale); - } else { - df = UtilDateTime.toDateFormat(format, timeZone, locale); - } - return df.format(new java.util.Date(dte.getTime())); - } else if ("Date".equals(type) || "java.sql.Date".equals(type)) { - return obj; - } else if ("Time".equals(type) || "java.sql.Time".equals(type)) { - throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); - } else if ("Timestamp".equals(type) || "java.sql.Timestamp".equals(type)) { - return new java.sql.Timestamp(dte.getTime()); - } else if ("List".equals(type) || "java.util.List".equals(type)) { - List<java.sql.Date> tempList = FastList.newInstance(); - tempList.add(dte); - return tempList; - } else if ("Set".equals(type) || "java.util.Set".equals(type)) { - Set<java.sql.Date> tempSet = FastSet.newInstance(); - tempSet.add(dte); - return tempSet; - } else if ("Long".equals(type) || "java.lang.Long".equals(type)) { - return Long.valueOf(dte.getTime()); - } else { - throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); - } - } else if (obj instanceof java.sql.Time) { - fromType = "Time"; - java.sql.Time tme = (java.sql.Time) obj; - - if ("String".equals(type) || "java.lang.String".equals(type)) { - DateFormat df = null; - if (format == null || format.length() == 0) { - df = UtilDateTime.toTimeFormat(UtilDateTime.TIME_FORMAT, timeZone, locale); - } else { - df = UtilDateTime.toTimeFormat(format, timeZone, locale); - } - return df.format(new java.util.Date(tme.getTime())); - } else if ("Date".equals(type) || "java.sql.Date".equals(type)) { - throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); - } else if ("Time".equals(type) || "java.sql.Time".equals(type)) { - return obj; - } else if ("Timestamp".equals(type) || "java.sql.Timestamp".equals(type)) { - return new java.sql.Timestamp(tme.getTime()); - } else if ("List".equals(type) || "java.util.List".equals(type)) { - List<java.sql.Time> tempList = FastList.newInstance(); - tempList.add(tme); - return tempList; - } else if ("Set".equals(type) || "java.util.Set".equals(type)) { - Set<java.sql.Time> tempSet = FastSet.newInstance(); - tempSet.add(tme); - return tempSet; - } else { - throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); - } - } else if (obj instanceof java.sql.Timestamp) { - fromType = "Timestamp"; - java.sql.Timestamp tme = (java.sql.Timestamp) obj; - - if ("String".equals(type) || "java.lang.String".equals(type)) { - DateFormat df = null; - if (format == null || format.length() == 0) { - df = UtilDateTime.toDateTimeFormat(UtilDateTime.DATE_TIME_FORMAT, timeZone, locale); - } else { - df = UtilDateTime.toDateTimeFormat(format, timeZone, locale); - } - return df.format(new java.util.Date(tme.getTime())); - } else if ("Date".equals(type) || "java.sql.Date".equals(type)) { - return new java.sql.Date(tme.getTime()); - } else if ("Time".equals(type) || "java.sql.Time".equals(type)) { - return new java.sql.Time(tme.getTime()); - } else if ("Timestamp".equals(type) || "java.sql.Timestamp".equals(type)) { - return obj; - } else if ("List".equals(type) || "java.util.List".equals(type)) { - List<java.sql.Timestamp> tempList = FastList.newInstance(); - tempList.add(tme); - return tempList; - } else if ("Set".equals(type) || "java.util.Set".equals(type)) { - Set<java.sql.Timestamp> tempSet = FastSet.newInstance(); - tempSet.add(tme); - return tempSet; - } else if ("Long".equals(type) || "java.lang.Long".equals(type)) { - return Long.valueOf(tme.getTime()); - } else { - throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); - } - } else if (obj instanceof java.lang.Boolean) { - fromType = "Boolean"; - Boolean bol = (Boolean) obj; - if ("Boolean".equals(type) || "java.lang.Boolean".equals(type)) { - return bol; - } else if ("String".equals(type) || "java.lang.String".equals(type)) { - return bol.toString(); - } else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) { - if (bol.booleanValue()) { - return Integer.valueOf(1); - } else { - return Integer.valueOf(0); - } - } else if ("List".equals(type) || "java.util.List".equals(type)) { - List<Boolean> tempList = FastList.newInstance(); - tempList.add(bol); - return tempList; - } else if ("Set".equals(type) || "java.util.Set".equals(type)) { - Set<Boolean> tempSet = FastSet.newInstance(); - tempSet.add(bol); - return tempSet; - } else { - throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); - } - } else if (obj instanceof java.util.Locale) { - fromType = "Locale"; - Locale loc = (Locale) obj; - if ("Locale".equals(type) || "java.util.Locale".equals(type)) { - return loc; - } else if ("String".equals(type) || "java.lang.String".equals(type)) { - return loc.toString(); - } else { - throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); - } - } else if (obj instanceof java.util.TimeZone) { - fromType = "TimeZone"; - TimeZone tz = (TimeZone) obj; - if ("TimeZone".equals(type) || "java.util.TimeZone".equals(type)) { - return tz; - } else if ("String".equals(type) || "java.lang.String".equals(type)) { - return tz.getID(); - } else { - throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); - } - } else if (obj.getClass().getName().equals("org.ofbiz.entity.GenericValue")) { - fromType = "GenericValue"; - if ("GenericValue".equals(type) || "org.ofbiz.entity.GenericValue".equals(type)) { - return obj; - } else if ("Map".equals(type) || "java.util.Map".equals(type)) { - return obj; - } else if ("String".equals(type) || "java.lang.String".equals(type)) { - return obj.toString(); - } else if ("List".equals(type) || "java.util.List".equals(type)) { - List<Object> tempList = FastList.newInstance(); - tempList.add(obj); - return tempList; - } else if ("Set".equals(type) || "java.util.Set".equals(type)) { - Set<Object> tempSet = FastSet.newInstance(); - tempSet.add(obj); - return tempSet; - } else { - throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); - } - } else if (obj instanceof java.util.Map) { - fromType = "Map"; - Map<?,?> map = (Map<?,?>) obj; - if ("Map".equals(type) || "java.util.Map".equals(type)) { - return map; - } else if ("String".equals(type) || "java.lang.String".equals(type)) { - return map.toString(); - } else if ("List".equals(type) || "java.util.List".equals(type)) { - List<Map<?,?>> tempList = FastList.newInstance(); - tempList.add(map); - return tempList; - } else if ("Set".equals(type) || "java.util.Set".equals(type)) { - Set<Map<?,?>> tempSet = FastSet.newInstance(); - tempSet.add(map); - return tempSet; - } else { - throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); - } - } else if (obj instanceof java.util.List) { - fromType = "List"; - List<?> list = (List<?>) obj; - if ("List".equals(type) || "java.util.List".equals(type)) { - return list; - } else if ("String".equals(type) || "java.lang.String".equals(type)) { - return list.toString(); - } else { - throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); - } - } else if (obj instanceof java.nio.Buffer) { - fromType = "Buffer"; - Buffer buffer = (Buffer) obj; - if ("java.nio.ByteBuffer".equals(type)) { - return buffer; - } else { - throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); + try { + return converter.convert(obj); + } catch (ConversionException e) { + Debug.logError(e, module); } - } else if (obj instanceof Node) { + } + if (obj instanceof Node) { Node node = (Node) obj; String nodeValue = node.getTextContent(); - if ("String".equals(type) || "java.lang.String".equals(type)) { + if (targetClass.equals(String.class)) { return nodeValue; } else { return simpleTypeConvert(nodeValue, type, format, timeZone, locale, noTypeFail); } - } else if (obj instanceof TimeDuration) { - TimeDuration duration = (TimeDuration) obj; - if ("String".equals(type) || "java.lang.String".equals(type)) { - return obj.toString(); - } - long durationLong = TimeDuration.toLong(duration); - if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) { - return BigDecimal.valueOf(durationLong); - } else if ("Double".equals(type) || "java.lang.Double".equals(type)) { - return Double.valueOf(durationLong); - } else if ("Float".equals(type) || "java.lang.Float".equals(type)) { - return Float.valueOf(durationLong); - } else if ("Long".equals(type) || "java.lang.Long".equals(type)) { - return Long.valueOf(durationLong); - } else if ("List".equals(type) || "java.util.List".equals(type)) { - List<TimeDuration> tempList = FastList.newInstance(); - tempList.add(duration); - return tempList; - } else if ("Set".equals(type) || "java.util.Set".equals(type)) { - Set<TimeDuration> tempSet = FastSet.newInstance(); - tempSet.add(duration); - return tempSet; - } else { - throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); - } + } + // we can pretty much always do a conversion to a String, so do that here + if (targetClass.equals(String.class)) { + Debug.logWarning("No special conversion available for " + obj.getClass().getName() + " to String, returning object.toString().", module); + return obj.toString(); + } + if (noTypeFail) { + throw new GeneralException("Conversion from " + obj.getClass().getName() + " to " + type + " not currently supported"); } else { - // we can pretty much always do a conversion to a String, so do that here - if ("String".equals(type) || "java.lang.String".equals(type)) { - Debug.logWarning("No special conversion available for " + obj.getClass().getName() + " to String, returning object.toString().", module); - return obj.toString(); - } - - if (noTypeFail) { - throw new GeneralException("Conversion from " + obj.getClass().getName() + " to " + type + " not currently supported"); - } else { - if (Debug.infoOn()) Debug.logInfo("No type conversion available for " + obj.getClass().getName() + " to " + type + ", returning original object.", module); - return obj; - } + if (Debug.infoOn()) Debug.logInfo("No type conversion available for " + obj.getClass().getName() + " to " + targetClass.getName() + ", returning original object.", module); + return obj; } } |
Free forum by Nabble | Edit this page |