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