1 /*
2 * PROJECT : DAR Runtime and Tools
3 * COPYRIGHT : Copyright (C) 1999-2004 tim.stephenson@enableit.org
4 * LICENSE : GNU LESSER GENERAL PUBLIC LICENSE
5 * Version 2.1, February 1999
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 package org.enableit.db;
22
23 import java.math.BigDecimal;
24
25 import org.apache.log4j.Category;
26
27
28 /***
29 * General container for all type conversions.
30 * @deprecated Use org.apache.commons.beanutils.ConvertUtils.register()
31 */
32 public class ConvertUtils extends org.apache.commons.beanutils.ConvertUtils {
33 /*
34 * Properties
35 */
36
37 /***
38 * The Log4J <code>Category</code> doing the logging.
39 */
40 protected static Category logger = Category.getInstance(org.enableit.db.ConvertUtils.class);
41
42 /***
43 * CVS info about this class and its current version
44 */
45 public static final String ABOUT = "$Id: ConvertUtils.java,v 1.6 2004/03/19 19:10:33 tim Exp $";
46
47 /*
48 * Constructors
49 */
50
51 /***
52 * Default Constructor
53 */
54 public ConvertUtils() {
55 }
56
57 /*
58 * Methods
59 */
60 public static Object convert(Object value, Class type)
61 throws IllegalArgumentException {
62 logger.info("METHOD_ENTRY: convert ");
63 logger.info("... with params type=" + type + ", value=" + value);
64
65 Object returnVal = null;
66
67 if (value instanceof String) {
68 logger.debug("using ancestor converters");
69 returnVal = org.apache.commons.beanutils.ConvertUtils.convert((String) value,
70 type);
71
72 // this is turning into a farce, will sort it out when have the new commons-beanutils
73 if (returnVal instanceof String) {
74 logger.debug("ancestor converters did not work!!");
75
76 if (type.getName().endsWith("java.util.Date")) {
77 returnVal = DateConverter.convert((String) value);
78 } else {
79 logger.debug("ancestor converters resulted in "
80 + type.getName());
81 }
82 } else {
83 logger.debug("ancestor converters resulted in "
84 + returnVal.getClass().getName());
85 }
86 } else if (value instanceof java.math.BigDecimal) {
87 returnVal = convertBigDecimal((BigDecimal) value, type);
88 } else {
89 // try and continue anyway, it may work, as in the case of java.util.Date and java.sql.Date instance
90 returnVal = value;
91
92 // throw new IllegalArgumentException("Unconvertable type: " + value.getClass().getName()) ;
93 }
94
95 logger.info("METHOD_EXIT: convert");
96 logger.info("... with params returnVal=" + returnVal + ", type="
97 + returnVal.getClass().getName());
98
99 return returnVal;
100 }
101
102 private static Object convertBigDecimal(BigDecimal value, Class type) {
103 logger.info("METHOD_ENTRY: convert ");
104 logger.info("... with params type=" + type + ", value=" + value);
105
106 Object returnVal = null;
107
108 if (type == String.class) {
109 returnVal = value.toString();
110 } else if (type == Long.class) {
111 returnVal = new Long(value.longValue());
112 } else if (type == Integer.class) {
113 returnVal = new Integer(value.intValue());
114 } else if (type == Double.class) {
115 returnVal = new Double(value.doubleValue());
116 }
117
118 logger.info("METHOD_EXIT: convert");
119 logger.info("... with params returnVal=" + returnVal + ", type="
120 + returnVal.getClass().getName());
121
122 return returnVal;
123 }
124 }
This page was automatically generated by Maven