author | naoto |
Fri, 19 Feb 2016 09:55:37 -0800 | |
changeset 36014 | 3d50784f2dc2 |
parent 32510 | 7530a468314c |
child 38440 | 9e77c5b81def |
permissions | -rw-r--r-- |
13583 | 1 |
/* |
31263 | 2 |
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. |
13583 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package build.tools.cldrconverter; |
|
27 |
||
32510
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
28 |
import static build.tools.cldrconverter.Bundle.jreTimeZoneNames; |
14765
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
29 |
import build.tools.cldrconverter.BundleGenerator.BundleType; |
13583 | 30 |
import java.io.File; |
31 |
import java.nio.file.DirectoryStream; |
|
32 |
import java.nio.file.FileSystems; |
|
33 |
import java.nio.file.Files; |
|
34 |
import java.nio.file.Path; |
|
35 |
import java.util.*; |
|
31263 | 36 |
import java.util.ResourceBundle.Control; |
37 |
import java.util.logging.Level; |
|
38 |
import java.util.logging.Logger; |
|
13583 | 39 |
import javax.xml.parsers.SAXParser; |
40 |
import javax.xml.parsers.SAXParserFactory; |
|
17466
e2c55878b26a
8014500: bootcycle-images fails after upgrade to JAXP 1.5
alanb
parents:
16852
diff
changeset
|
41 |
import org.xml.sax.SAXNotRecognizedException; |
e2c55878b26a
8014500: bootcycle-images fails after upgrade to JAXP 1.5
alanb
parents:
16852
diff
changeset
|
42 |
import org.xml.sax.SAXNotSupportedException; |
13583 | 43 |
|
44 |
||
45 |
/** |
|
46 |
* Converts locale data from "Locale Data Markup Language" format to |
|
47 |
* JRE resource bundle format. LDML is the format used by the Common |
|
48 |
* Locale Data Repository maintained by the Unicode Consortium. |
|
49 |
*/ |
|
50 |
public class CLDRConverter { |
|
51 |
||
52 |
static final String LDML_DTD_SYSTEM_ID = "http://www.unicode.org/cldr/dtd/2.0/ldml.dtd"; |
|
53 |
static final String SPPL_LDML_DTD_SYSTEM_ID = "http://www.unicode.org/cldr/dtd/2.0/ldmlSupplemental.dtd"; |
|
54 |
||
55 |
private static String CLDR_BASE = "../CLDR/21.0.1/"; |
|
56 |
static String LOCAL_LDML_DTD; |
|
57 |
static String LOCAL_SPPL_LDML_DTD; |
|
58 |
private static String SOURCE_FILE_DIR; |
|
59 |
private static String SPPL_SOURCE_FILE; |
|
60 |
private static String NUMBERING_SOURCE_FILE; |
|
61 |
private static String METAZONES_SOURCE_FILE; |
|
62 |
static String DESTINATION_DIR = "build/gensrc"; |
|
63 |
||
64 |
static final String LOCALE_NAME_PREFIX = "locale.displayname."; |
|
65 |
static final String CURRENCY_SYMBOL_PREFIX = "currency.symbol."; |
|
66 |
static final String CURRENCY_NAME_PREFIX = "currency.displayname."; |
|
15281
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
67 |
static final String CALENDAR_NAME_PREFIX = "calendarname."; |
13583 | 68 |
static final String TIMEZONE_ID_PREFIX = "timezone.id."; |
14765
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
69 |
static final String ZONE_NAME_PREFIX = "timezone.displayname."; |
13583 | 70 |
static final String METAZONE_ID_PREFIX = "metazone.id."; |
31263 | 71 |
static final String PARENT_LOCALE_PREFIX = "parentLocale."; |
13583 | 72 |
|
73 |
private static SupplementDataParseHandler handlerSuppl; |
|
74 |
static NumberingSystemsParseHandler handlerNumbering; |
|
75 |
static MetaZonesParseHandler handlerMetaZones; |
|
76 |
private static BundleGenerator bundleGenerator; |
|
77 |
||
31263 | 78 |
// java.base module related |
79 |
static boolean isBaseModule = false; |
|
80 |
static final Set<Locale> BASE_LOCALES = new HashSet<>(); |
|
81 |
||
82 |
// "parentLocales" map |
|
83 |
private static final Map<String, SortedSet<String>> parentLocalesMap = new HashMap<>(); |
|
84 |
private static final ResourceBundle.Control defCon = |
|
85 |
ResourceBundle.Control.getControl(ResourceBundle.Control.FORMAT_DEFAULT); |
|
86 |
||
16852 | 87 |
static enum DraftType { |
88 |
UNCONFIRMED, |
|
89 |
PROVISIONAL, |
|
90 |
CONTRIBUTED, |
|
91 |
APPROVED; |
|
92 |
||
93 |
private static final Map<String, DraftType> map = new HashMap<>(); |
|
94 |
static { |
|
95 |
for (DraftType dt : values()) { |
|
96 |
map.put(dt.getKeyword(), dt); |
|
97 |
} |
|
98 |
} |
|
99 |
static private DraftType defaultType = CONTRIBUTED; |
|
100 |
||
101 |
private final String keyword; |
|
102 |
||
103 |
private DraftType() { |
|
104 |
keyword = this.name().toLowerCase(Locale.ROOT); |
|
13583 | 105 |
|
16852 | 106 |
} |
107 |
||
108 |
static DraftType forKeyword(String keyword) { |
|
109 |
return map.get(keyword); |
|
110 |
} |
|
111 |
||
112 |
static DraftType getDefault() { |
|
113 |
return defaultType; |
|
114 |
} |
|
115 |
||
116 |
static void setDefault(String keyword) { |
|
117 |
defaultType = Objects.requireNonNull(forKeyword(keyword)); |
|
118 |
} |
|
119 |
||
120 |
String getKeyword() { |
|
121 |
return keyword; |
|
122 |
} |
|
123 |
} |
|
13583 | 124 |
|
125 |
static boolean USE_UTF8 = false; |
|
126 |
private static boolean verbose; |
|
127 |
||
128 |
private CLDRConverter() { |
|
129 |
// no instantiation |
|
130 |
} |
|
131 |
||
132 |
@SuppressWarnings("AssignmentToForLoopParameter") |
|
133 |
public static void main(String[] args) throws Exception { |
|
134 |
if (args.length != 0) { |
|
135 |
String currentArg = null; |
|
136 |
try { |
|
137 |
for (int i = 0; i < args.length; i++) { |
|
138 |
currentArg = args[i]; |
|
139 |
switch (currentArg) { |
|
140 |
case "-draft": |
|
141 |
String draftDataType = args[++i]; |
|
142 |
try { |
|
16852 | 143 |
DraftType.setDefault(draftDataType); |
13583 | 144 |
} catch (NullPointerException e) { |
145 |
severe("Error: incorrect draft value: %s%n", draftDataType); |
|
146 |
System.exit(1); |
|
147 |
} |
|
148 |
info("Using the specified data type: %s%n", draftDataType); |
|
149 |
break; |
|
150 |
||
151 |
case "-base": |
|
152 |
// base directory for input files |
|
153 |
CLDR_BASE = args[++i]; |
|
154 |
if (!CLDR_BASE.endsWith("/")) { |
|
155 |
CLDR_BASE += "/"; |
|
156 |
} |
|
157 |
break; |
|
158 |
||
31263 | 159 |
case "-baselocales": |
160 |
// base locales |
|
161 |
setupBaseLocales(args[++i]); |
|
162 |
break; |
|
163 |
||
164 |
case "-basemodule": |
|
165 |
// indicates java.base module resource generation |
|
166 |
isBaseModule = true; |
|
167 |
break; |
|
168 |
||
13583 | 169 |
case "-o": |
170 |
// output directory |
|
171 |
DESTINATION_DIR = args[++i]; |
|
172 |
break; |
|
173 |
||
174 |
case "-utf8": |
|
175 |
USE_UTF8 = true; |
|
176 |
break; |
|
177 |
||
178 |
case "-verbose": |
|
179 |
verbose = true; |
|
180 |
break; |
|
181 |
||
182 |
case "-help": |
|
183 |
usage(); |
|
184 |
System.exit(0); |
|
185 |
break; |
|
186 |
||
187 |
default: |
|
188 |
throw new RuntimeException(); |
|
189 |
} |
|
190 |
} |
|
191 |
} catch (RuntimeException e) { |
|
192 |
severe("unknown or imcomplete arg(s): " + currentArg); |
|
193 |
usage(); |
|
194 |
System.exit(1); |
|
195 |
} |
|
196 |
} |
|
197 |
||
198 |
// Set up path names |
|
199 |
LOCAL_LDML_DTD = CLDR_BASE + "common/dtd/ldml.dtd"; |
|
200 |
LOCAL_SPPL_LDML_DTD = CLDR_BASE + "common/dtd/ldmlSupplemental.dtd"; |
|
201 |
SOURCE_FILE_DIR = CLDR_BASE + "common/main"; |
|
202 |
SPPL_SOURCE_FILE = CLDR_BASE + "common/supplemental/supplementalData.xml"; |
|
203 |
NUMBERING_SOURCE_FILE = CLDR_BASE + "common/supplemental/numberingSystems.xml"; |
|
204 |
METAZONES_SOURCE_FILE = CLDR_BASE + "common/supplemental/metaZones.xml"; |
|
205 |
||
31263 | 206 |
if (BASE_LOCALES.isEmpty()) { |
207 |
setupBaseLocales("en-US"); |
|
208 |
} |
|
209 |
||
13583 | 210 |
bundleGenerator = new ResourceBundleGenerator(); |
211 |
||
31263 | 212 |
// Parse data independent of locales |
213 |
parseSupplemental(); |
|
214 |
||
13583 | 215 |
List<Bundle> bundles = readBundleList(); |
216 |
convertBundles(bundles); |
|
217 |
} |
|
218 |
||
219 |
private static void usage() { |
|
220 |
errout("Usage: java CLDRConverter [options]%n" |
|
221 |
+ "\t-help output this usage message and exit%n" |
|
222 |
+ "\t-verbose output information%n" |
|
223 |
+ "\t-draft [approved | provisional | unconfirmed]%n" |
|
224 |
+ "\t\t draft level for using data (default: approved)%n" |
|
225 |
+ "\t-base dir base directory for CLDR input files%n" |
|
31263 | 226 |
+ "\t-basemodule generates bundles that go into java.base module%n" |
227 |
+ "\t-baselocales loc(,loc)* locales that go into the base module%n" |
|
228 |
+ "\t-o dir output directory (default: ./build/gensrc)%n" |
|
13583 | 229 |
+ "\t-o dir output directory (defaut: ./build/gensrc)%n" |
230 |
+ "\t-utf8 use UTF-8 rather than \\uxxxx (for debug)%n"); |
|
231 |
} |
|
232 |
||
233 |
static void info(String fmt, Object... args) { |
|
234 |
if (verbose) { |
|
235 |
System.out.printf(fmt, args); |
|
236 |
} |
|
237 |
} |
|
238 |
||
239 |
static void info(String msg) { |
|
240 |
if (verbose) { |
|
241 |
System.out.println(msg); |
|
242 |
} |
|
243 |
} |
|
244 |
||
245 |
static void warning(String fmt, Object... args) { |
|
246 |
System.err.print("Warning: "); |
|
247 |
System.err.printf(fmt, args); |
|
248 |
} |
|
249 |
||
250 |
static void warning(String msg) { |
|
251 |
System.err.print("Warning: "); |
|
252 |
errout(msg); |
|
253 |
} |
|
254 |
||
255 |
static void severe(String fmt, Object... args) { |
|
256 |
System.err.print("Error: "); |
|
257 |
System.err.printf(fmt, args); |
|
258 |
} |
|
259 |
||
260 |
static void severe(String msg) { |
|
261 |
System.err.print("Error: "); |
|
262 |
errout(msg); |
|
263 |
} |
|
264 |
||
265 |
private static void errout(String msg) { |
|
266 |
if (msg.contains("%n")) { |
|
267 |
System.err.printf(msg); |
|
268 |
} else { |
|
269 |
System.err.println(msg); |
|
270 |
} |
|
271 |
} |
|
272 |
||
17466
e2c55878b26a
8014500: bootcycle-images fails after upgrade to JAXP 1.5
alanb
parents:
16852
diff
changeset
|
273 |
/** |
e2c55878b26a
8014500: bootcycle-images fails after upgrade to JAXP 1.5
alanb
parents:
16852
diff
changeset
|
274 |
* Configure the parser to allow access to DTDs on the file system. |
e2c55878b26a
8014500: bootcycle-images fails after upgrade to JAXP 1.5
alanb
parents:
16852
diff
changeset
|
275 |
*/ |
e2c55878b26a
8014500: bootcycle-images fails after upgrade to JAXP 1.5
alanb
parents:
16852
diff
changeset
|
276 |
private static void enableFileAccess(SAXParser parser) throws SAXNotSupportedException { |
e2c55878b26a
8014500: bootcycle-images fails after upgrade to JAXP 1.5
alanb
parents:
16852
diff
changeset
|
277 |
try { |
e2c55878b26a
8014500: bootcycle-images fails after upgrade to JAXP 1.5
alanb
parents:
16852
diff
changeset
|
278 |
parser.setProperty("http://javax.xml.XMLConstants/property/accessExternalDTD", "file"); |
e2c55878b26a
8014500: bootcycle-images fails after upgrade to JAXP 1.5
alanb
parents:
16852
diff
changeset
|
279 |
} catch (SAXNotRecognizedException ignore) { |
e2c55878b26a
8014500: bootcycle-images fails after upgrade to JAXP 1.5
alanb
parents:
16852
diff
changeset
|
280 |
// property requires >= JAXP 1.5 |
e2c55878b26a
8014500: bootcycle-images fails after upgrade to JAXP 1.5
alanb
parents:
16852
diff
changeset
|
281 |
} |
e2c55878b26a
8014500: bootcycle-images fails after upgrade to JAXP 1.5
alanb
parents:
16852
diff
changeset
|
282 |
} |
e2c55878b26a
8014500: bootcycle-images fails after upgrade to JAXP 1.5
alanb
parents:
16852
diff
changeset
|
283 |
|
13583 | 284 |
private static List<Bundle> readBundleList() throws Exception { |
285 |
List<Bundle> retList = new ArrayList<>(); |
|
286 |
Path path = FileSystems.getDefault().getPath(SOURCE_FILE_DIR); |
|
287 |
try (DirectoryStream<Path> dirStr = Files.newDirectoryStream(path)) { |
|
288 |
for (Path entry : dirStr) { |
|
289 |
String fileName = entry.getFileName().toString(); |
|
290 |
if (fileName.endsWith(".xml")) { |
|
291 |
String id = fileName.substring(0, fileName.indexOf('.')); |
|
292 |
Locale cldrLoc = Locale.forLanguageTag(toLanguageTag(id)); |
|
31263 | 293 |
List<Locale> candList = applyParentLocales("", defCon.getCandidateLocales("", cldrLoc)); |
13583 | 294 |
StringBuilder sb = new StringBuilder(); |
295 |
for (Locale loc : candList) { |
|
296 |
if (!loc.equals(Locale.ROOT)) { |
|
297 |
sb.append(toLocaleName(loc.toLanguageTag())); |
|
298 |
sb.append(","); |
|
299 |
} |
|
300 |
} |
|
301 |
if (sb.indexOf("root") == -1) { |
|
302 |
sb.append("root"); |
|
303 |
} |
|
14765
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
304 |
Bundle b = new Bundle(id, sb.toString(), null, null); |
31263 | 305 |
// Insert the bundle for root at the top so that it will get |
14765
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
306 |
// processed first. |
31263 | 307 |
if ("root".equals(id)) { |
14765
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
308 |
retList.add(0, b); |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
309 |
} else { |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
310 |
retList.add(b); |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
311 |
} |
13583 | 312 |
} |
313 |
} |
|
314 |
} |
|
315 |
return retList; |
|
316 |
} |
|
317 |
||
31263 | 318 |
private static final Map<String, Map<String, Object>> cldrBundles = new HashMap<>(); |
13583 | 319 |
|
320 |
static Map<String, Object> getCLDRBundle(String id) throws Exception { |
|
321 |
Map<String, Object> bundle = cldrBundles.get(id); |
|
322 |
if (bundle != null) { |
|
323 |
return bundle; |
|
324 |
} |
|
325 |
SAXParserFactory factory = SAXParserFactory.newInstance(); |
|
326 |
factory.setValidating(true); |
|
327 |
SAXParser parser = factory.newSAXParser(); |
|
17466
e2c55878b26a
8014500: bootcycle-images fails after upgrade to JAXP 1.5
alanb
parents:
16852
diff
changeset
|
328 |
enableFileAccess(parser); |
13583 | 329 |
LDMLParseHandler handler = new LDMLParseHandler(id); |
330 |
File file = new File(SOURCE_FILE_DIR + File.separator + id + ".xml"); |
|
331 |
if (!file.exists()) { |
|
332 |
// Skip if the file doesn't exist. |
|
333 |
return Collections.emptyMap(); |
|
334 |
} |
|
335 |
||
336 |
info("..... main directory ....."); |
|
337 |
info("Reading file " + file); |
|
338 |
parser.parse(file, handler); |
|
339 |
||
340 |
bundle = handler.getData(); |
|
341 |
cldrBundles.put(id, bundle); |
|
342 |
String country = getCountryCode(id); |
|
343 |
if (country != null) { |
|
344 |
bundle = handlerSuppl.getData(country); |
|
345 |
if (bundle != null) { |
|
346 |
//merge two maps into one map |
|
347 |
Map<String, Object> temp = cldrBundles.remove(id); |
|
348 |
bundle.putAll(temp); |
|
349 |
cldrBundles.put(id, bundle); |
|
350 |
} |
|
351 |
} |
|
352 |
return bundle; |
|
353 |
} |
|
354 |
||
31263 | 355 |
// Parsers for data in "supplemental" directory |
356 |
// |
|
357 |
private static void parseSupplemental() throws Exception { |
|
13583 | 358 |
// Parse SupplementalData file and store the information in the HashMap |
359 |
// Calendar information such as firstDay and minDay are stored in |
|
360 |
// supplementalData.xml as of CLDR1.4. Individual territory is listed |
|
361 |
// with its ISO 3166 country code while default is listed using UNM49 |
|
362 |
// region and composition numerical code (001 for World.) |
|
31263 | 363 |
// |
364 |
// SupplementalData file also provides the "parent" locales which |
|
365 |
// are othrwise not to be fallen back. Process them here as well. |
|
366 |
// |
|
367 |
info("..... Parsing supplementalData.xml ....."); |
|
13583 | 368 |
SAXParserFactory factorySuppl = SAXParserFactory.newInstance(); |
369 |
factorySuppl.setValidating(true); |
|
370 |
SAXParser parserSuppl = factorySuppl.newSAXParser(); |
|
17466
e2c55878b26a
8014500: bootcycle-images fails after upgrade to JAXP 1.5
alanb
parents:
16852
diff
changeset
|
371 |
enableFileAccess(parserSuppl); |
13583 | 372 |
handlerSuppl = new SupplementDataParseHandler(); |
373 |
File fileSupply = new File(SPPL_SOURCE_FILE); |
|
374 |
parserSuppl.parse(fileSupply, handlerSuppl); |
|
31263 | 375 |
Map<String, Object> parentData = handlerSuppl.getData("root"); |
376 |
parentData.keySet().forEach(key -> { |
|
377 |
parentLocalesMap.put(key, new TreeSet( |
|
378 |
Arrays.asList(((String)parentData.get(key)).split(" ")))); |
|
379 |
}); |
|
13583 | 380 |
|
381 |
// Parse numberingSystems to get digit zero character information. |
|
31263 | 382 |
info("..... Parsing numberingSystem.xml ....."); |
13583 | 383 |
SAXParserFactory numberingParser = SAXParserFactory.newInstance(); |
384 |
numberingParser.setValidating(true); |
|
385 |
SAXParser parserNumbering = numberingParser.newSAXParser(); |
|
17466
e2c55878b26a
8014500: bootcycle-images fails after upgrade to JAXP 1.5
alanb
parents:
16852
diff
changeset
|
386 |
enableFileAccess(parserNumbering); |
13583 | 387 |
handlerNumbering = new NumberingSystemsParseHandler(); |
388 |
File fileNumbering = new File(NUMBERING_SOURCE_FILE); |
|
389 |
parserNumbering.parse(fileNumbering, handlerNumbering); |
|
390 |
||
391 |
// Parse metaZones to create mappings between Olson tzids and CLDR meta zone names |
|
31263 | 392 |
info("..... Parsing metaZones.xml ....."); |
13583 | 393 |
SAXParserFactory metazonesParser = SAXParserFactory.newInstance(); |
394 |
metazonesParser.setValidating(true); |
|
395 |
SAXParser parserMetaZones = metazonesParser.newSAXParser(); |
|
17466
e2c55878b26a
8014500: bootcycle-images fails after upgrade to JAXP 1.5
alanb
parents:
16852
diff
changeset
|
396 |
enableFileAccess(parserMetaZones); |
13583 | 397 |
handlerMetaZones = new MetaZonesParseHandler(); |
398 |
File fileMetaZones = new File(METAZONES_SOURCE_FILE); |
|
399 |
parserNumbering.parse(fileMetaZones, handlerMetaZones); |
|
31263 | 400 |
} |
13583 | 401 |
|
31263 | 402 |
private static void convertBundles(List<Bundle> bundles) throws Exception { |
13583 | 403 |
// For generating information on supported locales. |
404 |
Map<String, SortedSet<String>> metaInfo = new HashMap<>(); |
|
31263 | 405 |
metaInfo.put("LocaleNames", new TreeSet<>()); |
406 |
metaInfo.put("CurrencyNames", new TreeSet<>()); |
|
407 |
metaInfo.put("TimeZoneNames", new TreeSet<>()); |
|
408 |
metaInfo.put("CalendarData", new TreeSet<>()); |
|
409 |
metaInfo.put("FormatData", new TreeSet<>()); |
|
410 |
metaInfo.put("AvailableLocales", new TreeSet<>()); |
|
411 |
||
412 |
// parent locales map. The mappings are put in base metaInfo file |
|
413 |
// for now. |
|
414 |
if (isBaseModule) { |
|
415 |
metaInfo.putAll(parentLocalesMap); |
|
416 |
} |
|
13583 | 417 |
|
418 |
for (Bundle bundle : bundles) { |
|
419 |
// Get the target map, which contains all the data that should be |
|
420 |
// visible for the bundle's locale |
|
421 |
||
422 |
Map<String, Object> targetMap = bundle.getTargetMap(); |
|
423 |
||
424 |
EnumSet<Bundle.Type> bundleTypes = bundle.getBundleTypes(); |
|
425 |
||
426 |
if (bundle.isRoot()) { |
|
427 |
// Add DateTimePatternChars because CLDR no longer supports localized patterns. |
|
428 |
targetMap.put("DateTimePatternChars", "GyMdkHmsSEDFwWahKzZ"); |
|
429 |
} |
|
430 |
||
431 |
// Now the map contains just the entries that need to be in the resources bundles. |
|
432 |
// Go ahead and generate them. |
|
433 |
if (bundleTypes.contains(Bundle.Type.LOCALENAMES)) { |
|
434 |
Map<String, Object> localeNamesMap = extractLocaleNames(targetMap, bundle.getID()); |
|
435 |
if (!localeNamesMap.isEmpty() || bundle.isRoot()) { |
|
436 |
metaInfo.get("LocaleNames").add(toLanguageTag(bundle.getID())); |
|
32030
f2c22ba89d0f
8132494: Wrong CLDR resource bundle names for legacy ISO language codes
naoto
parents:
31263
diff
changeset
|
437 |
bundleGenerator.generateBundle("util", "LocaleNames", bundle.getJavaID(), true, localeNamesMap, BundleType.OPEN); |
13583 | 438 |
} |
439 |
} |
|
440 |
if (bundleTypes.contains(Bundle.Type.CURRENCYNAMES)) { |
|
441 |
Map<String, Object> currencyNamesMap = extractCurrencyNames(targetMap, bundle.getID(), bundle.getCurrencies()); |
|
442 |
if (!currencyNamesMap.isEmpty() || bundle.isRoot()) { |
|
443 |
metaInfo.get("CurrencyNames").add(toLanguageTag(bundle.getID())); |
|
32030
f2c22ba89d0f
8132494: Wrong CLDR resource bundle names for legacy ISO language codes
naoto
parents:
31263
diff
changeset
|
444 |
bundleGenerator.generateBundle("util", "CurrencyNames", bundle.getJavaID(), true, currencyNamesMap, BundleType.OPEN); |
13583 | 445 |
} |
446 |
} |
|
447 |
if (bundleTypes.contains(Bundle.Type.TIMEZONENAMES)) { |
|
448 |
Map<String, Object> zoneNamesMap = extractZoneNames(targetMap, bundle.getID()); |
|
14765
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
449 |
if (!zoneNamesMap.isEmpty() || bundle.isRoot()) { |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
450 |
metaInfo.get("TimeZoneNames").add(toLanguageTag(bundle.getID())); |
32030
f2c22ba89d0f
8132494: Wrong CLDR resource bundle names for legacy ISO language codes
naoto
parents:
31263
diff
changeset
|
451 |
bundleGenerator.generateBundle("util", "TimeZoneNames", bundle.getJavaID(), true, zoneNamesMap, BundleType.TIMEZONE); |
14765
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
452 |
} |
13583 | 453 |
} |
454 |
if (bundleTypes.contains(Bundle.Type.CALENDARDATA)) { |
|
455 |
Map<String, Object> calendarDataMap = extractCalendarData(targetMap, bundle.getID()); |
|
456 |
if (!calendarDataMap.isEmpty() || bundle.isRoot()) { |
|
457 |
metaInfo.get("CalendarData").add(toLanguageTag(bundle.getID())); |
|
32030
f2c22ba89d0f
8132494: Wrong CLDR resource bundle names for legacy ISO language codes
naoto
parents:
31263
diff
changeset
|
458 |
bundleGenerator.generateBundle("util", "CalendarData", bundle.getJavaID(), true, calendarDataMap, BundleType.PLAIN); |
13583 | 459 |
} |
460 |
} |
|
461 |
if (bundleTypes.contains(Bundle.Type.FORMATDATA)) { |
|
462 |
Map<String, Object> formatDataMap = extractFormatData(targetMap, bundle.getID()); |
|
463 |
if (!formatDataMap.isEmpty() || bundle.isRoot()) { |
|
464 |
metaInfo.get("FormatData").add(toLanguageTag(bundle.getID())); |
|
32030
f2c22ba89d0f
8132494: Wrong CLDR resource bundle names for legacy ISO language codes
naoto
parents:
31263
diff
changeset
|
465 |
bundleGenerator.generateBundle("text", "FormatData", bundle.getJavaID(), true, formatDataMap, BundleType.PLAIN); |
13583 | 466 |
} |
467 |
} |
|
14765
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
468 |
|
31263 | 469 |
// For AvailableLocales |
470 |
metaInfo.get("AvailableLocales").add(toLanguageTag(bundle.getID())); |
|
13583 | 471 |
} |
472 |
||
473 |
bundleGenerator.generateMetaInfo(metaInfo); |
|
474 |
} |
|
475 |
||
31263 | 476 |
static final Map<String, String> aliases = new HashMap<>(); |
477 |
||
478 |
/** |
|
479 |
* Translate the aliases into the real entries in the bundle map. |
|
480 |
*/ |
|
481 |
static void handleAliases(Map<String, Object> bundleMap) { |
|
482 |
Set bundleKeys = bundleMap.keySet(); |
|
483 |
try { |
|
484 |
for (String key : aliases.keySet()) { |
|
485 |
String targetKey = aliases.get(key); |
|
486 |
if (bundleKeys.contains(targetKey)) { |
|
487 |
bundleMap.putIfAbsent(key, bundleMap.get(targetKey)); |
|
488 |
} |
|
489 |
} |
|
490 |
} catch (Exception ex) { |
|
491 |
Logger.getLogger(CLDRConverter.class.getName()).log(Level.SEVERE, null, ex); |
|
492 |
} |
|
493 |
} |
|
494 |
||
13583 | 495 |
/* |
496 |
* Returns the language portion of the given id. |
|
497 |
* If id is "root", "" is returned. |
|
498 |
*/ |
|
499 |
static String getLanguageCode(String id) { |
|
31263 | 500 |
return "root".equals(id) ? "" : Locale.forLanguageTag(id.replaceAll("_", "-")).getLanguage(); |
13583 | 501 |
} |
502 |
||
503 |
/** |
|
504 |
* Examine if the id includes the country (territory) code. If it does, it returns |
|
505 |
* the country code. |
|
506 |
* Otherwise, it returns null. eg. when the id is "zh_Hans_SG", it return "SG". |
|
36014
3d50784f2dc2
8148346: Reduce number of packages in jdk.localedata module
naoto
parents:
32510
diff
changeset
|
507 |
* It does NOT return UN M.49 code, e.g., '001', as those three digit numbers cannot |
31263 | 508 |
* be translated into package names. |
13583 | 509 |
*/ |
31263 | 510 |
static String getCountryCode(String id) { |
36014
3d50784f2dc2
8148346: Reduce number of packages in jdk.localedata module
naoto
parents:
32510
diff
changeset
|
511 |
String rgn = getRegionCode(id); |
3d50784f2dc2
8148346: Reduce number of packages in jdk.localedata module
naoto
parents:
32510
diff
changeset
|
512 |
return rgn.length() == 2 ? rgn: null; |
3d50784f2dc2
8148346: Reduce number of packages in jdk.localedata module
naoto
parents:
32510
diff
changeset
|
513 |
} |
3d50784f2dc2
8148346: Reduce number of packages in jdk.localedata module
naoto
parents:
32510
diff
changeset
|
514 |
|
3d50784f2dc2
8148346: Reduce number of packages in jdk.localedata module
naoto
parents:
32510
diff
changeset
|
515 |
/** |
3d50784f2dc2
8148346: Reduce number of packages in jdk.localedata module
naoto
parents:
32510
diff
changeset
|
516 |
* Examine if the id includes the region code. If it does, it returns |
3d50784f2dc2
8148346: Reduce number of packages in jdk.localedata module
naoto
parents:
32510
diff
changeset
|
517 |
* the region code. |
3d50784f2dc2
8148346: Reduce number of packages in jdk.localedata module
naoto
parents:
32510
diff
changeset
|
518 |
* Otherwise, it returns null. eg. when the id is "zh_Hans_SG", it return "SG". |
3d50784f2dc2
8148346: Reduce number of packages in jdk.localedata module
naoto
parents:
32510
diff
changeset
|
519 |
* It DOES return UN M.49 code, e.g., '001', as well as ISO 3166 two letter country codes. |
3d50784f2dc2
8148346: Reduce number of packages in jdk.localedata module
naoto
parents:
32510
diff
changeset
|
520 |
*/ |
3d50784f2dc2
8148346: Reduce number of packages in jdk.localedata module
naoto
parents:
32510
diff
changeset
|
521 |
static String getRegionCode(String id) { |
3d50784f2dc2
8148346: Reduce number of packages in jdk.localedata module
naoto
parents:
32510
diff
changeset
|
522 |
return Locale.forLanguageTag(id.replaceAll("_", "-")).getCountry(); |
13583 | 523 |
} |
524 |
||
525 |
private static class KeyComparator implements Comparator<String> { |
|
526 |
static KeyComparator INSTANCE = new KeyComparator(); |
|
527 |
||
528 |
private KeyComparator() { |
|
529 |
} |
|
530 |
||
14765
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
531 |
@Override |
13583 | 532 |
public int compare(String o1, String o2) { |
533 |
int len1 = o1.length(); |
|
534 |
int len2 = o2.length(); |
|
535 |
if (!isDigit(o1.charAt(0)) && !isDigit(o2.charAt(0))) { |
|
536 |
// Shorter string comes first unless either starts with a digit. |
|
537 |
if (len1 < len2) { |
|
538 |
return -1; |
|
539 |
} |
|
540 |
if (len1 > len2) { |
|
541 |
return 1; |
|
542 |
} |
|
543 |
} |
|
544 |
return o1.compareTo(o2); |
|
545 |
} |
|
546 |
||
547 |
private boolean isDigit(char c) { |
|
548 |
return c >= '0' && c <= '9'; |
|
549 |
} |
|
550 |
} |
|
551 |
||
552 |
private static Map<String, Object> extractLocaleNames(Map<String, Object> map, String id) { |
|
553 |
Map<String, Object> localeNames = new TreeMap<>(KeyComparator.INSTANCE); |
|
554 |
for (String key : map.keySet()) { |
|
555 |
if (key.startsWith(LOCALE_NAME_PREFIX)) { |
|
556 |
localeNames.put(key.substring(LOCALE_NAME_PREFIX.length()), map.get(key)); |
|
557 |
} |
|
558 |
} |
|
559 |
return localeNames; |
|
560 |
} |
|
561 |
||
562 |
@SuppressWarnings("AssignmentToForLoopParameter") |
|
563 |
private static Map<String, Object> extractCurrencyNames(Map<String, Object> map, String id, String names) |
|
564 |
throws Exception { |
|
565 |
Map<String, Object> currencyNames = new TreeMap<>(KeyComparator.INSTANCE); |
|
566 |
for (String key : map.keySet()) { |
|
567 |
if (key.startsWith(CURRENCY_NAME_PREFIX)) { |
|
568 |
currencyNames.put(key.substring(CURRENCY_NAME_PREFIX.length()), map.get(key)); |
|
569 |
} else if (key.startsWith(CURRENCY_SYMBOL_PREFIX)) { |
|
570 |
currencyNames.put(key.substring(CURRENCY_SYMBOL_PREFIX.length()), map.get(key)); |
|
571 |
} |
|
572 |
} |
|
573 |
return currencyNames; |
|
574 |
} |
|
575 |
||
576 |
private static Map<String, Object> extractZoneNames(Map<String, Object> map, String id) { |
|
14765
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
577 |
Map<String, Object> names = new HashMap<>(); |
32510
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
578 |
|
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
579 |
// Copy over missing time zone ids from JRE for English locale |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
580 |
if (id.equals("en")) { |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
581 |
Map<String[], String> jreMetaMap = new HashMap<>(); |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
582 |
jreTimeZoneNames.stream().forEach(e -> { |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
583 |
String tzid = (String)e[0]; |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
584 |
String[] data = (String[])e[1]; |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
585 |
|
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
586 |
if (map.get(TIMEZONE_ID_PREFIX + tzid) == null && |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
587 |
handlerMetaZones.get(tzid) == null) { |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
588 |
// First, check the CLDR meta key |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
589 |
Optional<Map.Entry<String, String>> cldrMeta = |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
590 |
handlerMetaZones.getData().entrySet().stream() |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
591 |
.filter(me -> |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
592 |
Arrays.deepEquals(data, |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
593 |
(String[])map.get(METAZONE_ID_PREFIX + me.getValue()))) |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
594 |
.findAny(); |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
595 |
if (cldrMeta.isPresent()) { |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
596 |
names.put(tzid, cldrMeta.get().getValue()); |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
597 |
} else { |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
598 |
// check the JRE meta key, add if there is not. |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
599 |
Optional<Map.Entry<String[], String>> jreMeta = |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
600 |
jreMetaMap.entrySet().stream() |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
601 |
.filter(jm -> Arrays.deepEquals(data, jm.getKey())) |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
602 |
.findAny(); |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
603 |
if (jreMeta.isPresent()) { |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
604 |
names.put(tzid, jreMeta.get().getValue()); |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
605 |
} else { |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
606 |
String metaName = "JRE_" + tzid.replaceAll("[/-]", "_"); |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
607 |
names.put(METAZONE_ID_PREFIX + metaName, data); |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
608 |
names.put(tzid, metaName); |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
609 |
jreMetaMap.put(data, metaName); |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
610 |
} |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
611 |
} |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
612 |
} |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
613 |
}); |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
614 |
} |
7530a468314c
8134384: Continuation of JDK-8130845 : A date string created by Date#toString() is not parseable neither with ENGLISH, US nor ROOT locale
naoto
parents:
32030
diff
changeset
|
615 |
|
14765
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
616 |
for (String tzid : handlerMetaZones.keySet()) { |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
617 |
String tzKey = TIMEZONE_ID_PREFIX + tzid; |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
618 |
Object data = map.get(tzKey); |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
619 |
if (data instanceof String[]) { |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
620 |
names.put(tzid, data); |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
621 |
} else { |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
622 |
String meta = handlerMetaZones.get(tzid); |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
623 |
if (meta != null) { |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
624 |
String metaKey = METAZONE_ID_PREFIX + meta; |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
625 |
data = map.get(metaKey); |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
626 |
if (data instanceof String[]) { |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
627 |
// Keep the metazone prefix here. |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
628 |
names.put(metaKey, data); |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
629 |
names.put(tzid, meta); |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
630 |
} |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
631 |
} |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
632 |
} |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
633 |
} |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
634 |
return names; |
13583 | 635 |
} |
636 |
||
637 |
private static Map<String, Object> extractCalendarData(Map<String, Object> map, String id) { |
|
638 |
Map<String, Object> calendarData = new LinkedHashMap<>(); |
|
639 |
copyIfPresent(map, "firstDayOfWeek", calendarData); |
|
640 |
copyIfPresent(map, "minimalDaysInFirstWeek", calendarData); |
|
641 |
return calendarData; |
|
642 |
} |
|
643 |
||
15281
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
644 |
static final String[] FORMAT_DATA_ELEMENTS = { |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
645 |
"MonthNames", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
646 |
"standalone.MonthNames", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
647 |
"MonthAbbreviations", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
648 |
"standalone.MonthAbbreviations", |
16852 | 649 |
"MonthNarrows", |
15281
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
650 |
"standalone.MonthNarrows", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
651 |
"DayNames", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
652 |
"standalone.DayNames", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
653 |
"DayAbbreviations", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
654 |
"standalone.DayAbbreviations", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
655 |
"DayNarrows", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
656 |
"standalone.DayNarrows", |
16852 | 657 |
"QuarterNames", |
658 |
"standalone.QuarterNames", |
|
659 |
"QuarterAbbreviations", |
|
660 |
"standalone.QuarterAbbreviations", |
|
661 |
"QuarterNarrows", |
|
662 |
"standalone.QuarterNarrows", |
|
15281
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
663 |
"AmPmMarkers", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
664 |
"narrow.AmPmMarkers", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
665 |
"long.Eras", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
666 |
"Eras", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
667 |
"narrow.Eras", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
668 |
"field.era", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
669 |
"field.year", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
670 |
"field.month", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
671 |
"field.week", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
672 |
"field.weekday", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
673 |
"field.dayperiod", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
674 |
"field.hour", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
675 |
"field.minute", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
676 |
"field.second", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
677 |
"field.zone", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
678 |
"TimePatterns", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
679 |
"DatePatterns", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
680 |
"DateTimePatterns", |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
681 |
"DateTimePatternChars" |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
682 |
}; |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
683 |
|
13583 | 684 |
private static Map<String, Object> extractFormatData(Map<String, Object> map, String id) { |
685 |
Map<String, Object> formatData = new LinkedHashMap<>(); |
|
686 |
for (CalendarType calendarType : CalendarType.values()) { |
|
31263 | 687 |
if (calendarType == CalendarType.GENERIC) { |
688 |
continue; |
|
689 |
} |
|
13583 | 690 |
String prefix = calendarType.keyElementName(); |
15281
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
691 |
for (String element : FORMAT_DATA_ELEMENTS) { |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
692 |
String key = prefix + element; |
16852 | 693 |
copyIfPresent(map, "java.time." + key, formatData); |
15281
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
694 |
copyIfPresent(map, key, formatData); |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
695 |
} |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
696 |
} |
31263 | 697 |
|
698 |
for (String key : map.keySet()) { |
|
15281
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
699 |
// Copy available calendar names |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
700 |
if (key.startsWith(CLDRConverter.CALENDAR_NAME_PREFIX)) { |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
701 |
String type = key.substring(CLDRConverter.CALENDAR_NAME_PREFIX.length()); |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
702 |
for (CalendarType calendarType : CalendarType.values()) { |
31263 | 703 |
if (calendarType == CalendarType.GENERIC) { |
704 |
continue; |
|
705 |
} |
|
15281
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
706 |
if (type.equals(calendarType.lname())) { |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
707 |
Object value = map.get(key); |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
708 |
formatData.put(key, value); |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
709 |
String ukey = CLDRConverter.CALENDAR_NAME_PREFIX + calendarType.uname(); |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
710 |
if (!key.equals(ukey)) { |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
711 |
formatData.put(ukey, value); |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
712 |
} |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
713 |
} |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
714 |
} |
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
715 |
} |
13583 | 716 |
} |
717 |
||
718 |
copyIfPresent(map, "DefaultNumberingSystem", formatData); |
|
15281
dd43cb9be0e1
8004489: Add support for Minguo and Hijrah calendars to CalendarNameProvider SPI
okutsu
parents:
14765
diff
changeset
|
719 |
|
13583 | 720 |
@SuppressWarnings("unchecked") |
721 |
List<String> numberingScripts = (List<String>) map.remove("numberingScripts"); |
|
722 |
if (numberingScripts != null) { |
|
723 |
for (String script : numberingScripts) { |
|
724 |
copyIfPresent(map, script + "." + "NumberElements", formatData); |
|
725 |
} |
|
726 |
} else { |
|
727 |
copyIfPresent(map, "NumberElements", formatData); |
|
728 |
} |
|
729 |
copyIfPresent(map, "NumberPatterns", formatData); |
|
730 |
return formatData; |
|
731 |
} |
|
732 |
||
733 |
private static void copyIfPresent(Map<String, Object> src, String key, Map<String, Object> dest) { |
|
734 |
Object value = src.get(key); |
|
735 |
if (value != null) { |
|
736 |
dest.put(key, value); |
|
737 |
} |
|
738 |
} |
|
739 |
||
740 |
// --- code below here is adapted from java.util.Properties --- |
|
741 |
private static final String specialSaveCharsJava = "\""; |
|
742 |
private static final String specialSaveCharsProperties = "=: \t\r\n\f#!"; |
|
743 |
||
744 |
/* |
|
745 |
* Converts unicodes to encoded \uxxxx |
|
746 |
* and writes out any of the characters in specialSaveChars |
|
747 |
* with a preceding slash |
|
748 |
*/ |
|
749 |
static String saveConvert(String theString, boolean useJava) { |
|
750 |
if (theString == null) { |
|
751 |
return ""; |
|
752 |
} |
|
753 |
||
754 |
String specialSaveChars; |
|
755 |
if (useJava) { |
|
756 |
specialSaveChars = specialSaveCharsJava; |
|
757 |
} else { |
|
758 |
specialSaveChars = specialSaveCharsProperties; |
|
759 |
} |
|
760 |
boolean escapeSpace = false; |
|
761 |
||
762 |
int len = theString.length(); |
|
763 |
StringBuilder outBuffer = new StringBuilder(len * 2); |
|
764 |
Formatter formatter = new Formatter(outBuffer, Locale.ROOT); |
|
765 |
||
766 |
for (int x = 0; x < len; x++) { |
|
767 |
char aChar = theString.charAt(x); |
|
768 |
switch (aChar) { |
|
769 |
case ' ': |
|
770 |
if (x == 0 || escapeSpace) { |
|
771 |
outBuffer.append('\\'); |
|
772 |
} |
|
773 |
outBuffer.append(' '); |
|
774 |
break; |
|
775 |
case '\\': |
|
776 |
outBuffer.append('\\'); |
|
777 |
outBuffer.append('\\'); |
|
778 |
break; |
|
779 |
case '\t': |
|
780 |
outBuffer.append('\\'); |
|
781 |
outBuffer.append('t'); |
|
782 |
break; |
|
783 |
case '\n': |
|
784 |
outBuffer.append('\\'); |
|
785 |
outBuffer.append('n'); |
|
786 |
break; |
|
787 |
case '\r': |
|
788 |
outBuffer.append('\\'); |
|
789 |
outBuffer.append('r'); |
|
790 |
break; |
|
791 |
case '\f': |
|
792 |
outBuffer.append('\\'); |
|
793 |
outBuffer.append('f'); |
|
794 |
break; |
|
795 |
default: |
|
14765
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
13583
diff
changeset
|
796 |
if (aChar < 0x0020 || (!USE_UTF8 && aChar > 0x007e)) { |
13583 | 797 |
formatter.format("\\u%04x", (int)aChar); |
798 |
} else { |
|
799 |
if (specialSaveChars.indexOf(aChar) != -1) { |
|
800 |
outBuffer.append('\\'); |
|
801 |
} |
|
802 |
outBuffer.append(aChar); |
|
803 |
} |
|
804 |
} |
|
805 |
} |
|
806 |
return outBuffer.toString(); |
|
807 |
} |
|
808 |
||
809 |
private static String toLanguageTag(String locName) { |
|
810 |
if (locName.indexOf('_') == -1) { |
|
811 |
return locName; |
|
812 |
} |
|
813 |
String tag = locName.replaceAll("_", "-"); |
|
814 |
Locale loc = Locale.forLanguageTag(tag); |
|
815 |
return loc.toLanguageTag(); |
|
816 |
} |
|
817 |
||
818 |
private static String toLocaleName(String tag) { |
|
819 |
if (tag.indexOf('-') == -1) { |
|
820 |
return tag; |
|
821 |
} |
|
822 |
return tag.replaceAll("-", "_"); |
|
823 |
} |
|
31263 | 824 |
|
825 |
private static void setupBaseLocales(String localeList) { |
|
826 |
Arrays.stream(localeList.split(",")) |
|
827 |
.map(Locale::forLanguageTag) |
|
828 |
.map(l -> Control.getControl(Control.FORMAT_DEFAULT) |
|
829 |
.getCandidateLocales("", l)) |
|
830 |
.forEach(BASE_LOCALES::addAll); |
|
13583 | 831 |
} |
31263 | 832 |
|
833 |
// applying parent locale rules to the passed candidates list |
|
834 |
// This has to match with the one in sun.util.cldr.CLDRLocaleProviderAdapter |
|
835 |
private static Map<Locale, Locale> childToParentLocaleMap = null; |
|
836 |
private static List<Locale> applyParentLocales(String baseName, List<Locale> candidates) { |
|
837 |
if (Objects.isNull(childToParentLocaleMap)) { |
|
838 |
childToParentLocaleMap = new HashMap<>(); |
|
839 |
parentLocalesMap.keySet().forEach(key -> { |
|
840 |
String parent = key.substring(PARENT_LOCALE_PREFIX.length()).replaceAll("_", "-"); |
|
841 |
parentLocalesMap.get(key).stream().forEach(child -> { |
|
842 |
childToParentLocaleMap.put(Locale.forLanguageTag(child), |
|
843 |
"root".equals(parent) ? Locale.ROOT : Locale.forLanguageTag(parent)); |
|
844 |
}); |
|
845 |
}); |
|
846 |
} |
|
847 |
||
848 |
// check irregular parents |
|
849 |
for (int i = 0; i < candidates.size(); i++) { |
|
850 |
Locale l = candidates.get(i); |
|
851 |
Locale p = childToParentLocaleMap.get(l); |
|
852 |
if (!l.equals(Locale.ROOT) && |
|
853 |
Objects.nonNull(p) && |
|
854 |
!candidates.get(i+1).equals(p)) { |
|
855 |
List<Locale> applied = candidates.subList(0, i+1); |
|
856 |
applied.addAll(applyParentLocales(baseName, defCon.getCandidateLocales(baseName, p))); |
|
857 |
return applied; |
|
858 |
} |
|
859 |
} |
|
860 |
||
861 |
return candidates; |
|
862 |
} |
|
863 |
} |