Browse Source
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@1911 50f2f4bb-b051-0410-bef5-90022cba6387pull/1/head
35 changed files with 324 additions and 1866 deletions
@ -1,103 +0,0 @@
@@ -1,103 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import org.springframework.core.convert.ConversionFailedException; |
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
|
||||
/** |
||||
* Base class for converters that convert to and from collection types (arrays and java.util.Collection types). |
||||
* |
||||
* @author Keith Donald |
||||
* @since 3.0 |
||||
*/ |
||||
abstract class AbstractCollectionConverter implements ConversionExecutor { |
||||
|
||||
private final GenericConversionService conversionService; |
||||
|
||||
private final TypeDescriptor sourceCollectionType; |
||||
|
||||
private final TypeDescriptor targetCollectionType; |
||||
|
||||
private final ConversionExecutor elementConverter; |
||||
|
||||
public AbstractCollectionConverter(TypeDescriptor sourceCollectionType, TypeDescriptor targetCollectionType, GenericConversionService conversionService) { |
||||
this.conversionService = conversionService; |
||||
this.sourceCollectionType = sourceCollectionType; |
||||
this.targetCollectionType = targetCollectionType; |
||||
Class<?> sourceElementType = sourceCollectionType.getElementType(); |
||||
Class<?> targetElementType = targetCollectionType.getElementType(); |
||||
if (sourceElementType != null && targetElementType != null) { |
||||
ConversionExecutor executor = conversionService.getConversionExecutor(sourceElementType, TypeDescriptor.valueOf(targetElementType)); |
||||
if (executor != null) { |
||||
this.elementConverter = executor; |
||||
} |
||||
else { |
||||
this.elementConverter = NoOpConversionExecutor.INSTANCE; |
||||
} |
||||
} |
||||
else { |
||||
this.elementConverter = NoOpConversionExecutor.INSTANCE; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* The collection type to convert to. |
||||
*/ |
||||
protected Class<?> getTargetCollectionType() { |
||||
return targetCollectionType.getType(); |
||||
} |
||||
|
||||
/** |
||||
* The type of elements in the target collection. |
||||
*/ |
||||
protected Class<?> getTargetElementType() { |
||||
return this.targetCollectionType.getElementType(); |
||||
} |
||||
|
||||
protected GenericConversionService getConversionService() { |
||||
return this.conversionService; |
||||
} |
||||
|
||||
/** |
||||
* The converter to use to convert elements to the {@link #getTargetElementType()}. |
||||
* Returns {@link NoOpConversionExecutor#INSTANCE} if no converter could be eagerly resolved from type descriptor metadata. |
||||
*/ |
||||
protected ConversionExecutor getElementConverter() { |
||||
return this.elementConverter; |
||||
} |
||||
|
||||
|
||||
public Object execute(Object source) { |
||||
try { |
||||
return doExecute(source); |
||||
} |
||||
catch (Exception ex) { |
||||
throw new ConversionFailedException(source, this.sourceCollectionType.getType(), this.targetCollectionType.getType(), ex); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Override to perform collection conversion. |
||||
* @param sourceCollection the source collection to convert from, an instance of sourceCollectionType, |
||||
* which must be either an array or java.util.Collection type. |
||||
* @return the converted target collection, an instance of targetCollectionType |
||||
* @throws Exception an exception occurred during the conversion |
||||
*/ |
||||
protected abstract Object doExecute(Object sourceCollection) throws Exception; |
||||
|
||||
} |
||||
@ -1,49 +0,0 @@
@@ -1,49 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import java.lang.reflect.Array; |
||||
|
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
|
||||
/** |
||||
* Special one-way converter that converts from a source array to a target array. Supports type conversion of the |
||||
* individual array elements; for example, the ability to convert a String[] to an Integer[]. Mainly used internally by |
||||
* {@link org.springframework.core.convert.ConversionService} implementations. |
||||
* |
||||
* @author Keith Donald |
||||
* @author Juergen Hoeller |
||||
* @since 3.0 |
||||
*/ |
||||
class ArrayToArray extends AbstractCollectionConverter { |
||||
|
||||
public ArrayToArray(TypeDescriptor sourceArrayType, TypeDescriptor targetArrayType, GenericConversionService conversionService) { |
||||
super(sourceArrayType, targetArrayType, conversionService); |
||||
} |
||||
|
||||
@Override |
||||
public Object doExecute(Object sourceArray) { |
||||
int length = Array.getLength(sourceArray); |
||||
Object targetArray = Array.newInstance(getTargetElementType(), length); |
||||
for (int i = 0; i < length; i++) { |
||||
Object value = Array.get(sourceArray, i); |
||||
Array.set(targetArray, i, getElementConverter().execute(value)); |
||||
} |
||||
return targetArray; |
||||
} |
||||
|
||||
} |
||||
@ -1,52 +0,0 @@
@@ -1,52 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import java.lang.reflect.Array; |
||||
import java.util.Collection; |
||||
|
||||
import org.springframework.core.CollectionFactory; |
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
|
||||
/** |
||||
* Special converter that converts from a source array to a target collection. Supports the selection of an |
||||
* "approximate" collection implementation when a target collection interface such as <code>List.class</code> is |
||||
* specified. Supports type conversion of array elements when a parameterized target collection type descriptor is provided. |
||||
* |
||||
* @author Keith Donald |
||||
* @author Juergen Hoeller |
||||
* @since 3.0 |
||||
*/ |
||||
class ArrayToCollection extends AbstractCollectionConverter { |
||||
|
||||
public ArrayToCollection(TypeDescriptor sourceArrayType, TypeDescriptor targetCollectionType, |
||||
GenericConversionService conversionService) { |
||||
super(sourceArrayType, targetCollectionType, conversionService); |
||||
} |
||||
|
||||
@Override |
||||
protected Object doExecute(Object sourceArray) throws Exception { |
||||
int length = Array.getLength(sourceArray); |
||||
Collection collection = CollectionFactory.createCollection(getTargetCollectionType(), length); |
||||
ConversionExecutor elementConverter = getElementConverter(); |
||||
for (int i = 0; i < length; i++) { |
||||
collection.add(elementConverter.execute(Array.get(sourceArray, i))); |
||||
} |
||||
return collection; |
||||
} |
||||
|
||||
} |
||||
@ -1,65 +0,0 @@
@@ -1,65 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import java.lang.reflect.Array; |
||||
import java.util.Collection; |
||||
import java.util.Iterator; |
||||
|
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
|
||||
/** |
||||
* Special converter that converts from target collection to a source array. |
||||
* |
||||
* @author Keith Donald |
||||
* @author Juergen Hoeller |
||||
* @since 3.0 |
||||
*/ |
||||
class CollectionToArray extends AbstractCollectionConverter { |
||||
|
||||
public CollectionToArray(TypeDescriptor sourceCollectionType, TypeDescriptor targetArrayType, |
||||
GenericConversionService conversionService) { |
||||
super(sourceCollectionType, targetArrayType, conversionService); |
||||
} |
||||
|
||||
@Override |
||||
protected Object doExecute(Object source) throws Exception { |
||||
Collection<?> collection = (Collection<?>) source; |
||||
Object array = Array.newInstance(getTargetElementType(), collection.size()); |
||||
int i = 0; |
||||
ConversionExecutor elementConverter = getElementConverter(collection); |
||||
for (Iterator<?> it = collection.iterator(); it.hasNext(); i++) { |
||||
Array.set(array, i, elementConverter.execute(it.next())); |
||||
} |
||||
return array; |
||||
} |
||||
|
||||
private ConversionExecutor getElementConverter(Collection<?> source) { |
||||
ConversionExecutor elementConverter = getElementConverter(); |
||||
if (elementConverter == NoOpConversionExecutor.INSTANCE) { |
||||
for (Object value : source) { |
||||
if (value != null) { |
||||
elementConverter = getConversionService() |
||||
.getConversionExecutor(value.getClass(), TypeDescriptor.valueOf(getTargetElementType())); |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
return (elementConverter != null ? elementConverter : NoOpConversionExecutor.INSTANCE); |
||||
} |
||||
|
||||
} |
||||
@ -1,64 +0,0 @@
@@ -1,64 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import java.util.Collection; |
||||
|
||||
import org.springframework.core.CollectionFactory; |
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
|
||||
/** |
||||
* A converter that can convert from one collection type to another. |
||||
* |
||||
* @author Keith Donald |
||||
* @author Juergen Hoeller |
||||
* @since 3.0 |
||||
*/ |
||||
@SuppressWarnings("unchecked") |
||||
class CollectionToCollection extends AbstractCollectionConverter { |
||||
|
||||
public CollectionToCollection(TypeDescriptor sourceCollectionType, TypeDescriptor targetCollectionType, |
||||
GenericConversionService conversionService) { |
||||
super(sourceCollectionType, targetCollectionType, conversionService); |
||||
} |
||||
|
||||
@Override |
||||
protected Object doExecute(Object source) throws Exception { |
||||
Collection sourceCollection = (Collection) source; |
||||
Collection targetCollection = CollectionFactory.createCollection(getTargetCollectionType(), sourceCollection.size()); |
||||
ConversionExecutor elementConverter = getElementConverter(sourceCollection); |
||||
for (Object aSourceCollection : sourceCollection) { |
||||
targetCollection.add(elementConverter.execute(aSourceCollection)); |
||||
} |
||||
return targetCollection; |
||||
} |
||||
|
||||
private ConversionExecutor getElementConverter(Collection<?> source) { |
||||
ConversionExecutor elementConverter = getElementConverter(); |
||||
if (elementConverter == NoOpConversionExecutor.INSTANCE && getTargetElementType() != null) { |
||||
for (Object value : source) { |
||||
if (value != null) { |
||||
elementConverter = getConversionService().getConversionExecutor( |
||||
value.getClass(), TypeDescriptor.valueOf(getTargetElementType())); |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
return elementConverter; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,78 @@
@@ -0,0 +1,78 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import java.util.Collection; |
||||
|
||||
import org.springframework.core.CollectionFactory; |
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
|
||||
/** |
||||
* A generic converter that can convert from one collection type to another. |
||||
* |
||||
* @author Keith Donald |
||||
* @author Juergen Hoeller |
||||
* @since 3.0 |
||||
*/ |
||||
class CollectionToCollectionGenericConverter implements GenericConverter { |
||||
|
||||
private GenericConversionService conversionService; |
||||
|
||||
public CollectionToCollectionGenericConverter(GenericConversionService conversionService) { |
||||
this.conversionService = conversionService; |
||||
} |
||||
|
||||
public Object convert(Object source, TypeDescriptor targetType) { |
||||
Collection sourceCollection = (Collection) source; |
||||
Object firstNotNullElement = getFirstNotNullElement(sourceCollection); |
||||
if (firstNotNullElement == null) { |
||||
return compatibleCollectionWithoutElementConversion(sourceCollection, targetType); |
||||
} |
||||
Class targetElementType = targetType.getElementType(); |
||||
if (targetElementType == null || targetElementType.isAssignableFrom(firstNotNullElement.getClass())) { |
||||
return compatibleCollectionWithoutElementConversion(sourceCollection, targetType); |
||||
} |
||||
Collection targetCollection = CollectionFactory.createCollection(targetType.getType(), sourceCollection.size()); |
||||
GenericConverter elementConverter = conversionService.getConverter(firstNotNullElement.getClass(), targetElementType); |
||||
for (Object element : sourceCollection) { |
||||
targetCollection.add(elementConverter.convert(element, TypeDescriptor.valueOf(targetElementType))); |
||||
} |
||||
return targetCollection; |
||||
} |
||||
|
||||
private Collection compatibleCollectionWithoutElementConversion(Collection source, TypeDescriptor targetType) { |
||||
if (targetType.getType().isAssignableFrom(source.getClass())) { |
||||
return source; |
||||
} else { |
||||
Collection target = CollectionFactory.createCollection(targetType.getType(), source.size()); |
||||
for (Object element : source) { |
||||
target.addAll(source); |
||||
} |
||||
return target; |
||||
} |
||||
} |
||||
|
||||
private Object getFirstNotNullElement(Collection collection) { |
||||
for (Object element : collection) { |
||||
if (element != null) { |
||||
return element; |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
} |
||||
@ -1,38 +0,0 @@
@@ -1,38 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import org.springframework.core.convert.ConversionFailedException; |
||||
|
||||
/** |
||||
* A command parameterized with the information necessary to perform a conversion of a source input to a |
||||
* target output. Encapsulates knowledge about how to convert source objects to a specific target type |
||||
* using a specific converter. |
||||
* |
||||
* @author Keith Donald |
||||
* @since 3.0 |
||||
*/ |
||||
interface ConversionExecutor { |
||||
|
||||
/** |
||||
* Convert the source. |
||||
* @param source the source to convert |
||||
* @throws ConversionFailedException if an exception occurs during type conversion |
||||
*/ |
||||
Object execute(Object source); |
||||
|
||||
} |
||||
@ -0,0 +1,9 @@
@@ -0,0 +1,9 @@
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
|
||||
interface GenericConverter { |
||||
|
||||
Object convert(Object source, TypeDescriptor type); |
||||
|
||||
} |
||||
@ -1,56 +0,0 @@
@@ -1,56 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
/** |
||||
* A helper for convertering map keys and values. |
||||
* |
||||
* @author Keith Donald |
||||
* @since 3.0 |
||||
*/ |
||||
class MapEntryConverter { |
||||
|
||||
public static final MapEntryConverter NO_OP_INSTANCE = new MapEntryConverter(null, null); |
||||
|
||||
private final ConversionExecutor keyConverter; |
||||
|
||||
private final ConversionExecutor valueConverter; |
||||
|
||||
public MapEntryConverter(ConversionExecutor keyConverter, ConversionExecutor valueConverter) { |
||||
this.keyConverter = keyConverter; |
||||
this.valueConverter = valueConverter; |
||||
} |
||||
|
||||
public Object convertKey(Object key) { |
||||
if (this.keyConverter != null) { |
||||
return this.keyConverter.execute(key); |
||||
} |
||||
else { |
||||
return key; |
||||
} |
||||
} |
||||
|
||||
public Object convertValue(Object value) { |
||||
if (this.valueConverter != null) { |
||||
return this.valueConverter.execute(value); |
||||
} |
||||
else { |
||||
return value; |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -1,113 +0,0 @@
@@ -1,113 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import java.util.Map; |
||||
|
||||
import org.springframework.core.CollectionFactory; |
||||
import org.springframework.core.convert.ConversionFailedException; |
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
|
||||
/** |
||||
* Converts from one map to another map, with support for converting individual map elements |
||||
* based on generic type information. |
||||
* |
||||
* @author Keith Donald |
||||
* @since 3.0 |
||||
*/ |
||||
class MapToMap implements ConversionExecutor { |
||||
|
||||
private final TypeDescriptor sourceType; |
||||
|
||||
private final TypeDescriptor targetType; |
||||
|
||||
private final GenericConversionService conversionService; |
||||
|
||||
private final MapEntryConverter entryConverter; |
||||
|
||||
/** |
||||
* Creates a new map-to-map converter |
||||
* @param sourceType the source map type |
||||
* @param targetType the target map type |
||||
* @param conversionService the conversion service |
||||
*/ |
||||
public MapToMap(TypeDescriptor sourceType, TypeDescriptor targetType, GenericConversionService conversionService) { |
||||
this.sourceType = sourceType; |
||||
this.targetType = targetType; |
||||
this.conversionService = conversionService; |
||||
this.entryConverter = createEntryConverter(); |
||||
} |
||||
|
||||
private MapEntryConverter createEntryConverter() { |
||||
if (this.sourceType.isMapEntryTypeKnown() && this.targetType.isMapEntryTypeKnown()) { |
||||
ConversionExecutor keyConverter = this.conversionService.getConversionExecutor( |
||||
this.sourceType.getMapKeyType(), TypeDescriptor.valueOf(this.targetType.getMapKeyType())); |
||||
ConversionExecutor valueConverter = this.conversionService.getConversionExecutor( |
||||
this.sourceType.getMapValueType(), TypeDescriptor.valueOf(this.targetType.getMapValueType())); |
||||
return new MapEntryConverter(keyConverter, valueConverter); |
||||
} |
||||
else { |
||||
return MapEntryConverter.NO_OP_INSTANCE; |
||||
} |
||||
} |
||||
|
||||
@SuppressWarnings("unchecked") |
||||
public Object execute(Object source) throws ConversionFailedException { |
||||
try { |
||||
Map<?, ?> map = (Map<?, ?>) source; |
||||
Map targetMap = CollectionFactory.createMap(this.targetType.getType(), map.size()); |
||||
MapEntryConverter converter = getEntryConverter(map); |
||||
for (Map.Entry<?, ?> entry : map.entrySet()) { |
||||
targetMap.put(converter.convertKey(entry.getKey()), converter.convertValue(entry.getValue())); |
||||
} |
||||
return targetMap; |
||||
} |
||||
catch (Exception ex) { |
||||
throw new ConversionFailedException(source, this.sourceType.getType(), this.targetType.getType(), ex); |
||||
} |
||||
} |
||||
|
||||
private MapEntryConverter getEntryConverter(Map<?, ?> map) { |
||||
MapEntryConverter entryConverter = this.entryConverter; |
||||
if (entryConverter == MapEntryConverter.NO_OP_INSTANCE) { |
||||
Class<?> targetKeyType = targetType.getMapKeyType(); |
||||
Class<?> targetValueType = targetType.getMapValueType(); |
||||
if (targetKeyType != null && targetValueType != null) { |
||||
ConversionExecutor keyConverter = null; |
||||
ConversionExecutor valueConverter = null; |
||||
for (Map.Entry<?, ?> entry : map.entrySet()) { |
||||
Object key = entry.getKey(); |
||||
Object value = entry.getValue(); |
||||
if (keyConverter == null && key != null) { |
||||
keyConverter = conversionService |
||||
.getConversionExecutor(key.getClass(), TypeDescriptor.valueOf(targetKeyType)); |
||||
} |
||||
if (valueConverter == null && value != null) { |
||||
valueConverter = conversionService |
||||
.getConversionExecutor(value.getClass(), TypeDescriptor.valueOf(targetValueType)); |
||||
} |
||||
if (keyConverter != null && valueConverter != null) { |
||||
break; |
||||
} |
||||
} |
||||
entryConverter = new MapEntryConverter(keyConverter, valueConverter); |
||||
} |
||||
} |
||||
return entryConverter; |
||||
} |
||||
|
||||
} |
||||
@ -1,74 +0,0 @@
@@ -1,74 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import java.lang.reflect.Array; |
||||
import java.util.Map; |
||||
|
||||
import org.springframework.core.convert.ConversionFailedException; |
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
|
||||
/** |
||||
* Converts a Map to a String array, where each element in the array |
||||
* is of the format key=value. |
||||
* |
||||
* @author Keith Donald |
||||
* @since 3.0 |
||||
*/ |
||||
class MapToStringArray implements ConversionExecutor { |
||||
|
||||
private final TypeDescriptor targetType; |
||||
|
||||
private final GenericConversionService conversionService; |
||||
|
||||
private final MapEntryConverter entryConverter; |
||||
|
||||
public MapToStringArray(TypeDescriptor targetType, GenericConversionService conversionService) { |
||||
this.targetType = targetType; |
||||
this.conversionService = conversionService; |
||||
this.entryConverter = createEntryConverter(); |
||||
} |
||||
|
||||
private MapEntryConverter createEntryConverter() { |
||||
if (this.targetType.isMapEntryTypeKnown()) { |
||||
ConversionExecutor keyConverter = this.conversionService.getConversionExecutor(this.targetType.getMapKeyType(), |
||||
TypeDescriptor.valueOf(String.class)); |
||||
ConversionExecutor valueConverter = this.conversionService.getConversionExecutor(this.targetType.getMapValueType(), |
||||
TypeDescriptor.valueOf(String.class)); |
||||
return new MapEntryConverter(keyConverter, valueConverter); |
||||
} |
||||
else { |
||||
return MapEntryConverter.NO_OP_INSTANCE; |
||||
} |
||||
} |
||||
|
||||
public Object execute(Object source) throws ConversionFailedException { |
||||
Map sourceMap = (Map) source; |
||||
Object array = Array.newInstance(this.targetType.getElementType(), sourceMap.size()); |
||||
int i = 0; |
||||
for (Object entry : sourceMap.entrySet()) { |
||||
Map.Entry mapEntry = (Map.Entry) entry; |
||||
Object key = mapEntry.getKey(); |
||||
Object value = mapEntry.getValue(); |
||||
String property = this.entryConverter.convertKey(key) + "=" + this.entryConverter.convertValue(value); |
||||
Array.set(array, i, property); |
||||
i++; |
||||
} |
||||
return array; |
||||
} |
||||
|
||||
} |
||||
@ -1,48 +0,0 @@
@@ -1,48 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import java.util.Map; |
||||
|
||||
import org.springframework.core.convert.ConversionFailedException; |
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
|
||||
/** |
||||
* Converts a Map to a String collection, where each element in the collection |
||||
* is of the format key=value. |
||||
* |
||||
* @author Keith Donald |
||||
* @since 3.0 |
||||
*/ |
||||
class MapToStringCollection implements ConversionExecutor { |
||||
|
||||
private final MapToStringArray converter; |
||||
|
||||
private final ArrayToCollection collectionConverter; |
||||
|
||||
public MapToStringCollection(TypeDescriptor targetType, GenericConversionService conversionService) { |
||||
this.converter = new MapToStringArray(targetType, conversionService); |
||||
this.collectionConverter = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), targetType, conversionService); |
||||
} |
||||
|
||||
public Object execute(Object source) throws ConversionFailedException { |
||||
Map map = (Map) source; |
||||
Object array = this.converter.execute(map); |
||||
return this.collectionConverter.execute(array); |
||||
} |
||||
|
||||
} |
||||
@ -1,39 +0,0 @@
@@ -1,39 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.core.convert.support; |
||||
|
||||
import org.springframework.core.convert.ConversionFailedException; |
||||
|
||||
/** |
||||
* Conversion executor that does nothing. |
||||
* Access singleton using {@link #INSTANCE}. |
||||
* |
||||
* @author Keith Donald |
||||
* @since 3.0 |
||||
*/ |
||||
class NoOpConversionExecutor implements ConversionExecutor { |
||||
|
||||
public static final ConversionExecutor INSTANCE = new NoOpConversionExecutor(); |
||||
|
||||
private NoOpConversionExecutor() { |
||||
} |
||||
|
||||
public Object execute(Object source) throws ConversionFailedException { |
||||
// does nothing
|
||||
return source; |
||||
} |
||||
|
||||
} |
||||
@ -1,51 +0,0 @@
@@ -1,51 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import java.lang.reflect.Array; |
||||
|
||||
import org.springframework.core.convert.ConversionFailedException; |
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
|
||||
/** |
||||
* Converts an object to a single-element array. |
||||
* TODO - this class throws cryptic exception if it can't convert to required target array element type. |
||||
* |
||||
* @author Keith Donald |
||||
* @since 3.0 |
||||
*/ |
||||
class ObjectToArray implements ConversionExecutor { |
||||
|
||||
private final Class elementType; |
||||
|
||||
private final ConversionExecutor elementConverter; |
||||
|
||||
public ObjectToArray(TypeDescriptor sourceObjectType, TypeDescriptor targetArrayType, |
||||
GenericConversionService conversionService) { |
||||
this.elementType = targetArrayType.getElementType(); |
||||
this.elementConverter = conversionService.getConversionExecutor( |
||||
sourceObjectType.getType(), TypeDescriptor.valueOf(this.elementType)); |
||||
} |
||||
|
||||
public Object execute(Object source) throws ConversionFailedException { |
||||
Object array = Array.newInstance(this.elementType, 1); |
||||
Object element = this.elementConverter.execute(source); |
||||
Array.set(array, 0, element); |
||||
return array; |
||||
} |
||||
|
||||
} |
||||
@ -1,55 +0,0 @@
@@ -1,55 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import java.util.Collection; |
||||
|
||||
import org.springframework.core.CollectionFactory; |
||||
import org.springframework.core.convert.ConversionFailedException; |
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
|
||||
/** |
||||
* Converts an object to a single-element collection. |
||||
* |
||||
* @author Keith Donald |
||||
* @since 3.0 |
||||
*/ |
||||
class ObjectToCollection implements ConversionExecutor { |
||||
|
||||
private final TypeDescriptor targetCollectionType; |
||||
|
||||
private final ConversionExecutor elementConverter; |
||||
|
||||
public ObjectToCollection(TypeDescriptor sourceObjectType, TypeDescriptor targetCollectionType, |
||||
GenericConversionService typeConverter) { |
||||
this.targetCollectionType = targetCollectionType; |
||||
Class<?> elementType = targetCollectionType.getElementType(); |
||||
if (elementType != null) { |
||||
this.elementConverter = typeConverter.getConversionExecutor(sourceObjectType.getType(), TypeDescriptor.valueOf(elementType)); |
||||
} |
||||
else { |
||||
this.elementConverter = NoOpConversionExecutor.INSTANCE; |
||||
} |
||||
} |
||||
|
||||
public Object execute(Object source) throws ConversionFailedException { |
||||
Collection collection = CollectionFactory.createCollection(this.targetCollectionType.getType(), 1); |
||||
collection.add(this.elementConverter.execute(source)); |
||||
return collection; |
||||
} |
||||
|
||||
} |
||||
@ -1,75 +0,0 @@
@@ -1,75 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import org.springframework.core.convert.ConversionFailedException; |
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
import org.springframework.core.convert.converter.Converter; |
||||
|
||||
/** |
||||
* Default conversion executor implementation for converters. |
||||
* |
||||
* @author Keith Donald |
||||
* @author Juergen Hoeller |
||||
* @since 3.0 |
||||
*/ |
||||
class StaticConversionExecutor implements ConversionExecutor { |
||||
|
||||
private final TypeDescriptor sourceType; |
||||
|
||||
private final TypeDescriptor targetType; |
||||
|
||||
private final Converter converter; |
||||
|
||||
public StaticConversionExecutor(TypeDescriptor sourceType, TypeDescriptor targetType, Converter converter) { |
||||
this.sourceType = sourceType; |
||||
this.targetType = targetType; |
||||
this.converter = converter; |
||||
} |
||||
|
||||
public Object execute(Object source) throws ConversionFailedException { |
||||
if (source == null) { |
||||
return null; |
||||
} |
||||
if (!this.sourceType.isAssignableValue(source)) { |
||||
throw new ConversionFailedException(source, this.sourceType.getType(), this.targetType.getType(), |
||||
"Source object " + source + " to convert is expected to be an instance of [" + this.sourceType.getName() + "]"); |
||||
} |
||||
try { |
||||
return this.converter.convert(source); |
||||
} |
||||
catch (Exception ex) { |
||||
throw new ConversionFailedException(source, this.sourceType.getType(), this.targetType.getType(), ex); |
||||
} |
||||
} |
||||
|
||||
public boolean equals(Object obj) { |
||||
if (this == obj) { |
||||
return true; |
||||
} |
||||
if (!(obj instanceof StaticConversionExecutor)) { |
||||
return false; |
||||
} |
||||
StaticConversionExecutor other = (StaticConversionExecutor) obj; |
||||
return this.sourceType.equals(other.sourceType) && this.targetType.equals(other.targetType); |
||||
} |
||||
|
||||
public int hashCode() { |
||||
return this.sourceType.hashCode() + this.targetType.hashCode(); |
||||
} |
||||
|
||||
} |
||||
@ -1,84 +0,0 @@
@@ -1,84 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import java.lang.reflect.Array; |
||||
import java.util.Map; |
||||
|
||||
import org.springframework.core.CollectionFactory; |
||||
import org.springframework.core.convert.ConversionFailedException; |
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
|
||||
/** |
||||
* Converts a String array to a Map. |
||||
* Each element in the array must be formatted as key=value. |
||||
* |
||||
* @author Keith Donald |
||||
* @since 3.0 |
||||
*/ |
||||
class StringArrayToMap implements ConversionExecutor { |
||||
|
||||
private final TypeDescriptor sourceType; |
||||
|
||||
private final TypeDescriptor targetType; |
||||
|
||||
private final GenericConversionService conversionService; |
||||
|
||||
private final MapEntryConverter entryConverter; |
||||
|
||||
public StringArrayToMap(TypeDescriptor sourceType, TypeDescriptor targetType, GenericConversionService conversionService) { |
||||
this.sourceType = sourceType; |
||||
this.targetType = targetType; |
||||
this.conversionService = conversionService; |
||||
this.entryConverter = createEntryConverter(); |
||||
} |
||||
|
||||
private MapEntryConverter createEntryConverter() { |
||||
if (this.targetType.isMapEntryTypeKnown()) { |
||||
ConversionExecutor keyConverter = this.conversionService.getConversionExecutor(String.class, |
||||
TypeDescriptor.valueOf(this.targetType.getMapKeyType())); |
||||
ConversionExecutor valueConverter = this.conversionService.getConversionExecutor(String.class, |
||||
TypeDescriptor.valueOf(this.targetType.getMapValueType())); |
||||
return new MapEntryConverter(keyConverter, valueConverter); |
||||
} |
||||
else { |
||||
return MapEntryConverter.NO_OP_INSTANCE; |
||||
} |
||||
} |
||||
|
||||
public Object execute(Object source) throws ConversionFailedException { |
||||
try { |
||||
int length = Array.getLength(source); |
||||
Map targetMap = CollectionFactory.createMap(this.targetType.getType(), length); |
||||
for (int i = 0; i < length; i++) { |
||||
String property = (String) Array.get(source, i); |
||||
String[] fields = property.split("="); |
||||
if (fields.length < 2) { |
||||
throw new IllegalArgumentException("Invalid String property: " + property); |
||||
} |
||||
String key = fields[0]; |
||||
String value = fields[1]; |
||||
targetMap.put(this.entryConverter.convertKey(key), this.entryConverter.convertValue(value)); |
||||
} |
||||
return targetMap; |
||||
} |
||||
catch (Exception ex) { |
||||
throw new ConversionFailedException(source, this.sourceType.getType(), this.targetType.getType(), ex); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -1,43 +0,0 @@
@@ -1,43 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import org.springframework.core.convert.ConversionFailedException; |
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
import org.springframework.util.ObjectUtils; |
||||
import org.springframework.util.StringUtils; |
||||
|
||||
/** |
||||
* Converts a String array to a single element. |
||||
* |
||||
* @author Juergen Hoeller |
||||
* @since 3.0 |
||||
*/ |
||||
class StringArrayToObject implements ConversionExecutor { |
||||
|
||||
private final ConversionExecutor elementConverter; |
||||
|
||||
public StringArrayToObject(TypeDescriptor targetType, GenericConversionService conversionService) { |
||||
this.elementConverter = conversionService.getConversionExecutor(String.class, targetType); |
||||
} |
||||
|
||||
public Object execute(Object source) throws ConversionFailedException { |
||||
String str = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(source)); |
||||
return this.elementConverter.execute(str); |
||||
} |
||||
|
||||
} |
||||
@ -1,45 +0,0 @@
@@ -1,45 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import java.util.Collection; |
||||
|
||||
import org.springframework.core.convert.ConversionFailedException; |
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
|
||||
/** |
||||
* Converts a String collection to a Map. |
||||
* Each element in the collection must be formatted as key=value. |
||||
* |
||||
* @author Keith Donald |
||||
* @since 3.0 |
||||
*/ |
||||
@SuppressWarnings("unchecked") |
||||
class StringCollectionToMap implements ConversionExecutor { |
||||
|
||||
private StringArrayToMap converter; |
||||
|
||||
public StringCollectionToMap(TypeDescriptor sourceType, TypeDescriptor targetType, GenericConversionService conversionService) { |
||||
converter = new StringArrayToMap(sourceType, targetType, conversionService); |
||||
} |
||||
|
||||
public Object execute(Object source) throws ConversionFailedException { |
||||
Collection collection = (Collection) source; |
||||
return converter.execute(collection.toArray()); |
||||
} |
||||
|
||||
} |
||||
@ -1,43 +0,0 @@
@@ -1,43 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import org.springframework.core.convert.ConversionFailedException; |
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
|
||||
/** |
||||
* Converts a comma-delimited string to a collection. |
||||
* |
||||
* @author Keith Donald |
||||
* @since 3.0 |
||||
*/ |
||||
@SuppressWarnings("unchecked") |
||||
class StringToCollection implements ConversionExecutor { |
||||
|
||||
private final ArrayToCollection converter; |
||||
|
||||
public StringToCollection(TypeDescriptor sourceType, TypeDescriptor targetType, GenericConversionService conversionService) { |
||||
this.converter = new ArrayToCollection(sourceType, targetType, conversionService); |
||||
} |
||||
|
||||
public Object execute(Object source) throws ConversionFailedException { |
||||
String string = (String) source; |
||||
String[] fields = string.split(","); |
||||
return this.converter.execute(fields); |
||||
} |
||||
|
||||
} |
||||
@ -1,49 +0,0 @@
@@ -1,49 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import org.springframework.core.convert.ConversionFailedException; |
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
|
||||
/** |
||||
* Converts a String to a map. |
||||
* The String should be in the format: |
||||
* <pre> |
||||
* key=value |
||||
* key=value |
||||
* key=value |
||||
* key=value |
||||
* </pre> |
||||
* |
||||
* @author Keith Donald |
||||
* @since 3.0 |
||||
*/ |
||||
class StringToMap implements ConversionExecutor { |
||||
|
||||
private StringArrayToMap converter; |
||||
|
||||
public StringToMap(TypeDescriptor sourceType, TypeDescriptor targetType, GenericConversionService conversionService) { |
||||
converter = new StringArrayToMap(sourceType, targetType, conversionService); |
||||
} |
||||
|
||||
public Object execute(Object source) throws ConversionFailedException { |
||||
String string = (String) source; |
||||
String[] entries = string.split(" "); |
||||
return converter.execute(entries); |
||||
} |
||||
|
||||
} |
||||
@ -1,39 +0,0 @@
@@ -1,39 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import static org.junit.Assert.*; |
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
|
||||
/** |
||||
* @author Keith Donald |
||||
*/ |
||||
public class ArrayToArrayTests { |
||||
|
||||
@Test |
||||
public void testArrayToArrayConversion() { |
||||
DefaultConversionService service = new DefaultConversionService(); |
||||
ArrayToArray c = new ArrayToArray(TypeDescriptor.valueOf(String[].class), TypeDescriptor.valueOf(Integer[].class), service); |
||||
Integer[] result = (Integer[]) c.execute(new String[] { "1", "2", "3" }); |
||||
assertEquals(new Integer(1), result[0]); |
||||
assertEquals(new Integer(2), result[1]); |
||||
assertEquals(new Integer(3), result[2]); |
||||
} |
||||
|
||||
} |
||||
@ -1,85 +0,0 @@
@@ -1,85 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import java.util.Collection; |
||||
import java.util.LinkedList; |
||||
import java.util.List; |
||||
import java.util.Set; |
||||
import java.util.SortedSet; |
||||
|
||||
import static org.junit.Assert.*; |
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
|
||||
/** |
||||
* @author Keith Donald |
||||
*/ |
||||
public class ArrayToCollectionTests { |
||||
|
||||
@Test |
||||
public void testArrayToCollectionConversion() throws Exception { |
||||
DefaultConversionService service = new DefaultConversionService(); |
||||
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("bindTarget")), service); |
||||
Collection result = (Collection) c.execute(new String[] { "1", "2", "3" }); |
||||
assertEquals(3, result.size()); |
||||
assertTrue(result.contains(1)); |
||||
assertTrue(result.contains(2)); |
||||
assertTrue(result.contains(3)); |
||||
} |
||||
|
||||
@Test |
||||
public void testArrayToSetConversion() throws Exception { |
||||
DefaultConversionService service = new DefaultConversionService(); |
||||
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("setTarget")), service); |
||||
Set result = (Set) c.execute(new String[] { "1" }); |
||||
assertEquals("1", result.iterator().next()); |
||||
} |
||||
|
||||
@Test |
||||
public void testArrayToSortedSetConversion() throws Exception { |
||||
DefaultConversionService service = new DefaultConversionService(); |
||||
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("sortedSetTarget")), service); |
||||
SortedSet result = (SortedSet) c.execute(new String[] { "1" }); |
||||
assertEquals(new Integer(1), result.iterator().next()); |
||||
} |
||||
|
||||
@Test |
||||
public void testArrayToCollectionImplConversion() throws Exception { |
||||
DefaultConversionService service = new DefaultConversionService(); |
||||
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("implTarget")), service); |
||||
LinkedList result = (LinkedList) c.execute(new String[] { "1" }); |
||||
assertEquals("1", result.iterator().next()); |
||||
} |
||||
|
||||
@Test |
||||
public void testArrayToNonGenericCollectionConversionNullElement() throws Exception { |
||||
DefaultConversionService service = new DefaultConversionService(); |
||||
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("listTarget")), service); |
||||
List result = (List) c.execute(new Integer[] { null, new Integer(1) }); |
||||
assertEquals(null, result.get(0)); |
||||
assertEquals(new Integer(1), result.get(1)); |
||||
} |
||||
|
||||
public Collection<Integer> bindTarget; |
||||
public List listTarget; |
||||
public Set setTarget; |
||||
public SortedSet<Integer> sortedSetTarget; |
||||
public LinkedList<String> implTarget; |
||||
|
||||
} |
||||
@ -1,79 +0,0 @@
@@ -1,79 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collection; |
||||
|
||||
import static org.junit.Assert.*; |
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
|
||||
/** |
||||
* @author Keith Donald |
||||
*/ |
||||
public class CollectionToArrayTests { |
||||
|
||||
@Test |
||||
public void testCollectionToArrayConversion() throws Exception { |
||||
DefaultConversionService service = new DefaultConversionService(); |
||||
CollectionToArray c = new CollectionToArray(new TypeDescriptor(getClass().getField("bindTarget")), |
||||
TypeDescriptor.valueOf(Integer[].class), service); |
||||
bindTarget.add("1"); |
||||
bindTarget.add("2"); |
||||
bindTarget.add("3"); |
||||
Integer[] result = (Integer[]) c.execute(bindTarget); |
||||
assertEquals(new Integer(1), result[0]); |
||||
assertEquals(new Integer(2), result[1]); |
||||
assertEquals(new Integer(3), result[2]); |
||||
} |
||||
|
||||
@Test |
||||
public void testCollectionToArrayConversionNoGenericInfo() throws Exception { |
||||
DefaultConversionService service = new DefaultConversionService(); |
||||
CollectionToArray c = new CollectionToArray(TypeDescriptor.valueOf(Collection.class), TypeDescriptor |
||||
.valueOf(Integer[].class), service); |
||||
bindTarget.add("1"); |
||||
bindTarget.add("2"); |
||||
bindTarget.add("3"); |
||||
Integer[] result = (Integer[]) c.execute(bindTarget); |
||||
assertEquals(new Integer(1), result[0]); |
||||
assertEquals(new Integer(2), result[1]); |
||||
assertEquals(new Integer(3), result[2]); |
||||
} |
||||
|
||||
@Test |
||||
public void testCollectionToArrayConversionNoGenericInfoNullElement() throws Exception { |
||||
DefaultConversionService service = new DefaultConversionService(); |
||||
CollectionToArray c = new CollectionToArray(TypeDescriptor.valueOf(Collection.class), TypeDescriptor |
||||
.valueOf(Integer[].class), service); |
||||
bindTarget.add(null); |
||||
bindTarget.add("1"); |
||||
bindTarget.add("2"); |
||||
bindTarget.add("3"); |
||||
Integer[] result = (Integer[]) c.execute(bindTarget); |
||||
assertEquals(null, result[0]); |
||||
assertEquals(new Integer(1), result[1]); |
||||
assertEquals(new Integer(2), result[2]); |
||||
assertEquals(new Integer(3), result[3]); |
||||
} |
||||
|
||||
public Collection<String> bindTarget = new ArrayList<String>(); |
||||
|
||||
|
||||
} |
||||
@ -1,109 +0,0 @@
@@ -1,109 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collection; |
||||
import java.util.Iterator; |
||||
import java.util.List; |
||||
|
||||
import static org.junit.Assert.*; |
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
|
||||
/** |
||||
* @author Keith Donald |
||||
*/ |
||||
public class CollectionToCollectionTests { |
||||
|
||||
@Test |
||||
public void testCollectionToCollectionConversion() throws Exception { |
||||
DefaultConversionService service = new DefaultConversionService(); |
||||
CollectionToCollection c = new CollectionToCollection(new TypeDescriptor(getClass().getField("bindTarget")), |
||||
new TypeDescriptor(getClass().getField("integerTarget")), service); |
||||
bindTarget.add("1"); |
||||
bindTarget.add("2"); |
||||
bindTarget.add("3"); |
||||
Collection result = (Collection) c.execute(bindTarget); |
||||
assertEquals(3, result.size()); |
||||
assertTrue(result.contains(1)); |
||||
assertTrue(result.contains(2)); |
||||
assertTrue(result.contains(3)); |
||||
} |
||||
|
||||
@Test |
||||
public void testCollectionToCollectionConversionNoGenericInfo() throws Exception { |
||||
DefaultConversionService service = new DefaultConversionService(); |
||||
CollectionToCollection c = new CollectionToCollection(TypeDescriptor.valueOf(Collection.class), |
||||
TypeDescriptor.valueOf(List.class), service); |
||||
bindTarget.add("1"); |
||||
bindTarget.add("2"); |
||||
bindTarget.add("3"); |
||||
Collection result = (Collection) c.execute(bindTarget); |
||||
assertEquals(3, result.size()); |
||||
assertTrue(result.contains("1")); |
||||
assertTrue(result.contains("2")); |
||||
assertTrue(result.contains("3")); |
||||
} |
||||
|
||||
@Test |
||||
public void testCollectionToCollectionConversionNoGenericInfoSource() throws Exception { |
||||
DefaultConversionService service = new DefaultConversionService(); |
||||
CollectionToCollection c = new CollectionToCollection(TypeDescriptor.valueOf(Collection.class), |
||||
new TypeDescriptor(getClass().getField("integerTarget")), service); |
||||
bindTarget.add("1"); |
||||
bindTarget.add("2"); |
||||
bindTarget.add("3"); |
||||
Collection result = (Collection) c.execute(bindTarget); |
||||
assertEquals(3, result.size()); |
||||
assertTrue(result.contains(1)); |
||||
assertTrue(result.contains(2)); |
||||
assertTrue(result.contains(3)); |
||||
} |
||||
|
||||
@Test |
||||
public void testCollectionToCollectionConversionNoGenericInfoSourceNullValue() throws Exception { |
||||
DefaultConversionService service = new DefaultConversionService(); |
||||
CollectionToCollection c = new CollectionToCollection(TypeDescriptor.valueOf(Collection.class), |
||||
new TypeDescriptor(getClass().getField("integerTarget")), service); |
||||
bindTarget.add(null); |
||||
bindTarget.add("1"); |
||||
bindTarget.add("2"); |
||||
bindTarget.add("3"); |
||||
Collection result = (Collection) c.execute(bindTarget); |
||||
Iterator it = result.iterator(); |
||||
assertEquals(null, it.next()); |
||||
assertEquals(new Integer(1), it.next()); |
||||
assertEquals(new Integer(2), it.next()); |
||||
assertEquals(new Integer(3), it.next()); |
||||
} |
||||
|
||||
@Test |
||||
public void testCollectionToCollectionConversionNoGenericInfoSourceEmpty() throws Exception { |
||||
DefaultConversionService service = new DefaultConversionService(); |
||||
CollectionToCollection c = new CollectionToCollection(TypeDescriptor.valueOf(Collection.class), |
||||
new TypeDescriptor(getClass().getField("integerTarget")), service); |
||||
Collection result = (Collection) c.execute(bindTarget); |
||||
assertTrue(result.isEmpty()); |
||||
} |
||||
|
||||
|
||||
public Collection<String> bindTarget = new ArrayList<String>(); |
||||
public List<Integer> integerTarget = new ArrayList<Integer>(); |
||||
|
||||
} |
||||
@ -1,78 +0,0 @@
@@ -1,78 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import static org.junit.Assert.assertEquals; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
import org.junit.Test; |
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
import org.springframework.core.convert.support.DefaultConversionService; |
||||
import org.springframework.core.convert.support.MapToMap; |
||||
|
||||
/** |
||||
* @author Keith Donald |
||||
*/ |
||||
public class MapToMapTests { |
||||
|
||||
@Test |
||||
public void testMapToMapConversion() throws Exception { |
||||
DefaultConversionService converter = new DefaultConversionService(); |
||||
MapToMap c = new MapToMap(new TypeDescriptor(getClass().getField("source")), |
||||
new TypeDescriptor(getClass().getField("bindTarget")), converter); |
||||
source.put("1", "BAR"); |
||||
source.put("2", "BAZ"); |
||||
Map<String, FooEnum> result = (Map<String, FooEnum>) c.execute(source); |
||||
assertEquals(FooEnum.BAR, result.get(1)); |
||||
assertEquals(FooEnum.BAZ, result.get(2)); |
||||
} |
||||
|
||||
@Test |
||||
public void testMapToMapConversionNoGenericInfoOnSource() throws Exception { |
||||
DefaultConversionService service = new DefaultConversionService(); |
||||
MapToMap c = new MapToMap(TypeDescriptor.valueOf(Map.class), |
||||
new TypeDescriptor(getClass().getField("bindTarget")), service); |
||||
source.put("1", "BAR"); |
||||
source.put("2", "BAZ"); |
||||
Map result = (Map) c.execute(source); |
||||
assertEquals(FooEnum.BAR, result.get(1)); |
||||
assertEquals(FooEnum.BAZ, result.get(2)); |
||||
} |
||||
|
||||
@Test |
||||
public void testMapToMapConversionNoGenericInfo() throws Exception { |
||||
DefaultConversionService service = new DefaultConversionService(); |
||||
MapToMap c = new MapToMap(TypeDescriptor.valueOf(Map.class), |
||||
TypeDescriptor.valueOf(Map.class), service); |
||||
source.put("1", "BAR"); |
||||
source.put("2", "BAZ"); |
||||
Map result = (Map) c.execute(source); |
||||
assertEquals("BAR", result.get("1")); |
||||
assertEquals("BAZ", result.get("2")); |
||||
} |
||||
|
||||
|
||||
public Map<String, String> source = new HashMap<String, String>(); |
||||
public Map<Integer, FooEnum> bindTarget = new HashMap<Integer, FooEnum>(); |
||||
|
||||
public static enum FooEnum { |
||||
BAR, BAZ; |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue