1 package org.codehaus.mojo.jaxb2.shared.environment.locale;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.plugin.MojoExecutionException;
23 import org.apache.maven.plugin.logging.Log;
24 import org.codehaus.mojo.jaxb2.shared.Validate;
25 import org.codehaus.mojo.jaxb2.shared.environment.AbstractLogAwareFacet;
26
27 import java.util.Locale;
28 import java.util.StringTokenizer;
29
30
31
32
33
34
35
36 public class LocaleFacet extends AbstractLogAwareFacet {
37
38
39 private Locale originalLocale;
40 private Locale newLocale;
41
42
43
44
45
46
47
48 @SuppressWarnings("WeakerAccess")
49 public LocaleFacet(final Log log, final Locale newLocale) {
50 super(log);
51
52
53 Validate.notNull(newLocale, "usedLocale");
54
55
56 this.originalLocale = Locale.getDefault();
57 this.newLocale = newLocale;
58 }
59
60
61
62
63
64 @Override
65 public void setup() {
66
67 if (log.isInfoEnabled()) {
68 log.info("Setting default locale to [" + newLocale + "]");
69 }
70
71 try {
72 Locale.setDefault(newLocale);
73 } catch (Exception e) {
74 log.error("Could not switch locale to ["
75 + newLocale + "]. Continuing with standard locale.", e);
76 }
77 }
78
79
80
81
82
83 @Override
84 public void restore() {
85
86 if (log.isInfoEnabled()) {
87 log.info("Restoring default locale to [" + originalLocale + "]");
88 }
89
90 try {
91 Locale.setDefault(originalLocale);
92 } catch (Exception e) {
93 log.error("Could not restore locale to [" + originalLocale + "]. Continuing with ["
94 + Locale.getDefault() + "]", e);
95 }
96 }
97
98
99
100
101
102
103
104
105
106
107 public static LocaleFacet createFor(final String localeString, final Log log) throws MojoExecutionException {
108
109
110 Validate.notNull(log, "log");
111 Validate.notEmpty(localeString, "localeString");
112
113 final StringTokenizer tok = new StringTokenizer(localeString, ",", false);
114 final int numTokens = tok.countTokens();
115 if (numTokens > 3 || numTokens == 0) {
116 throw new MojoExecutionException("A localeString must consist of up to 3 comma-separated parts on the "
117 + "form <language>[,<country>[,<variant>]]. Received incorrect value '" + localeString + "'");
118 }
119
120
121 final String language = tok.nextToken().trim();
122 final String country = numTokens > 1 ? tok.nextToken().trim() : null;
123 final String variant = numTokens > 2 ? tok.nextToken().trim() : null;
124
125
126 return new LocaleFacet(log, findOptimumLocale(language, country, variant));
127 }
128
129
130
131
132
133
134
135
136
137
138
139
140 @SuppressWarnings("All")
141 public static Locale findOptimumLocale(final String language, final String country, final String variant) {
142
143 final boolean hasCountry = country != null && !country.isEmpty();
144 final boolean hasVariant = variant != null && !variant.isEmpty();
145
146 final Locale[] availableLocales = Locale.getAvailableLocales();
147 for (int i = 0; i < availableLocales.length; i++) {
148
149 final Locale current = availableLocales[i];
150
151
152 final String currentLanguage = current.getLanguage();
153 final String currentCountry = current.getCountry();
154 final String currentVariant = current.getVariant();
155
156
157 final boolean isLanguageMatch = language.equalsIgnoreCase(currentLanguage);
158 final boolean isCountryMatch = (hasCountry && country.equalsIgnoreCase(currentCountry))
159 || (!hasCountry && (currentCountry == null || currentCountry.isEmpty()));
160 final boolean isVariantMatch = (hasVariant && variant.equalsIgnoreCase(currentVariant))
161 || (!hasVariant && (currentVariant == null || currentVariant.isEmpty()));
162
163 if (isLanguageMatch && isCountryMatch && isVariantMatch) {
164 return current;
165 }
166 }
167
168
169 return Locale.getDefault();
170 }
171 }