general polishing
This commit is contained in:
@@ -25,16 +25,16 @@ package org.springframework.core.convert.converter;
|
||||
*/
|
||||
public interface ConverterRegistry {
|
||||
|
||||
/**
|
||||
* Add a generic converter to this registry.
|
||||
*/
|
||||
void addConverter(GenericConverter converter);
|
||||
|
||||
/**
|
||||
* Add a plain converter to this registry.
|
||||
*/
|
||||
void addConverter(Converter<?, ?> converter);
|
||||
|
||||
/**
|
||||
* Add a generic converter to this registry.
|
||||
*/
|
||||
void addConverter(GenericConverter converter);
|
||||
|
||||
/**
|
||||
* Add a ranged converter factory to this registry.
|
||||
*/
|
||||
|
||||
@@ -46,29 +46,39 @@ public abstract class ConversionServiceFactory {
|
||||
* Populate the given ConversionService instance with all applicable default converters.
|
||||
*/
|
||||
public static void addDefaultConverters(GenericConversionService conversionService) {
|
||||
conversionService.addConverter(new ArrayToArrayConverter(conversionService));
|
||||
conversionService.addConverter(new ArrayToCollectionConverter(conversionService));
|
||||
conversionService.addConverter(new ArrayToStringConverter(conversionService));
|
||||
conversionService.addConverter(new ArrayToObjectConverter(conversionService));
|
||||
conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
|
||||
conversionService.addConverter(new CollectionToArrayConverter(conversionService));
|
||||
conversionService.addConverter(new CollectionToStringConverter(conversionService));
|
||||
conversionService.addConverter(new CollectionToObjectConverter(conversionService));
|
||||
conversionService.addConverter(new MapToMapConverter(conversionService));
|
||||
conversionService.addConverter(new PropertiesToStringConverter(conversionService));
|
||||
|
||||
conversionService.addConverter(new ArrayToStringConverter(conversionService));
|
||||
conversionService.addConverter(new StringToArrayConverter(conversionService));
|
||||
conversionService.addConverter(new StringToCollectionConverter(conversionService));
|
||||
conversionService.addConverter(new StringToPropertiesConverter(conversionService));
|
||||
|
||||
conversionService.addConverter(new ArrayToObjectConverter(conversionService));
|
||||
conversionService.addConverter(new ObjectToArrayConverter(conversionService));
|
||||
|
||||
conversionService.addConverter(new CollectionToStringConverter(conversionService));
|
||||
conversionService.addConverter(new StringToCollectionConverter(conversionService));
|
||||
|
||||
conversionService.addConverter(new CollectionToObjectConverter(conversionService));
|
||||
conversionService.addConverter(new ObjectToCollectionConverter(conversionService));
|
||||
conversionService.addConverterFactory(new CharacterToNumberFactory());
|
||||
conversionService.addConverter(new NumberToCharacterConverter());
|
||||
conversionService.addConverterFactory(new NumberToNumberConverterFactory());
|
||||
|
||||
conversionService.addConverter(new ArrayToArrayConverter(conversionService));
|
||||
conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
|
||||
conversionService.addConverter(new MapToMapConverter(conversionService));
|
||||
|
||||
conversionService.addConverter(new PropertiesToStringConverter());
|
||||
conversionService.addConverter(new StringToPropertiesConverter());
|
||||
|
||||
conversionService.addConverter(new StringToBooleanConverter());
|
||||
conversionService.addConverter(new StringToCharacterConverter());
|
||||
conversionService.addConverterFactory(new StringToEnumConverterFactory());
|
||||
conversionService.addConverter(new StringToCharacterConverter());
|
||||
conversionService.addConverter(new StringToLocaleConverter());
|
||||
conversionService.addConverterFactory(new StringToNumberConverterFactory());
|
||||
conversionService.addConverterFactory(new StringToEnumConverterFactory());
|
||||
|
||||
conversionService.addConverter(new NumberToCharacterConverter());
|
||||
conversionService.addConverterFactory(new CharacterToNumberFactory());
|
||||
|
||||
conversionService.addConverterFactory(new NumberToNumberConverterFactory());
|
||||
|
||||
conversionService.addConverter(new ObjectToStringConverter());
|
||||
conversionService.addConverter(new ObjectToObjectConverter());
|
||||
conversionService.addConverter(new IdToEntityConverter(conversionService));
|
||||
|
||||
@@ -18,93 +18,28 @@ package org.springframework.core.convert.support;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.converter.ConditionalGenericConverter;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
|
||||
/**
|
||||
* Converts from a Map to a String by storing each source Map entry into the String as a property (name=value pair).
|
||||
* Converts from a Properties to a String by calling {@link Properties#store(java.io.OutputStream, String)}.
|
||||
* Decodes with the ISO-8859-1 charset before returning the String.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
* @see Properties#store(java.io.OutputStream, String)
|
||||
*/
|
||||
final class PropertiesToStringConverter implements ConditionalGenericConverter {
|
||||
final class PropertiesToStringConverter implements Converter<Properties, String> {
|
||||
|
||||
private final GenericConversionService conversionService;
|
||||
|
||||
public PropertiesToStringConverter(GenericConversionService conversionService) {
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
|
||||
public Set<ConvertiblePair> getConvertibleTypes() {
|
||||
return Collections.singleton(new ConvertiblePair(Properties.class, String.class));
|
||||
}
|
||||
|
||||
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
return this.conversionService.canConvert(sourceType.getMapKeyTypeDescriptor(), targetType)
|
||||
&& this.conversionService.canConvert(sourceType.getMapValueTypeDescriptor(), targetType);
|
||||
}
|
||||
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
if (source == null) {
|
||||
return this.conversionService.convertNullSource(sourceType, targetType);
|
||||
}
|
||||
Map<?, ?> sourceMap = (Map<?, ?>) source;
|
||||
if (sourceMap.size() == 0) {
|
||||
return "";
|
||||
}
|
||||
else {
|
||||
TypeDescriptor sourceKeyType = sourceType.getMapKeyTypeDescriptor();
|
||||
TypeDescriptor sourceValueType = sourceType.getMapValueTypeDescriptor();
|
||||
if (sourceKeyType == TypeDescriptor.NULL || sourceValueType == TypeDescriptor.NULL) {
|
||||
TypeDescriptor[] sourceEntryTypes = ConversionUtils.getMapEntryTypes(sourceMap);
|
||||
sourceKeyType = sourceEntryTypes[0];
|
||||
sourceValueType = sourceEntryTypes[1];
|
||||
}
|
||||
boolean keysCompatible = false;
|
||||
if (sourceKeyType != TypeDescriptor.NULL && sourceKeyType.isAssignableTo(targetType)) {
|
||||
keysCompatible = true;
|
||||
}
|
||||
boolean valuesCompatible = false;
|
||||
if (sourceValueType != TypeDescriptor.NULL && sourceValueType.isAssignableTo(targetType)) {
|
||||
valuesCompatible = true;
|
||||
}
|
||||
Properties props = new Properties();
|
||||
if (keysCompatible && valuesCompatible) {
|
||||
for (Object entry : sourceMap.entrySet()) {
|
||||
Map.Entry<?, ?> mapEntry = (Map.Entry<?, ?>) entry;
|
||||
props.setProperty((String) mapEntry.getKey(), (String) mapEntry.getValue());
|
||||
}
|
||||
return store(props);
|
||||
}
|
||||
else {
|
||||
MapEntryConverter converter = new MapEntryConverter(sourceKeyType, sourceValueType, targetType,
|
||||
targetType, keysCompatible, valuesCompatible, this.conversionService);
|
||||
for (Object entry : sourceMap.entrySet()) {
|
||||
Map.Entry<?, ?> mapEntry = (Map.Entry<?, ?>) entry;
|
||||
Object key = converter.convertKey(mapEntry.getKey());
|
||||
Object value = converter.convertValue(mapEntry.getValue());
|
||||
props.setProperty((String) key, (String) value);
|
||||
}
|
||||
return store(props);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String store(Properties props) {
|
||||
public String convert(Properties source) {
|
||||
try {
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
props.store(os, null);
|
||||
source.store(os, null);
|
||||
return os.toString("ISO-8859-1");
|
||||
}
|
||||
catch (IOException ex) {
|
||||
// Should never happen.
|
||||
throw new IllegalArgumentException("Failed to store [" + props + "] into String", ex);
|
||||
throw new IllegalArgumentException("Failed to store [" + source + "] into String", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,55 +17,29 @@
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.util.Collections;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.converter.ConditionalGenericConverter;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
|
||||
/**
|
||||
* Converts a String to a Map by loading the list of properties (name=value pairs) encoded in the String.
|
||||
*
|
||||
* Converts a String to a Properties by calling Properties#load(java.io.InputStream).
|
||||
* Uses ISO-8559-1 encoding required by Properties.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
* @see Properties#load(java.io.InputStream)
|
||||
*/
|
||||
final class StringToPropertiesConverter implements ConditionalGenericConverter {
|
||||
final class StringToPropertiesConverter implements Converter<String, Properties> {
|
||||
|
||||
private final GenericConversionService conversionService;
|
||||
|
||||
public StringToPropertiesConverter(GenericConversionService conversionService) {
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
|
||||
public Set<ConvertiblePair> getConvertibleTypes() {
|
||||
return Collections.singleton(new ConvertiblePair(String.class, Properties.class));
|
||||
}
|
||||
|
||||
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
return this.conversionService.canConvert(sourceType, targetType.getMapKeyTypeDescriptor()) &&
|
||||
this.conversionService.canConvert(sourceType, targetType.getMapValueTypeDescriptor());
|
||||
}
|
||||
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
if (source == null) {
|
||||
return this.conversionService.convertNullSource(sourceType, targetType);
|
||||
}
|
||||
String string = (String) source;
|
||||
return this.conversionService.convert(loadProperties(string), TypeDescriptor.valueOf(Properties.class), targetType);
|
||||
}
|
||||
|
||||
private Properties loadProperties(String string) {
|
||||
public Properties convert(String source) {
|
||||
try {
|
||||
Properties props = new Properties();
|
||||
// Must use the ISO-8859-1 encoding because Properties.load(stream) expects it.
|
||||
props.load(new ByteArrayInputStream(string.getBytes("ISO-8859-1")));
|
||||
props.load(new ByteArrayInputStream(source.getBytes("ISO-8859-1")));
|
||||
return props;
|
||||
}
|
||||
catch (Exception ex) {
|
||||
// Should never happen.
|
||||
throw new IllegalArgumentException("Failed to parse [" + string + "] into Properties", ex);
|
||||
throw new IllegalArgumentException("Failed to parse [" + source + "] into Properties", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user