Skip to content

Commit

Permalink
8041488: Locale-Dependent List Patterns
Browse files Browse the repository at this point in the history
Reviewed-by: joehw, rriggs
  • Loading branch information
naotoj committed Sep 11, 2023
1 parent dd214d0 commit d0be73a
Show file tree
Hide file tree
Showing 7 changed files with 1,019 additions and 8 deletions.
8 changes: 7 additions & 1 deletion make/jdk/src/classes/build/tools/cldrconverter/Bundle.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2023, 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
Expand Down Expand Up @@ -108,6 +108,12 @@ static enum Type {
"narrow.Eras"
};

static final String[] LIST_PATTERN_KEYS = {
"ListPatterns_standard",
"ListPatterns_or",
"ListPatterns_unit",
};

// DateFormatItem prefix
static final String DATEFORMATITEM_KEY_PREFIX = "DateFormatItem.";
static final String DATEFORMATITEM_INPUT_REGIONS_PREFIX = "DateFormatItemInputRegions.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,14 @@ private static void convertBundles(List<Bundle> bundles) throws Exception {
*/
static void handleAliases(Map<String, Object> bundleMap) {
for (String key : aliases.keySet()) {
var source = bundleMap.get(aliases.get(key));
var sourceKey = aliases.get(key);
if (key.startsWith("ListPatterns_")) {
String k;
while ((k = aliases.get(sourceKey)) != null) {
sourceKey = k;
}
}
var source = bundleMap.get(sourceKey);
if (source != null) {
if (bundleMap.get(key) instanceof String[] sa) {
// fill missing elements in case of String array
Expand Down Expand Up @@ -871,6 +878,7 @@ private static Map<String, Object> extractCalendarData(Map<String, Object> map,
"DayPeriodRules",
"DateFormatItemInputRegions.allowed",
"DateFormatItemInputRegions.preferred",
"ListPatterns",
};

static final Set<String> availableSkeletons = new HashSet<>();
Expand Down Expand Up @@ -935,6 +943,14 @@ private static Map<String, Object> extractFormatData(Map<String, Object> map, St
formatData.put(k + ".NumberElements", neNew);
});
}

// ListPatterns
for (var lpKey : Bundle.LIST_PATTERN_KEYS) {
copyIfPresent(map, lpKey, formatData);
copyIfPresent(map, lpKey + "-short", formatData);
copyIfPresent(map, lpKey + "-narrow", formatData);
}

return formatData;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2023, 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
Expand Down Expand Up @@ -35,6 +35,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
Expand Down Expand Up @@ -812,6 +813,7 @@ public void startElement(String uri, String localName, String qName, Attributes
&& ((currentContainer.getqName().equals("decimalFormatLength"))
|| (currentContainer.getqName().equals("currencyFormat"))
|| (currentContainer.getqName().equals("percentFormat"))
|| (currentContainer.getqName().equals("listPattern"))
|| (currentCalendarType != null && !currentCalendarType.lname().startsWith("islamic-")))) { // ignore islamic variants
pushAliasEntry(qName, attributes, attributes.getValue("path"));
} else {
Expand All @@ -820,6 +822,28 @@ public void startElement(String uri, String localName, String qName, Attributes
}
break;

// ListPatterns
case "listPattern":
currentStyle = Optional.ofNullable(attributes.getValue("type")).orElse("standard");
pushStringArrayEntry(qName, attributes, "ListPatterns_" + currentStyle, 5);
break;
case "listPatternPart":
type = attributes.getValue("type");
pushStringArrayElement(qName, attributes,
switch (type) {
case "start" -> 0;
case "middle" -> 1;
case "end" -> 2;
case "2" -> 3;
case "3" -> 4;
default -> throw new IllegalArgumentException(
"""
The "type" attribute value for "listPatternPart" element is not recognized: %s
""".formatted(type)
);
});
break;

default:
// treat anything else as a container
pushContainer(qName, attributes);
Expand Down Expand Up @@ -973,6 +997,9 @@ private String toJDKKey(String containerqName, String context, String type) {
"NumberPatterns/" +
(type.equals("standard") ? containerqName.replaceFirst("Format", "") : type);
break;
case "listPattern":
keyName = type;
break;
default:
keyName = "";
break;
Expand Down Expand Up @@ -1035,6 +1062,19 @@ private String getTarget(String path, String calType, String context, String wid
return toJDKKey(qName, "", style);
}

// listPattern
if (path.indexOf("../listPattern") != -1) {
typeKey = "[@type='";
start = path.indexOf(typeKey);
String style;
if (start != -1) {
style = "ListPatterns_" + path.substring(start + typeKey.length(), path.indexOf("']", start));
} else {
style = "ListPatterns_standard";
}
return toJDKKey(qName, "", style);
}

return calType + "." + toJDKKey(qName, context, width);
}

Expand Down Expand Up @@ -1107,6 +1147,10 @@ public void endElement(String uri, String localName, String qName) throws SAXExc
case "timeFormatLength":
currentStyle = "";
break;
case "listPattern":
currentStyle = "";
putIfEntry();
break;
default:
putIfEntry();
}
Expand All @@ -1128,6 +1172,12 @@ private Object putIfEntry() {
toJDKKey(containerqName, "", kc.getKey()),
getTarget(entry.getKey(), "", "", "")
);
} else if (containerqName.equals("listPattern")) {
var sae = (StringArrayEntry)entry.getParent();
CLDRConverter.aliases.put(
toJDKKey(containerqName, "", sae.getKey()),
getTarget(entry.getKey(), "", "", "")
);
} else {
Set<String> keyNames = populateAliasKeys(containerqName, currentContext, currentWidth);
if (!keyNames.isEmpty()) {
Expand Down
11 changes: 6 additions & 5 deletions src/java.base/share/classes/java/text/Format.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2023, 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
Expand Down Expand Up @@ -42,7 +42,7 @@

/**
* {@code Format} is an abstract base class for formatting locale-sensitive
* information such as dates, messages, and numbers.
* information such as dates, messages, numbers, and lists.
*
* <p>
* {@code Format} defines the programming interface for formatting
Expand All @@ -61,9 +61,9 @@
* <h2>Subclassing</h2>
*
* <p>
* The Java Platform provides three specialized subclasses of {@code Format}--
* {@code DateFormat}, {@code MessageFormat}, and
* {@code NumberFormat}--for formatting dates, messages, and numbers,
* The Java Platform provides specialized subclasses of {@code Format}--
* {@code DateFormat}, {@code MessageFormat}, {@code NumberFormat}, and
* {@code ListFormat}--for formatting dates, messages, numbers, and lists
* respectively.
* <p>
* Concrete subclasses must implement three methods:
Expand Down Expand Up @@ -128,6 +128,7 @@
* @see java.text.NumberFormat
* @see java.text.DateFormat
* @see java.text.MessageFormat
* @see java.text.ListFormat
* @author Mark Davis
* @since 1.1
*/
Expand Down
Loading

1 comment on commit d0be73a

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.