001/*
002 * Copyright 2002-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.format.support;
018
019import java.util.Set;
020
021import org.springframework.beans.factory.FactoryBean;
022import org.springframework.beans.factory.InitializingBean;
023import org.springframework.context.EmbeddedValueResolverAware;
024import org.springframework.core.convert.support.ConversionServiceFactory;
025import org.springframework.format.AnnotationFormatterFactory;
026import org.springframework.format.Formatter;
027import org.springframework.format.FormatterRegistrar;
028import org.springframework.format.FormatterRegistry;
029import org.springframework.format.Parser;
030import org.springframework.format.Printer;
031import org.springframework.util.StringValueResolver;
032
033/**
034 * A factory providing convenient access to a {@code FormattingConversionService}
035 * configured with converters and formatters for common types such as numbers and
036 * datetimes.
037 *
038 * <p>Additional converters and formatters can be registered declaratively through
039 * {@link #setConverters(Set)} and {@link #setFormatters(Set)}. Another option
040 * is to register converters and formatters in code by implementing the
041 * {@link FormatterRegistrar} interface. You can then configure provide the set
042 * of registrars to use through {@link #setFormatterRegistrars(Set)}.
043 *
044 * <p>A good example for registering converters and formatters in code is
045 * {@code JodaTimeFormatterRegistrar}, which registers a number of
046 * date-related formatters and converters. For a more detailed list of cases
047 * see {@link #setFormatterRegistrars(Set)}
048 *
049 * <p>Like all {@code FactoryBean} implementations, this class is suitable for
050 * use when configuring a Spring application context using Spring {@code <beans>}
051 * XML. When configuring the container with
052 * {@link org.springframework.context.annotation.Configuration @Configuration}
053 * classes, simply instantiate, configure and return the appropriate
054 * {@code FormattingConversionService} object from a
055 * {@link org.springframework.context.annotation.Bean @Bean} method.
056 *
057 * @author Keith Donald
058 * @author Juergen Hoeller
059 * @author Rossen Stoyanchev
060 * @author Chris Beams
061 * @since 3.0
062 */
063public class FormattingConversionServiceFactoryBean
064                implements FactoryBean<FormattingConversionService>, EmbeddedValueResolverAware, InitializingBean {
065
066        private Set<?> converters;
067
068        private Set<?> formatters;
069
070        private Set<FormatterRegistrar> formatterRegistrars;
071
072        private boolean registerDefaultFormatters = true;
073
074        private StringValueResolver embeddedValueResolver;
075
076        private FormattingConversionService conversionService;
077
078
079        /**
080         * Configure the set of custom converter objects that should be added.
081         * @param converters instances of any of the following:
082         * {@link org.springframework.core.convert.converter.Converter},
083         * {@link org.springframework.core.convert.converter.ConverterFactory},
084         * {@link org.springframework.core.convert.converter.GenericConverter}
085         */
086        public void setConverters(Set<?> converters) {
087                this.converters = converters;
088        }
089
090        /**
091         * Configure the set of custom formatter objects that should be added.
092         * @param formatters instances of {@link Formatter} or {@link AnnotationFormatterFactory}
093         */
094        public void setFormatters(Set<?> formatters) {
095                this.formatters = formatters;
096        }
097
098        /**
099         * <p>Configure the set of FormatterRegistrars to invoke to register
100         * Converters and Formatters in addition to those added declaratively
101         * via {@link #setConverters(Set)} and {@link #setFormatters(Set)}.
102         * <p>FormatterRegistrars are useful when registering multiple related
103         * converters and formatters for a formatting category, such as Date
104         * formatting. All types related needed to support the formatting
105         * category can be registered from one place.
106         * <p>FormatterRegistrars can also be used to register Formatters
107         * indexed under a specific field type different from its own &lt;T&gt;,
108         * or when registering a Formatter from a Printer/Parser pair.
109         * @see FormatterRegistry#addFormatterForFieldType(Class, Formatter)
110         * @see FormatterRegistry#addFormatterForFieldType(Class, Printer, Parser)
111         */
112        public void setFormatterRegistrars(Set<FormatterRegistrar> formatterRegistrars) {
113                this.formatterRegistrars = formatterRegistrars;
114        }
115
116        /**
117         * Indicate whether default formatters should be registered or not.
118         * <p>By default, built-in formatters are registered. This flag can be used
119         * to turn that off and rely on explicitly registered formatters only.
120         * @see #setFormatters(Set)
121         * @see #setFormatterRegistrars(Set)
122         */
123        public void setRegisterDefaultFormatters(boolean registerDefaultFormatters) {
124                this.registerDefaultFormatters = registerDefaultFormatters;
125        }
126
127        @Override
128        public void setEmbeddedValueResolver(StringValueResolver embeddedValueResolver) {
129                this.embeddedValueResolver = embeddedValueResolver;
130        }
131
132
133        @Override
134        public void afterPropertiesSet() {
135                this.conversionService = new DefaultFormattingConversionService(this.embeddedValueResolver, this.registerDefaultFormatters);
136                ConversionServiceFactory.registerConverters(this.converters, this.conversionService);
137                registerFormatters();
138        }
139
140        private void registerFormatters() {
141                if (this.formatters != null) {
142                        for (Object formatter : this.formatters) {
143                                if (formatter instanceof Formatter<?>) {
144                                        this.conversionService.addFormatter((Formatter<?>) formatter);
145                                }
146                                else if (formatter instanceof AnnotationFormatterFactory<?>) {
147                                        this.conversionService.addFormatterForFieldAnnotation((AnnotationFormatterFactory<?>) formatter);
148                                }
149                                else {
150                                        throw new IllegalArgumentException(
151                                                        "Custom formatters must be implementations of Formatter or AnnotationFormatterFactory");
152                                }
153                        }
154                }
155                if (this.formatterRegistrars != null) {
156                        for (FormatterRegistrar registrar : this.formatterRegistrars) {
157                                registrar.registerFormatters(this.conversionService);
158                        }
159                }
160        }
161
162
163        @Override
164        public FormattingConversionService getObject() {
165                return this.conversionService;
166        }
167
168        @Override
169        public Class<? extends FormattingConversionService> getObjectType() {
170                return FormattingConversionService.class;
171        }
172
173        @Override
174        public boolean isSingleton() {
175                return true;
176        }
177
178}