# HG changeset patch # User naoto # Date 1509654701 25200 # Thu Nov 02 13:31:41 2017 -0700 # Node ID e24c8d8c2648d5d7d8b8506a034e25fe6ebe4450 # Parent 77a5f2ef1807ec8f3b27de84b91957786b233271 [mq]: 8176841 diff --git a/make/jdk/src/classes/build/tools/cldrconverter/CLDRConverter.java b/make/jdk/src/classes/build/tools/cldrconverter/CLDRConverter.java --- a/make/jdk/src/classes/build/tools/cldrconverter/CLDRConverter.java +++ b/make/jdk/src/classes/build/tools/cldrconverter/CLDRConverter.java @@ -52,18 +52,27 @@ static final String LDML_DTD_SYSTEM_ID = "http://www.unicode.org/cldr/dtd/2.0/ldml.dtd"; static final String SPPL_LDML_DTD_SYSTEM_ID = "http://www.unicode.org/cldr/dtd/2.0/ldmlSupplemental.dtd"; + static final String BCP47_LDML_DTD_SYSTEM_ID = "http://www.unicode.org/cldr/dtd/2.0/ldmlBCP47.dtd"; + private static String CLDR_BASE = "../CLDR/21.0.1/"; static String LOCAL_LDML_DTD; static String LOCAL_SPPL_LDML_DTD; + static String LOCAL_BCP47_LDML_DTD; private static String SOURCE_FILE_DIR; private static String SPPL_SOURCE_FILE; private static String NUMBERING_SOURCE_FILE; private static String METAZONES_SOURCE_FILE; private static String LIKELYSUBTAGS_SOURCE_FILE; + private static String TIMEZONE_SOURCE_FILE; static String DESTINATION_DIR = "build/gensrc"; static final String LOCALE_NAME_PREFIX = "locale.displayname."; + static final String LOCALE_SEPARATOR = LOCALE_NAME_PREFIX + "separator"; + static final String LOCALE_KEYTYPE = LOCALE_NAME_PREFIX + "keytype"; + static final String LOCALE_KEY_PREFIX = LOCALE_NAME_PREFIX + "key."; + static final String LOCALE_TYPE_PREFIX = LOCALE_NAME_PREFIX + "type."; + static final String LOCALE_TYPE_PREFIX_CA = LOCALE_TYPE_PREFIX + "ca."; static final String CURRENCY_SYMBOL_PREFIX = "currency.symbol."; static final String CURRENCY_NAME_PREFIX = "currency.displayname."; static final String CALENDAR_NAME_PREFIX = "calendarname."; @@ -76,6 +85,7 @@ private static LikelySubtagsParseHandler handlerLikelySubtags; static NumberingSystemsParseHandler handlerNumbering; static MetaZonesParseHandler handlerMetaZones; + static TimeZoneParseHandler handlerTimeZone; private static BundleGenerator bundleGenerator; // java.base module related @@ -201,11 +211,13 @@ // Set up path names LOCAL_LDML_DTD = CLDR_BASE + "/dtd/ldml.dtd"; LOCAL_SPPL_LDML_DTD = CLDR_BASE + "/dtd/ldmlSupplemental.dtd"; + LOCAL_BCP47_LDML_DTD = CLDR_BASE + "/dtd/ldmlBCP47.dtd"; SOURCE_FILE_DIR = CLDR_BASE + "/main"; SPPL_SOURCE_FILE = CLDR_BASE + "/supplemental/supplementalData.xml"; LIKELYSUBTAGS_SOURCE_FILE = CLDR_BASE + "/supplemental/likelySubtags.xml"; NUMBERING_SOURCE_FILE = CLDR_BASE + "/supplemental/numberingSystems.xml"; METAZONES_SOURCE_FILE = CLDR_BASE + "/supplemental/metaZones.xml"; + TIMEZONE_SOURCE_FILE = CLDR_BASE + "/bcp47/timezone.xml"; if (BASE_LOCALES.isEmpty()) { setupBaseLocales("en-US"); @@ -215,6 +227,7 @@ // Parse data independent of locales parseSupplemental(); + parseBCP47(); List bundles = readBundleList(); convertBundles(bundles); @@ -321,11 +334,6 @@ static { // For generating information on supported locales. - metaInfo.put("LocaleNames", new TreeSet<>()); - metaInfo.put("CurrencyNames", new TreeSet<>()); - metaInfo.put("TimeZoneNames", new TreeSet<>()); - metaInfo.put("CalendarData", new TreeSet<>()); - metaInfo.put("FormatData", new TreeSet<>()); metaInfo.put("AvailableLocales", new TreeSet<>()); } @@ -337,20 +345,15 @@ if (bundle != null) { return bundle; } - SAXParserFactory factory = SAXParserFactory.newInstance(); - factory.setValidating(true); - SAXParser parser = factory.newSAXParser(); - enableFileAccess(parser); - LDMLParseHandler handler = new LDMLParseHandler(id); File file = new File(SOURCE_FILE_DIR + File.separator + id + ".xml"); if (!file.exists()) { // Skip if the file doesn't exist. return Collections.emptyMap(); } - + info("..... main directory ....."); - info("Reading file " + file); - parser.parse(file, handler); + LDMLParseHandler handler = new LDMLParseHandler(id); + parseLDMLFile(file, handler); bundle = handler.getData(); cldrBundles.put(id, bundle); @@ -379,14 +382,8 @@ // SupplementalData file also provides the "parent" locales which // are othrwise not to be fallen back. Process them here as well. // - info("..... Parsing supplementalData.xml ....."); - SAXParserFactory factorySuppl = SAXParserFactory.newInstance(); - factorySuppl.setValidating(true); - SAXParser parserSuppl = factorySuppl.newSAXParser(); - enableFileAccess(parserSuppl); handlerSuppl = new SupplementDataParseHandler(); - File fileSupply = new File(SPPL_SOURCE_FILE); - parserSuppl.parse(fileSupply, handlerSuppl); + parseLDMLFile(new File(SPPL_SOURCE_FILE), handlerSuppl); Map parentData = handlerSuppl.getData("root"); parentData.keySet().forEach(key -> { parentLocalesMap.put(key, new TreeSet( @@ -394,33 +391,33 @@ }); // Parse numberingSystems to get digit zero character information. - SAXParserFactory numberingParser = SAXParserFactory.newInstance(); - numberingParser.setValidating(true); - SAXParser parserNumbering = numberingParser.newSAXParser(); - enableFileAccess(parserNumbering); handlerNumbering = new NumberingSystemsParseHandler(); - File fileNumbering = new File(NUMBERING_SOURCE_FILE); - parserNumbering.parse(fileNumbering, handlerNumbering); + parseLDMLFile(new File(NUMBERING_SOURCE_FILE), handlerNumbering); // Parse metaZones to create mappings between Olson tzids and CLDR meta zone names - info("..... Parsing metaZones.xml ....."); - SAXParserFactory metazonesParser = SAXParserFactory.newInstance(); - metazonesParser.setValidating(true); - SAXParser parserMetaZones = metazonesParser.newSAXParser(); - enableFileAccess(parserMetaZones); handlerMetaZones = new MetaZonesParseHandler(); - File fileMetaZones = new File(METAZONES_SOURCE_FILE); - parserMetaZones.parse(fileMetaZones, handlerMetaZones); + parseLDMLFile(new File(METAZONES_SOURCE_FILE), handlerMetaZones); // Parse likelySubtags - info("..... Parsing likelySubtags.xml ....."); - SAXParserFactory likelySubtagsParser = SAXParserFactory.newInstance(); - likelySubtagsParser.setValidating(true); - SAXParser parserLikelySubtags = likelySubtagsParser.newSAXParser(); - enableFileAccess(parserLikelySubtags); handlerLikelySubtags = new LikelySubtagsParseHandler(); - File fileLikelySubtags = new File(LIKELYSUBTAGS_SOURCE_FILE); - parserLikelySubtags.parse(fileLikelySubtags, handlerLikelySubtags); + parseLDMLFile(new File(LIKELYSUBTAGS_SOURCE_FILE), handlerLikelySubtags); + } + + // Parsers for data in "bcp47" directory + // + private static void parseBCP47() throws Exception { + // Parse timezone + handlerTimeZone = new TimeZoneParseHandler(); + parseLDMLFile(new File(TIMEZONE_SOURCE_FILE), handlerTimeZone); + } + + private static void parseLDMLFile(File srcfile, AbstractLDMLHandler handler) throws Exception { + info("..... Parsing " + srcfile.getName() + " ....."); + SAXParserFactory pf = SAXParserFactory.newInstance(); + pf.setValidating(true); + SAXParser parser = pf.newSAXParser(); + enableFileAccess(parser); + parser.parse(srcfile, handler); } /** @@ -528,40 +525,30 @@ if (bundleTypes.contains(Bundle.Type.LOCALENAMES)) { Map localeNamesMap = extractLocaleNames(targetMap, bundle.getID()); if (!localeNamesMap.isEmpty() || bundle.isRoot()) { - metaInfo.get("LocaleNames").add(toLanguageTag(bundle.getID())); - addLikelySubtags(metaInfo, "LocaleNames", bundle.getID()); bundleGenerator.generateBundle("util", "LocaleNames", bundle.getJavaID(), true, localeNamesMap, BundleType.OPEN); } } if (bundleTypes.contains(Bundle.Type.CURRENCYNAMES)) { Map currencyNamesMap = extractCurrencyNames(targetMap, bundle.getID(), bundle.getCurrencies()); if (!currencyNamesMap.isEmpty() || bundle.isRoot()) { - metaInfo.get("CurrencyNames").add(toLanguageTag(bundle.getID())); - addLikelySubtags(metaInfo, "CurrencyNames", bundle.getID()); bundleGenerator.generateBundle("util", "CurrencyNames", bundle.getJavaID(), true, currencyNamesMap, BundleType.OPEN); } } if (bundleTypes.contains(Bundle.Type.TIMEZONENAMES)) { Map zoneNamesMap = extractZoneNames(targetMap, bundle.getID()); if (!zoneNamesMap.isEmpty() || bundle.isRoot()) { - metaInfo.get("TimeZoneNames").add(toLanguageTag(bundle.getID())); - addLikelySubtags(metaInfo, "TimeZoneNames", bundle.getID()); bundleGenerator.generateBundle("util", "TimeZoneNames", bundle.getJavaID(), true, zoneNamesMap, BundleType.TIMEZONE); } } if (bundleTypes.contains(Bundle.Type.CALENDARDATA)) { Map calendarDataMap = extractCalendarData(targetMap, bundle.getID()); if (!calendarDataMap.isEmpty() || bundle.isRoot()) { - metaInfo.get("CalendarData").add(toLanguageTag(bundle.getID())); - addLikelySubtags(metaInfo, "CalendarData", bundle.getID()); bundleGenerator.generateBundle("util", "CalendarData", bundle.getJavaID(), true, calendarDataMap, BundleType.PLAIN); } } if (bundleTypes.contains(Bundle.Type.FORMATDATA)) { Map formatDataMap = extractFormatData(targetMap, bundle.getID()); if (!formatDataMap.isEmpty() || bundle.isRoot()) { - metaInfo.get("FormatData").add(toLanguageTag(bundle.getID())); - addLikelySubtags(metaInfo, "FormatData", bundle.getID()); bundleGenerator.generateBundle("text", "FormatData", bundle.getJavaID(), true, formatDataMap, BundleType.PLAIN); } } @@ -570,43 +557,9 @@ metaInfo.get("AvailableLocales").add(toLanguageTag(bundle.getID())); addLikelySubtags(metaInfo, "AvailableLocales", bundle.getID()); } - addCldrImplicitLocales(metaInfo); bundleGenerator.generateMetaInfo(metaInfo); } - /** - * These are the Locales that are implicitly supported by CLDR. - * Adding them explicitly as likelySubtags here, will ensure that - * COMPAT locales do not precede them during ResourceBundle search path. - */ - private static void addCldrImplicitLocales(Map> metaInfo) { - metaInfo.get("LocaleNames").add("zh-Hans-CN"); - metaInfo.get("LocaleNames").add("zh-Hans-SG"); - metaInfo.get("LocaleNames").add("zh-Hant-HK"); - metaInfo.get("LocaleNames").add("zh-Hant-MO"); - metaInfo.get("LocaleNames").add("zh-Hant-TW"); - metaInfo.get("CurrencyNames").add("zh-Hans-CN"); - metaInfo.get("CurrencyNames").add("zh-Hans-SG"); - metaInfo.get("CurrencyNames").add("zh-Hant-HK"); - metaInfo.get("CurrencyNames").add("zh-Hant-MO"); - metaInfo.get("CurrencyNames").add("zh-Hant-TW"); - metaInfo.get("TimeZoneNames").add("zh-Hans-CN"); - metaInfo.get("TimeZoneNames").add("zh-Hans-SG"); - metaInfo.get("TimeZoneNames").add("zh-Hant-HK"); - metaInfo.get("TimeZoneNames").add("zh-Hant-MO"); - metaInfo.get("TimeZoneNames").add("zh-Hant-TW"); - metaInfo.get("TimeZoneNames").add("zh-HK"); - metaInfo.get("CalendarData").add("zh-Hans-CN"); - metaInfo.get("CalendarData").add("zh-Hans-SG"); - metaInfo.get("CalendarData").add("zh-Hant-HK"); - metaInfo.get("CalendarData").add("zh-Hant-MO"); - metaInfo.get("CalendarData").add("zh-Hant-TW"); - metaInfo.get("FormatData").add("zh-Hans-CN"); - metaInfo.get("FormatData").add("zh-Hans-SG"); - metaInfo.get("FormatData").add("zh-Hant-HK"); - metaInfo.get("FormatData").add("zh-Hant-MO"); - metaInfo.get("FormatData").add("zh-Hant-TW"); - } static final Map aliases = new HashMap<>(); /** @@ -695,9 +648,25 @@ Map localeNames = new TreeMap<>(KeyComparator.INSTANCE); for (String key : map.keySet()) { if (key.startsWith(LOCALE_NAME_PREFIX)) { - localeNames.put(key.substring(LOCALE_NAME_PREFIX.length()), map.get(key)); + switch (key) { + case LOCALE_SEPARATOR: + localeNames.put("ListCompositionPattern", map.get(key)); + break; + case LOCALE_KEYTYPE: + localeNames.put("ListKeyTypePattern", map.get(key)); + break; + default: + localeNames.put(key.substring(LOCALE_NAME_PREFIX.length()), map.get(key)); + break; + } } } + + if (id.equals("root")) { + // Add display name pattern, which is not in CLDR + localeNames.put("DisplayNamePattern", "{0,choice,0#|1#{1}|2#{1} ({2})}"); + } + return localeNames; } @@ -844,17 +813,19 @@ for (String key : map.keySet()) { // Copy available calendar names - if (key.startsWith(CLDRConverter.CALENDAR_NAME_PREFIX)) { - String type = key.substring(CLDRConverter.CALENDAR_NAME_PREFIX.length()); + if (key.startsWith(CLDRConverter.LOCALE_TYPE_PREFIX_CA)) { + String type = key.substring(CLDRConverter.LOCALE_TYPE_PREFIX_CA.length()); for (CalendarType calendarType : CalendarType.values()) { if (calendarType == CalendarType.GENERIC) { continue; } if (type.equals(calendarType.lname())) { Object value = map.get(key); - formatData.put(key, value); - String ukey = CLDRConverter.CALENDAR_NAME_PREFIX + calendarType.uname(); - if (!key.equals(ukey)) { + String dataKey = key.replace(LOCALE_TYPE_PREFIX_CA, + CALENDAR_NAME_PREFIX); + formatData.put(dataKey, value); + String ukey = CALENDAR_NAME_PREFIX + calendarType.uname(); + if (!dataKey.equals(ukey)) { formatData.put(ukey, value); } } @@ -874,6 +845,18 @@ copyIfPresent(map, "NumberElements", formatData); } copyIfPresent(map, "NumberPatterns", formatData); + + // put extra number elements for available scripts into formatData, if it is "root" + if (id.equals("root")) { + handlerNumbering.keySet().stream() + .filter(k -> !numberingScripts.contains(k)) + .forEach(k -> { + String[] ne = (String[])map.get("latn.NumberElements"); + String[] neNew = Arrays.copyOf(ne, ne.length); + neNew[4] = handlerNumbering.get(k).substring(0, 1); + formatData.put(k + ".NumberElements", neNew); + }); + } return formatData; } diff --git a/make/jdk/src/classes/build/tools/cldrconverter/LDMLParseHandler.java b/make/jdk/src/classes/build/tools/cldrconverter/LDMLParseHandler.java --- a/make/jdk/src/classes/build/tools/cldrconverter/LDMLParseHandler.java +++ b/make/jdk/src/classes/build/tools/cldrconverter/LDMLParseHandler.java @@ -76,12 +76,16 @@ // ignore this element - it has language and territory elements that aren't locale data pushIgnoredContainer(qName); break; - case "type": - if ("calendar".equals(attributes.getValue("key"))) { - pushStringEntry(qName, attributes, CLDRConverter.CALENDAR_NAME_PREFIX + attributes.getValue("type")); - } else { - pushIgnoredContainer(qName); - } + + // for LocaleNames + // copy string + case "localeSeparator": + pushStringEntry(qName, attributes, + CLDRConverter.LOCALE_SEPARATOR); + break; + case "localeKeyTypePattern": + pushStringEntry(qName, attributes, + CLDRConverter.LOCALE_KEYTYPE); break; case "language": @@ -96,6 +100,24 @@ attributes.getValue("type")); break; + case "key": + // for LocaleNames + // copy string + pushStringEntry(qName, attributes, + CLDRConverter.LOCALE_KEY_PREFIX + + convertOldKeyName(attributes.getValue("type"))); + break; + + case "type": + // for LocaleNames/CalendarNames + // copy string + pushStringEntry(qName, attributes, + CLDRConverter.LOCALE_TYPE_PREFIX + + convertOldKeyName(attributes.getValue("key")) + "." + + attributes.getValue("type")); + + break; + // // Currency information // @@ -515,26 +537,10 @@ currentNumberingSystem = script + "."; String digits = CLDRConverter.handlerNumbering.get(script); if (digits == null) { - throw new InternalError("null digits for " + script); - } - if (Character.isSurrogate(digits.charAt(0))) { - // DecimalFormatSymbols doesn't support supplementary characters as digit zero. pushIgnoredContainer(qName); break; } - // in case digits are in the reversed order, reverse back the order. - if (digits.charAt(0) > digits.charAt(digits.length() - 1)) { - StringBuilder sb = new StringBuilder(digits); - digits = sb.reverse().toString(); - } - // Check if the order is sequential. - char c0 = digits.charAt(0); - for (int i = 1; i < digits.length(); i++) { - if (digits.charAt(i) != c0 + i) { - pushIgnoredContainer(qName); - break symbols; - } - } + @SuppressWarnings("unchecked") List numberingScripts = (List) get("numberingScripts"); if (numberingScripts == null) { @@ -924,17 +930,36 @@ } } } else if (currentContainer instanceof Entry) { - Entry entry = (Entry) currentContainer; - Object value = entry.getValue(); - if (value != null) { - String key = entry.getKey(); - // Tweak for MonthNames for the root locale, Needed for - // SimpleDateFormat.format()/parse() roundtrip. - if (id.equals("root") && key.startsWith("MonthNames")) { - value = new DateFormatSymbols(Locale.US).getShortMonths(); - } - put(entry.getKey(), value); + Entry entry = (Entry) currentContainer; + Object value = entry.getValue(); + if (value != null) { + String key = entry.getKey(); + // Tweak for MonthNames for the root locale, Needed for + // SimpleDateFormat.format()/parse() roundtrip. + if (id.equals("root") && key.startsWith("MonthNames")) { + value = new DateFormatSymbols(Locale.US).getShortMonths(); } + put(entry.getKey(), value); } } } + + public String convertOldKeyName(String key) { + // TODO: This should not be hard coded. Instead, obtained from "alias" + // attribute in each "key" element. + switch (key) { + case "calendar": + return "ca"; + case "currency": + return "cu"; + case "collation": + return "co"; + case "numbers": + return "nu"; + case "timezone": + return "tz"; + default: + return key; + } + } +} diff --git a/make/jdk/src/classes/build/tools/cldrconverter/NumberingSystemsParseHandler.java b/make/jdk/src/classes/build/tools/cldrconverter/NumberingSystemsParseHandler.java --- a/make/jdk/src/classes/build/tools/cldrconverter/NumberingSystemsParseHandler.java +++ b/make/jdk/src/classes/build/tools/cldrconverter/NumberingSystemsParseHandler.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -54,9 +54,32 @@ public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { switch (qName) { case "numberingSystem": - if ("numeric".equals(attributes.getValue("type"))) { - // eg, - put(attributes.getValue("id"), attributes.getValue("digits")); + numberingSystem: { + if ("numeric".equals(attributes.getValue("type"))) { + // eg, + String script = attributes.getValue("id"); + String digits = attributes.getValue("digits"); + + if (Character.isSurrogate(digits.charAt(0))) { + // DecimalFormatSymbols doesn't support supplementary characters as digit zero. + break numberingSystem; + } + // in case digits are in the reversed order, reverse back the order. + if (digits.charAt(0) > digits.charAt(digits.length() - 1)) { + StringBuilder sb = new StringBuilder(digits); + digits = sb.reverse().toString(); + } + // Check if the order is sequential. + char c0 = digits.charAt(0); + for (int i = 1; i < digits.length(); i++) { + if (digits.charAt(i) != c0 + i) { + break numberingSystem; + } + } + + // script/digits are acceptable. + put(script, digits); + } } pushIgnoredContainer(qName); break; diff --git a/make/jdk/src/classes/build/tools/cldrconverter/ResourceBundleGenerator.java b/make/jdk/src/classes/build/tools/cldrconverter/ResourceBundleGenerator.java --- a/make/jdk/src/classes/build/tools/cldrconverter/ResourceBundleGenerator.java +++ b/make/jdk/src/classes/build/tools/cldrconverter/ResourceBundleGenerator.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -256,20 +256,21 @@ CLDRConverter.info("Generating file " + file); try (PrintWriter out = new PrintWriter(file, "us-ascii")) { - out.println(CopyrightHeaders.getOpenJDKCopyright()); + out.printf(CopyrightHeaders.getOpenJDKCopyright()); - out.println((CLDRConverter.isBaseModule ? "package sun.util.cldr;\n\n" : + out.printf((CLDRConverter.isBaseModule ? "package sun.util.cldr;\n\n" : "package sun.util.resources.cldr.provider;\n\n") + "import java.util.HashMap;\n" + "import java.util.Locale;\n" + "import java.util.Map;\n" - + "import sun.util.locale.provider.LocaleProviderAdapter;\n" - + "import sun.util.locale.provider.LocaleDataMetaInfo;\n"); + + "import sun.util.locale.provider.LocaleDataMetaInfo;\n" + + "import sun.util.locale.provider.LocaleProviderAdapter;\n\n"); out.printf("public class %s implements LocaleDataMetaInfo {\n", className); - out.println(" private static final Map resourceNameToLocales = new HashMap<>();\n" + - (CLDRConverter.isBaseModule ? - " private static final Map parentLocalesMap = new HashMap<>();\n\n" : "\n") + - " static {\n"); + out.printf(" private static final Map resourceNameToLocales = new HashMap<>();\n" + + (CLDRConverter.isBaseModule ? + " private static final Map parentLocalesMap = new HashMap<>();\n\n" : + "\n") + + " static {\n"); for (String key : metaInfo.keySet()) { if (key.startsWith(CLDRConverter.PARENT_LOCALE_PREFIX)) { @@ -296,30 +297,50 @@ } out.printf("\n });\n"); } else { - out.printf(" resourceNameToLocales.put(\"%s\",\n", key); - out.printf(" \"%s\");\n", - toLocaleList(key.equals("FormatData") ? metaInfo.get("AvailableLocales") : - metaInfo.get(key), false)); + if ("AvailableLocales".equals(key)) { + out.printf(" resourceNameToLocales.put(\"%s\",\n", key); + out.printf(" \"%s\");\n", toLocaleList(metaInfo.get(key), false)); + } } } - out.println(" }\n\n"); - out.println(" @Override\n" + + out.printf(" }\n\n"); + + // end of static initializer block. + + // Short TZ names for delayed initialization + if (CLDRConverter.isBaseModule) { + out.printf(" private static class TZShortIDMapHolder {\n"); + out.printf(" static final Map tzShortIDMap = new HashMap<>();\n"); + out.printf(" static {\n"); + CLDRConverter.handlerTimeZone.getData().entrySet().stream() + .forEach(e -> { + out.printf(" tzShortIDMap.put(\"%s\", \"%s\");\n", e.getKey(), + ((String)e.getValue())); + }); + out.printf(" }\n }\n\n"); + } + + out.printf(" @Override\n" + " public LocaleProviderAdapter.Type getType() {\n" + " return LocaleProviderAdapter.Type.CLDR;\n" + " }\n\n"); - out.println(" @Override\n" + + out.printf(" @Override\n" + " public String availableLanguageTags(String category) {\n" + " return resourceNameToLocales.getOrDefault(category, \"\");\n" + " }\n\n"); if (CLDRConverter.isBaseModule) { + out.printf(" @Override\n" + + " public Map tzShortIDs() {\n" + + " return TZShortIDMapHolder.tzShortIDMap;\n" + + " }\n\n"); out.printf(" public Map parentLocales() {\n" + " return parentLocalesMap;\n" + " }\n}"); } else { - out.println("}"); + out.printf("}"); } } } diff --git a/make/jdk/src/classes/build/tools/cldrconverter/TimeZoneParseHandler.java b/make/jdk/src/classes/build/tools/cldrconverter/TimeZoneParseHandler.java new file mode 100644 --- /dev/null +++ b/make/jdk/src/classes/build/tools/cldrconverter/TimeZoneParseHandler.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package build.tools.cldrconverter; + +import java.io.File; +import java.io.IOException; +import org.xml.sax.Attributes; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +/** + * Handles parsing of timezone.xml and produces a map from short timezone IDs to + * tz database IDs. + */ + +class TimeZoneParseHandler extends AbstractLDMLHandler { + + @Override + public InputSource resolveEntity(String publicID, String systemID) throws IOException, SAXException { + // avoid HTTP traffic to unicode.org + if (systemID.startsWith(CLDRConverter.BCP47_LDML_DTD_SYSTEM_ID)) { + return new InputSource((new File(CLDRConverter.LOCAL_BCP47_LDML_DTD)).toURI().toString()); + } + return null; + } + + @Override + public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { + switch (qName) { + case "type": + if (!isIgnored(attributes) && !attributes.getValue("deprecated").equals("true")) { + put(attributes.getValue("name"), attributes.getValue("alias")); + } + break; + default: + // treat anything else as a container + pushContainer(qName, attributes); + break; + } + } +} diff --git a/src/java.base/share/classes/java/text/DateFormat.java b/src/java.base/share/classes/java/text/DateFormat.java --- a/src/java.base/share/classes/java/text/DateFormat.java +++ b/src/java.base/share/classes/java/text/DateFormat.java @@ -97,6 +97,13 @@ * DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE); * } * + * + *

If the specified locale contains "ca" (calendar), "rg" (region override), + * and/or "tz" (timezone) Unicode + * extensions, the calendar, the country and/or the time zone for formatting + * are overriden. If both "ca" and "rg" are specified, the calendar from "ca" + * extension supersedes the implicit one from "rg" extension. + * *

You can use a DateFormat to parse also. *

*
{@code
diff --git a/src/java.base/share/classes/java/text/DateFormatSymbols.java b/src/java.base/share/classes/java/text/DateFormatSymbols.java
--- a/src/java.base/share/classes/java/text/DateFormatSymbols.java
+++ b/src/java.base/share/classes/java/text/DateFormatSymbols.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -49,6 +49,7 @@
 import java.util.ResourceBundle;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
+import sun.util.locale.provider.CalendarDataUtility;
 import sun.util.locale.provider.LocaleProviderAdapter;
 import sun.util.locale.provider.LocaleServiceProviderPool;
 import sun.util.locale.provider.ResourceBundleBasedAdapter;
@@ -82,6 +83,10 @@
  * 
*
* + *

If the locale contains "rg" (region override) + * Unicode extension, + * the symbols are overriden for the designated region. + * *

* DateFormatSymbols objects are cloneable. When you obtain * a DateFormatSymbols object, feel free to modify the @@ -716,15 +721,18 @@ } dfs = new DateFormatSymbols(false); + // check for region override + Locale override = CalendarDataUtility.findRegionOverride(locale).orElse(locale); + // Initialize the fields from the ResourceBundle for locale. LocaleProviderAdapter adapter - = LocaleProviderAdapter.getAdapter(DateFormatSymbolsProvider.class, locale); + = LocaleProviderAdapter.getAdapter(DateFormatSymbolsProvider.class, override); // Avoid any potential recursions if (!(adapter instanceof ResourceBundleBasedAdapter)) { adapter = LocaleProviderAdapter.getResourceBundleBased(); } ResourceBundle resource - = ((ResourceBundleBasedAdapter)adapter).getLocaleData().getDateFormatData(locale); + = ((ResourceBundleBasedAdapter)adapter).getLocaleData().getDateFormatData(override); dfs.locale = locale; // JRE and CLDR use different keys diff --git a/src/java.base/share/classes/java/text/DecimalFormatSymbols.java b/src/java.base/share/classes/java/text/DecimalFormatSymbols.java --- a/src/java.base/share/classes/java/text/DecimalFormatSymbols.java +++ b/src/java.base/share/classes/java/text/DecimalFormatSymbols.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -44,6 +44,7 @@ import java.text.spi.DecimalFormatSymbolsProvider; import java.util.Currency; import java.util.Locale; +import sun.util.locale.provider.CalendarDataUtility; import sun.util.locale.provider.LocaleProviderAdapter; import sun.util.locale.provider.LocaleServiceProviderPool; import sun.util.locale.provider.ResourceBundleBasedAdapter; @@ -56,6 +57,10 @@ * of these symbols, you can get the DecimalFormatSymbols object from * your DecimalFormat and modify it. * + *

If the locale contains "rg" (region override) + * Unicode extension, + * the symbols are overriden for the designated region. + * * @see java.util.Locale * @see DecimalFormat * @author Mark Davis @@ -609,13 +614,18 @@ private void initialize( Locale locale ) { this.locale = locale; + // check for region override + Locale override = locale.getUnicodeLocaleType("nu") == null ? + CalendarDataUtility.findRegionOverride(locale).orElse(locale) : + locale; + // get resource bundle data - LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(DecimalFormatSymbolsProvider.class, locale); + LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(DecimalFormatSymbolsProvider.class, override); // Avoid potential recursions if (!(adapter instanceof ResourceBundleBasedAdapter)) { adapter = LocaleProviderAdapter.getResourceBundleBased(); } - Object[] data = adapter.getLocaleResources(locale).getDecimalFormatSymbolsData(); + Object[] data = adapter.getLocaleResources(override).getDecimalFormatSymbolsData(); String[] numberElements = (String[]) data[0]; decimalSeparator = numberElements[0].charAt(0); diff --git a/src/java.base/share/classes/java/text/NumberFormat.java b/src/java.base/share/classes/java/text/NumberFormat.java --- a/src/java.base/share/classes/java/text/NumberFormat.java +++ b/src/java.base/share/classes/java/text/NumberFormat.java @@ -96,7 +96,14 @@ * NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH); * } * - * You can also use a NumberFormat to parse numbers: + * + *

If the locale contains "nu" (numbers) and/or "rg" (region override) + * Unicode extensions, + * the decimal digits, and/or the country used for formatting are overriden. + * If both "nu" and "rg" are specified, the decimal digits from "nu" + * extension supersedes the implicit one from "rg" extension. + * + *

You can also use a NumberFormat to parse numbers: *

*
{@code
  * myNumber = nf.parse(myString);
diff --git a/src/java.base/share/classes/java/text/SimpleDateFormat.java b/src/java.base/share/classes/java/text/SimpleDateFormat.java
--- a/src/java.base/share/classes/java/text/SimpleDateFormat.java
+++ b/src/java.base/share/classes/java/text/SimpleDateFormat.java
@@ -672,7 +672,7 @@
             // However, the calendar should use the current default TimeZone.
             // If this is not contained in the locale zone strings, then the zone
             // will be formatted using generic GMT+/-H:MM nomenclature.
-            calendar = Calendar.getInstance(TimeZone.getDefault(), loc);
+            calendar = Calendar.getInstance(loc);
         }
     }
 
diff --git a/src/java.base/share/classes/java/time/format/DateTimeFormatter.java b/src/java.base/share/classes/java/time/format/DateTimeFormatter.java
--- a/src/java.base/share/classes/java/time/format/DateTimeFormatter.java
+++ b/src/java.base/share/classes/java/time/format/DateTimeFormatter.java
@@ -97,6 +97,7 @@
 import java.util.Map;
 import java.util.Objects;
 import java.util.Set;
+import sun.util.locale.provider.TimeZoneNameUtility;
 
 /**
  * Formatter for printing and parsing date-time objects.
@@ -129,7 +130,12 @@
  * The {@link #withLocale withLocale} method returns a new formatter that
  * overrides the locale. The locale affects some aspects of formatting and
  * parsing. For example, the {@link #ofLocalizedDate ofLocalizedDate} provides a
- * formatter that uses the locale specific date format.
+ * formatter that uses the locale specific date format. If the locale contains
+ * "ca" (calendar), "rg" (region override) and/or "tz" (timezone)
+ * Unicode extensions,
+ * the chronology and/or the zone are also overriden. If both "ca" and "rg" are
+ * specified, the chronology from "ca" extension supersedes the implicit one
+ * from "rg" extension.
  * 

* The {@link #withChronology withChronology} method returns a new formatter * that overrides the chronology. If overridden, the date-time value is @@ -548,8 +554,13 @@ * For example, {@code d MMM uuuu} will format 2011-12-03 as '3 Dec 2011'. *

* The formatter will use the {@link Locale#getDefault(Locale.Category) default FORMAT locale}. - * This can be changed using {@link DateTimeFormatter#withLocale(Locale)} on the returned formatter + * This can be changed using {@link DateTimeFormatter#withLocale(Locale)} on the returned formatter. * Alternatively use the {@link #ofPattern(String, Locale)} variant of this method. + * If the default locale contains "ca" (calendar), "rg" (region override) and/or "tz" (timezone) + * Unicode extensions, + * the chronology and/or the zone are overriden. If both "ca" and "rg" are + * specified, the chronology from "ca" extension supersedes the implicit one + * from "rg" extension. *

* The returned formatter has no override chronology or zone. * It uses {@link ResolverStyle#SMART SMART} resolver style. @@ -572,7 +583,12 @@ * For example, {@code d MMM uuuu} will format 2011-12-03 as '3 Dec 2011'. *

* The formatter will use the specified locale. - * This can be changed using {@link DateTimeFormatter#withLocale(Locale)} on the returned formatter + * This can be changed using {@link DateTimeFormatter#withLocale(Locale)} on the returned formatter. + * If the specified locale contains "ca" (calendar), "rg" (region override) and/or "tz" (timezone) + * Unicode extensions, + * the chronology and/or the zone are overriden. If both "ca" and "rg" are + * specified, the chronology from "ca" extension supersedes the implicit one + * from "rg" extension. *

* The returned formatter has no override chronology or zone. * It uses {@link ResolverStyle#SMART SMART} resolver style. @@ -1441,7 +1457,12 @@ * Returns a copy of this formatter with a new locale. *

* This is used to lookup any part of the formatter needing specific - * localization, such as the text or localized pattern. + * localization, such as the text or localized pattern. If the new + * locale contains "ca" (calendar), "rg" (region override) and/or "tz" (timezone) + * Unicode extensions, + * the chronology and/or the zone are also overriden. If both "ca" and "rg" are + * specified, the chronology from "ca" extension supersedes the implicit one + * from "rg" extension. *

* This instance is immutable and unaffected by this method call. * @@ -1452,7 +1473,17 @@ if (this.locale.equals(locale)) { return this; } - return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, resolverFields, chrono, zone); + + // Check for chronology/timezone in locale object + Chronology c = locale.getUnicodeLocaleType("ca") != null ? + Chronology.ofLocale(locale) : chrono; + String tzType = locale.getUnicodeLocaleType("tz"); + ZoneId z = tzType != null ? + TimeZoneNameUtility.convertLDMLShortID(tzType) + .map(ZoneId::of) + .orElse(zone) : + zone; + return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, resolverFields, c, z); } //----------------------------------------------------------------------- diff --git a/src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java b/src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java --- a/src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java +++ b/src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -120,6 +120,7 @@ import java.util.concurrent.ConcurrentMap; import sun.text.spi.JavaTimeDateTimePatternProvider; +import sun.util.locale.provider.CalendarDataUtility; import sun.util.locale.provider.LocaleProviderAdapter; import sun.util.locale.provider.LocaleResources; import sun.util.locale.provider.TimeZoneNameUtility; @@ -216,7 +217,8 @@ LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(JavaTimeDateTimePatternProvider.class, locale); JavaTimeDateTimePatternProvider provider = adapter.getJavaTimeDateTimePatternProvider(); String pattern = provider.getJavaTimeDateTimePattern(convertStyle(timeStyle), - convertStyle(dateStyle), chrono.getCalendarType(), locale); + convertStyle(dateStyle), chrono.getCalendarType(), + CalendarDataUtility.findRegionOverride(locale).orElse(locale)); return pattern; } @@ -2160,6 +2162,12 @@ * This will create a formatter with the {@linkplain Locale#getDefault(Locale.Category) default FORMAT locale}. * Numbers will be printed and parsed using the standard DecimalStyle. * The resolver style will be {@link ResolverStyle#SMART SMART}. + * If the default locale contains "ca" (calendar), "rg" (region override) + * and/or "tz" (timezone) + * Unicode extensions, + * the chronology and/or the zone are overriden. If both "ca" and "rg" are + * specified, the chronology from "ca" extension supersedes the implicit one + * from "rg" extension. *

* Calling this method will end any open optional sections by repeatedly * calling {@link #optionalEnd()} before creating the formatter. @@ -2180,6 +2188,12 @@ * This will create a formatter with the specified locale. * Numbers will be printed and parsed using the standard DecimalStyle. * The resolver style will be {@link ResolverStyle#SMART SMART}. + * If the specified locale contains "ca" (calendar), "rg" (region override) + * and/or "tz" (timezone) + * Unicode extensions, + * the chronology and/or the zone are overriden. If both "ca" and "rg" are + * specified, the chronology from "ca" extension supersedes the implicit one + * from "rg" extension. *

* Calling this method will end any open optional sections by repeatedly * calling {@link #optionalEnd()} before creating the formatter. @@ -2218,8 +2232,18 @@ optionalEnd(); } CompositePrinterParser pp = new CompositePrinterParser(printerParsers, false); + + // Check for chronology/timezone in locale object + Chronology c = locale.getUnicodeLocaleType("ca") != null ? + Chronology.ofLocale(locale) : chrono; + String tzType = locale.getUnicodeLocaleType("tz"); + ZoneId z = tzType != null ? + TimeZoneNameUtility.convertLDMLShortID(tzType) + .map(ZoneId::of) + .orElse(null) : + null; return new DateTimeFormatter(pp, locale, DecimalStyle.STANDARD, - resolverStyle, null, chrono, null); + resolverStyle, null, c, z); } //----------------------------------------------------------------------- diff --git a/src/java.base/share/classes/java/time/format/DateTimeTextProvider.java b/src/java.base/share/classes/java/time/format/DateTimeTextProvider.java --- a/src/java.base/share/classes/java/time/format/DateTimeTextProvider.java +++ b/src/java.base/share/classes/java/time/format/DateTimeTextProvider.java @@ -510,7 +510,9 @@ @SuppressWarnings("unchecked") static T getLocalizedResource(String key, Locale locale) { LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased() - .getLocaleResources(locale); + .getLocaleResources( + CalendarDataUtility.findRegionOverride(locale) + .orElse(locale)); ResourceBundle rb = lr.getJavaTimeFormatData(); return rb.containsKey(key) ? (T) rb.getObject(key) : null; } diff --git a/src/java.base/share/classes/java/time/temporal/WeekFields.java b/src/java.base/share/classes/java/time/temporal/WeekFields.java --- a/src/java.base/share/classes/java/time/temporal/WeekFields.java +++ b/src/java.base/share/classes/java/time/temporal/WeekFields.java @@ -286,13 +286,17 @@ * Obtains an instance of {@code WeekFields} appropriate for a locale. *

* This will look up appropriate values from the provider of localization data. + * If the locale contains "fw" (First day of week) and/or "rg" + * (Region Override) + * Unicode extensions, returned instance will reflect the values specified with + * those extensions. If both "fw" and "rg" are specified, the value from + * "fw" extension supersedes the implicit one from "rg" extension. * * @param locale the locale to use, not null * @return the week-definition, not null */ public static WeekFields of(Locale locale) { Objects.requireNonNull(locale, "locale"); - locale = new Locale(locale.getLanguage(), locale.getCountry()); // elminate variants int calDow = CalendarDataUtility.retrieveFirstDayOfWeek(locale); DayOfWeek dow = DayOfWeek.SUNDAY.plus(calDow - 1); diff --git a/src/java.base/share/classes/java/util/Calendar.java b/src/java.base/share/classes/java/util/Calendar.java --- a/src/java.base/share/classes/java/util/Calendar.java +++ b/src/java.base/share/classes/java/util/Calendar.java @@ -58,6 +58,7 @@ import sun.util.calendar.ZoneInfo; import sun.util.locale.provider.CalendarDataUtility; import sun.util.locale.provider.LocaleProviderAdapter; +import sun.util.locale.provider.TimeZoneNameUtility; import sun.util.spi.CalendarProvider; /** @@ -128,9 +129,14 @@ * * Calendar defines a locale-specific seven day week using two * parameters: the first day of the week and the minimal days in first week - * (from 1 to 7). These numbers are taken from the locale resource data when a - * Calendar is constructed. They may also be specified explicitly - * through the methods for setting their values. + * (from 1 to 7). These numbers are taken from the locale resource data or the + * locale itself when a Calendar is constructed. If the designated + * locale contains "fw" and/or "rg" + * Unicode extensions, the first day of the week will be obtained according to + * those extensions. If both "fw" and "rg" are specified, the value from "fw" + * extension supersedes the implicit one from "rg" extension. + * They may also be specified explicitly through the methods for setting their + * values. * *

When setting or getting the WEEK_OF_MONTH or * WEEK_OF_YEAR fields, Calendar must determine the @@ -1463,7 +1469,7 @@ locale = Locale.getDefault(); } if (zone == null) { - zone = TimeZone.getDefault(); + zone = defaultTimeZone(locale); } Calendar cal; if (type == null) { @@ -1610,7 +1616,8 @@ */ public static Calendar getInstance() { - return createCalendar(TimeZone.getDefault(), Locale.getDefault(Locale.Category.FORMAT)); + Locale aLocale = Locale.getDefault(Locale.Category.FORMAT); + return createCalendar(defaultTimeZone(aLocale), aLocale); } /** @@ -1637,7 +1644,7 @@ */ public static Calendar getInstance(Locale aLocale) { - return createCalendar(TimeZone.getDefault(), aLocale); + return createCalendar(defaultTimeZone(aLocale), aLocale); } /** @@ -1655,6 +1662,16 @@ return createCalendar(zone, aLocale); } + private static TimeZone defaultTimeZone(Locale l) { + TimeZone defaultTZ = TimeZone.getDefault(); + String shortTZID = l.getUnicodeLocaleType("tz"); + return shortTZID != null ? + TimeZoneNameUtility.convertLDMLShortID(shortTZID) + .map(TimeZone::getTimeZone) + .orElse(defaultTZ) : + defaultTZ; + } + private static Calendar createCalendar(TimeZone zone, Locale aLocale) { diff --git a/src/java.base/share/classes/java/util/Currency.java b/src/java.base/share/classes/java/util/Currency.java --- a/src/java.base/share/classes/java/util/Currency.java +++ b/src/java.base/share/classes/java/util/Currency.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -28,7 +28,6 @@ import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.File; -import java.io.FileInputStream; import java.io.FileReader; import java.io.InputStream; import java.io.IOException; @@ -42,6 +41,7 @@ import java.util.regex.Pattern; import java.util.regex.Matcher; import java.util.spi.CurrencyNameProvider; +import sun.util.locale.provider.CalendarDataUtility; import sun.util.locale.provider.LocaleServiceProviderPool; import sun.util.logging.PlatformLogger; @@ -348,6 +348,13 @@ * until December 31, 2001, and the Euro from January 1, 2002, local time * of the respective countries. *

+ * If the specified locale contains "cu" and/or "rg" + * Unicode extensions, + * the instance returned from this method reflects + * the values specified with those extensions. If both "cu" and "rg" are + * specified, the currency from "cu" extension supersedes the implicit one + * from "rg" extension. + *

* The method returns null for territories that don't * have a currency, such as Antarctica. * @@ -361,12 +368,19 @@ * is not a supported ISO 3166 country code. */ public static Currency getInstance(Locale locale) { - String country = locale.getCountry(); - if (country == null) { - throw new NullPointerException(); + // check for locale overrides + String override = locale.getUnicodeLocaleType("cu"); + if (override != null) { + try { + return getInstance(override.toUpperCase(Locale.ROOT)); + } catch (IllegalArgumentException iae) { + // override currency is invalid. Fall through. + } } - if (country.length() != 2) { + String country = CalendarDataUtility.findRegionOverride(locale).orElse(locale).getCountry(); + + if (country == null || !country.matches("^[a-zA-Z]{2}$")) { throw new IllegalArgumentException(); } @@ -482,6 +496,12 @@ * locale is the US, while for other locales it may be "US$". If no * symbol can be determined, the ISO 4217 currency code is returned. *

+ * If the default {@link Locale.Category#DISPLAY DISPLAY} locale + * contains "rg" (region override) + * Unicode extension, + * the symbol returned from this method reflects + * the value specified with that extension. + *

* This is equivalent to calling * {@link #getSymbol(Locale) * getSymbol(Locale.getDefault(Locale.Category.DISPLAY))}. @@ -498,6 +518,11 @@ * For example, for the US Dollar, the symbol is "$" if the specified * locale is the US, while for other locales it may be "US$". If no * symbol can be determined, the ISO 4217 currency code is returned. + *

+ * If the specified locale contains "rg" (region override) + * Unicode extension, + * the symbol returned from this method reflects + * the value specified with that extension. * * @param locale the locale for which a display name for this currency is * needed @@ -507,6 +532,7 @@ public String getSymbol(Locale locale) { LocaleServiceProviderPool pool = LocaleServiceProviderPool.getPool(CurrencyNameProvider.class); + locale = CalendarDataUtility.findRegionOverride(locale).orElse(locale); String symbol = pool.getLocalizedObject( CurrencyNameGetter.INSTANCE, locale, currencyCode, SYMBOL); diff --git a/src/java.base/share/classes/java/util/Locale.java b/src/java.base/share/classes/java/util/Locale.java --- a/src/java.base/share/classes/java/util/Locale.java +++ b/src/java.base/share/classes/java/util/Locale.java @@ -48,6 +48,7 @@ import java.text.MessageFormat; import java.util.concurrent.ConcurrentHashMap; import java.util.spi.LocaleNameProvider; +import java.util.stream.Collectors; import sun.security.action.GetPropertyAction; import sun.util.locale.BaseLocale; @@ -62,6 +63,7 @@ import sun.util.locale.provider.LocaleProviderAdapter; import sun.util.locale.provider.LocaleResources; import sun.util.locale.provider.LocaleServiceProviderPool; +import sun.util.locale.provider.TimeZoneNameUtility; /** * A Locale object represents a specific geographical, political, @@ -665,10 +667,12 @@ /** * Display types for retrieving localized names from the name providers. */ - private static final int DISPLAY_LANGUAGE = 0; - private static final int DISPLAY_COUNTRY = 1; - private static final int DISPLAY_VARIANT = 2; - private static final int DISPLAY_SCRIPT = 3; + private static final int DISPLAY_LANGUAGE = 0; + private static final int DISPLAY_COUNTRY = 1; + private static final int DISPLAY_VARIANT = 2; + private static final int DISPLAY_SCRIPT = 3; + private static final int DISPLAY_UEXT_KEY = 4; + private static final int DISPLAY_UEXT_TYPE = 5; /** * Private constructor used by getInstance method @@ -942,11 +946,14 @@ variant = props.getProperty("user.variant", ""); } - return getInstance(language, script, country, variant, null); + return getInstance(language, script, country, variant, + getDefaultExtensions(props.getProperty("user.extensions", "")) + .orElse(null)); } private static Locale initDefault(Locale.Category category) { Properties props = GetPropertyAction.privilegedGetProperties(); + return getInstance( props.getProperty(category.languageKey, defaultLocale.getLanguage()), @@ -956,7 +963,22 @@ defaultLocale.getCountry()), props.getProperty(category.variantKey, defaultLocale.getVariant()), - null); + getDefaultExtensions(props.getProperty(category.extensionsKey, "")) + .orElse(defaultLocale.getLocaleExtensions())); + } + + private static Optional getDefaultExtensions(String extensionsProp) { + LocaleExtensions exts = null; + + try { + exts = new InternalLocaleBuilder() + .setExtensions(extensionsProp) + .getLocaleExtensions(); + } catch (LocaleSyntaxException e) { + // just ignore this incorrect property + } + + return Optional.ofNullable(exts); } /** @@ -1771,7 +1793,7 @@ * @exception NullPointerException if inLocale is null */ public String getDisplayLanguage(Locale inLocale) { - return getDisplayString(baseLocale.getLanguage(), inLocale, DISPLAY_LANGUAGE); + return getDisplayString(baseLocale.getLanguage(), null, inLocale, DISPLAY_LANGUAGE); } /** @@ -1801,7 +1823,7 @@ * @since 1.7 */ public String getDisplayScript(Locale inLocale) { - return getDisplayString(baseLocale.getScript(), inLocale, DISPLAY_SCRIPT); + return getDisplayString(baseLocale.getScript(), null, inLocale, DISPLAY_SCRIPT); } /** @@ -1844,29 +1866,24 @@ * @exception NullPointerException if inLocale is null */ public String getDisplayCountry(Locale inLocale) { - return getDisplayString(baseLocale.getRegion(), inLocale, DISPLAY_COUNTRY); + return getDisplayString(baseLocale.getRegion(), null, inLocale, DISPLAY_COUNTRY); } - private String getDisplayString(String code, Locale inLocale, int type) { - if (code.length() == 0) { + private String getDisplayString(String code, String cat, Locale inLocale, int type) { + Objects.requireNonNull(inLocale); + Objects.requireNonNull(code); + + if (code.isEmpty()) { return ""; } - if (inLocale == null) { - throw new NullPointerException(); - } - LocaleServiceProviderPool pool = LocaleServiceProviderPool.getPool(LocaleNameProvider.class); - String key = (type == DISPLAY_VARIANT ? "%%"+code : code); + String rbKey = (type == DISPLAY_VARIANT ? "%%"+code : code); String result = pool.getLocalizedObject( LocaleNameGetter.INSTANCE, - inLocale, key, type, code); - if (result != null) { - return result; - } - - return code; + inLocale, rbKey, type, code, cat); + return result != null ? result : code; } /** @@ -1894,29 +1911,31 @@ if (baseLocale.getVariant().length() == 0) return ""; - LocaleResources lr = LocaleProviderAdapter.forJRE().getLocaleResources(inLocale); + LocaleResources lr = LocaleProviderAdapter + .getResourceBundleBased() + .getLocaleResources(inLocale); String names[] = getDisplayVariantArray(inLocale); // Get the localized patterns for formatting a list, and use // them to format the list. return formatList(names, - lr.getLocaleName("ListPattern"), lr.getLocaleName("ListCompositionPattern")); } /** * Returns a name for the locale that is appropriate for display to the * user. This will be the values returned by getDisplayLanguage(), - * getDisplayScript(), getDisplayCountry(), and getDisplayVariant() assembled - * into a single string. The non-empty values are used in order, - * with the second and subsequent names in parentheses. For example: + * getDisplayScript(), getDisplayCountry(), getDisplayVariant() and + * optional Unicode extensions + * assembled into a single string. The non-empty values are used in order, with + * the second and subsequent names in parentheses. For example: *

- * language (script, country, variant)
- * language (country)
- * language (variant)
- * script (country)
- * country
+ * language (script, country, variant(, extension)*)
+ * language (country(, extension)*)
+ * language (variant(, extension)*)
+ * script (country(, extension)*)
+ * country (extension)*
*
* depending on which fields are specified in the locale. If the * language, script, country, and variant fields are all empty, @@ -1931,16 +1950,17 @@ /** * Returns a name for the locale that is appropriate for display * to the user. This will be the values returned by - * getDisplayLanguage(), getDisplayScript(),getDisplayCountry(), - * and getDisplayVariant() assembled into a single string. - * The non-empty values are used in order, - * with the second and subsequent names in parentheses. For example: + * getDisplayLanguage(), getDisplayScript(),getDisplayCountry() + * getDisplayVariant(), and optional + * Unicode extensions assembled into a single string. The non-empty + * values are used in order, with the second and subsequent names in + * parentheses. For example: *
- * language (script, country, variant)
- * language (country)
- * language (variant)
- * script (country)
- * country
+ * language (script, country, variant(, extension)*)
+ * language (country(, extension)*)
+ * language (variant(, extension)*)
+ * script (country(, extension)*)
+ * country (extension)*
*
* depending on which fields are specified in the locale. If the * language, script, country, and variant fields are all empty, @@ -1951,7 +1971,9 @@ * @throws NullPointerException if inLocale is null */ public String getDisplayName(Locale inLocale) { - LocaleResources lr = LocaleProviderAdapter.forJRE().getLocaleResources(inLocale); + LocaleResources lr = LocaleProviderAdapter + .getResourceBundleBased() + .getLocaleResources(inLocale); String languageName = getDisplayLanguage(inLocale); String scriptName = getDisplayScript(inLocale); @@ -1960,7 +1982,6 @@ // Get the localized patterns for formatting a display name. String displayNamePattern = lr.getLocaleName("DisplayNamePattern"); - String listPattern = lr.getLocaleName("ListPattern"); String listCompositionPattern = lr.getLocaleName("ListCompositionPattern"); // The display name consists of a main name, followed by qualifiers. @@ -1977,7 +1998,7 @@ if (variantNames.length == 0) { return ""; } else { - return formatList(variantNames, listPattern, listCompositionPattern); + return formatList(variantNames, listCompositionPattern); } } ArrayList names = new ArrayList<>(4); @@ -1994,6 +2015,16 @@ names.addAll(Arrays.asList(variantNames)); } + // add Unicode extensions + if (localeExtensions != null) { + localeExtensions.getUnicodeLocaleAttributes().stream() + .map(key -> getDisplayString(key, null, inLocale, DISPLAY_UEXT_KEY)) + .forEach(names::add); + localeExtensions.getUnicodeLocaleKeys().stream() + .map(key -> getDisplayKeyTypeExtensionString(key, lr, inLocale)) + .forEach(names::add); + } + // The first one in the main name mainName = names.get(0); @@ -2014,7 +2045,7 @@ // list case, but this is more efficient, and we want it to be // efficient since all the language-only locales will not have any // qualifiers. - qualifierNames.length != 0 ? formatList(qualifierNames, listPattern, listCompositionPattern) : null + qualifierNames.length != 0 ? formatList(qualifierNames, listCompositionPattern) : null }; if (displayNamePattern != null) { @@ -2121,74 +2152,77 @@ // For each variant token, lookup the display name. If // not found, use the variant name itself. for (int i=0; i 3) { - MessageFormat format = new MessageFormat(listCompositionPattern); - stringList = composeList(format, stringList); + switch (stringList.length) { + case 0: + return ""; + case 1: + return stringList[0]; + default: + return Arrays.stream(stringList).reduce("", + (s1, s2) -> { + if (s1.equals("")) { + return s2; + } + if (s2.equals("")) { + return s1; + } + return MessageFormat.format(pattern, s1, s2); + }); } - - // Rebuild the argument list with the list length as the first element - Object[] args = new Object[stringList.length + 1]; - System.arraycopy(stringList, 0, args, 1, stringList.length); - args[0] = stringList.length; - - // Format it using the pattern in the resource - MessageFormat format = new MessageFormat(listPattern); - return format.format(args); - } - - /** - * Given a list of strings, return a list shortened to three elements. - * Shorten it by applying the given format to the first two elements - * recursively. - * @param format a format which takes two arguments - * @param list a list of strings - * @return if the list is three elements or shorter, the same list; - * otherwise, a new list of three elements. - */ - private static String[] composeList(MessageFormat format, String[] list) { - if (list.length <= 3) return list; - - // Use the given format to compose the first two elements into one - String[] listItems = { list[0], list[1] }; - String newItem = format.format(listItems); - - // Form a new list one element shorter - String[] newList = new String[list.length-1]; - System.arraycopy(list, 2, newList, 1, newList.length-1); - newList[0] = newItem; - - // Recurse - return composeList(format, newList); } // Duplicate of sun.util.locale.UnicodeLocaleExtension.isKey in order to @@ -2345,9 +2379,10 @@ Locale locale, String key, Object... params) { - assert params.length == 2; + assert params.length == 3; int type = (Integer)params[0]; String code = (String)params[1]; + String cat = (String)params[2]; switch(type) { case DISPLAY_LANGUAGE: @@ -2358,6 +2393,10 @@ return localeNameProvider.getDisplayVariant(code, locale); case DISPLAY_SCRIPT: return localeNameProvider.getDisplayScript(code, locale); + case DISPLAY_UEXT_KEY: + return localeNameProvider.getDisplayUnicodeExtensionKey(code, locale); + case DISPLAY_UEXT_TYPE: + return localeNameProvider.getDisplayUnicodeExtensionType(code, cat, locale); default: assert false; // shouldn't happen } @@ -2384,7 +2423,8 @@ DISPLAY("user.language.display", "user.script.display", "user.country.display", - "user.variant.display"), + "user.variant.display", + "user.extensions.display"), /** * Category used to represent the default locale for @@ -2393,19 +2433,23 @@ FORMAT("user.language.format", "user.script.format", "user.country.format", - "user.variant.format"); + "user.variant.format", + "user.extensions.format"); - Category(String languageKey, String scriptKey, String countryKey, String variantKey) { + Category(String languageKey, String scriptKey, String countryKey, + String variantKey, String extensionsKey) { this.languageKey = languageKey; this.scriptKey = scriptKey; this.countryKey = countryKey; this.variantKey = variantKey; + this.extensionsKey = extensionsKey; } final String languageKey; final String scriptKey; final String countryKey; final String variantKey; + final String extensionsKey; } /** diff --git a/src/java.base/share/classes/java/util/spi/LocaleNameProvider.java b/src/java.base/share/classes/java/util/spi/LocaleNameProvider.java --- a/src/java.base/share/classes/java/util/spi/LocaleNameProvider.java +++ b/src/java.base/share/classes/java/util/spi/LocaleNameProvider.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -26,6 +26,7 @@ package java.util.spi; import java.util.Locale; +import java.util.Objects; /** * An abstract class for service providers that @@ -141,4 +142,54 @@ * @see java.util.Locale#getDisplayVariant(java.util.Locale) */ public abstract String getDisplayVariant(String variant, Locale locale); + + /** + * Returns a localized name for the given + * Unicode extension key, + * and the given locale that is appropriate for display to the user. + * If the name returned cannot be localized according to locale, + * this method returns null. + * @implSpec the default implementation returns null. + * @param key the Unicode Extension key, not null. + * @param locale the desired locale, not null. + * @return the name of the given key string for the specified locale, + * or null if it's not available. + * @exception NullPointerException if key or locale is null + * @exception IllegalArgumentException if locale isn't + * one of the locales returned from + * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales() + * getAvailableLocales()}. + * @since 10 + */ + public String getDisplayUnicodeExtensionKey(String key, Locale locale) { + Objects.requireNonNull(key); + Objects.requireNonNull(locale); + return null; + } + + /** + * Returns a localized name for the given + * Unicode extension type, + * and the given locale that is appropriate for display to the user. + * If the name returned cannot be localized according to locale, + * this method returns null. + * @implSpec the default implementation returns null. + * @param type the Unicode Extension type, not null. + * @param key the Unicode Extension key for this type, not null. + * @param locale the desired locale, not null. + * @return the name of the given type string for the specified locale, + * or null if it's not available. + * @exception NullPointerException if key, type or locale is null + * @exception IllegalArgumentException if locale isn't + * one of the locales returned from + * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales() + * getAvailableLocales()}. + * @since 10 + */ + public String getDisplayUnicodeExtensionType(String type, String key, Locale locale) { + Objects.requireNonNull(type); + Objects.requireNonNull(key); + Objects.requireNonNull(locale); + return null; + } } diff --git a/src/java.base/share/classes/sun/launcher/LauncherHelper.java b/src/java.base/share/classes/sun/launcher/LauncherHelper.java --- a/src/java.base/share/classes/sun/launcher/LauncherHelper.java +++ b/src/java.base/share/classes/sun/launcher/LauncherHelper.java @@ -268,7 +268,7 @@ Locale locale = Locale.getDefault(); ostream.println(LOCALE_SETTINGS); ostream.println(INDENT + "default locale = " + - locale.getDisplayLanguage()); + locale.getDisplayName()); ostream.println(INDENT + "default display locale = " + Locale.getDefault(Category.DISPLAY).getDisplayName()); ostream.println(INDENT + "default format locale = " + diff --git a/src/java.base/share/classes/sun/util/cldr/CLDRLocaleProviderAdapter.java b/src/java.base/share/classes/sun/util/cldr/CLDRLocaleProviderAdapter.java --- a/src/java.base/share/classes/sun/util/cldr/CLDRLocaleProviderAdapter.java +++ b/src/java.base/share/classes/sun/util/cldr/CLDRLocaleProviderAdapter.java @@ -37,15 +37,15 @@ import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.Objects; +import java.util.Optional; import java.util.ServiceLoader; import java.util.ServiceConfigurationError; import java.util.Set; import java.util.StringTokenizer; import java.util.concurrent.ConcurrentHashMap; import sun.util.locale.provider.JRELocaleProviderAdapter; +import sun.util.locale.provider.LocaleDataMetaInfo; import sun.util.locale.provider.LocaleProviderAdapter; -import sun.util.locale.provider.LocaleDataMetaInfo; /** * LocaleProviderAdapter implementation for the CLDR locale data. @@ -123,6 +123,10 @@ @Override protected Set createLanguageTagSet(String category) { + // Assume all categories support the same set as AvailableLocales + // in CLDR adapter. + category = "AvailableLocales"; + // Directly call Base tags, as we know it's in the base module. String supportedLocaleString = baseMetaInfo.availableLanguageTags(category); String nonBaseTags = null; @@ -220,4 +224,11 @@ || langtags.contains(locale.stripExtensions().toLanguageTag()) || langtags.contains(getEquivalentLoc(locale).toLanguageTag()); } + + /** + * Returns the time zone ID from an LDML's short ID + */ + public Optional getTimeZoneID(String shortID) { + return Optional.ofNullable(baseMetaInfo.tzShortIDs().get(shortID)); + } } diff --git a/src/java.base/share/classes/sun/util/locale/provider/CalendarDataUtility.java b/src/java.base/share/classes/sun/util/locale/provider/CalendarDataUtility.java --- a/src/java.base/share/classes/sun/util/locale/provider/CalendarDataUtility.java +++ b/src/java.base/share/classes/sun/util/locale/provider/CalendarDataUtility.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -28,6 +28,7 @@ import static java.util.Calendar.*; import java.util.Locale; import java.util.Map; +import java.util.Optional; import java.util.spi.CalendarDataProvider; import java.util.spi.CalendarNameProvider; @@ -47,10 +48,34 @@ } public static int retrieveFirstDayOfWeek(Locale locale) { + // Look for the Unicode Extension in the locale parameter + if (locale.hasExtensions()) { + String fw = locale.getUnicodeLocaleType("fw"); + if (fw != null) { + switch (fw.toLowerCase(Locale.ROOT)) { + case "mon": + return MONDAY; + case "tue": + return TUESDAY; + case "wed": + return WEDNESDAY; + case "thu": + return THURSDAY; + case "fri": + return FRIDAY; + case "sat": + return SATURDAY; + case "sun": + return SUNDAY; + } + } + } + LocaleServiceProviderPool pool = LocaleServiceProviderPool.getPool(CalendarDataProvider.class); Integer value = pool.getLocalizedObject(CalendarWeekParameterGetter.INSTANCE, - locale, true, FIRST_DAY_OF_WEEK); + findRegionOverride(locale).orElse(locale), + true, FIRST_DAY_OF_WEEK); return (value != null && (value >= SUNDAY && value <= SATURDAY)) ? value : SUNDAY; } @@ -58,7 +83,8 @@ LocaleServiceProviderPool pool = LocaleServiceProviderPool.getPool(CalendarDataProvider.class); Integer value = pool.getLocalizedObject(CalendarWeekParameterGetter.INSTANCE, - locale, true, MINIMAL_DAYS_IN_FIRST_WEEK); + findRegionOverride(locale).orElse(locale), + true, MINIMAL_DAYS_IN_FIRST_WEEK); return (value != null && (value >= 1 && value <= 7)) ? value : 1; } @@ -102,6 +128,31 @@ return map; } + /** + * Utility to look for a region override extension + */ + public static Optional findRegionOverride(Locale l) { + String rg = l.getUnicodeLocaleType("rg"); + Locale override = null; + + if (rg != null && rg.length() == 6) { + // UN M.49 code should not be allowed here + // cannot use regex here, as it could be a recursive call + rg = rg.toUpperCase(Locale.ROOT); + if (rg.charAt(0) >= 0x0041 && + rg.charAt(0) <= 0x005A && + rg.charAt(1) >= 0x0041 && + rg.charAt(1) <= 0x005A && + rg.substring(2).equals("ZZZZ")) { + override = new Locale.Builder().setLocale(l) + .setRegion(rg.substring(0, 2)) + .build(); + } + } + + return Optional.ofNullable(override); + } + static String normalizeCalendarType(String requestID) { String type; if (requestID.equals("gregorian") || requestID.equals("iso8601")) { @@ -179,7 +230,7 @@ } } - private static class CalendarWeekParameterGetter + private static class CalendarWeekParameterGetter implements LocaleServiceProviderPool.LocalizedObjectGetter { private static final CalendarWeekParameterGetter INSTANCE = diff --git a/src/java.base/share/classes/sun/util/locale/provider/DateFormatProviderImpl.java b/src/java.base/share/classes/sun/util/locale/provider/DateFormatProviderImpl.java --- a/src/java.base/share/classes/sun/util/locale/provider/DateFormatProviderImpl.java +++ b/src/java.base/share/classes/sun/util/locale/provider/DateFormatProviderImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -32,6 +32,7 @@ import java.util.Locale; import java.util.MissingResourceException; import java.util.Set; +import java.util.TimeZone; /** * Concrete implementation of the {@link java.text.spi.DateFormatProvider @@ -147,11 +148,14 @@ throw new NullPointerException(); } - SimpleDateFormat sdf = new SimpleDateFormat("", locale); + // Check for region override + Locale rg = CalendarDataUtility.findRegionOverride(locale).orElse(locale); + + SimpleDateFormat sdf = new SimpleDateFormat("", rg); Calendar cal = sdf.getCalendar(); try { String pattern = LocaleProviderAdapter.forType(type) - .getLocaleResources(locale).getDateTimePattern(timeStyle, dateStyle, + .getLocaleResources(rg).getDateTimePattern(timeStyle, dateStyle, cal); sdf.applyPattern(pattern); } catch (MissingResourceException mre) { @@ -159,6 +163,15 @@ sdf.applyPattern("M/d/yy h:mm a"); } + // Check for timezone override + String tz = locale.getUnicodeLocaleType("tz"); + if (tz != null) { + sdf.setTimeZone( + TimeZoneNameUtility.convertLDMLShortID(tz) + .map(TimeZone::getTimeZone) + .orElseGet(sdf::getTimeZone)); + } + return sdf; } diff --git a/src/java.base/share/classes/sun/util/locale/provider/LocaleDataMetaInfo.java b/src/java.base/share/classes/sun/util/locale/provider/LocaleDataMetaInfo.java --- a/src/java.base/share/classes/sun/util/locale/provider/LocaleDataMetaInfo.java +++ b/src/java.base/share/classes/sun/util/locale/provider/LocaleDataMetaInfo.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,6 +25,8 @@ package sun.util.locale.provider; +import java.util.Map; + /** * LocaleData meta info SPI * @@ -46,4 +48,13 @@ * @return concatenated language tags, separated by a space. */ public String availableLanguageTags(String category); + + /** + * Returns a map for short time zone ids in BCP47 Unicode extension and + * the long time zone ids. + * @return map of short id to long ids, separated by a space. + */ + default public Map tzShortIDs() { + return null; + } } diff --git a/src/java.base/share/classes/sun/util/locale/provider/LocaleNameProviderImpl.java b/src/java.base/share/classes/sun/util/locale/provider/LocaleNameProviderImpl.java --- a/src/java.base/share/classes/sun/util/locale/provider/LocaleNameProviderImpl.java +++ b/src/java.base/share/classes/sun/util/locale/provider/LocaleNameProviderImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -168,6 +168,26 @@ return getDisplayString("%%"+vrnt, locale); } + /** + * @inheritDoc + */ + @Override + public String getDisplayUnicodeExtensionKey(String key, Locale locale) { + String rbKey = "key." + key; + String name = getDisplayString(rbKey, locale); + return rbKey.equals(name) ? key : name; + } + + /** + * @inheritDoc + */ + @Override + public String getDisplayUnicodeExtensionType(String extType, String key, Locale locale) { + String rbKey = "type." + key + "." + extType; + String name = getDisplayString(rbKey, locale); + return rbKey.equals(name) ? extType : name; + } + private String getDisplayString(String key, Locale locale) { if (key == null || locale == null) { throw new NullPointerException(); diff --git a/src/java.base/share/classes/sun/util/locale/provider/NumberFormatProviderImpl.java b/src/java.base/share/classes/sun/util/locale/provider/NumberFormatProviderImpl.java --- a/src/java.base/share/classes/sun/util/locale/provider/NumberFormatProviderImpl.java +++ b/src/java.base/share/classes/sun/util/locale/provider/NumberFormatProviderImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -173,9 +173,14 @@ throw new NullPointerException(); } + // Check for region override + Locale override = locale.getUnicodeLocaleType("nu") == null ? + CalendarDataUtility.findRegionOverride(locale).orElse(locale) : + locale; + LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type); - String[] numberPatterns = adapter.getLocaleResources(locale).getNumberPatterns(); - DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale); + String[] numberPatterns = adapter.getLocaleResources(override).getNumberPatterns(); + DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(override); int entry = (choice == INTEGERSTYLE) ? NUMBERSTYLE : choice; DecimalFormat format = new DecimalFormat(numberPatterns[entry], symbols); diff --git a/src/java.base/share/classes/sun/util/locale/provider/SPILocaleProviderAdapter.java b/src/java.base/share/classes/sun/util/locale/provider/SPILocaleProviderAdapter.java --- a/src/java.base/share/classes/sun/util/locale/provider/SPILocaleProviderAdapter.java +++ b/src/java.base/share/classes/sun/util/locale/provider/SPILocaleProviderAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -571,6 +571,20 @@ assert lnp != null; return lnp.getDisplayVariant(variant, locale); } + + @Override + public String getDisplayUnicodeExtensionKey(String key, Locale locale) { + LocaleNameProvider lnp = getImpl(locale); + assert lnp != null; + return lnp.getDisplayUnicodeExtensionKey(key, locale); + } + + @Override + public String getDisplayUnicodeExtensionType(String extType, String key, Locale locale) { + LocaleNameProvider lnp = getImpl(locale); + assert lnp != null; + return lnp.getDisplayUnicodeExtensionType(extType, key, locale); + } } static class TimeZoneNameProviderDelegate extends TimeZoneNameProvider diff --git a/src/java.base/share/classes/sun/util/locale/provider/TimeZoneNameUtility.java b/src/java.base/share/classes/sun/util/locale/provider/TimeZoneNameUtility.java --- a/src/java.base/share/classes/sun/util/locale/provider/TimeZoneNameUtility.java +++ b/src/java.base/share/classes/sun/util/locale/provider/TimeZoneNameUtility.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -31,10 +31,13 @@ import java.util.Locale; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.spi.TimeZoneNameProvider; import sun.util.calendar.ZoneInfo; +import sun.util.cldr.CLDRLocaleProviderAdapter; +import static sun.util.locale.provider.LocaleProviderAdapter.Type; /** * Utility class that deals with the localized time zone names @@ -152,6 +155,18 @@ } } + /** + * Converts the time zone id from LDML's 5-letter id to tzdb's id + * + * @param shortID time zone short ID defined in LDML + * @return the tzdb's time zone ID + */ + public static Optional convertLDMLShortID(String shortID) { + return ((CLDRLocaleProviderAdapter)LocaleProviderAdapter.forType(Type.CLDR)) + .getTimeZoneID(shortID) + .map(id -> id.replaceAll("\\s.*", "")); + } + private static String[] retrieveDisplayNamesImpl(String id, Locale locale) { LocaleServiceProviderPool pool = LocaleServiceProviderPool.getPool(TimeZoneNameProvider.class); diff --git a/src/java.base/share/classes/sun/util/resources/LocaleNames.properties b/src/java.base/share/classes/sun/util/resources/LocaleNames.properties --- a/src/java.base/share/classes/sun/util/resources/LocaleNames.properties +++ b/src/java.base/share/classes/sun/util/resources/LocaleNames.properties @@ -1,5 +1,5 @@ # -# Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved. # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. # # This code is free software; you can redistribute it and/or modify it @@ -1164,8 +1164,7 @@ # locale name patterns -# rarely localized DisplayNamePattern={0,choice,0#|1#{1}|2#{1} ({2})} -ListPattern={0,choice,0#|1#{1}|2#{1},{2}|3#{1},{2},{3}} +ListKeyTypePattern={0}:{1} ListCompositionPattern={0},{1} diff --git a/src/jdk.localedata/share/classes/sun/util/cldr/resources/common/bcp47/timezone.xml b/src/jdk.localedata/share/classes/sun/util/cldr/resources/common/bcp47/timezone.xml new file mode 100644 --- /dev/null +++ b/src/jdk.localedata/share/classes/sun/util/cldr/resources/common/bcp47/timezone.xml @@ -0,0 +1,470 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/jdk.localedata/share/classes/sun/util/cldr/resources/common/dtd/ldmlBCP47.dtd b/src/jdk.localedata/share/classes/sun/util/cldr/resources/common/dtd/ldmlBCP47.dtd new file mode 100644 --- /dev/null +++ b/src/jdk.localedata/share/classes/sun/util/cldr/resources/common/dtd/ldmlBCP47.dtd @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/jdk/java/util/Calendar/Bug4302966.java b/test/jdk/java/util/Calendar/Bug4302966.java --- a/test/jdk/java/util/Calendar/Bug4302966.java +++ b/test/jdk/java/util/Calendar/Bug4302966.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,7 +23,7 @@ /* * @test - * @bug 4302966 + * @bug 4302966 8176841 * @modules jdk.localedata * @summary In Czech Republic first day of week is Monday not Sunday */ @@ -34,7 +34,7 @@ public class Bug4302966 { public static void main(String[] args) { - Calendar czechCalendar = Calendar.getInstance(new Locale("cs")); + Calendar czechCalendar = Calendar.getInstance(new Locale("cs", "CZ")); int firstDayOfWeek = czechCalendar.getFirstDayOfWeek(); if (firstDayOfWeek != Calendar.MONDAY) { throw new RuntimeException(); diff --git a/test/jdk/java/util/Locale/bcp47u/CalendarTests.java b/test/jdk/java/util/Locale/bcp47u/CalendarTests.java new file mode 100644 --- /dev/null +++ b/test/jdk/java/util/Locale/bcp47u/CalendarTests.java @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * + * @test + * @bug 8176841 + * @summary Tests Calendar class deals with Unicode extensions + * correctly. + * @modules jdk.localedata + * @run testng/othervm CalendarTests + */ + +import static org.testng.Assert.assertEquals; + +import java.text.DateFormat; +import java.util.Calendar; +import java.util.Locale; +import java.util.TimeZone; + +import org.testng.annotations.AfterTest; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +/** + * Test Calendar with BCP47 U extensions + */ +@Test +public class CalendarTests { + private static TimeZone defaultTZ; + + private static final TimeZone ASIATOKYO = TimeZone.getTimeZone("Asia/Tokyo"); + private static final TimeZone AMLA = TimeZone.getTimeZone("America/Los_Angeles"); + + private static final Locale JPTYO = Locale.forLanguageTag("en-u-tz-jptyo"); + private static final Locale USLAX = Locale.forLanguageTag("en-u-tz-uslax"); + + private static final Locale FW_SUN = Locale.forLanguageTag("en-US-u-fw-sun"); + private static final Locale FW_MON = Locale.forLanguageTag("en-US-u-fw-mon"); + private static final Locale FW_TUE = Locale.forLanguageTag("en-US-u-fw-tue"); + private static final Locale FW_WED = Locale.forLanguageTag("en-US-u-fw-wed"); + private static final Locale FW_THU = Locale.forLanguageTag("en-US-u-fw-thu"); + private static final Locale FW_FRI = Locale.forLanguageTag("en-US-u-fw-fri"); + private static final Locale FW_SAT = Locale.forLanguageTag("en-US-u-fw-sat"); + + @BeforeTest + public void beforeTest() { + defaultTZ = TimeZone.getDefault(); + TimeZone.setDefault(AMLA); + } + + @AfterTest + public void afterTest() { + TimeZone.setDefault(defaultTZ); + } + + @DataProvider(name="tz") + Object[][] tz() { + return new Object[][] { + // Locale, Expected Zone, + {JPTYO, ASIATOKYO}, + {USLAX, AMLA}, + + // invalid + {Locale.forLanguageTag("en-US-u-tz-jpzzz"), AMLA} + }; + } + + @DataProvider(name="firstDayOfWeek") + Object[][] firstDayOfWeek () { + return new Object[][] { + // Locale, Expected DayOfWeek, + {Locale.US, Calendar.SUNDAY}, + {FW_SUN, Calendar.SUNDAY}, + {FW_MON, Calendar.MONDAY}, + {FW_TUE, Calendar.TUESDAY}, + {FW_WED, Calendar.WEDNESDAY}, + {FW_THU, Calendar.THURSDAY}, + {FW_FRI, Calendar.FRIDAY}, + {FW_SAT, Calendar.SATURDAY}, + + // invalid case + {Locale.forLanguageTag("en-US-u-fw-xxx"), Calendar.SUNDAY}, + + // region override + {Locale.forLanguageTag("en-US-u-rg-gbzzzz"), Calendar.MONDAY}, + + // "fw" and "rg". + {Locale.forLanguageTag("en-US-u-fw-wed-rg-gbzzzz"), Calendar.WEDNESDAY}, + {Locale.forLanguageTag("en-US-u-fw-xxx-rg-gbzzzz"), Calendar.MONDAY}, + {Locale.forLanguageTag("en-US-u-fw-xxx-rg-zzzz"), Calendar.SUNDAY}, + }; + } + + @DataProvider(name="minDaysInFirstWeek") + Object[][] minDaysInFrstWeek () { + return new Object[][] { + // Locale, Expected minDay, + {Locale.US, 1}, + + // region override + {Locale.forLanguageTag("en-US-u-rg-gbzzzz"), 4}, + }; + } + + @Test(dataProvider="tz") + public void test_tz(Locale locale, TimeZone zoneExpected) { + DateFormat df = DateFormat.getTimeInstance(DateFormat.FULL, locale); + assertEquals(df.getTimeZone(), zoneExpected); + + Calendar c = Calendar.getInstance(locale); + assertEquals(c.getTimeZone(), zoneExpected); + + c = new Calendar.Builder().setLocale(locale).build(); + assertEquals(c.getTimeZone(), zoneExpected); + } + + @Test(dataProvider="firstDayOfWeek") + public void test_firstDayOfWeek(Locale locale, int dowExpected) { + Calendar c = Calendar.getInstance(locale); + assertEquals(c.getFirstDayOfWeek(), dowExpected); + + c = new Calendar.Builder().setLocale(locale).build(); + assertEquals(c.getFirstDayOfWeek(), dowExpected); + } + + @Test(dataProvider="minDaysInFirstWeek") + public void test_minDaysInFirstWeek(Locale locale, int minDaysExpected) { + Calendar c = Calendar.getInstance(locale); + assertEquals(c.getMinimalDaysInFirstWeek(), minDaysExpected); + + c = new Calendar.Builder().setLocale(locale).build(); + assertEquals(c.getMinimalDaysInFirstWeek(), minDaysExpected); + } +} diff --git a/test/jdk/java/util/Locale/bcp47u/CurrencyTests.java b/test/jdk/java/util/Locale/bcp47u/CurrencyTests.java new file mode 100644 --- /dev/null +++ b/test/jdk/java/util/Locale/bcp47u/CurrencyTests.java @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * + * @test + * @bug 8176841 + * @summary Tests Currency class instantiates correctly with Unicode + * extensions + * @modules jdk.localedata + * @run testng/othervm CurrencyTests + */ + +import static org.testng.Assert.assertEquals; + +import java.util.Currency; +import java.util.Locale; + +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +/** + * Test Currency with BCP47 U extensions + */ +@Test +public class CurrencyTests { + private static final Currency USD = Currency.getInstance("USD"); + private static final Currency CAD = Currency.getInstance("CAD"); + private static final Currency JPY = Currency.getInstance("JPY"); + + @DataProvider(name="getInstanceData") + Object[][] getInstanceData() { + return new Object[][] { + // Locale, Expected Currency + // "cu" + {Locale.forLanguageTag("en-US-u-cu-jpy"), JPY}, + {Locale.forLanguageTag("ja-JP-u-cu-usd"), USD}, + {Locale.forLanguageTag("en-US-u-cu-foobar"), USD}, + {Locale.forLanguageTag("en-US-u-cu-zzz"), USD}, + + // "rg" + {Locale.forLanguageTag("en-US-u-rg-jpzzzz"), JPY}, + {Locale.forLanguageTag("ja-JP-u-rg-uszzzz"), USD}, + {Locale.forLanguageTag("ja-JP-u-rg-001zzzz"), JPY}, + {Locale.forLanguageTag("en-US-u-rg-jpz"), USD}, + + // "cu" and "rg". "cu" should win + {Locale.forLanguageTag("en-CA-u-cu-jpy-rg-uszzzz"), JPY}, + + // invaid "cu" and valid "rg". "rg" should win + {Locale.forLanguageTag("en-CA-u-cu-jpyy-rg-uszzzz"), USD}, + {Locale.forLanguageTag("en-CA-u-cu-zzz-rg-uszzzz"), USD}, + + // invaid "cu" and invalid "rg". both should be ignored + {Locale.forLanguageTag("en-CA-u-cu-jpyy-rg-jpzz"), CAD}, + }; + } + + @DataProvider(name="getSymbolData") + Object[][] getSymbolData() { + return new Object[][] { + // Currency, DisplayLocale, expected Symbol + {USD, Locale.forLanguageTag("en-US-u-rg-jpzzzz"), "$"}, + {USD, Locale.forLanguageTag("en-US-u-rg-cazzzz"), "US$"}, + {USD, Locale.forLanguageTag("en-CA-u-rg-uszzzz"), "$"}, + + {CAD, Locale.forLanguageTag("en-US-u-rg-jpzzzz"), "CA$"}, + {CAD, Locale.forLanguageTag("en-US-u-rg-cazzzz"), "$"}, + {CAD, Locale.forLanguageTag("en-CA-u-rg-uszzzz"), "CA$"}, + + {JPY, Locale.forLanguageTag("ja-JP-u-rg-uszzzz"), "\uffe5"}, + {JPY, Locale.forLanguageTag("en-US-u-rg-jpzzzz"), "\u00a5"}, + {JPY, Locale.forLanguageTag("ko-KR-u-rg-jpzzzz"), "JP\u00a5"}, + }; + } + + @Test(dataProvider="getInstanceData") + public void test_getInstance(Locale locale, Currency currencyExpected) { + assertEquals(Currency.getInstance(locale), currencyExpected); + } + + @Test(dataProvider="getSymbolData") + public void test_getSymbol(Currency c, Locale locale, String expected) { + assertEquals(c.getSymbol(locale), expected); + } +} diff --git a/test/jdk/java/util/Locale/bcp47u/DefaultLocaleTest.java b/test/jdk/java/util/Locale/bcp47u/DefaultLocaleTest.java new file mode 100644 --- /dev/null +++ b/test/jdk/java/util/Locale/bcp47u/DefaultLocaleTest.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.util.Locale; + +/* + * Test application that verifies default locales. Invoked from + * SystemPropertyTests + */ +public class DefaultLocaleTest { + public static void main(String... args) { + String defLoc = Locale.getDefault().toString(); + String defFmtLoc = Locale.getDefault(Locale.Category.FORMAT).toString(); + String defDspLoc = Locale.getDefault(Locale.Category.DISPLAY).toString(); + + if (!defLoc.equals(args[0]) || + !defFmtLoc.equals(args[1]) || + !defDspLoc.equals(args[2])) { + System.err.println("Some default locale(s) don't match.\n" + + "Default Locale expected: " + args[0] + ", result: " + defLoc + "\n" + + "Default Format Locale expected: " + args[1] + ", result: " + defFmtLoc + "\n" + + "Default Display Locale expected: " + args[2] + ", result: " + defDspLoc); + System.exit(-1); + } + } +} diff --git a/test/jdk/java/util/Locale/bcp47u/DisplayNameTests.java b/test/jdk/java/util/Locale/bcp47u/DisplayNameTests.java new file mode 100644 --- /dev/null +++ b/test/jdk/java/util/Locale/bcp47u/DisplayNameTests.java @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * + * @test + * @bug 8176841 + * @summary Tests the display names for BCP 47 U extensions + * @modules jdk.localedata + * @run testng/othervm -Djava.locale.providers=CLDR DisplayNameTests + */ + +import static org.testng.Assert.assertEquals; + +import java.util.Locale; + +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +/** + * Test Locale.getDisplayName() with BCP47 U extensions. Note that the + * result may change depending on the CLDR releases. + */ +@Test +public class DisplayNameTests { + private static final Locale loc1 = Locale.forLanguageTag("en-Latn-US-u" + + "-ca-japanese" + + "-cf-account" + + "-co-pinyin" + + "-cu-jpy" + + "-em-emoji" + + "-fw-wed" + + "-hc-h23" + + "-lb-loose" + + "-lw-breakall" + + "-ms-uksystem" + + "-nu-roman" + + "-rg-gbzzzz" + + "-sd-gbsct" + + "-ss-standard" + + "-tz-jptyo" + + "-va-posix"); + private static final Locale loc2 = new Locale("ja", "JP", "JP"); + private static final Locale loc3 = new Locale.Builder() + .setRegion("US") + .setScript("Latn") + .setUnicodeLocaleKeyword("ca", "japanese") + .build(); + private static final Locale loc4 = new Locale.Builder() + .setRegion("US") + .setUnicodeLocaleKeyword("ca", "japanese") + .build(); + private static final Locale loc5 = new Locale.Builder() + .setUnicodeLocaleKeyword("ca", "japanese") + .build(); + + @DataProvider(name="locales") + Object[][] tz() { + return new Object[][] { + // Locale for display, Test Locale, Expected output, + {Locale.US, loc1, + "English (Latin, United States, Japanese Calendar, Accounting Currency Format, Pinyin Sort Order, Currency: Japanese Yen, Prefer Emoji Presentation For Emoji Characters, First Day of Week Is Wednesday, 24 Hour System (0\u201323), Loose Line Break Style, Allow Line Breaks In All Words, Imperial Measurement System, Roman Numerals, Region For Supplemental Data: United Kingdom, Region Subdivision: gbsct, Suppress Sentence Breaks After Standard Abbreviations, Time Zone: Japan Time, POSIX Compliant Locale)"}, + {Locale.JAPAN, loc1, + "\u82f1\u8a9e (\u30e9\u30c6\u30f3\u6587\u5b57\u3001\u30a2\u30e1\u30ea\u30ab\u5408\u8846\u56fd\u3001\u548c\u66a6\u3001cf: account\u3001\u30d4\u30f3\u30a4\u30f3\u9806\u3001\u901a\u8ca8: \u65e5\u672c\u5186\u3001em: emoji\u3001fw: wed\u300124\u6642\u9593\u5236(0\u301c23)\u3001\u7981\u5247\u51e6\u7406(\u5f31)\u3001lw: breakall\u3001\u30e4\u30fc\u30c9\u30fb\u30dd\u30f3\u30c9\u6cd5\u3001\u30ed\u30fc\u30de\u6570\u5b57\u3001rg: \u30a4\u30ae\u30ea\u30b9\u3001sd: gbsct\u3001ss: standard\u3001\u30bf\u30a4\u30e0\u30be\u30fc\u30f3: \u65e5\u672c\u6642\u9593\u3001\u30ed\u30b1\u30fc\u30eb\u306e\u30d0\u30ea\u30a2\u30f3\u30c8: posix)"}, + {Locale.forLanguageTag("hi-IN"), loc1, + "\u0905\u0902\u0917\u094d\u0930\u0947\u091c\u093c\u0940 (\u0932\u0948\u091f\u093f\u0928, \u0938\u0902\u092f\u0941\u0915\u094d\u0924 \u0930\u093e\u091c\u094d\u092f, \u091c\u093e\u092a\u093e\u0928\u0940 \u092a\u0902\u091a\u093e\u0902\u0917, cf: account, \u092a\u093f\u0928\u092f\u0940\u0928 \u0935\u0930\u094d\u0917\u0940\u0915\u0930\u0923, \u092e\u0941\u0926\u094d\u0930\u093e: \u091c\u093e\u092a\u093e\u0928\u0940 \u092f\u0947\u0928, em: emoji, fw: wed, 24 \u0918\u0902\u091f\u094b\u0902 \u0915\u0940 \u092a\u094d\u0930\u0923\u093e\u0932\u0940 (0\u201323), \u0922\u0940\u0932\u0940 \u092a\u0902\u0915\u094d\u0924\u093f \u0935\u093f\u091a\u094d\u091b\u0947\u0926 \u0936\u0948\u0932\u0940, lw: breakall, \u0907\u092e\u094d\u092a\u0940\u0930\u093f\u092f\u0932 \u092e\u093e\u092a\u0928 \u092a\u094d\u0930\u0923\u093e\u0932\u0940, \u0930\u094b\u092e\u0928 \u0938\u0902\u0916\u094d\u092f\u093e\u090f\u0901, rg: \u092f\u0942\u0928\u093e\u0907\u091f\u0947\u0921 \u0915\u093f\u0902\u0917\u0921\u092e, sd: gbsct, ss: standard, \u0938\u092e\u092f \u0915\u094d\u0937\u0947\u0924\u094d\u0930: \u091c\u093e\u092a\u093e\u0928 \u0938\u092e\u092f, \u0938\u094d\u0925\u093e\u0928\u0940\u092f \u092a\u094d\u0930\u0915\u093e\u0930: posix)"}, + + // cases where no localized types are available. fall back to "key: type" + {Locale.US, Locale.forLanguageTag("en-u-ca-unknown"), "English (Calendar: unknown)"}, + + // cases with variant, w/o language, script + {Locale.US, loc2, "Japanese (Japan, JP, Japanese Calendar)"}, + {Locale.US, loc3, "Latin (United States, Japanese Calendar)"}, + {Locale.US, loc4, "United States (Japanese Calendar)"}, + {Locale.US, loc5, ""}, + }; + } + + @Test(dataProvider="locales") + public void test_locales(Locale inLocale, Locale testLocale, String expected) { + String result = testLocale.getDisplayName(inLocale); + assertEquals(result, expected); + } +} diff --git a/test/jdk/java/util/Locale/bcp47u/FormatTests.java b/test/jdk/java/util/Locale/bcp47u/FormatTests.java new file mode 100644 --- /dev/null +++ b/test/jdk/java/util/Locale/bcp47u/FormatTests.java @@ -0,0 +1,167 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * + * @test + * @bug 8176841 + * @summary Tests *Format class deals with Unicode extensions + * correctly. + * @modules jdk.localedata + * @run testng/othervm -Djava.locale.providers=CLDR FormatTests + */ + +import static org.testng.Assert.assertEquals; + +import java.text.DateFormat; +import java.text.NumberFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; + +import org.testng.annotations.AfterTest; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +/** + * Test *Format classes with BCP47 U extensions + */ +@Test +public class FormatTests { + private static TimeZone defaultTZ; + + private static final TimeZone ASIATOKYO = TimeZone.getTimeZone("Asia/Tokyo"); + private static final TimeZone AMLA = TimeZone.getTimeZone("America/Los_Angeles"); + + private static final Locale JPTYO = Locale.forLanguageTag("en-u-tz-jptyo"); + private static final Locale JCAL = Locale.forLanguageTag("en-u-ca-japanese"); + private static final Locale USLAX = Locale.forLanguageTag("en-u-tz-uslax"); + + private static final Locale RG_GB = Locale.forLanguageTag("en-US-u-rg-gbzzzz"); + private static final Locale RG_DE = Locale.forLanguageTag("en-US-u-rg-dezzzz"); + + private static final Locale NU_DEVA = Locale.forLanguageTag("en-US-u-nu-deva"); + private static final Locale NU_SINH = Locale.forLanguageTag("en-US-u-nu-sinh"); + private static final Locale NU_ZZZZ = Locale.forLanguageTag("en-US-u-nu-zzzz"); + + private static final double testNum = 12345.6789; + private static final String NUM_US = "12,345.6789"; + private static final String NUM_DE = "12.345,6789"; + private static final String NUM_DEVA = "\u0967\u0968,\u0969\u096a\u096b.\u096c\u096d\u096e\u096f"; + private static final String NUM_SINH = "\u0de7\u0de8,\u0de9\u0dea\u0deb.\u0dec\u0ded\u0dee\u0def"; + + private static final Date testDate = new Calendar.Builder() + .setDate(2017, 7, 10) + .setTimeOfDay(15, 15, 0) + .setTimeZone(AMLA) + .build() + .getTime(); + + @BeforeTest + public void beforeTest() { + defaultTZ = TimeZone.getDefault(); + TimeZone.setDefault(AMLA); + } + + @AfterTest + public void afterTest() { + TimeZone.setDefault(defaultTZ); + } + + @DataProvider(name="dateFormatData") + Object[][] dateFormatData() { + return new Object[][] { + // Locale, Expected calendar, Expected timezone, Expected formatted string + + // -ca + {JCAL, "java.util.JapaneseImperialCalendar", null, + "Thursday, August 10, 29 Heisei at 3:15:00 PM Pacific Daylight Time" + }, + + // -tz + {JPTYO, null, ASIATOKYO, + "Friday, August 11, 2017 at 7:15:00 AM Japan Standard Time" + }, + {USLAX, null, AMLA, + "Thursday, August 10, 2017 at 3:15:00 PM Pacific Daylight Time" + }, + + // -rg + {RG_GB, null, null, + "Thursday, 10 August 2017 at 15:15:00 Pacific Daylight Time" + }, + }; + } + + @DataProvider(name="numberFormatData") + Object[][] numberFormatData() { + return new Object[][] { + // Locale, number, expected format + + // -nu + {NU_DEVA, testNum, NUM_DEVA}, + {NU_SINH, testNum, NUM_SINH}, + {NU_ZZZZ, testNum, NUM_US}, + + // -rg + {RG_DE, testNum, NUM_DE}, + + // -nu & -rg, valid & invalid + {Locale.forLanguageTag("en-US-u-nu-deva-rg-dezzzz"), testNum, NUM_DEVA}, + {Locale.forLanguageTag("en-US-u-nu-zzzz-rg-dezzzz"), testNum, NUM_US}, + {Locale.forLanguageTag("en-US-u-nu-zzzz-rg-zzzz"), testNum, NUM_US}, + }; + } + + @Test(dataProvider="dateFormatData") + public void test_DateFormat(Locale locale, String calClass, TimeZone tz, + String formatExpected) throws Exception { + DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale); + if (calClass != null) { + try { + Class expected = Class.forName(calClass); + assertEquals(df.getCalendar().getClass(), expected); + } catch (Exception e) { + throw e; + } + } + if (tz != null) { + assertEquals(df.getTimeZone(), tz); + } + String formatted = df.format(testDate); + assertEquals(formatted, formatExpected); + assertEquals(df.parse(formatted), testDate); + } + + @Test(dataProvider="numberFormatData") + public void test_NumberFormat(Locale locale, double num, + String formatExpected) throws Exception { + NumberFormat nf = NumberFormat.getNumberInstance(locale); + nf.setMaximumFractionDigits(4); + String formatted = nf.format(num); + assertEquals(nf.format(num), formatExpected); + assertEquals(nf.parse(formatted), num); + } +} diff --git a/test/jdk/java/util/Locale/bcp47u/JavaTimeTests.java b/test/jdk/java/util/Locale/bcp47u/JavaTimeTests.java new file mode 100644 --- /dev/null +++ b/test/jdk/java/util/Locale/bcp47u/JavaTimeTests.java @@ -0,0 +1,259 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * + * @test + * @bug 8176841 + * @summary Tests java.time classes deals with Unicode extensions + * correctly. + * @modules jdk.localedata + * @run testng/othervm -Djava.locale.providers=CLDR JavaTimeTests + */ + +import static org.testng.Assert.assertEquals; + +import java.time.DayOfWeek; +import java.time.ZonedDateTime; +import java.time.ZoneId; +import java.time.chrono.Chronology; +import java.time.chrono.HijrahChronology; +import java.time.chrono.JapaneseChronology; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeFormatterBuilder; +import java.time.format.FormatStyle; +import java.time.temporal.WeekFields; +import java.util.Locale; +import java.util.TimeZone; + +import org.testng.annotations.AfterTest; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +/** + * Test JavaTime with BCP47 U extensions + */ +@Test +public class JavaTimeTests { + private static TimeZone defaultTZ; + + private static final Chronology JAPANESE = JapaneseChronology.INSTANCE; + private static final Chronology HIJRAH = HijrahChronology.INSTANCE; + + private static final ZoneId ASIATOKYO = ZoneId.of("Asia/Tokyo"); + private static final ZoneId AMLA = ZoneId.of("America/Los_Angeles"); + + private static final Locale JPTYO = Locale.forLanguageTag("en-u-tz-jptyo"); + private static final Locale JCAL = Locale.forLanguageTag("en-u-ca-japanese"); + private static final Locale HCAL = Locale.forLanguageTag("en-u-ca-islamic-umalqura"); + + private static final Locale FW_SUN = Locale.forLanguageTag("en-US-u-fw-sun"); + private static final Locale FW_MON = Locale.forLanguageTag("en-US-u-fw-mon"); + private static final Locale FW_TUE = Locale.forLanguageTag("en-US-u-fw-tue"); + private static final Locale FW_WED = Locale.forLanguageTag("en-US-u-fw-wed"); + private static final Locale FW_THU = Locale.forLanguageTag("en-US-u-fw-thu"); + private static final Locale FW_FRI = Locale.forLanguageTag("en-US-u-fw-fri"); + private static final Locale FW_SAT = Locale.forLanguageTag("en-US-u-fw-sat"); + + private static final Locale RG_GB = Locale.forLanguageTag("en-US-u-rg-gbzzzz"); + + private static final ZonedDateTime ZDT = ZonedDateTime.of(2017, 8, 10, 15, 15, 0, 0, AMLA); + + private static final String PATTERN = "GGGG MMMM-dd-uu HH:mm:ss zzzz"; + + @BeforeTest + public void beforeTest() { + defaultTZ = TimeZone.getDefault(); + TimeZone.setDefault(TimeZone.getTimeZone(AMLA)); + } + + @AfterTest + public void afterTest() { + TimeZone.setDefault(defaultTZ); + } + + @DataProvider(name="withLocale") + Object[][] withLocale() { + return new Object[][] { + // Locale, Chrono override, Zone override, Expected Chrono, Expected Zone, + // Expected formatted string + {Locale.JAPAN, null, null, null, null, + "2017\u5e748\u670810\u65e5\u6728\u66dc\u65e5 15\u664215\u520600\u79d2 \u30a2\u30e1\u30ea\u30ab\u592a\u5e73\u6d0b\u590f\u6642\u9593" + }, + {Locale.JAPAN, JAPANESE, null, JAPANESE, null, + "\u5e73\u621029\u5e748\u670810\u65e5\u6728\u66dc\u65e5 15\u664215\u520600\u79d2 \u30a2\u30e1\u30ea\u30ab\u592a\u5e73\u6d0b\u590f\u6642\u9593" + }, + {Locale.JAPAN, JAPANESE, ASIATOKYO, JAPANESE, ASIATOKYO, + "\u5e73\u621029\u5e748\u670811\u65e5\u91d1\u66dc\u65e5 7\u664215\u520600\u79d2 \u65e5\u672c\u6a19\u6e96\u6642" + }, + + {JCAL, null, null, JAPANESE, null, + "Thursday, August 10, 29 Heisei at 3:15:00 PM Pacific Daylight Time" + }, + {JCAL, HIJRAH, null, JAPANESE, null, + "Thursday, August 10, 29 Heisei at 3:15:00 PM Pacific Daylight Time" + }, + {HCAL, JAPANESE, null, HIJRAH, null, + "Thursday, Dhu\u02bbl-Qi\u02bbdah 18, 1438 AH at 3:15:00 PM Pacific Daylight Time" + }, + + + {JPTYO, null, null, null, ASIATOKYO, + "Friday, August 11, 2017 at 7:15:00 AM Japan Standard Time" + }, + {JPTYO, null, AMLA, null, ASIATOKYO, + "Friday, August 11, 2017 at 7:15:00 AM Japan Standard Time" + }, + // invalid tz + {Locale.forLanguageTag("en-US-u-tz-jpzzz"), null, null, null, null, + "Thursday, August 10, 2017 at 3:15:00 PM Pacific Daylight Time" + }, + {Locale.forLanguageTag("en-US-u-tz-jpzzz"), null, AMLA, null, AMLA, + "Thursday, August 10, 2017 at 3:15:00 PM Pacific Daylight Time" + }, + + {RG_GB, null, null, null, null, + "Thursday, 10 August 2017 at 15:15:00 Pacific Daylight Time" + }, + }; + } + + @DataProvider(name="firstDayOfWeek") + Object[][] firstDayOfWeek () { + return new Object[][] { + // Locale, Expected DayOfWeek, + {Locale.US, DayOfWeek.SUNDAY}, + {FW_SUN, DayOfWeek.SUNDAY}, + {FW_MON, DayOfWeek.MONDAY}, + {FW_TUE, DayOfWeek.TUESDAY}, + {FW_WED, DayOfWeek.WEDNESDAY}, + {FW_THU, DayOfWeek.THURSDAY}, + {FW_FRI, DayOfWeek.FRIDAY}, + {FW_SAT, DayOfWeek.SATURDAY}, + + // invalid case + {Locale.forLanguageTag("en-US-u-fw-xxx"), DayOfWeek.SUNDAY}, + + // region override + {RG_GB, DayOfWeek.MONDAY}, + + // "fw" and "rg". + {Locale.forLanguageTag("en-US-u-fw-wed-rg-gbzzzz"), DayOfWeek.WEDNESDAY}, + {Locale.forLanguageTag("en-US-u-fw-xxx-rg-gbzzzz"), DayOfWeek.MONDAY}, + {Locale.forLanguageTag("en-US-u-fw-xxx-rg-zzzz"), DayOfWeek.SUNDAY}, + }; + } + + @DataProvider(name="minDaysInFirstWeek") + Object[][] minDaysInFrstWeek () { + return new Object[][] { + // Locale, Expected minDay, + {Locale.US, 1}, + + // region override + {RG_GB, 4}, + }; + } + + @DataProvider(name="ofPattern") + Object[][] ofPattern() { + return new Object[][] { + // Locale, Expected Chrono, Expected Zone, + // Expected formatted string + {JCAL, JAPANESE, null, + "Heisei August-10-17 15:15:00 Pacific Daylight Time" + }, + {HCAL, HIJRAH, null, + "AH Dhu\u02bbl-Qi\u02bbdah-18-38 15:15:00 Pacific Daylight Time" + }, + + {JPTYO, null, ASIATOKYO, + "Anno Domini August-11-17 07:15:00 Japan Standard Time" + }, + {Locale.forLanguageTag("en-US-u-tz-jpzzz"), null, null, + "Anno Domini August-10-17 15:15:00 Pacific Daylight Time" + }, + + {RG_GB, null, null, + "Anno Domini August-10-17 15:15:00 Pacific Daylight Time" + }, + + }; + } + + @Test(dataProvider="withLocale") + public void test_withLocale(Locale locale, Chronology chrono, ZoneId zone, + Chronology chronoExpected, ZoneId zoneExpected, + String formatExpected) { + DateTimeFormatter dtf = + DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL, FormatStyle.FULL) + .withChronology(chrono).withZone(zone).withLocale(locale); + assertEquals(dtf.getChronology(), chronoExpected); + assertEquals(dtf.getZone(), zoneExpected); + String formatted = dtf.format(ZDT); + assertEquals(formatted, formatExpected); + assertEquals(dtf.parse(formatted, ZonedDateTime::from), + zoneExpected != null ? ZDT.withZoneSameInstant(zoneExpected) : ZDT); + } + + @Test(dataProvider="firstDayOfWeek") + public void test_firstDayOfWeek(Locale locale, DayOfWeek dowExpected) { + DayOfWeek dow = WeekFields.of(locale).getFirstDayOfWeek(); + assertEquals(dow, dowExpected); + } + + @Test(dataProvider="minDaysInFirstWeek") + public void test_minDaysInFirstWeek(Locale locale, int minDaysExpected) { + int minDays = WeekFields.of(locale).getMinimalDaysInFirstWeek(); + assertEquals(minDays, minDaysExpected); + } + + @Test(dataProvider="ofPattern") + public void test_ofPattern(Locale locale, + Chronology chronoExpected, ZoneId zoneExpected, + String formatExpected) { + DateTimeFormatter dtf = + DateTimeFormatter.ofPattern(PATTERN, locale); + assertEquals(dtf.getChronology(), chronoExpected); + assertEquals(dtf.getZone(), zoneExpected); + String formatted = dtf.format(ZDT); + assertEquals(formatted, formatExpected); + assertEquals(dtf.parse(formatted, ZonedDateTime::from), + zoneExpected != null ? ZDT.withZoneSameInstant(zoneExpected) : ZDT); + } + + @Test(dataProvider="ofPattern") + public void test_toFormatter(Locale locale, + Chronology chronoExpected, ZoneId zoneExpected, + String formatExpected) { + DateTimeFormatter dtf = + new DateTimeFormatterBuilder().appendPattern(PATTERN).toFormatter(locale); + assertEquals(dtf.getChronology(), chronoExpected); + assertEquals(dtf.getZone(), zoneExpected); + String formatted = dtf.format(ZDT); + assertEquals(formatted, formatExpected); + assertEquals(dtf.parse(formatted, ZonedDateTime::from), + zoneExpected != null ? ZDT.withZoneSameInstant(zoneExpected) : ZDT); + } +} diff --git a/test/jdk/java/util/Locale/bcp47u/SymbolsTests.java b/test/jdk/java/util/Locale/bcp47u/SymbolsTests.java new file mode 100644 --- /dev/null +++ b/test/jdk/java/util/Locale/bcp47u/SymbolsTests.java @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * + * @test + * @bug 8176841 + * @summary Tests *FormatSymbols class deals with Unicode extensions + * correctly. + * @modules jdk.localedata + * @run testng/othervm -Djava.locale.providers=CLDR FormatTests + */ + +import static org.testng.Assert.assertEquals; + +import java.text.DateFormatSymbols; +import java.text.DecimalFormatSymbols; +import java.util.Locale; + +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +/** + * Test *FormatSymbols classes with BCP47 U extensions + */ +@Test +public class SymbolsTests { + + private static final Locale RG_GB = Locale.forLanguageTag("en-US-u-rg-gbzzzz"); + private static final Locale RG_IE = Locale.forLanguageTag("en-US-u-rg-iezzzz"); + private static final Locale RG_AT = Locale.forLanguageTag("en-US-u-rg-atzzzz"); + + @DataProvider(name="dateFormatSymbolsData") + Object[][] dateFormatSymbolsData() { + return new Object[][] { + // Locale, expected AM string, expected PM string + + {RG_GB, "am", "pm"}, + {RG_IE, "a.m.", "p.m."}, + {Locale.US, "AM", "PM"}, + }; + } + + @DataProvider(name="decimalFormatSymbolsData") + Object[][] decimalFormatSymbolsData() { + return new Object[][] { + // Locale, expected decimal separator, expected grouping separator + + {RG_AT, ",", "."}, + {Locale.US, ".", ","}, + + // -nu & -rg mixed. -nu should win + {Locale.forLanguageTag("ar-EG-u-nu-latn-rg-mazzzz"), ".", ","}, + }; + } + + @Test(dataProvider="dateFormatSymbolsData") + public void test_DateFormatSymbols(Locale locale, String amExpected, String pmExpected) { + DateFormatSymbols dfs = DateFormatSymbols.getInstance(locale); + String[] ampm = dfs.getAmPmStrings(); + assertEquals(ampm[0], amExpected); + assertEquals(ampm[1], pmExpected); + } + + @Test(dataProvider="decimalFormatSymbolsData") + public void test_DecimalFormatSymbols(Locale locale, char decimal, char grouping) { + DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(locale); + assertEquals(dfs.getDecimalSeparator(), decimal); + assertEquals(dfs.getGroupingSeparator(), grouping); + } +} diff --git a/test/jdk/java/util/Locale/bcp47u/SystemPropertyTests.java b/test/jdk/java/util/Locale/bcp47u/SystemPropertyTests.java new file mode 100644 --- /dev/null +++ b/test/jdk/java/util/Locale/bcp47u/SystemPropertyTests.java @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @library /lib/testlibrary + * @bug 8189134 + * @summary Tests the system properties + * @modules jdk.localedata + * @build DefaultLocaleTest jdk.testlibrary.* + * @run testng/othervm SystemPropertyTests + */ + +import static jdk.testlibrary.ProcessTools.executeTestJava; +import static org.testng.Assert.assertTrue; + +import java.util.Locale; + +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +/** + * Test Locale.getDefault() reflects the system property. Note that the + * result may change depending on the CLDR releases. + */ +@Test +public class SystemPropertyTests { + + private static String LANGPROP = "-Duser.language=en"; + private static String CTRYPROP = "-Duser.country=US"; + + @DataProvider(name="data") + Object[][] data() { + return new Object[][] { + // system property, expected default, expected format, expected display + {"-Duser.extensions=u-ca-japanese", + "en_US_#u-ca-japanese", + "en_US_#u-ca-japanese", + "en_US_#u-ca-japanese", + }, + + {"-Duser.extensions=u-ca-japanese-nu-thai", + "en_US_#u-ca-japanese-nu-thai", + "en_US_#u-ca-japanese-nu-thai", + "en_US_#u-ca-japanese-nu-thai", + }, + + {"-Duser.extensions=foo", + "en_US", + "en_US", + "en_US", + }, + + {"-Duser.extensions.format=u-ca-japanese", + "en_US", + "en_US_#u-ca-japanese", + "en_US", + }, + + {"-Duser.extensions.display=u-ca-japanese", + "en_US", + "en_US", + "en_US_#u-ca-japanese", + }, + }; + } + + @Test(dataProvider="data") + public void runTest(String extprop, String defLoc, + String defFmtLoc, String defDspLoc) throws Exception { + int exitValue = executeTestJava(LANGPROP, CTRYPROP, + extprop, "DefaultLocaleTest", defLoc, defFmtLoc, defDspLoc) + .outputTo(System.out) + .errorTo(System.out) + .getExitValue(); + + assertTrue(exitValue == 0); + } +} diff --git a/test/jdk/java/util/Locale/bcp47u/spi/LocaleNameProviderTests.java b/test/jdk/java/util/Locale/bcp47u/spi/LocaleNameProviderTests.java new file mode 100644 --- /dev/null +++ b/test/jdk/java/util/Locale/bcp47u/spi/LocaleNameProviderTests.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * + * @test + * @bug 8176841 + * @summary Tests LocaleNameProvider SPIs + * @library provider + * @build provider/module-info provider/foo.LocaleNameProviderImpl + * @run main/othervm -Djava.locale.providers=SPI LocaleNameProviderTests + */ + +import java.util.Locale; + +/** + * Test LocaleNameProvider SPI with BCP47 U extensions + * + * Verifies getUnicodeExtensionKey() and getUnicodeExtensionType() methods in + * LocaleNameProvider works. + */ +public class LocaleNameProviderTests { + private static final String expected = "foo (foo_ca:foo_japanese)"; + + public static void main(String... args) { + String name = Locale.forLanguageTag("foo-u-ca-japanese").getDisplayName(new Locale("foo")); + if (!name.equals(expected)) { + throw new RuntimeException("Unicode extension key and/or type name(s) is incorrect. " + + "Expected: \"" + expected + "\", got: \"" + name + "\""); + } + } +} diff --git a/test/jdk/java/util/Locale/bcp47u/spi/provider/foo/LocaleNameProviderImpl.java b/test/jdk/java/util/Locale/bcp47u/spi/provider/foo/LocaleNameProviderImpl.java new file mode 100644 --- /dev/null +++ b/test/jdk/java/util/Locale/bcp47u/spi/provider/foo/LocaleNameProviderImpl.java @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package foo; + +import java.util.Locale; +import java.util.spi.LocaleNameProvider; + +/* + * Implements LocaleNameProvider SPI, augmenting the default + * values for Unicode Locale Extension key/type names. + */ +public class LocaleNameProviderImpl extends LocaleNameProvider { + private static final Locale[] avail = {new Locale("foo")}; + + @Override + public Locale[] getAvailableLocales() { + return avail; + } + + @Override + public String getDisplayLanguage(String lang, Locale target) { + return null; + } + + @Override + public String getDisplayCountry(String ctry, Locale target) { + return null; + } + + @Override + public String getDisplayVariant(String vrnt, Locale target) { + return null; + } + + @Override + public String getDisplayUnicodeExtensionKey(String key, Locale target) { + return "foo_" + key; + } + + @Override + public String getDisplayUnicodeExtensionType(String extType, String key, Locale target) { + return "foo_" + key + ":foo_" + extType; + } +} diff --git a/test/jdk/java/util/Locale/bcp47u/spi/provider/module-info.java b/test/jdk/java/util/Locale/bcp47u/spi/provider/module-info.java new file mode 100644 --- /dev/null +++ b/test/jdk/java/util/Locale/bcp47u/spi/provider/module-info.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +module provider { + exports foo; + provides java.util.spi.LocaleNameProvider with foo.LocaleNameProviderImpl; +} diff --git a/test/jdk/tools/jlink/plugins/IncludeLocalesPluginTest.java b/test/jdk/tools/jlink/plugins/IncludeLocalesPluginTest.java --- a/test/jdk/tools/jlink/plugins/IncludeLocalesPluginTest.java +++ b/test/jdk/tools/jlink/plugins/IncludeLocalesPluginTest.java @@ -40,7 +40,7 @@ /* * @test - * @bug 8152143 8152704 8155649 8165804 8185841 + * @bug 8152143 8152704 8155649 8165804 8185841 8176841 * @summary IncludeLocalesPlugin tests * @author Naoto Sato * @library ../../lib @@ -70,6 +70,14 @@ private static int errors; private final static Object[][] testData = { + // Test data should include: + // - --include-locales command line option + // - --add-modules command line option values + // - List of required resources in the result image + // - List of resources that should not exist in the result image + // - List of available locales in the result image + // - Error message + // without --include-locales option: should include all locales { "", @@ -387,6 +395,34 @@ "", }, + // Langtag including extensions. Should be ignored. + { + "--include-locales=en,ja-u-nu-thai", + "jdk.localedata", + List.of( + "/jdk.localedata/sun/text/resources/ext/FormatData_en_GB.class", + "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_en_001.class"), + List.of( + "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_ja.class", + "/jdk.localedata/sun/text/resources/ext/FormatData_th.class"), + List.of( + "(root)", "en", "en_001", "en_150", "en_AG", "en_AI", "en_AS", "en_AT", + "en_AU", "en_BB", "en_BE", "en_BI", "en_BM", "en_BS", "en_BW", "en_BZ", + "en_CA", "en_CC", "en_CH", "en_CK", "en_CM", "en_CX", "en_CY", "en_DE", + "en_DG", "en_DK", "en_DM", "en_ER", "en_FI", "en_FJ", "en_FK", "en_FM", + "en_GB", "en_GD", "en_GG", "en_GH", "en_GI", "en_GM", "en_GU", "en_GY", + "en_HK", "en_IE", "en_IL", "en_IM", "en_IN", "en_IO", "en_JE", "en_JM", + "en_KE", "en_KI", "en_KN", "en_KY", "en_LC", "en_LR", "en_LS", "en_MG", + "en_MH", "en_MO", "en_MP", "en_MS", "en_MT", "en_MU", "en_MW", "en_MY", + "en_NA", "en_NF", "en_NG", "en_NL", "en_NR", "en_NU", "en_NZ", "en_PG", + "en_PH", "en_PK", "en_PN", "en_PR", "en_PW", "en_RW", "en_SB", "en_SC", + "en_SD", "en_SE", "en_SG", "en_SH", "en_SI", "en_SL", "en_SS", "en_SX", + "en_SZ", "en_TC", "en_TK", "en_TO", "en_TT", "en_TV", "en_TZ", "en_UG", + "en_UM", "en_US", "en_US_POSIX", "en_VC", "en_VG", "en_VI", "en_VU", + "en_WS", "en_ZA", "en_ZM", "en_ZW"), + "", + }, + // Error case: No matching locales { "--include-locales=xyz",