Source Code Cross Referenced for Converter.java in  » Web-Services-AXIS2 » adb » org » apache » axis2 » databinding » utils » 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 Services AXIS2 » adb » org.apache.axis2.databinding.utils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one
003:         * or more contributor license agreements. See the NOTICE file
004:         * distributed with this work for additional information
005:         * regarding copyright ownership. The ASF licenses this file
006:         * to you under the Apache License, Version 2.0 (the
007:         * "License"); you may not use this file except in compliance
008:         * with the License. You may obtain a copy of the License at
009:         *
010:         * http://www.apache.org/licenses/LICENSE-2.0
011:         *
012:         * Unless required by applicable law or agreed to in writing,
013:         * software distributed under the License is distributed on an
014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015:         * KIND, either express or implied. See the License for the
016:         * specific language governing permissions and limitations
017:         * under the License.
018:         */
019:
020:        package org.apache.axis2.databinding.utils;
021:
022:        import java.lang.reflect.Array;
023:        import java.util.ArrayList;
024:        import java.util.Calendar;
025:        import java.util.Collection;
026:        import java.util.Date;
027:        import java.util.HashMap;
028:        import java.util.HashSet;
029:        import java.util.Hashtable;
030:        import java.util.Iterator;
031:        import java.util.Set;
032:
033:        /** Converter */
034:        public class Converter {
035:            public static Object convert(Object arg, Class destClass) {
036:                if (destClass == null) {
037:                    return arg;
038:                }
039:
040:                Class argHeldType = null;
041:                if (arg != null) {
042:                    argHeldType = getHolderValueType(arg.getClass());
043:                }
044:
045:                if (arg != null && argHeldType == null
046:                        && destClass.isAssignableFrom(arg.getClass())) {
047:                    return arg;
048:                }
049:
050:                // See if a previously converted value is stored in the argument.
051:                Object destValue = null;
052:
053:                // Get the destination held type or the argument held type if they exist
054:                Class destHeldType = getHolderValueType(destClass);
055:
056:                // Convert between Calendar and Date
057:                if (arg instanceof  Calendar && destClass == Date.class) {
058:                    return ((Calendar) arg).getTime();
059:                }
060:                if (arg instanceof  Date && destClass == Calendar.class) {
061:                    Calendar calendar = Calendar.getInstance();
062:                    calendar.setTime((Date) arg);
063:                    return calendar;
064:                }
065:
066:                // Convert between Calendar and java.sql.Date
067:                if (arg instanceof  Calendar && destClass == java.sql.Date.class) {
068:                    return new java.sql.Date(((Calendar) arg).getTime()
069:                            .getTime());
070:                }
071:
072:                // Convert between HashMap and Hashtable
073:                if (arg instanceof  HashMap && destClass == Hashtable.class) {
074:                    return new Hashtable((HashMap) arg);
075:                }
076:
077:                // If the destination is an array and the source
078:                // is a suitable component, return an array with
079:                // the single item.
080:                if (arg != null
081:                        && destClass.isArray()
082:                        && !destClass.getComponentType().equals(Object.class)
083:                        && destClass.getComponentType().isAssignableFrom(
084:                                arg.getClass())) {
085:                    Object array = Array.newInstance(destClass
086:                            .getComponentType(), 1);
087:                    Array.set(array, 0, arg);
088:                    return array;
089:                }
090:
091:                // Return if no conversion is available
092:                if (!(arg instanceof  Collection || (arg != null && arg
093:                        .getClass().isArray()))
094:                        && ((destHeldType == null && argHeldType == null) || (destHeldType != null && argHeldType != null))) {
095:                    return arg;
096:                }
097:
098:                // Flow to here indicates that neither arg or destClass is a Holder
099:
100:                if (arg == null) {
101:                    return arg;
102:                }
103:
104:                // The arg may be an array or List
105:                int length = 0;
106:                if (arg.getClass().isArray()) {
107:                    length = Array.getLength(arg);
108:                } else {
109:                    length = ((Collection) arg).size();
110:                }
111:                if (destClass.isArray()) {
112:                    if (destClass.getComponentType().isPrimitive()) {
113:
114:                        Object array = Array.newInstance(destClass
115:                                .getComponentType(), length);
116:                        // Assign array elements
117:                        if (arg.getClass().isArray()) {
118:                            for (int i = 0; i < length; i++) {
119:                                Array.set(array, i, Array.get(arg, i));
120:                            }
121:                        } else {
122:                            int idx = 0;
123:                            for (Iterator i = ((Collection) arg).iterator(); i
124:                                    .hasNext();) {
125:                                Array.set(array, idx++, i.next());
126:                            }
127:                        }
128:                        destValue = array;
129:
130:                    } else {
131:                        Object[] array;
132:                        try {
133:                            array = (Object[]) Array.newInstance(destClass
134:                                    .getComponentType(), length);
135:                        } catch (Exception e) {
136:                            return arg;
137:                        }
138:
139:                        // Use convert to assign array elements.
140:                        if (arg.getClass().isArray()) {
141:                            for (int i = 0; i < length; i++) {
142:                                array[i] = convert(Array.get(arg, i), destClass
143:                                        .getComponentType());
144:                            }
145:                        } else {
146:                            int idx = 0;
147:                            for (Iterator i = ((Collection) arg).iterator(); i
148:                                    .hasNext();) {
149:                                array[idx++] = convert(i.next(), destClass
150:                                        .getComponentType());
151:                            }
152:                        }
153:                        destValue = array;
154:                    }
155:                } else if (Collection.class.isAssignableFrom(destClass)) {
156:                    Collection newList = null;
157:                    try {
158:                        // if we are trying to create an interface, build something
159:                        // that implements the interface
160:                        if (destClass == Collection.class
161:                                || destClass == java.util.List.class) {
162:                            newList = new ArrayList();
163:                        } else if (destClass == Set.class) {
164:                            newList = new HashSet();
165:                        } else {
166:                            newList = (Collection) destClass.newInstance();
167:                        }
168:                    } catch (Exception e) {
169:                        // Couldn't build one for some reason... so forget it.
170:                        return arg;
171:                    }
172:
173:                    if (arg.getClass().isArray()) {
174:                        for (int j = 0; j < length; j++) {
175:                            newList.add(Array.get(arg, j));
176:                        }
177:                    } else {
178:                        for (Iterator j = ((Collection) arg).iterator(); j
179:                                .hasNext();) {
180:                            newList.add(j.next());
181:                        }
182:                    }
183:                    destValue = newList;
184:                } else {
185:                    destValue = arg;
186:                }
187:
188:                return destValue;
189:            }
190:
191:            private static Class getHolderValueType(Class aClass) {
192:                return null;
193:            }
194:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.