Browse Source
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@1523 50f2f4bb-b051-0410-bef5-90022cba6387pull/1/head
9 changed files with 445 additions and 43 deletions
@ -0,0 +1,57 @@
@@ -0,0 +1,57 @@
|
||||
/* |
||||
* Copyright 2004-2008 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(); |
||||
|
||||
private ConversionExecutor keyConverter; |
||||
|
||||
private ConversionExecutor valueConverter; |
||||
|
||||
public MapEntryConverter(ConversionExecutor keyConverter, ConversionExecutor valueConverter) { |
||||
this.keyConverter = keyConverter; |
||||
this.valueConverter = valueConverter; |
||||
} |
||||
|
||||
public Object convertKey(Object key) { |
||||
if (keyConverter != null) { |
||||
return keyConverter.execute(key); |
||||
} else { |
||||
return key; |
||||
} |
||||
} |
||||
|
||||
public Object convertValue(Object value) { |
||||
if (valueConverter != null) { |
||||
return valueConverter.execute(value); |
||||
} else { |
||||
return value; |
||||
} |
||||
} |
||||
|
||||
// internal
|
||||
|
||||
private MapEntryConverter() { |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,72 @@
@@ -0,0 +1,72 @@
|
||||
/* |
||||
* Copyright 2004-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 |
||||
*/ |
||||
@SuppressWarnings("unchecked") |
||||
class MapToStringArray implements ConversionExecutor { |
||||
|
||||
private TypeDescriptor targetType; |
||||
|
||||
private GenericTypeConverter conversionService; |
||||
|
||||
private MapEntryConverter entryConverter; |
||||
|
||||
public MapToStringArray(TypeDescriptor sourceType, TypeDescriptor targetType, GenericTypeConverter conversionService) { |
||||
this.targetType = targetType; |
||||
this.conversionService = conversionService; |
||||
this.entryConverter = createEntryConverter(); |
||||
} |
||||
|
||||
private MapEntryConverter createEntryConverter() { |
||||
if (targetType.isMapEntryTypeKnown()) { |
||||
ConversionExecutor keyConverter = conversionService.getConversionExecutor(targetType.getMapKeyType(), |
||||
TypeDescriptor.valueOf(String.class)); |
||||
ConversionExecutor valueConverter = conversionService.getConversionExecutor(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(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 = entryConverter.convertKey(key) + "=" + entryConverter.convertValue(value); |
||||
Array.set(array, i, property); |
||||
i++; |
||||
} |
||||
return array; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,47 @@
@@ -0,0 +1,47 @@
|
||||
/* |
||||
* Copyright 2004-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 |
||||
*/ |
||||
@SuppressWarnings("unchecked") |
||||
class MapToStringCollection implements ConversionExecutor { |
||||
|
||||
private MapToStringArray converter; |
||||
|
||||
private ArrayToCollection collectionConverter; |
||||
|
||||
public MapToStringCollection(TypeDescriptor sourceType, TypeDescriptor targetType, GenericTypeConverter conversionService) { |
||||
converter = new MapToStringArray(sourceType, targetType, conversionService); |
||||
collectionConverter = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), targetType, conversionService); |
||||
} |
||||
|
||||
public Object execute(Object source) throws ConversionFailedException { |
||||
Map map = (Map) source; |
||||
Object array = converter.execute(map); |
||||
return collectionConverter.execute(array); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,47 @@
@@ -0,0 +1,47 @@
|
||||
/* |
||||
* Copyright 2004-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. |
||||
* @author Keith Donald |
||||
* @since 3.0 |
||||
*/ |
||||
@SuppressWarnings("unchecked") |
||||
class ObjectToArray implements ConversionExecutor { |
||||
|
||||
private TypeDescriptor targetArrayType; |
||||
|
||||
private ConversionExecutor elementConverter; |
||||
|
||||
public ObjectToArray(TypeDescriptor sourceObjectType, TypeDescriptor targetArrayType, |
||||
GenericTypeConverter conversionService) { |
||||
this.targetArrayType = targetArrayType; |
||||
this.elementConverter = conversionService.getConversionExecutor(sourceObjectType.getType(), TypeDescriptor.valueOf(targetArrayType.getElementType())); |
||||
} |
||||
|
||||
public Object execute(Object source) throws ConversionFailedException { |
||||
Object array = Array.newInstance(targetArrayType.getType(), 1); |
||||
Array.set(array, 0, elementConverter.execute(source)); |
||||
return array; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,69 @@
@@ -0,0 +1,69 @@
|
||||
/* |
||||
* Copyright 2004-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 an object to a single-element collection. |
||||
* @author Keith Donald |
||||
* @since 3.0 |
||||
*/ |
||||
@SuppressWarnings("unchecked") |
||||
class ObjectToCollection implements ConversionExecutor { |
||||
|
||||
private TypeDescriptor sourceObjectType; |
||||
|
||||
private TypeDescriptor targetCollectionType; |
||||
|
||||
private GenericTypeConverter typeConverter; |
||||
|
||||
private ConversionExecutor elementConverter; |
||||
|
||||
public ObjectToCollection(TypeDescriptor sourceObjectType, TypeDescriptor targetCollectionType, |
||||
GenericTypeConverter typeConverter) { |
||||
this.sourceObjectType = sourceObjectType; |
||||
this.targetCollectionType = targetCollectionType; |
||||
this.typeConverter = typeConverter; |
||||
initElementConverter(); |
||||
} |
||||
|
||||
public Object execute(Object source) throws ConversionFailedException { |
||||
Class implClass = ConversionUtils.getCollectionImpl(targetCollectionType.getType()); |
||||
Collection collection; |
||||
try { |
||||
collection = (Collection) implClass.newInstance(); |
||||
} catch (InstantiationException e) { |
||||
throw new ConversionFailedException(source, sourceObjectType.getType(), targetCollectionType.getType(), e); |
||||
} catch (IllegalAccessException e) { |
||||
throw new ConversionFailedException(source, sourceObjectType.getType(), targetCollectionType.getType(), e); |
||||
} |
||||
collection.add(elementConverter.execute(source)); |
||||
return collection; |
||||
} |
||||
|
||||
private void initElementConverter() { |
||||
Class<?> elementType = targetCollectionType.getElementType(); |
||||
if (elementType != null) { |
||||
this.elementConverter = typeConverter.getConversionExecutor(sourceObjectType.getType(), TypeDescriptor.valueOf(elementType)); |
||||
} else { |
||||
this.elementConverter = NoOpConversionExecutor.INSTANCE; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,43 @@
@@ -0,0 +1,43 @@
|
||||
/* |
||||
* Copyright 2004-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, GenericTypeConverter conversionService) { |
||||
converter = new StringArrayToMap(sourceType, targetType, conversionService); |
||||
} |
||||
|
||||
public Object execute(Object source) throws ConversionFailedException { |
||||
Collection collection = (Collection) source; |
||||
return converter.execute(collection.toArray()); |
||||
} |
||||
|
||||
} |
||||
@ -1,40 +1,39 @@
@@ -1,40 +1,39 @@
|
||||
/* |
||||
* Copyright 2004-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.converter.Converter; |
||||
|
||||
/** |
||||
* A converter that does nothing. Access singleton at {@link #INSTANCE}. |
||||
* @author Keith Donald |
||||
*/ |
||||
@SuppressWarnings("unchecked") |
||||
class NoOpConverter implements Converter { |
||||
|
||||
public static final Converter INSTANCE = new NoOpConverter(); |
||||
|
||||
private NoOpConverter() { |
||||
|
||||
} |
||||
|
||||
public Object convert(Object source) throws Exception { |
||||
return source; |
||||
} |
||||
|
||||
public Object convertBack(Object target) throws Exception { |
||||
return target; |
||||
} |
||||
} |
||||
/* |
||||
* Copyright 2004-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.TypeDescriptor; |
||||
|
||||
/** |
||||
* Converts a comma-delimited string to an array. |
||||
* @author Keith Donald |
||||
* @since 3.0 |
||||
*/ |
||||
@SuppressWarnings("unchecked") |
||||
class StringToArray implements ConversionExecutor { |
||||
|
||||
private ArrayToArray converter; |
||||
|
||||
public StringToArray(TypeDescriptor sourceType, TypeDescriptor targetType, GenericTypeConverter conversionService) { |
||||
converter = new ArrayToArray(TypeDescriptor.valueOf(String[].class), targetType, conversionService); |
||||
} |
||||
|
||||
public Object execute(Object source) { |
||||
String string = (String) source; |
||||
String[] fields = string.split(","); |
||||
return converter.execute(fields); |
||||
} |
||||
} |
||||
@ -0,0 +1,41 @@
@@ -0,0 +1,41 @@
|
||||
/* |
||||
* Copyright 2004-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 ArrayToCollection converter; |
||||
|
||||
public StringToCollection(TypeDescriptor sourceType, TypeDescriptor targetType, GenericTypeConverter conversionService) { |
||||
converter = new ArrayToCollection(sourceType, targetType, conversionService); |
||||
} |
||||
|
||||
public Object execute(Object source) throws ConversionFailedException { |
||||
String string = (String) source; |
||||
String[] fields = string.split(","); |
||||
return converter.execute(fields); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue