1 package org.apache.turbine.util;
2
3 import java.nio.charset.Charset;
4 import java.nio.charset.IllegalCharsetNameException;
5 import java.nio.charset.StandardCharsets;
6 import java.nio.charset.UnsupportedCharsetException;
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 import java.util.Locale;
28
29 import org.apache.commons.lang3.StringUtils;
30 import org.apache.fulcrum.mimetype.MimeTypeService;
31 import org.apache.logging.log4j.LogManager;
32 import org.apache.logging.log4j.Logger;
33 import org.apache.turbine.Turbine;
34 import org.apache.turbine.TurbineConstants;
35 import org.apache.turbine.services.ServiceManager;
36 import org.apache.turbine.services.TurbineServices;
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 public class LocaleUtils
62 {
63
64 private static final Logger log = LogManager.getLogger(LocaleUtils.class);
65
66
67 private static Locale defaultLocale = null;
68
69
70 private static Charset defaultCharSet = null;
71
72
73
74
75
76
77 public static String getDefaultInputEncoding()
78 {
79
80 String inputEncoding = Turbine.getConfiguration()
81 .getString(TurbineConstants.PARAMETER_ENCODING_KEY,
82 TurbineConstants.PARAMETER_ENCODING_DEFAULT);
83
84 log.debug("Input Encoding has been set to {}", inputEncoding);
85
86 return inputEncoding;
87 }
88
89
90
91
92
93
94
95
96
97
98 public static Locale getDefaultLocale()
99 {
100 if (defaultLocale == null)
101 {
102
103 String lang = Turbine.getConfiguration()
104 .getString(TurbineConstants.LOCALE_DEFAULT_LANGUAGE_KEY,
105 TurbineConstants.LOCALE_DEFAULT_LANGUAGE_DEFAULT);
106
107 String country = Turbine.getConfiguration()
108 .getString(TurbineConstants.LOCALE_DEFAULT_COUNTRY_KEY,
109 TurbineConstants.LOCALE_DEFAULT_COUNTRY_DEFAULT);
110
111
112 defaultLocale = new Locale(lang, country);
113 }
114
115 return defaultLocale;
116 }
117
118
119
120
121
122
123
124 @Deprecated
125 public static String getDefaultCharSet()
126 {
127 return getDefaultCharset().name();
128 }
129
130
131
132
133
134
135
136 public static Charset getDefaultCharset()
137 {
138 if (defaultCharSet == null)
139 {
140
141 String charSet = Turbine.getConfiguration()
142 .getString(TurbineConstants.LOCALE_DEFAULT_CHARSET_KEY,
143 TurbineConstants.LOCALE_DEFAULT_CHARSET_DEFAULT);
144
145 if (StringUtils.isNotEmpty(charSet))
146 {
147 defaultCharSet = charSetForName(charSet);
148 log.debug("defaultCharSet = {} (From Properties)", defaultCharSet);
149 }
150 }
151
152 Charset charset = defaultCharSet;
153
154 if (charset == null)
155 {
156 log.debug("Default charset is empty!");
157
158 Locale locale = getDefaultLocale();
159 log.debug("Locale is {}", locale);
160
161 if (!locale.equals(Locale.US))
162 {
163 log.debug("We don't have US Locale!");
164 ServiceManager serviceManager = TurbineServices.getInstance();
165 if (serviceManager.isRegistered(MimeTypeService.ROLE))
166 {
167 try
168 {
169 MimeTypeService mimeTypeService = (MimeTypeService) serviceManager.getService(MimeTypeService.ROLE);
170 charset = charSetForName(mimeTypeService.getCharSet(locale));
171 }
172 catch (Exception e)
173 {
174 throw new RuntimeException(e);
175 }
176
177 log.debug("Charset now {}", charset);
178 }
179 }
180
181
182 if (charset == null)
183 {
184 charset = StandardCharsets.ISO_8859_1;
185 }
186 }
187
188 log.debug("Returning default Charset of {}", charset);
189 return charset;
190 }
191
192
193
194
195
196
197
198
199 @Deprecated
200 public static String getOverrideCharSet()
201 {
202 return Turbine.getConfiguration()
203 .getString(TurbineConstants.LOCALE_OVERRIDE_CHARSET_KEY);
204 }
205
206
207
208
209
210
211
212
213 public static Charset getOverrideCharset()
214 {
215 String charset = Turbine.getConfiguration()
216 .getString(TurbineConstants.LOCALE_OVERRIDE_CHARSET_KEY);
217
218 if (StringUtils.isEmpty(charset))
219 {
220 return null;
221 }
222
223 return charSetForName(charset);
224 }
225
226
227
228
229
230
231
232
233
234 private static Charset charSetForName(String charSet)
235 {
236 try
237 {
238 return Charset.forName(charSet);
239 }
240 catch (IllegalCharsetNameException | UnsupportedCharsetException e)
241 {
242 log.error("Illegal default charset {}", charSet);
243 }
244
245 return null;
246 }
247
248 }