Source Code Cross Referenced for Convert.java in  » Web-Framework » rife-1.6.1 » com » uwyn » rife » tools » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » rife 1.6.1 » com.uwyn.rife.tools 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003:         * Distributed under the terms of either:
004:         * - the common development and distribution license (CDDL), v1.0; or
005:         * - the GNU Lesser General Public License, v2.1 or later
006:         * $Id: Convert.java 3691 2007-03-13 22:32:59Z gbevin $
007:         */
008:        package com.uwyn.rife.tools;
009:
010:        import com.uwyn.rife.config.RifeConfig;
011:        import com.uwyn.rife.tools.exceptions.ConversionException;
012:        import java.text.ParseException;
013:        import java.util.Calendar;
014:        import java.util.Date;
015:
016:        public class Convert {
017:            public static Object toType(Object value, Class target)
018:                    throws ConversionException {
019:                if (null == target)
020:                    return null;
021:
022:                if (target.isPrimitive()) {
023:                    if (target == boolean.class)
024:                        return toBoolean(value);
025:                    if (target == char.class)
026:                        return toChar(value);
027:                    if (target == byte.class)
028:                        return toByte(value);
029:                    if (target == short.class)
030:                        return toShort(value);
031:                    if (target == int.class)
032:                        return toInt(value);
033:                    if (target == long.class)
034:                        return toLong(value);
035:                    if (target == float.class)
036:                        return toFloat(value);
037:                    if (target == double.class)
038:                        return toDouble(value);
039:                }
040:
041:                if (null == value)
042:                    return null;
043:
044:                if (target == Boolean.class)
045:                    return toBoolean(value);
046:                if (target == Character.class)
047:                    return toChar(value);
048:                if (target == Byte.class)
049:                    return toByte(value);
050:                if (target == Short.class)
051:                    return toShort(value);
052:                if (target == Integer.class)
053:                    return toInt(value);
054:                if (target == Long.class)
055:                    return toLong(value);
056:                if (target == Float.class)
057:                    return toFloat(value);
058:                if (target == Double.class)
059:                    return toDouble(value);
060:                if (target == Date.class)
061:                    return toDate(value);
062:                if (String.class.isAssignableFrom(target))
063:                    return toString(value);
064:                if (target.isAssignableFrom(value.getClass()))
065:                    return value;
066:
067:                throw new ConversionException(value, target, null);
068:            }
069:
070:            public static String toString(Object value) {
071:                if (null == value) {
072:                    return null;
073:                }
074:
075:                return value.toString();
076:            }
077:
078:            public static char toChar(Object value, char defaultValue) {
079:                try {
080:                    return toChar(value);
081:                } catch (ConversionException e) {
082:                    return defaultValue;
083:                }
084:            }
085:
086:            public static char toChar(Object value) throws ConversionException {
087:                if (null == value) {
088:                    throw new ConversionException(value, char.class, null);
089:                }
090:
091:                if (value instanceof  Character) {
092:                    return ((Character) value).charValue();
093:                }
094:
095:                if (value instanceof  Number) {
096:                    int int_value = ((Number) value).intValue();
097:                    return (char) int_value;
098:                }
099:
100:                if (value instanceof  String && 1 == ((String) value).length()) {
101:                    return ((String) value).charAt(0);
102:                }
103:
104:                throw new ConversionException(value, char.class, null);
105:            }
106:
107:            public static boolean toBoolean(Object value, boolean defaultValue) {
108:                try {
109:                    return toBoolean(value);
110:                } catch (ConversionException e) {
111:                    return defaultValue;
112:                }
113:            }
114:
115:            public static boolean toBoolean(Object value)
116:                    throws ConversionException {
117:                if (null == value) {
118:                    throw new ConversionException(value, boolean.class, null);
119:                }
120:
121:                if (value instanceof  Boolean) {
122:                    return ((Boolean) value).booleanValue();
123:                }
124:
125:                if (value instanceof  Number) {
126:                    byte byte_value = ((Number) value).byteValue();
127:                    if (0 == byte_value) {
128:                        return false;
129:                    }
130:
131:                    if (1 == byte_value) {
132:                        return true;
133:                    }
134:
135:                    throw new ConversionException(value, boolean.class, null);
136:                }
137:
138:                if (value instanceof  String) {
139:                    return StringUtils.convertToBoolean((String) value);
140:                }
141:
142:                throw new ConversionException(value, boolean.class, null);
143:            }
144:
145:            public static byte toByte(Object value, byte defaultValue) {
146:                try {
147:                    return toByte(value);
148:                } catch (ConversionException e) {
149:                    return defaultValue;
150:                }
151:            }
152:
153:            public static byte toByte(Object value) throws ConversionException {
154:                if (null == value) {
155:                    throw new ConversionException(value, byte.class, null);
156:                }
157:
158:                if (value instanceof  Number) {
159:                    return ((Number) value).byteValue();
160:                }
161:
162:                if (value instanceof  String) {
163:                    try {
164:                        return Byte.parseByte((String) value);
165:                    } catch (NumberFormatException e) {
166:                        throw new ConversionException(value, byte.class, e);
167:                    }
168:                }
169:
170:                throw new ConversionException(value, byte.class, null);
171:            }
172:
173:            public static short toShort(Object value, short defaultValue) {
174:                try {
175:                    return toShort(value);
176:                } catch (ConversionException e) {
177:                    return defaultValue;
178:                }
179:            }
180:
181:            public static short toShort(Object value)
182:                    throws ConversionException {
183:                if (null == value) {
184:                    throw new ConversionException(value, short.class, null);
185:                }
186:
187:                if (value instanceof  Number) {
188:                    return ((Number) value).shortValue();
189:                }
190:
191:                if (value instanceof  String) {
192:                    try {
193:                        return Short.parseShort((String) value);
194:                    } catch (NumberFormatException e) {
195:                        throw new ConversionException(value, short.class, e);
196:                    }
197:                }
198:
199:                throw new ConversionException(value, short.class, null);
200:            }
201:
202:            public static int toInt(Object value, int defaultValue) {
203:                try {
204:                    return toInt(value);
205:                } catch (ConversionException e) {
206:                    return defaultValue;
207:                }
208:            }
209:
210:            public static int toInt(Object value) throws ConversionException {
211:                if (null == value) {
212:                    throw new ConversionException(value, int.class, null);
213:                }
214:
215:                if (value instanceof  Number) {
216:                    return ((Number) value).intValue();
217:                }
218:
219:                if (value instanceof  String) {
220:                    try {
221:                        return Integer.parseInt((String) value);
222:                    } catch (NumberFormatException e) {
223:                        throw new ConversionException(value, int.class, e);
224:                    }
225:                }
226:
227:                throw new ConversionException(value, int.class, null);
228:            }
229:
230:            public static long toLong(Object value, long defaultValue) {
231:                try {
232:                    return toLong(value);
233:                } catch (ConversionException e) {
234:                    return defaultValue;
235:                }
236:            }
237:
238:            public static long toLong(Object value) throws ConversionException {
239:                if (null == value) {
240:                    throw new ConversionException(value, long.class, null);
241:                }
242:
243:                if (value instanceof  Number) {
244:                    return ((Number) value).longValue();
245:                }
246:
247:                if (value instanceof  String) {
248:                    try {
249:                        return Long.parseLong((String) value);
250:                    } catch (NumberFormatException e) {
251:                        throw new ConversionException(value, long.class, e);
252:                    }
253:                }
254:
255:                throw new ConversionException(value, long.class, null);
256:            }
257:
258:            public static float toFloat(Object value, float defaultValue) {
259:                try {
260:                    return toFloat(value);
261:                } catch (ConversionException e) {
262:                    return defaultValue;
263:                }
264:            }
265:
266:            public static float toFloat(Object value)
267:                    throws ConversionException {
268:                if (null == value) {
269:                    throw new ConversionException(value, float.class, null);
270:                }
271:
272:                if (value instanceof  Number) {
273:                    return ((Number) value).floatValue();
274:                }
275:
276:                if (value instanceof  String) {
277:                    try {
278:                        return Float.parseFloat((String) value);
279:                    } catch (NumberFormatException e) {
280:                        throw new ConversionException(value, float.class, e);
281:                    }
282:                }
283:
284:                throw new ConversionException(value, float.class, null);
285:            }
286:
287:            public static double toDouble(Object value, double defaultValue) {
288:                try {
289:                    return toDouble(value);
290:                } catch (ConversionException e) {
291:                    return defaultValue;
292:                }
293:            }
294:
295:            public static double toDouble(Object value)
296:                    throws ConversionException {
297:                if (null == value) {
298:                    throw new ConversionException(value, double.class, null);
299:                }
300:
301:                if (value instanceof  Number) {
302:                    return ((Number) value).doubleValue();
303:                }
304:
305:                if (value instanceof  String) {
306:                    try {
307:                        return Double.parseDouble((String) value);
308:                    } catch (NumberFormatException e) {
309:                        throw new ConversionException(value, double.class, e);
310:                    }
311:                }
312:
313:                throw new ConversionException(value, double.class, null);
314:            }
315:
316:            public static Date toDate(Object value) throws ConversionException {
317:                if (null == value) {
318:                    return null;
319:                }
320:
321:                if (value instanceof  Date) {
322:                    return (Date) value;
323:                }
324:
325:                if (value instanceof  Number) {
326:                    return new Date(((Number) value).longValue());
327:                }
328:
329:                if (value instanceof  String) {
330:                    try {
331:                        return new Date(Long.parseLong((String) value));
332:                    } catch (NumberFormatException e) {
333:                        try {
334:                            return BeanUtils.getConcisePreciseDateFormat()
335:                                    .parse((String) value);
336:                        } catch (ParseException e2) {
337:                            try {
338:                                return RifeConfig.Tools
339:                                        .getDefaultInputDateFormat().parse(
340:                                                (String) value);
341:                            } catch (ParseException e3) {
342:                                throw new ConversionException(value,
343:                                        Date.class, e2);
344:                            }
345:                        }
346:                    }
347:                }
348:
349:                if (value instanceof  Calendar) {
350:                    return ((Calendar) value).getTime();
351:                }
352:
353:                throw new ConversionException(value, Date.class, null);
354:            }
355:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.