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: }
|