forked from spring-projects/spring-framework
/
DefaultFormattingConversionService.java
135 lines (117 loc) · 5.58 KB
/
DefaultFormattingConversionService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright 2002-2020 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
*
* https://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 org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.DateFormatterRegistrar;
import org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar;
import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;
import org.springframework.format.number.NumberFormatAnnotationFormatterFactory;
import org.springframework.format.number.money.CurrencyUnitFormatter;
import org.springframework.format.number.money.Jsr354NumberFormatAnnotationFormatterFactory;
import org.springframework.format.number.money.MonetaryAmountFormatter;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringValueResolver;
/**
* A specialization of {@link FormattingConversionService} configured by default with
* converters and formatters appropriate for most applications.
*
* <p>Designed for direct instantiation but also exposes the static {@link #addDefaultFormatters}
* utility method for ad hoc use against any {@code FormatterRegistry} instance, just
* as {@code DefaultConversionService} exposes its own
* {@link DefaultConversionService#addDefaultConverters addDefaultConverters} method.
*
* <p>Automatically registers formatters for JSR-354 Money & Currency, JSR-310 Date-Time
* and/or Joda-Time 2.x, depending on the presence of the corresponding API on the classpath.
*
* @author Chris Beams
* @author Juergen Hoeller
* @since 3.1
*/
public class DefaultFormattingConversionService extends FormattingConversionService {
private static final boolean jsr354Present;
private static final boolean jodaTimePresent;
static {
ClassLoader classLoader = DefaultFormattingConversionService.class.getClassLoader();
jsr354Present = ClassUtils.isPresent("javax.money.MonetaryAmount", classLoader);
jodaTimePresent = ClassUtils.isPresent("org.joda.time.YearMonth", classLoader);
}
/**
* Create a new {@code DefaultFormattingConversionService} with the set of
* {@linkplain DefaultConversionService#addDefaultConverters default converters} and
* {@linkplain #addDefaultFormatters default formatters}.
*/
public DefaultFormattingConversionService() {
this(null, true);
}
/**
* Create a new {@code DefaultFormattingConversionService} with the set of
* {@linkplain DefaultConversionService#addDefaultConverters default converters} and,
* based on the value of {@code registerDefaultFormatters}, the set of
* {@linkplain #addDefaultFormatters default formatters}.
* @param registerDefaultFormatters whether to register default formatters
*/
public DefaultFormattingConversionService(boolean registerDefaultFormatters) {
this(null, registerDefaultFormatters);
}
/**
* Create a new {@code DefaultFormattingConversionService} with the set of
* {@linkplain DefaultConversionService#addDefaultConverters default converters} and,
* based on the value of {@code registerDefaultFormatters}, the set of
* {@linkplain #addDefaultFormatters default formatters}.
* @param embeddedValueResolver delegated to {@link #setEmbeddedValueResolver(StringValueResolver)}
* prior to calling {@link #addDefaultFormatters}.
* @param registerDefaultFormatters whether to register default formatters
*/
public DefaultFormattingConversionService(
@Nullable StringValueResolver embeddedValueResolver, boolean registerDefaultFormatters) {
if (embeddedValueResolver != null) {
setEmbeddedValueResolver(embeddedValueResolver);
}
DefaultConversionService.addDefaultConverters(this);
if (registerDefaultFormatters) {
addDefaultFormatters(this);
}
}
/**
* Add formatters appropriate for most environments: including number formatters,
* JSR-354 Money & Currency formatters, JSR-310 Date-Time and/or Joda-Time formatters,
* depending on the presence of the corresponding API on the classpath.
* @param formatterRegistry the service to register default formatters with
*/
public static void addDefaultFormatters(FormatterRegistry formatterRegistry) {
// Default handling of number values
formatterRegistry.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
// Default handling of monetary values
if (jsr354Present) {
formatterRegistry.addFormatter(new CurrencyUnitFormatter());
formatterRegistry.addFormatter(new MonetaryAmountFormatter());
formatterRegistry.addFormatterForFieldAnnotation(new Jsr354NumberFormatAnnotationFormatterFactory());
}
// Default handling of date-time values
// just handling JSR-310 specific date and time types
new DateTimeFormatterRegistrar().registerFormatters(formatterRegistry);
if (jodaTimePresent) {
// handles Joda-specific types as well as Date, Calendar, Long
new JodaTimeFormatterRegistrar().registerFormatters(formatterRegistry);
}
else {
// regular DateFormat-based Date, Calendar, Long converters
new DateFormatterRegistrar().registerFormatters(formatterRegistry);
}
}
}