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