Browse Source
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@1965 50f2f4bb-b051-0410-bef5-90022cba6387pull/1/head
6 changed files with 230 additions and 63 deletions
@ -0,0 +1,62 @@
@@ -0,0 +1,62 @@
|
||||
/** |
||||
* |
||||
*/ |
||||
package org.springframework.core.convert.support; |
||||
|
||||
import static org.springframework.core.convert.support.ConversionUtils.invokeConverter; |
||||
|
||||
import org.springframework.core.convert.ConverterNotFoundException; |
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
|
||||
class MapEntryConverter { |
||||
|
||||
private GenericConverter keyConverter; |
||||
|
||||
private GenericConverter valueConverter; |
||||
|
||||
private TypeDescriptor sourceKeyType; |
||||
|
||||
private TypeDescriptor sourceValueType; |
||||
|
||||
private TypeDescriptor targetKeyType; |
||||
|
||||
private TypeDescriptor targetValueType; |
||||
|
||||
public MapEntryConverter(TypeDescriptor sourceKeyType, TypeDescriptor sourceValueType, TypeDescriptor targetKeyType, |
||||
TypeDescriptor targetValueType, boolean keysCompatible, boolean valuesCompatible, |
||||
GenericConversionService conversionService) { |
||||
if (sourceKeyType != TypeDescriptor.NULL && targetKeyType != TypeDescriptor.NULL && !keysCompatible) { |
||||
this.keyConverter = conversionService.getConverter(sourceKeyType, targetKeyType); |
||||
if (this.keyConverter == null) { |
||||
throw new ConverterNotFoundException(sourceKeyType, targetKeyType); |
||||
} |
||||
this.sourceKeyType = sourceKeyType; |
||||
this.targetKeyType = targetKeyType; |
||||
} |
||||
if (sourceValueType != TypeDescriptor.NULL && targetValueType != TypeDescriptor.NULL && !valuesCompatible) { |
||||
this.valueConverter = conversionService.getConverter(sourceValueType, targetValueType); |
||||
if (this.valueConverter == null) { |
||||
throw new ConverterNotFoundException(sourceValueType, targetValueType); |
||||
} |
||||
this.targetKeyType = targetKeyType; |
||||
this.targetValueType = targetValueType; |
||||
} |
||||
} |
||||
|
||||
public Object convertKey(Object sourceKey) { |
||||
if (sourceKey != null && this.keyConverter != null) { |
||||
return invokeConverter(this.keyConverter, sourceKey, this.sourceKeyType, this.targetKeyType); |
||||
} else { |
||||
return sourceKey; |
||||
} |
||||
} |
||||
|
||||
public Object convertValue(Object sourceValue) { |
||||
if (sourceValue != null && this.valueConverter != null) { |
||||
return invokeConverter(this.valueConverter, sourceValue, this.sourceValueType, this.targetValueType); |
||||
} else { |
||||
return sourceValue; |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,84 @@
@@ -0,0 +1,84 @@
|
||||
/* |
||||
* 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.TypeDescriptor; |
||||
|
||||
final class StringArrayToMapGenericConverter implements GenericConverter { |
||||
|
||||
private final GenericConversionService conversionService; |
||||
|
||||
public StringArrayToMapGenericConverter(GenericConversionService conversionService) { |
||||
this.conversionService = conversionService; |
||||
} |
||||
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { |
||||
TypeDescriptor targetKeyType = targetType.getMapKeyTypeDescriptor(); |
||||
TypeDescriptor targetValueType = targetType.getMapValueTypeDescriptor(); |
||||
if (targetKeyType == TypeDescriptor.NULL && targetValueType == TypeDescriptor.NULL) { |
||||
return mapWithoutConversion(source, targetType); |
||||
} |
||||
TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor(); |
||||
boolean keysCompatible = false; |
||||
if (sourceElementType.isAssignableTo(targetKeyType)) { |
||||
keysCompatible = true; |
||||
} |
||||
boolean valuesCompatible = false; |
||||
if (sourceElementType.isAssignableTo(targetValueType)) { |
||||
valuesCompatible = true; |
||||
} |
||||
if (keysCompatible && valuesCompatible) { |
||||
return mapWithoutConversion(source, targetType); |
||||
} |
||||
int length = Array.getLength(source); |
||||
Map target = CollectionFactory.createMap(targetType.getType(), length); |
||||
MapEntryConverter converter = new MapEntryConverter(sourceElementType, sourceElementType, targetKeyType, targetValueType, keysCompatible, valuesCompatible, conversionService); |
||||
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 |
||||
+ "'; properties should be in the format name=value"); |
||||
} |
||||
Object targetKey = converter.convertKey(fields[0]); |
||||
Object targetValue = converter.convertValue(fields[1]); |
||||
target.put(targetKey, targetValue); |
||||
} |
||||
return target; |
||||
} |
||||
|
||||
private Map mapWithoutConversion(Object source, TypeDescriptor targetType) { |
||||
int length = Array.getLength(source); |
||||
Map target = CollectionFactory.createMap(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 |
||||
+ "'; properties should be in the format name=value"); |
||||
} |
||||
String key = fields[0]; |
||||
String value = fields[1]; |
||||
target.put(key, value); |
||||
} |
||||
return target; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,34 @@
@@ -0,0 +1,34 @@
|
||||
/* |
||||
* 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.TypeDescriptor; |
||||
|
||||
final class StringToMapGenericConverter implements GenericConverter { |
||||
|
||||
private final StringArrayToMapGenericConverter converter; |
||||
|
||||
public StringToMapGenericConverter(GenericConversionService conversionService) { |
||||
this.converter = new StringArrayToMapGenericConverter(conversionService); |
||||
} |
||||
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { |
||||
String string = (String) source; |
||||
String[] properties = string.split(" "); |
||||
return this.converter.convert(properties, TypeDescriptor.valueOf(String[].class), targetType); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue