diff --git a/org.springframework.context/src/main/java/org/springframework/format/support/FormattingConversionService.java b/org.springframework.context/src/main/java/org/springframework/format/support/FormattingConversionService.java index a75348d009b..1467c382a43 100644 --- a/org.springframework.context/src/main/java/org/springframework/format/support/FormattingConversionService.java +++ b/org.springframework.context/src/main/java/org/springframework/format/support/FormattingConversionService.java @@ -36,6 +36,7 @@ import org.springframework.format.Formatter; import org.springframework.format.FormatterRegistry; import org.springframework.format.Parser; import org.springframework.format.Printer; +import org.springframework.util.StringUtils; import org.springframework.util.StringValueResolver; /** @@ -63,6 +64,15 @@ public class FormattingConversionService extends GenericConversionService } + public void addFormatter(Formatter formatter) { + Class fieldType = GenericTypeResolver.resolveTypeArgument(formatter.getClass(), Formatter.class); + if (fieldType == null) { + throw new IllegalArgumentException("Unable to extract parameterized field type argument from Formatter [" + + formatter.getClass().getName() + "]; does the formatter parameterize the generic type?"); + } + addFormatterForFieldType(fieldType, formatter); + } + public void addFormatterForFieldType(Class fieldType, Formatter formatter) { addConverter(new PrinterConverter(fieldType, formatter, this)); addConverter(new ParserConverter(fieldType, formatter, this)); @@ -228,7 +238,7 @@ public class FormattingConversionService extends GenericConversionService public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { String text = (String) source; - if (text == null || text.length() == 0) { + if (!StringUtils.hasText(text)) { return null; } Object result; diff --git a/org.springframework.context/src/main/java/org/springframework/format/support/FormattingConversionServiceFactoryBean.java b/org.springframework.context/src/main/java/org/springframework/format/support/FormattingConversionServiceFactoryBean.java index 8c40588422f..3e9c798a632 100644 --- a/org.springframework.context/src/main/java/org/springframework/format/support/FormattingConversionServiceFactoryBean.java +++ b/org.springframework.context/src/main/java/org/springframework/format/support/FormattingConversionServiceFactoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 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. @@ -27,6 +27,7 @@ import org.springframework.beans.factory.InitializingBean; import org.springframework.context.EmbeddedValueResolverAware; import org.springframework.core.convert.support.ConversionServiceFactory; import org.springframework.format.AnnotationFormatterFactory; +import org.springframework.format.Formatter; import org.springframework.format.FormatterRegistry; import org.springframework.format.Parser; import org.springframework.format.Printer; @@ -55,35 +56,77 @@ public class FormattingConversionServiceFactoryBean private Set converters; + private Set formatters; + private StringValueResolver embeddedValueResolver; private FormattingConversionService conversionService; /** - * Configure the set of custom converter objects that should be added: - * implementing {@link org.springframework.core.convert.converter.Converter}, + * Configure the set of custom converter objects that should be added. + * @param converters instances of any of the following: + * {@link org.springframework.core.convert.converter.Converter}, * {@link org.springframework.core.convert.converter.ConverterFactory}, - * or {@link org.springframework.core.convert.converter.GenericConverter}. + * {@link org.springframework.core.convert.converter.GenericConverter} */ public void setConverters(Set converters) { this.converters = converters; } + /** + * Configure the set of custom formatter objects that should be added. + * @param formatters instances of {@link Formatter} or {@link AnnotationFormatterFactory} + */ + public void setFormatters(Set formatters) { + this.formatters = formatters; + } + public void setEmbeddedValueResolver(StringValueResolver embeddedValueResolver) { this.embeddedValueResolver = embeddedValueResolver; } + public void afterPropertiesSet() { this.conversionService = new FormattingConversionService(); this.conversionService.setEmbeddedValueResolver(this.embeddedValueResolver); ConversionServiceFactory.addDefaultConverters(this.conversionService); ConversionServiceFactory.registerConverters(this.converters, this.conversionService); + registerFormatters(); + } + + private void registerFormatters() { + if (this.formatters != null) { + for (Object formatter : this.formatters) { + if (formatter instanceof Formatter) { + this.conversionService.addFormatter((Formatter) formatter); + } + else if (formatter instanceof AnnotationFormatterFactory) { + this.conversionService.addFormatterForFieldAnnotation((AnnotationFormatterFactory) formatter); + } + else { + throw new IllegalArgumentException( + "Custom formatters must be implementations of Formatter or AnnotationFormatterFactory"); + } + } + } installFormatters(this.conversionService); } + /** + * Install Formatters and Converters into the new FormattingConversionService using the FormatterRegistry SPI. + * Subclasses may override to customize the set of formatters and/or converters that are installed. + */ + protected void installFormatters(FormatterRegistry registry) { + registry.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory()); + if (jodaTimePresent) { + new JodaTimeFormattingConfigurer().installJodaTimeFormatting(registry); + } + else { + registry.addFormatterForFieldAnnotation(new NoJodaDateTimeFormatAnnotationFormatterFactory()); + } + } - // implementing FactoryBean public FormattingConversionService getObject() { return this.conversionService; @@ -98,23 +141,6 @@ public class FormattingConversionServiceFactoryBean } - // subclassing hooks - - /** - * Install Formatters and Converters into the new FormattingConversionService using the FormatterRegistry SPI. - * Subclasses may override to customize the set of formatters and/or converters that are installed. - */ - protected void installFormatters(FormatterRegistry registry) { - registry.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory()); - if (jodaTimePresent) { - new JodaTimeFormattingConfigurer().installJodaTimeFormatting(registry); - } - else { - registry.addFormatterForFieldAnnotation(new NoJodaDateTimeFormatAnnotationFormatterFactory()); - } - } - - /** * Dummy AnnotationFormatterFactory that simply fails if @DateTimeFormat is being used * without the JodaTime library being present. diff --git a/org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceFactoryBeanTests.java b/org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceFactoryBeanTests.java new file mode 100644 index 00000000000..f1b79fec4ae --- /dev/null +++ b/org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceFactoryBeanTests.java @@ -0,0 +1,161 @@ +/* + * Copyright 2002-2011 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.format.support; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.text.ParseException; +import java.util.HashSet; +import java.util.Locale; +import java.util.Set; + +import org.junit.Test; + +import org.springframework.core.convert.TypeDescriptor; +import org.springframework.format.AnnotationFormatterFactory; +import org.springframework.format.Formatter; +import org.springframework.format.Parser; +import org.springframework.format.Printer; +import org.springframework.format.annotation.NumberFormat; +import org.springframework.format.annotation.NumberFormat.Style; + +import static org.junit.Assert.*; + +/** + * @author Rossen Stoyanchev + */ +public class FormattingConversionServiceFactoryBeanTests { + + @Test + public void testDefaultFormatters() throws Exception { + FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean(); + factory.afterPropertiesSet(); + FormattingConversionService fcs = factory.getObject(); + TypeDescriptor descriptor = new TypeDescriptor(TestBean.class.getDeclaredField("percent")); + Object value = fcs.convert("5%", TypeDescriptor.valueOf(String.class), descriptor); + assertEquals(.05, value); + value = fcs.convert(.05, descriptor, TypeDescriptor.valueOf(String.class)); + assertEquals("5%", value); + } + + @Test + public void testCustomFormatter() throws Exception { + FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean(); + Set formatters = new HashSet(); + formatters.add(new TestBeanFormatter()); + formatters.add(new SpecialIntAnnotationFormatterFactory()); + factory.setFormatters(formatters); + factory.afterPropertiesSet(); + FormattingConversionService fcs = factory.getObject(); + + TestBean testBean = fcs.convert("5", TestBean.class); + assertEquals(5, testBean.getSpecialInt()); + assertEquals("5", fcs.convert(testBean, String.class)); + + TypeDescriptor descriptor = new TypeDescriptor(TestBean.class.getDeclaredField("specialInt")); + Object value = fcs.convert(":5", TypeDescriptor.valueOf(String.class), descriptor); + assertEquals(5, value); + value = fcs.convert(5, descriptor, TypeDescriptor.valueOf(String.class)); + assertEquals(":5", value); + } + + @Test + public void testInvalidFormatter() throws Exception { + FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean(); + Set formatters = new HashSet(); + formatters.add(new Object()); + factory.setFormatters(formatters); + try { + factory.afterPropertiesSet(); + fail("Expected formatter to be rejected"); + } + catch (IllegalArgumentException ex) { + // expected + } + } + + + @Target({ ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER }) + @Retention(RetentionPolicy.RUNTIME) + private @interface SpecialInt { + } + + private static class TestBean { + + @SuppressWarnings("unused") + @NumberFormat(style = Style.PERCENT) + private double percent; + + @SpecialInt + private int specialInt; + + public int getSpecialInt() { + return specialInt; + } + + public void setSpecialInt(int field) { + this.specialInt = field; + } + + } + + private static class TestBeanFormatter implements Formatter { + + public String print(TestBean object, Locale locale) { + return String.valueOf(object.getSpecialInt()); + } + + public TestBean parse(String text, Locale locale) throws ParseException { + TestBean object = new TestBean(); + object.setSpecialInt(Integer.parseInt(text)); + return object; + } + + } + + private static class SpecialIntAnnotationFormatterFactory implements AnnotationFormatterFactory { + + private final Set> fieldTypes = new HashSet>(1); + + public SpecialIntAnnotationFormatterFactory() { + fieldTypes.add(Integer.class); + } + + public Set> getFieldTypes() { + return fieldTypes; + } + + public Printer getPrinter(SpecialInt annotation, Class fieldType) { + return new Printer() { + public String print(Integer object, Locale locale) { + return ":" + object.toString(); + } + }; + } + + public Parser getParser(SpecialInt annotation, Class fieldType) { + return new Parser() { + public Integer parse(String text, Locale locale) throws ParseException { + return Integer.parseInt(text.substring(1)); + } + }; + } + } + +}