in modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/DataConverter.java [172:348]
public static Object convert(Object arg, Class<?> destClass) {
if (destClass == null) {
return arg;
}
if (arg != null && destClass.isAssignableFrom(arg.getClass())) {
return arg;
}
// Convert between Calendar and Date
if (arg instanceof Calendar && destClass == Date.class) {
return ((Calendar)arg).getTime();
}
// Convert between HashMap and Hashtable
if (arg instanceof HashMap && destClass == Hashtable.class) {
return new Hashtable((HashMap)arg);
}
if (arg instanceof InputStream && destClass == byte[].class) {
try {
InputStream is = (InputStream)arg;
return getBytesFromStream(is);
} catch (IOException e) {
throw new ServiceRuntimeException(e);
}
}
if (arg instanceof Source && destClass == byte[].class) {
try {
if (arg instanceof StreamSource) {
InputStream is = ((StreamSource)arg).getInputStream();
if (is != null) {
return getBytesFromStream(is);
}
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
Result result = new StreamResult(out);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform((Source)arg, result);
byte[] bytes = out.toByteArray();
return bytes;
} catch (Exception e) {
throw new ServiceRuntimeException(e);
}
}
if (arg instanceof DataHandler) {
try {
InputStream is = ((DataHandler)arg).getInputStream();
if (destClass == Image.class) {
return ImageIO.read(is);
} else if (destClass == Source.class) {
return new StreamSource(is);
}
byte[] bytes = getBytesFromStream(is);
return convert(bytes, destClass);
} catch (Exception e) {
throw new ServiceRuntimeException(e);
}
}
if (arg instanceof byte[] && destClass == String.class) {
return new String((byte[])arg);
}
// If the destination is an array and the source
// is a suitable component, return an array with
// the single item.
/* REVIEW do we need to support atomic to array conversion ?
if (arg != null &&
destClass.isArray() &&
!destClass.getComponentType().equals(Object.class) &&
destClass.getComponentType().isAssignableFrom(arg.getClass())) {
Object array =
Array.newInstance(destClass.getComponentType(), 1);
Array.set(array, 0, arg);
return array;
}
*/
// Return if no conversion is available
if (!(arg instanceof Collection || (arg != null && arg.getClass().isArray()))) {
return arg;
}
if (arg == null) {
return null;
}
// The arg may be an array or List
Object destValue = null;
int length = 0;
if (arg.getClass().isArray()) {
length = Array.getLength(arg);
} else {
length = ((Collection)arg).size();
}
try {
if (destClass.isArray()) {
if (destClass.getComponentType().isPrimitive()) {
Object array = Array.newInstance(destClass.getComponentType(), length);
// Assign array elements
if (arg.getClass().isArray()) {
for (int i = 0; i < length; i++) {
Array.set(array, i, Array.get(arg, i));
}
} else {
int idx = 0;
for (Iterator i = ((Collection)arg).iterator(); i.hasNext();) {
Array.set(array, idx++, i.next());
}
}
destValue = array;
} else {
Object[] array;
try {
array = (Object[])Array.newInstance(destClass.getComponentType(), length);
} catch (Exception e) {
return arg;
}
// Use convert to assign array elements.
if (arg.getClass().isArray()) {
for (int i = 0; i < length; i++) {
array[i] = convert(Array.get(arg, i), destClass.getComponentType());
}
} else {
int idx = 0;
for (Iterator i = ((Collection)arg).iterator(); i.hasNext();) {
array[idx++] = convert(i.next(), destClass.getComponentType());
}
}
destValue = array;
}
} else if (Collection.class.isAssignableFrom(destClass)) {
Collection newList = null;
try {
// if we are trying to create an interface, build something
// that implements the interface
if (destClass == Collection.class || destClass == List.class) {
newList = new ArrayList();
} else if (destClass == Set.class) {
newList = new HashSet();
} else {
newList = (Collection)destClass.newInstance();
}
} catch (Exception e) {
// No FFDC code needed
// Couldn't build one for some reason... so forget it.
return arg;
}
if (arg.getClass().isArray()) {
for (int j = 0; j < length; j++) {
newList.add(Array.get(arg, j));
}
} else {
for (Iterator j = ((Collection)arg).iterator(); j.hasNext();) {
newList.add(j.next());
}
}
destValue = newList;
} else {
destValue = arg;
}
} catch (Throwable t) {
throw new ServiceRuntimeException(t);
}
return destValue;
}