/
ImplicitNumberConverter.java
137 lines (115 loc) · 4.02 KB
/
ImplicitNumberConverter.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
136
137
/*
* Copyright 2020 OmniFaces
*
* 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.omnifaces.converter;
import static org.omnifaces.util.Utils.isEmpty;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Currency;
import java.util.regex.Pattern;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.FacesConverter;
import javax.faces.convert.NumberConverter;
/**
* <p>
* This converter won't output the percent or currency symbols, that's up to the UI. This converter will implicitly
* infer percent or currency symbols on submitted value when absent, just to prevent an unnecessary conversion error.
*
* <h3>Usage</h3>
* <p>
* This converter is available by converter ID <code>omnifaces.ImplicitNumberConverter</code>. Just specify it as
* <code><o:converter></code> nested in the component referring the <code>Number</code> property. For example:
* <pre>
* <span class="currency">
* <span class="symbol">$</span>
* <h:inputText value="#{bean.price}">
* <o:converter converterId="omnifaces.ImplicitNumberConverter" type="currency" currencySymbol="$" />
* </h:inputText>
* </span>
* </pre>
*
* @author Bauke Scholtz
* @since 3.0
*/
@FacesConverter("omnifaces.ImplicitNumberConverter")
public class ImplicitNumberConverter extends NumberConverter {
private static final Pattern PATTERN_NUMBER = Pattern.compile("[\\d,.]+");
@Override
public String getAsString(FacesContext context, UIComponent component, Object modelValue) {
String string = super.getAsString(context, component, modelValue);
if (string != null) {
DecimalFormat formatter = getFormatter();
if (formatter != null) {
String symbol = getSymbol(formatter);
if (symbol != null) {
string = string.replace(symbol, "").trim();
}
}
}
return string;
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
String string = submittedValue;
if (!isEmpty(string)) {
DecimalFormat formatter = getFormatter();
if (formatter != null) {
String symbol = getSymbol(formatter);
if (!string.contains(symbol)) {
string = PATTERN_NUMBER.matcher(formatter.format(0)).replaceAll(submittedValue);
}
}
}
return super.getAsObject(context, component, string);
}
private boolean isPercent() {
return "percent".equals(getType());
}
private boolean isCurrency() {
return "currency".equals(getType());
}
private DecimalFormat getFormatter() {
if (isPercent()) {
return (DecimalFormat) NumberFormat.getPercentInstance(getLocale());
}
else if (isCurrency()) {
DecimalFormat formatter = (DecimalFormat) NumberFormat.getCurrencyInstance(getLocale());
String currencyCode = getCurrencyCode();
String currencySymbol = getCurrencySymbol();
if (currencyCode != null) {
formatter.setCurrency(Currency.getInstance(currencyCode));
}
else if (currencySymbol != null) {
DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols();
symbols.setCurrencySymbol(currencySymbol);
formatter.setDecimalFormatSymbols(symbols);
}
return formatter;
}
else {
return null;
}
}
private String getSymbol(DecimalFormat formatter) {
if (isPercent()) {
return String.valueOf(formatter.getDecimalFormatSymbols().getPercent());
}
else if (isCurrency()) {
return formatter.getDecimalFormatSymbols().getCurrencySymbol();
}
else {
return null;
}
}
}