activated DefaultConversionService in EL, linking convert and EL

This commit is contained in:
Keith Donald
2009-04-10 20:47:04 +00:00
parent 8b52b7eeef
commit 1d22b9fb88
7 changed files with 102 additions and 76 deletions

View File

@@ -0,0 +1,49 @@
/*
* 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.converter;
import org.springframework.util.NumberUtils;
/**
* Converts from any JDK-standard Number implementation to a Character and back.
*
* Support Number classes including Byte, Short, Integer, Float, Double, Long, BigInteger, BigDecimal. This class
* delegates to {@link NumberUtils#convertNumberToTargetClass(Number, Class)} to perform the conversion.
*
* @see java.lang.Byte
* @see java.lang.Short
* @see java.lang.Integer
* @see java.lang.Long
* @see java.math.BigInteger
* @see java.lang.Float
* @see java.lang.Double
* @see java.math.BigDecimal
* @see NumberUtils
*
* @author Keith Donald
*/
public class NumberToCharacter implements SuperTwoWayConverter<Number, Character> {
@SuppressWarnings("unchecked")
public <RT extends Character> RT convert(Number source, Class<RT> targetClass) {
return (RT) Character.valueOf((char) source.shortValue());
}
public <RS extends Number> RS convertBack(Character target, Class<RS> sourceClass) {
return NumberUtils.convertNumberToTargetClass((short) target.charValue(), sourceClass);
}
}

View File

@@ -0,0 +1,17 @@
package org.springframework.core.convert.converter;
import org.springframework.core.convert.service.DefaultConversionService;
/**
* Simply calls {@link Object#toString()} to convert any object to a string.
* Used by the {@link DefaultConversionService} as a fallback if there are no other explicit to string converters registered.
* @author Keith Donald
*/
public class ObjectToString implements SuperConverter<Object, String> {
@SuppressWarnings("unchecked")
public <RT extends String> RT convert(Object source, Class<RT> targetClass) {
return (RT) source.toString();
}
}

View File

@@ -20,7 +20,9 @@ import java.math.BigInteger;
import java.util.Date;
import java.util.Locale;
import org.springframework.core.convert.converter.NumberToCharacter;
import org.springframework.core.convert.converter.NumberToNumber;
import org.springframework.core.convert.converter.ObjectToString;
import org.springframework.core.convert.converter.StringToBigDecimal;
import org.springframework.core.convert.converter.StringToBigInteger;
import org.springframework.core.convert.converter.StringToBoolean;
@@ -67,6 +69,8 @@ public class DefaultConversionService extends GenericConversionService {
addConverter(new StringToLocale());
addConverter(new StringToEnum());
addConverter(new NumberToNumber());
addConverter(new NumberToCharacter());
addConverter(new ObjectToString());
}
protected void addDefaultAliases() {

View File

@@ -193,21 +193,21 @@ public class GenericConversionService implements ConversionService {
if (sourceType.isCollection()) {
return new CollectionToArray(sourceType, targetType, this);
} else {
throw new UnsupportedOperationException("Object to Array conversion not yet supported");
throw new ConversionExecutorNotFoundException(sourceType, targetType, "Object to Array conversion not yet supported");
}
}
if (sourceType.isCollection()) {
if (targetType.isCollection()) {
return new CollectionToCollection(sourceType, targetType, this);
} else {
throw new UnsupportedOperationException("Object to Collection conversion not yet supported");
throw new ConversionExecutorNotFoundException(sourceType, targetType, "Object to Collection conversion not yet supported");
}
}
if (sourceType.isMap()) {
if (targetType.isMap()) {
return new MapToMap(sourceType, targetType, this);
} else {
throw new UnsupportedOperationException("Object to Map conversion not yet supported");
throw new ConversionExecutorNotFoundException(sourceType, targetType, "Object to Map conversion not yet supported");
}
}
Converter converter = findRegisteredConverter(sourceClass, targetType.getType());

View File

@@ -8,7 +8,9 @@ import java.math.BigInteger;
import java.util.Locale;
import org.junit.Test;
import org.springframework.core.convert.converter.NumberToCharacter;
import org.springframework.core.convert.converter.NumberToNumber;
import org.springframework.core.convert.converter.ObjectToString;
import org.springframework.core.convert.converter.StringToBigDecimal;
import org.springframework.core.convert.converter.StringToBigInteger;
import org.springframework.core.convert.converter.StringToBoolean;
@@ -154,6 +156,19 @@ public class DefaultConverterTests {
}
}
@Test
public void testNumberToCharacter() {
NumberToCharacter n = new NumberToCharacter();
assertEquals(Character.valueOf('A'), n.convert(Integer.valueOf(65), Character.class));
assertEquals(Integer.valueOf(65), n.convertBack(Character.valueOf('A'), Integer.class));
}
@Test
public void testObjectToString() {
ObjectToString o = new ObjectToString();
assertEquals("3", o.convert(3, String.class));
}
public static class CustomNumber extends Number {
@Override