From 39bc7ba0325d5f0a512f023a185199a76faa4fc9 Mon Sep 17 00:00:00 2001 From: "Steven R. Loomis" Date: Fri, 13 Sep 2024 11:02:17 -0500 Subject: [PATCH] CLDR-15817 Update Example Generator (#3978) (#4046) Co-authored-by: haytenf --- .../unicode/cldr/test/ExampleGenerator.java | 460 +++++++++--------- .../cldr/unittest/TestExampleGenerator.java | 15 +- 2 files changed, 243 insertions(+), 232 deletions(-) diff --git a/tools/cldr-code/src/main/java/org/unicode/cldr/test/ExampleGenerator.java b/tools/cldr-code/src/main/java/org/unicode/cldr/test/ExampleGenerator.java index 1b2c6f485c5..414259e47d4 100644 --- a/tools/cldr-code/src/main/java/org/unicode/cldr/test/ExampleGenerator.java +++ b/tools/cldr-code/src/main/java/org/unicode/cldr/test/ExampleGenerator.java @@ -497,6 +497,9 @@ private String getExampleHtmlExtended(String xpath, String value, boolean nonTri /** * Do the main work of getExampleHtml given that the result was not found in the cache. * + *

Creates a list that the handlers in constructExampleHtmlExtended can add examples to, and + * then formats the example list appropriately. + * * @param xpath the path; e.g., "//ldml/dates/timeZoneNames/fallbackFormat" * @param value the value; e.g., "{1} [{0}]"; not necessarily the winning value * @param nonTrivial true if we should avoid returning a trivial example (just value wrapped in @@ -504,7 +507,20 @@ private String getExampleHtmlExtended(String xpath, String value, boolean nonTri * @return the example HTML, or null */ private String constructExampleHtml(String xpath, String value, boolean nonTrivial) { - String result = null; + List examples = new ArrayList<>(); + constructExampleHtmlExtended(xpath, value, examples); + String result = formatExampleList(examples); + if (result != null) { // Handle the outcome + if (nonTrivial && value.equals(result)) { + result = null; + } else { + result = finalizeBackground(result); + } + } + return result; + } + + private void constructExampleHtmlExtended(String xpath, String value, List examples) { boolean showContexts = isRTL || BIDI_MARKS.containsSome(value); // only used for certain example types /* @@ -513,91 +529,81 @@ private String constructExampleHtml(String xpath, String value, boolean nonTrivi */ XPathParts parts = XPathParts.getFrozenInstance(xpath).cloneAsThawed(); if (parts.contains("dateRangePattern")) { // {0} - {1} - result = handleDateRangePattern(value); + handleDateRangePattern(value, examples); } else if (parts.contains("timeZoneNames")) { - result = handleTimeZoneName(parts, value); + handleTimeZoneName(parts, value, examples); } else if (parts.contains("localeDisplayNames")) { - result = handleDisplayNames(xpath, parts, value); + handleDisplayNames(xpath, parts, value, examples); } else if (parts.contains("currency")) { - result = handleCurrency(xpath, parts, value); + handleCurrency(xpath, parts, value, examples); } else if (parts.contains("eras")) { - result = handleEras(parts, value); + handleEras(parts, value, examples); } else if (parts.contains("quarters")) { - result = handleQuarters(parts, value); + handleQuarters(parts, value, examples); } else if (parts.contains("dayPeriods")) { - result = handleDayPeriod(parts, value); + handleDayPeriod(parts, value, examples); } else if (parts.contains("monthContext")) { - result = handleDateSymbol(parts, value); + handleDateSymbol(parts, value, examples); } else if (parts.contains("pattern") || parts.contains("dateFormatItem")) { if (parts.contains("calendar")) { - result = handleDateFormatItem(xpath, value, showContexts); + handleDateFormatItem(xpath, value, showContexts, examples); } else if (parts.contains("miscPatterns")) { - result = handleMiscPatterns(parts, value); + handleMiscPatterns(parts, value, examples); } else if (parts.contains("numbers")) { if (parts.contains("currencyFormat")) { - result = handleCurrencyFormat(parts, value, showContexts); + handleCurrencyFormat(parts, value, showContexts, examples); } else { - result = handleDecimalFormat(parts, value, showContexts); + handleDecimalFormat(parts, value, showContexts, examples); } } } else if (parts.contains("minimumGroupingDigits")) { - result = handleMinimumGrouping(parts, value); + handleMinimumGrouping(parts, value, examples); } else if (parts.getElement(2).contains("symbols")) { - result = handleNumberSymbol(parts, value); + handleNumberSymbol(parts, value, examples); } else if (parts.contains("defaultNumberingSystem") || parts.contains("otherNumberingSystems")) { - result = handleNumberingSystem(value); + handleNumberingSystem(value, examples); } else if (parts.contains("currencyFormats") && parts.contains("unitPattern")) { - result = formatCountValue(xpath, parts, value); + formatCountValue(xpath, parts, value, examples); } else if (parts.getElement(-1).equals("compoundUnitPattern")) { - result = handleCompoundUnit(parts); + handleCompoundUnit(parts, examples); } else if (parts.getElement(-1).equals("compoundUnitPattern1") || parts.getElement(-1).equals("unitPrefixPattern")) { - result = handleCompoundUnit1(parts, value); + handleCompoundUnit1(parts, value, examples); } else if (parts.getElement(-2).equals("unit") && (parts.getElement(-1).equals("unitPattern") || parts.getElement(-1).equals("displayName"))) { - result = handleFormatUnit(parts, value); + handleFormatUnit(parts, value, examples); } else if (parts.getElement(-1).equals("perUnitPattern")) { - result = handleFormatPerUnit(value); + handleFormatPerUnit(value, examples); } else if (parts.getElement(-2).equals("minimalPairs")) { - result = handleMinimalPairs(parts, value); + handleMinimalPairs(parts, value, examples); } else if (parts.getElement(-1).equals("durationUnitPattern")) { - result = handleDurationUnit(value); + handleDurationUnit(value, examples); } else if (parts.contains("intervalFormats")) { - result = handleIntervalFormats(parts, value); + handleIntervalFormats(parts, value, examples); } else if (parts.getElement(1).equals("delimiters")) { - result = handleDelimiters(parts, xpath, value); + handleDelimiters(parts, xpath, value, examples); } else if (parts.getElement(1).equals("listPatterns")) { - result = handleListPatterns(parts, value); + handleListPatterns(parts, value, examples); } else if (parts.getElement(2).equals("ellipsis")) { - result = handleEllipsis(parts.getAttributeValue(-1, "type"), value); + handleEllipsis(parts.getAttributeValue(-1, "type"), value, examples); } else if (parts.getElement(-1).equals("monthPattern")) { - result = handleMonthPatterns(parts, value); + handleMonthPatterns(parts, value, examples); } else if (parts.getElement(-1).equals("appendItem")) { - result = handleAppendItems(parts, value); + handleAppendItems(parts, value, examples); } else if (parts.getElement(-1).equals("annotation")) { - result = handleAnnotationName(parts, value); + handleAnnotationName(parts, value, examples); } else if (parts.getElement(-1).equals("characterLabel")) { - result = handleLabel(parts, value); + handleLabel(parts, value, examples); } else if (parts.getElement(-1).equals("characterLabelPattern")) { - result = handleLabelPattern(parts, value); + handleLabelPattern(parts, value, examples); } else if (parts.getElement(1).equals("personNames")) { - result = handlePersonName(parts, value); + handlePersonName(parts, value, examples); } else if (parts.getElement(-1).equals("exemplarCharacters") || parts.getElement(-1).equals("parseLenient")) { - result = handleUnicodeSet(parts, xpath, value); + handleUnicodeSet(parts, xpath, value, examples); } - - // Handle the outcome - if (result != null) { - if (nonTrivial && value.equals(result)) { - result = null; - } else { - result = finalizeBackground(result); - } - } - return result; } // Note: may want to change to locale's order; if so, these would be instance fields @@ -615,13 +621,13 @@ private String constructExampleHtml(String xpath, String value, boolean nonTrivi * Add examples for UnicodeSets. First, show a hex format of non-spacing marks if there are any, * then show delta to the winning value if there are any. */ - private String handleUnicodeSet(XPathParts parts, String xpath, String value) { - ArrayList examples = new ArrayList<>(); + private void handleUnicodeSet( + XPathParts parts, String xpath, String value, List examples) { UnicodeSet valueSet; try { valueSet = new UnicodeSet(value); } catch (Exception e) { - return null; + return; } String winningValue = cldrFile.getWinningValue(xpath); if (!winningValue.equals(value)) { @@ -657,7 +663,6 @@ private String handleUnicodeSet(XPathParts parts, String xpath, String value) { } } examples.add(setBackground(INTERNAL) + valueSet.toPattern(false)); // internal format - return formatExampleList(examples); } /** @@ -715,7 +720,7 @@ public String toString() { private static final Function BACKGROUND_TRANSFORM = x -> backgroundStartSymbol + x + backgroundEndSymbol; - private String handlePersonName(XPathParts parts, String value) { + private void handlePersonName(XPathParts parts, String value, List examples) { // ldml/personNames/personName[@order="givenFirst"][@length="long"][@usage="addressing"][@style="formal"]/namePattern => {prefix} {surname} String debugState = "start"; try { @@ -726,12 +731,9 @@ private String handlePersonName(XPathParts parts, String value) { PersonNameFormatter.Usage.from(parts.getAttributeValue(2, "usage")), PersonNameFormatter.Formality.from( parts.getAttributeValue(2, "formality"))); - - List examples = null; final CLDRFile cldrFile2 = getCldrFile(); switch (parts.getElement(2)) { case "nameOrderLocales": - examples = new ArrayList<>(); for (String localeId : PersonNameFormatter.SPLIT_SPACE.split(value)) { final String name = localeId.equals("und") @@ -740,12 +742,7 @@ private String handlePersonName(XPathParts parts, String value) { examples.add(localeId + " = " + name); } break; - case "initialPattern": - return null; - case "sampleName": - return null; case "personName": - examples = new ArrayList<>(); Map sampleNames = personNamesCache.getSampleNames(cldrFile2); PersonNameFormatter personNameFormatter = @@ -797,7 +794,6 @@ private String handlePersonName(XPathParts parts, String value) { } break; } - return formatExampleList(examples); } catch (Exception e) { StringBuffer stackTrace; try (StringWriter sw = new StringWriter(); @@ -807,7 +803,8 @@ private String handlePersonName(XPathParts parts, String value) { } catch (Exception e2) { stackTrace = new StringBuffer("internal error"); } - return "Internal error: " + e.getMessage() + "\n" + debugState + "\n" + stackTrace; + examples.add( + "Internal error: " + e.getMessage() + "\n" + debugState + "\n" + stackTrace); } } @@ -882,9 +879,8 @@ private SimpleNameObject getBestAvailable( return null; } - private String handleLabelPattern(XPathParts parts, String value) { + private void handleLabelPattern(XPathParts parts, String value, List examples) { if ("category-list".equals(parts.getAttributeValue(-1, "type"))) { - List examples = new ArrayList<>(); CLDRFile cfile = getCldrFile(); SimpleFormatter initialPattern = SimpleFormatter.compile(setBackground(value)); String path = CLDRFile.getKey(CLDRFile.TERRITORY_NAME, "FR"); @@ -896,19 +892,16 @@ private String handleLabelPattern(XPathParts parts, String value) { EmojiConstants.getEmojiFromRegionCodes("FR") + " ⇒ " + initialPattern.format(flagName, regionName))); - return formatExampleList(examples); } - return null; } - private String handleLabel(XPathParts parts, String value) { + private void handleLabel(XPathParts parts, String value, List examples) { // "//ldml/characterLabels/characterLabel[@type=\"" + typeAttributeValue + "\"]" switch (parts.getAttributeValue(-1, "type")) { case "flag": { String value2 = backgroundStartSymbol + value + backgroundEndSymbol; CLDRFile cfile = getCldrFile(); - List examples = new ArrayList<>(); SimpleFormatter initialPattern = SimpleFormatter.compile( cfile.getStringValue( @@ -918,12 +911,11 @@ private String handleLabel(XPathParts parts, String value) { addSubdivisionFlag(value2, "gbeng", initialPattern, examples); addSubdivisionFlag(value2, "gbsct", initialPattern, examples); addSubdivisionFlag(value2, "gbwls", initialPattern, examples); - return formatExampleList(examples); + return; } case "keycap": { String value2 = backgroundStartSymbol + value + backgroundEndSymbol; - List examples = new ArrayList<>(); CLDRFile cfile = getCldrFile(); SimpleFormatter initialPattern = SimpleFormatter.compile( @@ -932,10 +924,10 @@ private String handleLabel(XPathParts parts, String value) { examples.add(invertBackground(initialPattern.format(value2, "1"))); examples.add(invertBackground(initialPattern.format(value2, "10"))); examples.add(invertBackground(initialPattern.format(value2, "#"))); - return formatExampleList(examples); + return; } default: - return null; + return; } } @@ -970,17 +962,16 @@ private void addSubdivisionFlag( + initialPattern.format(value2, subdivisionName))); } - private String handleAnnotationName(XPathParts parts, String value) { + private void handleAnnotationName(XPathParts parts, String value, List examples) { // ldml/annotations/annotation[@cp="🦰"][@type="tts"] // skip anything but the name if (!"tts".equals(parts.getAttributeValue(-1, "type"))) { - return null; + return; } String cp = parts.getAttributeValue(-1, "cp"); if (cp == null || cp.isEmpty()) { - return null; + return; } - Set examples = new LinkedHashSet<>(); int first = cp.codePointAt(0); switch (first) { case 0x1F46A: // 👪 U+1F46A FAMILY @@ -1046,7 +1037,6 @@ private String handleAnnotationName(XPathParts parts, String value) { } break; } - return formatExampleList(examples); } private String getEmojiName(CLDRFile cfile, String skin) { @@ -1115,13 +1105,12 @@ private void formatPeople( initialPattern.format(personName, skinName), hairName))); } - private String handleDayPeriod(XPathParts parts, String value) { + private void handleDayPeriod(XPathParts parts, String value, List examples) { // ldml/dates/calendars/calendar[@type="gregorian"]/dayPeriods/dayPeriodContext[@type="format"]/dayPeriodWidth[@type="wide"]/dayPeriod[@type="morning1"] // ldml/dates/calendars/calendar[@type="gregorian"]/dayPeriods/dayPeriodContext[@type="stand-alone"]/dayPeriodWidth[@type="wide"]/dayPeriod[@type="morning1"] - List examples = new ArrayList<>(); final String dayPeriodType = parts.getAttributeValue(5, "type"); if (dayPeriodType == null) { - return null; // formerly happened for some "/alias" paths + return; // formerly happened for some "/alias" paths } org.unicode.cldr.util.DayPeriodInfo.Type aType = dayPeriodType.equals("format") @@ -1131,7 +1120,7 @@ private String handleDayPeriod(XPathParts parts, String value) { supplementalDataInfo.getDayPeriods(aType, cldrFile.getLocaleID()); String periodString = parts.getAttributeValue(-1, "type"); if (periodString == null) { - return null; // formerly happened for some "/alias" paths + return; // formerly happened for some "/alias" paths } DayPeriod dayPeriod = DayPeriod.valueOf(periodString); String periods = dayPeriodInfo.toString(dayPeriod); @@ -1149,16 +1138,15 @@ private String handleDayPeriod(XPathParts parts, String value) { examples.add(invertBackground(timeFormatString)); } } - return formatExampleList(examples.toArray(new String[0])); } - private String handleDateSymbol(XPathParts parts, String value) { + private void handleDateSymbol(XPathParts parts, String value, List examples) { // Currently only called for month names, can expand in the future to handle other symbols. // The idea is to show format months in a yMMMM?d date format, and stand-alone months in a // yMMMM? format. String length = parts.findAttributeValue("monthWidth", "type"); // wide, abbreviated, narrow if (length.equals("narrow")) { - return null; // no examples for narrow + return; // no examples for narrow } String context = parts.findAttributeValue("monthContext", "type"); // format, stand-alone String calendarId = @@ -1193,7 +1181,7 @@ private String handleDateSymbol(XPathParts parts, String value) { dateFormat = cldrFile.getWinningValue(checkPath); } if (dateFormat == null) { - return null; + return; } SimpleDateFormat sdf = icuServiceBuilder.getDateFormat(calendarId, dateFormat); sdf.setTimeZone(ZONE_SAMPLE); @@ -1214,12 +1202,11 @@ private String handleDateSymbol(XPathParts parts, String value) { monthNames[8] = value; dfs.setMonths(monthNames, DateFormatSymbols.STANDALONE, DateFormatSymbols.ABBREVIATED); sdf.setDateFormatSymbols(dfs); - return sdf.format(DATE_SAMPLE); + examples.add(sdf.format(DATE_SAMPLE)); } - private String handleMinimalPairs(XPathParts parts, String minimalPattern) { - List examples = new ArrayList<>(); - + private void handleMinimalPairs( + XPathParts parts, String minimalPattern, List examples) { Output output = new Output<>(); String count; String otherCount; @@ -1269,7 +1256,7 @@ private String handleMinimalPairs(XPathParts parts, String minimalPattern) { sampleBad = bestMinimalPairSamples.getBestUnitWithGender(otherGender, output); break; default: - return null; + return; } String formattedUnit = format(minimalPattern, backgroundStartSymbol + sample + backgroundEndSymbol); @@ -1280,11 +1267,10 @@ private String handleMinimalPairs(XPathParts parts, String minimalPattern) { formattedUnit = format(minimalPattern, backgroundStartSymbol + sampleBad + backgroundEndSymbol); examples.add(EXAMPLE_OF_INCORRECT + formattedUnit); - return formatExampleList(examples); } private String getOtherGender(String gender) { - if (gender == null) { + if (gender == null || grammarInfo == null) { return null; } Collection unitGenders = @@ -1342,17 +1328,16 @@ private UnitLength getUnitLength(XPathParts parts) { return UnitLength.valueOf(parts.getAttributeValue(-3, "type").toUpperCase(Locale.ENGLISH)); } - private String handleFormatUnit(XPathParts parts, String unitPattern) { + private void handleFormatUnit(XPathParts parts, String unitPattern, List examples) { // Sample: // //ldml/units/unitLength[@type="long"]/unit[@type="duration-day"]/unitPattern[@count="one"][@case="accusative"] String longUnitId = parts.getAttributeValue(-2, "type"); final String shortUnitId = UNIT_CONVERTER.getShortId(longUnitId); if (UnitConverter.HACK_SKIP_UNIT_NAMES.contains(shortUnitId)) { - return null; + return; } - List examples = new ArrayList<>(); if (parts.getElement(-1).equals("unitPattern")) { String count = parts.getAttributeValue(-1, "count"); DecimalQuantity amount = getBest(Count.valueOf(count)); @@ -1396,7 +1381,6 @@ private String handleFormatUnit(XPathParts parts, String unitPattern) { relatedUnit, UnitSystem.getSystemsDisplay(systems))); } - return formatExampleList(examples); } /** @@ -1499,21 +1483,25 @@ private String getConstrastingCase( return null; } - private String handleFormatPerUnit(String value) { + private void handleFormatPerUnit(String value, List examples) { DecimalFormat numberFormat = icuServiceBuilder.getNumberFormat(1); - return format(value, backgroundStartSymbol + numberFormat.format(1) + backgroundEndSymbol); + examples.add( + format( + value, + backgroundStartSymbol + numberFormat.format(1) + backgroundEndSymbol)); } - public String handleCompoundUnit(XPathParts parts) { + public void handleCompoundUnit(XPathParts parts, List examples) { UnitLength unitLength = getUnitLength(parts); String compoundType = parts.getAttributeValue(-2, "type"); Count count = Count.valueOf(CldrUtility.ifNull(parts.getAttributeValue(-1, "count"), "other")); - return handleCompoundUnit(unitLength, compoundType, count); + handleCompoundUnit(unitLength, compoundType, count, examples); } @SuppressWarnings("deprecation") - public String handleCompoundUnit(UnitLength unitLength, String compoundType, Count count) { + public void handleCompoundUnit( + UnitLength unitLength, String compoundType, Count count, List examples) { /* * @@ -1538,7 +1526,8 @@ public String handleCompoundUnit(UnitLength unitLength, String compoundType, Cou // we want to get a number that works for the count passed in. DecimalQuantity amount = getBest(count); if (amount == null) { - return "n/a"; + examples.add("n/a"); + return; } DecimalQuantity oneValue = DecimalQuantity_DualStorageBCD.fromExponentString("1"); @@ -1546,7 +1535,8 @@ public String handleCompoundUnit(UnitLength unitLength, String compoundType, Cou String unit2mid; switch (compoundType) { default: - return "n/a"; + examples.add("n/a"); + return; case "per": unit1mid = getFormattedUnit("length-meter", unitLength, amount); unit2mid = getFormattedUnit("duration-second", unitLength, oneValue, ""); @@ -1567,20 +1557,22 @@ public String handleCompoundUnit(UnitLength unitLength, String compoundType, Cou String form = this.pluralInfo.getPluralRules().select(amount); // we rebuild a path, because we may have changed it. String perPath = makeCompoundUnitPath(unitLength, compoundType, "compoundUnitPattern"); - return format(getValueFromFormat(perPath, form), unit1, unit2); + examples.add(format(getValueFromFormat(perPath, form), unit1, unit2)); } - public String handleCompoundUnit1(XPathParts parts, String compoundPattern) { + public void handleCompoundUnit1( + XPathParts parts, String compoundPattern, List examples) { UnitLength unitLength = getUnitLength(parts); String pathCount = parts.getAttributeValue(-1, "count"); if (pathCount == null) { - return handleCompoundUnit1Name(unitLength, compoundPattern); + handleCompoundUnit1Name(unitLength, compoundPattern, examples); } else { - return handleCompoundUnit1(unitLength, Count.valueOf(pathCount), compoundPattern); + handleCompoundUnit1(unitLength, Count.valueOf(pathCount), compoundPattern, examples); } } - private String handleCompoundUnit1Name(UnitLength unitLength, String compoundPattern) { + private void handleCompoundUnit1Name( + UnitLength unitLength, String compoundPattern, List examples) { String pathFormat = "//ldml/units/unitLength" + unitLength.typeString @@ -1591,15 +1583,17 @@ private String handleCompoundUnit1Name(UnitLength unitLength, String compoundPat String modFormat = combinePrefix(meterFormat, compoundPattern, unitLength == UnitLength.LONG); - return removeEmptyRuns(modFormat); + examples.add(removeEmptyRuns(modFormat)); } - public String handleCompoundUnit1(UnitLength unitLength, Count count, String compoundPattern) { + public void handleCompoundUnit1( + UnitLength unitLength, Count count, String compoundPattern, List examples) { // we want to get a number that works for the count passed in. DecimalQuantity amount = getBest(count); if (amount == null) { - return "n/a"; + examples.add("n/a"); + return; } DecimalFormat numberFormat = icuServiceBuilder.getNumberFormat(1); @@ -1620,7 +1614,8 @@ public String handleCompoundUnit1(UnitLength unitLength, Count count, String com String modFormat = combinePrefix(meterFormat, compoundPattern, unitLength == UnitLength.LONG); - return removeEmptyRuns(format(modFormat, numberFormat.format(amount.toBigDecimal()))); + examples.add( + removeEmptyRuns(format(modFormat, numberFormat.format(amount.toBigDecimal())))); } // TODO, pass in unitLength instead of last parameter, and do work in Units.combinePattern. @@ -1668,28 +1663,30 @@ private DecimalQuantity getBest(Count count) { return range.end; } - private String handleMiscPatterns(XPathParts parts, String value) { + private void handleMiscPatterns(XPathParts parts, String value, List examples) { DecimalFormat numberFormat = icuServiceBuilder.getNumberFormat(0); String start = backgroundStartSymbol + numberFormat.format(99) + backgroundEndSymbol; if ("range".equals(parts.getAttributeValue(-1, "type"))) { String end = backgroundStartSymbol + numberFormat.format(144) + backgroundEndSymbol; - return format(value, start, end); + examples.add(format(value, start, end)); } else { - return format(value, start); + examples.add(format(value, start)); } } - private String handleIntervalFormats(XPathParts parts, String value) { + private void handleIntervalFormats(XPathParts parts, String value, List examples) { if (!parts.getAttributeValue(3, "type").equals("gregorian")) { - return null; + return; } if (parts.getElement(6).equals("intervalFormatFallback")) { SimpleDateFormat dateFormat = new SimpleDateFormat(); String fallbackFormat = invertBackground(setBackground(value)); - return format( - fallbackFormat, - dateFormat.format(FIRST_INTERVAL), - dateFormat.format(SECOND_INTERVAL.get("y"))); + examples.add( + format( + fallbackFormat, + dateFormat.format(FIRST_INTERVAL), + dateFormat.format(SECOND_INTERVAL.get("y")))); + return; } String greatestDifference = parts.getAttributeValue(-1, "id"); /* @@ -1715,12 +1712,13 @@ private String handleIntervalFormats(XPathParts parts, String value) { * Reference: https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table * For now, such paths do not get examples. */ - return null; + return; } - return intervalFormat.format(FIRST_INTERVAL, later); + examples.add(intervalFormat.format(FIRST_INTERVAL, later)); } - private String handleDelimiters(XPathParts parts, String xpath, String value) { + private void handleDelimiters( + XPathParts parts, String xpath, String value, List examples) { String lastElement = parts.getElement(-1); final String[] elements = { "quotationStart", "alternateQuotationStart", @@ -1741,65 +1739,70 @@ private String handleDelimiters(XPathParts parts, String xpath, String value) { "//ldml/localeDisplayNames/types/type[@key=\"calendar\"][@type=\"gregorian\"]"); // NOTE: the example provided here is partially in English because we don't // have a translated conversational example in CLDR. - return invertBackground( - format("{0}They said {1}" + example + "{2}.{3}", (Object[]) quotes)); + examples.add( + invertBackground( + format("{0}They said {1}" + example + "{2}.{3}", (Object[]) quotes))); } - private String handleListPatterns(XPathParts parts, String value) { + private void handleListPatterns(XPathParts parts, String value, List examples) { // listPatternType is either "duration" or null/other list String listPatternType = parts.getAttributeValue(-2, "type"); if (listPatternType == null || !listPatternType.contains("unit")) { - return handleRegularListPatterns(parts, value, ListTypeLength.from(listPatternType)); + handleRegularListPatterns(parts, value, ListTypeLength.from(listPatternType), examples); } else { - return handleDurationListPatterns(parts, value, UnitLength.from(listPatternType)); + handleDurationListPatterns(parts, value, UnitLength.from(listPatternType), examples); } } - private String handleRegularListPatterns( - XPathParts parts, String value, ListTypeLength listTypeLength) { + private void handleRegularListPatterns( + XPathParts parts, String value, ListTypeLength listTypeLength, List examples) { String patternType = parts.getAttributeValue(-1, "type"); if (patternType == null) { - return null; // formerly happened for some "/alias" paths + return; // formerly happened for some "/alias" paths } String pathFormat = "//ldml/localeDisplayNames/territories/territory[@type=\"{0}\"]"; String territory1 = getValueFromFormat(pathFormat, "CH"); String territory2 = getValueFromFormat(pathFormat, "JP"); if (patternType.equals("2")) { - return invertBackground(format(setBackground(value), territory1, territory2)); + examples.add(invertBackground(format(setBackground(value), territory1, territory2))); + return; } String territory3 = getValueFromFormat(pathFormat, "EG"); String territory4 = getValueFromFormat(pathFormat, "CA"); - return longListPatternExample( - listTypeLength.getPath(), - patternType, - value, - territory1, - territory2, - territory3, - territory4); - } - - private String handleDurationListPatterns( - XPathParts parts, String value, UnitLength unitWidth) { + examples.add( + longListPatternExample( + listTypeLength.getPath(), + patternType, + value, + territory1, + territory2, + territory3, + territory4)); + } + + private void handleDurationListPatterns( + XPathParts parts, String value, UnitLength unitWidth, List examples) { String patternType = parts.getAttributeValue(-1, "type"); if (patternType == null) { - return null; // formerly happened for some "/alias" paths + return; // formerly happened for some "/alias" paths } String duration1 = getFormattedUnit("duration-day", unitWidth, 4); String duration2 = getFormattedUnit("duration-hour", unitWidth, 2); if (patternType.equals("2")) { - return invertBackground(format(setBackground(value), duration1, duration2)); + examples.add(invertBackground(format(setBackground(value), duration1, duration2))); + return; } String duration3 = getFormattedUnit("duration-minute", unitWidth, 37); String duration4 = getFormattedUnit("duration-second", unitWidth, 23); - return longListPatternExample( - unitWidth.listTypeLength.getPath(), - patternType, - value, - duration1, - duration2, - duration3, - duration4); + examples.add( + longListPatternExample( + unitWidth.listTypeLength.getPath(), + patternType, + value, + duration1, + duration2, + duration3, + duration4)); } public enum UnitLength { @@ -1899,7 +1902,7 @@ private String getValueFromFormat(String format, Object... arguments) { return cldrFile.getWinningValue(format(format, arguments)); } - public String handleEllipsis(String type, String value) { + public void handleEllipsis(String type, String value, List examples) { String pathFormat = "//ldml/localeDisplayNames/territories/territory[@type=\"{0}\"]"; // {0} … // … {0} @@ -1914,7 +1917,7 @@ public String handleEllipsis(String type, String value) { if (type.contains("initial")) { territory1 = territory2; } - return invertBackground(format(setBackground(value), territory1, territory2)); + examples.add(invertBackground(format(setBackground(value), territory1, territory2))); } public static String clip(String text, int clipStart, int clipEnd) { @@ -1939,7 +1942,7 @@ public static String clip(String text, int clipStart, int clipEnd) { * @param value * @return */ - private String handleMonthPatterns(XPathParts parts, String value) { + private void handleMonthPatterns(XPathParts parts, String value, List examples) { String calendar = parts.getAttributeValue(3, "type"); String context = parts.getAttributeValue(5, "type"); String month = "8"; @@ -1949,20 +1952,20 @@ private String handleMonthPatterns(XPathParts parts, String value) { "//ldml/dates/calendars/calendar[@type=\"{0}\"]/months/monthContext[@type=\"{1}\"]/monthWidth[@type=\"{2}\"]/month[@type=\"8\"]"; month = getValueFromFormat(xpath, calendar, context, width); } - return invertBackground(format(setBackground(value), month)); + examples.add(invertBackground(format(setBackground(value), month))); } - private String handleAppendItems(XPathParts parts, String value) { + private void handleAppendItems(XPathParts parts, String value, List examples) { String request = parts.getAttributeValue(-1, "request"); if (!"Timezone".equals(request)) { - return null; + return; } String calendar = parts.getAttributeValue(3, "type"); SimpleDateFormat sdf = icuServiceBuilder.getDateFormat(calendar, 0, DateFormat.MEDIUM, null); String zone = cldrFile.getStringValue("//ldml/dates/timeZoneNames/gmtZeroFormat"); - return format(value, setBackground(sdf.format(DATE_SAMPLE)), setBackground(zone)); + examples.add(format(value, setBackground(sdf.format(DATE_SAMPLE)), setBackground(zone))); } private class IntervalFormat { @@ -2045,22 +2048,23 @@ public IntervalFormat setPattern(XPathParts parts, String pattern) { } } - private String handleDurationUnit(String value) { + private void handleDurationUnit(String value, List examples) { DateFormat df = this.icuServiceBuilder.getDateFormat("gregorian", value.replace('h', 'H')); df.setTimeZone(TimeZone.GMT_ZONE); long time = ((5 * 60 + 37) * 60 + 23) * 1000; try { - return df.format(new Date(time)); + examples.add(df.format(new Date(time))); } catch (IllegalArgumentException e) { // e.g., Illegal pattern character 'o' in "aɖabaƒoƒo m:ss" - return null; + return; } } @SuppressWarnings("deprecation") - private String formatCountValue(String xpath, XPathParts parts, String value) { + private void formatCountValue( + String xpath, XPathParts parts, String value, List examples) { if (!parts.containsAttribute("count")) { // no examples for items that don't format - return null; + return; } final PluralInfo plurals = supplementalDataInfo.getPlurals(PluralType.cardinal, cldrFile.getLocaleID()); @@ -2077,12 +2081,12 @@ private String formatCountValue(String xpath, XPathParts parts, String value) { final LinkedHashSet exampleCount = new LinkedHashSet<>(CURRENCY_SAMPLES); String countString = parts.getAttributeValue(-1, "count"); if (countString == null) { - return null; + return; } else { try { count = Count.valueOf(countString); } catch (Exception e) { - return null; // counts like 0 + return; // counts like 0 } } @@ -2158,7 +2162,7 @@ private String formatCountValue(String xpath, XPathParts parts, String value) { } } } - return result.isEmpty() ? null : result; + examples.add(result.isEmpty() ? null : result); } @SuppressWarnings("deprecation") @@ -2270,7 +2274,7 @@ public String countAttribute(Count count) { return "[@count=\"" + count + "\"]"; } - private String handleMinimumGrouping(XPathParts parts, String value) { + private void handleMinimumGrouping(XPathParts parts, String value, List examples) { String numberSystem = cldrFile.getWinningValue("//ldml/numbers/defaultNumberingSystem"); String checkPath = "//ldml/numbers/decimalFormats[@numberSystem=\"" @@ -2283,14 +2287,12 @@ private String handleMinimumGrouping(XPathParts parts, String value) { double sampleNum1 = 543.21; double sampleNum2 = 6543.21; double sampleNum3 = 76543.21; - String example = ""; - example = addExampleResult(formatNumber(numberFormat, sampleNum1), example); - example = addExampleResult(formatNumber(numberFormat, sampleNum2), example); - example = addExampleResult(formatNumber(numberFormat, sampleNum3), example); - return example; + examples.add(formatNumber(numberFormat, sampleNum1)); + examples.add(formatNumber(numberFormat, sampleNum2)); + examples.add(formatNumber(numberFormat, sampleNum3)); } - private String handleNumberSymbol(XPathParts parts, String value) { + private void handleNumberSymbol(XPathParts parts, String value, List examples) { String symbolType = parts.getElement(-1); String numberSystem = parts.getAttributeValue(2, "numberSystem"); // null if not present int index; // dec/percent/sci @@ -2325,7 +2327,7 @@ private String handleNumberSymbol(XPathParts parts, String value) { } else { // We don't need examples for standalone symbols, i.e. infinity and nan. // We don't have an example for the list symbol either. - return null; + return; } DecimalFormat x = icuServiceBuilder.getNumberFormat(index, numberSystem); String example; @@ -2354,16 +2356,16 @@ private String handleNumberSymbol(XPathParts parts, String value) { } example = x.format(numberSample); example = example.replace(originalValue, formattedValue); - return backgroundStartSymbol + example + backgroundEndSymbol; + examples.add(backgroundStartSymbol + example + backgroundEndSymbol); } - private String handleNumberingSystem(String value) { + private void handleNumberingSystem(String value, List examples) { NumberFormat x = icuServiceBuilder.getGenericNumberFormat(value); x.setGroupingUsed(false); - return x.format(NUMBER_SAMPLE_WHOLE); + examples.add(x.format(NUMBER_SAMPLE_WHOLE)); } - private String handleTimeZoneName(XPathParts parts, String value) { + private void handleTimeZoneName(XPathParts parts, String value, List examples) { String result = null; if (parts.contains("exemplarCity")) { // ldml/dates/timeZoneNames/zone[@type="America/Los_Angeles"]/exemplarCity @@ -2375,7 +2377,8 @@ private String handleTimeZoneName(XPathParts parts, String value) { } else { result = value; // trivial -- is this beneficial? } - return result; + examples.add(result); + return; } if (countryCode.equals("001")) { // GMT code, so format. @@ -2384,7 +2387,7 @@ private String handleTimeZoneName(XPathParts parts, String value) { int hours = Integer.parseInt(hourOffset); result = getGMTFormat(null, null, hours); } catch (RuntimeException e) { - return null; // fail, skip + return; // fail, skip } } else { result = setBackground(cldrFile.getName(CLDRFile.TERRITORY_NAME, countryCode)); @@ -2464,11 +2467,12 @@ private String handleTimeZoneName(XPathParts parts, String value) { } } } - return result; + examples.add(result); } @SuppressWarnings("deprecation") - private String handleDateFormatItem(String xpath, String value, boolean showContexts) { + private void handleDateFormatItem( + String xpath, String value, boolean showContexts, List examples) { // Get here if parts contains "calendar" and either of "pattern", "dateFormatItem" String fullpath = cldrFile.getFullXPath(xpath); @@ -2515,7 +2519,7 @@ private String handleDateFormatItem(String xpath, String value, boolean showCont cldrFile.getWinningPath(xpath.replaceAll("dateTimeFormat", "timeFormat")); int tfLengthOffset = timeFormatXPathForPrefix.indexOf("timeFormatLength"); if (tfLengthOffset < 0) { - return ""; + return; } String timeFormatXPathPrefix = timeFormatXPathForPrefix.substring(0, tfLengthOffset); String timeLongerFormatXPath = @@ -2548,8 +2552,6 @@ private String handleDateFormatItem(String xpath, String value, boolean showCont "//ldml/dates/fields/field[@type=\"day\"]/relative[@type=\"0\"]"); String relativeDayValue = cldrFile.getWinningValue(relativeDayXPath); - List examples = new ArrayList<>(); - String dfResult = df.format(DATE_SAMPLE); String tlfResult = tlf.format(DATE_SAMPLE); String tsfResult = tsf.format(DATE_SAMPLE); // DATE_SAMPLE is in the afternoon @@ -2644,11 +2646,12 @@ private String handleDateFormatItem(String xpath, String value, boolean showCont examples.add(dtf.format(DATE_SAMPLE)); } - return formatExampleList(examples.toArray(new String[0])); + return; } else { String id = parts.findAttributeValue("dateFormatItem", "id"); if ("NEW".equals(id) || value == null) { - return startItalicSymbol + "n/a" + endItalicSymbol; + examples.add(startItalicSymbol + "n/a" + endItalicSymbol); + return; } else { String numbersOverride = parts.findAttributeValue("pattern", "numbers"); SimpleDateFormat sdf = @@ -2668,7 +2671,8 @@ private String handleDateFormatItem(String xpath, String value, boolean showCont // Standard date/time format, or availableFormat without dayPeriod if (value.contains("MMM") || value.contains("LLL")) { // alpha month, do not need context examples - return sdf.format(DATE_SAMPLE); + examples.add(sdf.format(DATE_SAMPLE)); + return; } else { // Use contextExamples if showContexts T String example = @@ -2683,10 +2687,10 @@ private String handleDateFormatItem(String xpath, String value, boolean showCont if (!sup_twelve_example.equals(sub_ten_example)) { example = addExampleResult(sub_ten_example, example, showContexts); } - return example; + examples.add(example); + return; } } else { - List examples = new ArrayList<>(); DayPeriodInfo dayPeriodInfo = supplementalDataInfo.getDayPeriods( DayPeriodInfo.Type.format, cldrFile.getLocaleID()); @@ -2708,7 +2712,7 @@ private String handleDateFormatItem(String xpath, String value, boolean showCont examples.add(formatted); } } - return formatExampleList(examples.toArray(new String[0])); + return; } } } @@ -2741,7 +2745,8 @@ private boolean symbolIsLetters(String currencySymbol, boolean onBothSides) { * @param value * @return */ - private String handleCurrencyFormat(XPathParts parts, String value, boolean showContexts) { + private void handleCurrencyFormat( + XPathParts parts, String value, boolean showContexts, List examples) { String example = showContexts ? exampleStartHeaderSymbol + contextheader + exampleEndSymbol : ""; @@ -2766,7 +2771,8 @@ private String handleCurrencyFormat(XPathParts parts, String value, boolean show String countValue = parts.getAttributeValue(-1, "count"); if (countValue != null) { - return formatCountDecimal(df, countValue); + examples.add(formatCountDecimal(df, countValue)); + return; } double sampleAmount = 1295.00; @@ -2790,7 +2796,7 @@ private String handleCurrencyFormat(XPathParts parts, String value, boolean show example = addExampleResult(formatNumber(df, -sampleAmount), example, showContexts); } - return example; + examples.add(example); } private String getDefaultTerritory() { @@ -2823,14 +2829,16 @@ private String getDefaultTerritory() { * @param value * @return */ - private String handleDecimalFormat(XPathParts parts, String value, boolean showContexts) { + private void handleDecimalFormat( + XPathParts parts, String value, boolean showContexts, List examples) { String example = showContexts ? exampleStartHeaderSymbol + contextheader + exampleEndSymbol : ""; String numberSystem = parts.getAttributeValue(2, "numberSystem"); // null if not present DecimalFormat numberFormat = icuServiceBuilder.getNumberFormat(value, numberSystem); String countValue = parts.getAttributeValue(-1, "count"); if (countValue != null) { - return formatCountDecimal(numberFormat, countValue); + examples.add(formatCountDecimal(numberFormat, countValue)); + return; } double sampleNum1 = 5.43; @@ -2842,7 +2850,7 @@ private String handleDecimalFormat(XPathParts parts, String value, boolean showC example = addExampleResult(formatNumber(numberFormat, sampleNum2), example, showContexts); // have positive and negative example = addExampleResult(formatNumber(numberFormat, -sampleNum2), example, showContexts); - return example; + examples.add(example); } private String formatCountDecimal(DecimalFormat numberFormat, String countValue) { @@ -2899,7 +2907,8 @@ private Double getExampleForPattern(DecimalFormat format, Count count) { return samples.get(count); } - private String handleCurrency(String xpath, XPathParts parts, String value) { + private void handleCurrency( + String xpath, XPathParts parts, String value, List examples) { String currency = parts.getAttributeValue(-2, "type"); String fullPath = cldrFile.getFullXPath(xpath, false); if (parts.contains("symbol")) { @@ -2922,29 +2931,27 @@ private String handleCurrency(String xpath, XPathParts parts, String value) { result = setBackground(result) .replace(value, backgroundEndSymbol + value + backgroundStartSymbol); - return result; + examples.add(result); } else if (parts.contains("displayName")) { - return formatCountValue(xpath, parts, value); + formatCountValue(xpath, parts, value, examples); } - return null; + return; } - private String handleDateRangePattern(String value) { - String result; + private void handleDateRangePattern(String value, List examples) { SimpleDateFormat dateFormat = icuServiceBuilder.getDateFormat("gregorian", 2, 0); - result = + examples.add( format( value, setBackground(dateFormat.format(DATE_SAMPLE)), - setBackground(dateFormat.format(DATE_SAMPLE2))); - return result; + setBackground(dateFormat.format(DATE_SAMPLE2)))); } /** * Add examples for eras. First checks if there is info for this calendar type and this era type * in the CALENDAR_ERAS map, then generates a sample date based on this info and formats it */ - private String handleEras(XPathParts parts, String value) { + private void handleEras(XPathParts parts, String value, List examples) { String calendarId = parts.getAttributeValue(3, "type"); String type = parts.getAttributeValue(-1, "type"); String id = @@ -2952,12 +2959,12 @@ private String handleEras(XPathParts parts, String value) { ? "islamic" : calendarId; // islamic variations map to same sample if (!CALENDAR_ERAS.containsKey(id)) { - return null; + return; } int typeIndex = Integer.parseInt(type); if (calendarId.equals("japanese")) { if (typeIndex < 235) { // examples only for 2 most recent eras - return null; + return; } else { typeIndex %= 235; // map to length 2 list } @@ -2978,21 +2985,21 @@ private String handleEras(XPathParts parts, String value) { eraNames[typeIndex] = value; // overwrite era to current value dfs.setEras(eraNames); sdf.setDateFormatSymbols(dfs); - return sdf.format(sample); + examples.add(sdf.format(sample)); } /** * Add examples for quarters for the gregorian calendar, matching each quarter type (1, 2, 3, 4) * to a corresponding sample month and formatting an example with that date */ - private String handleQuarters(XPathParts parts, String value) { + void handleQuarters(XPathParts parts, String value, List examples) { String calendarId = parts.getAttributeValue(3, "type"); if (!calendarId.equals("gregorian")) { - return null; + return; } String width = parts.findAttributeValue("quarterWidth", "type"); if (width.equals("narrow")) { - return null; + return; } String context = parts.findAttributeValue("quarterContext", "type"); String type = parts.getAttributeValue(-1, "type"); // 1-indexed @@ -3017,7 +3024,7 @@ private String handleQuarters(XPathParts parts, String value) { int month = monthSamples[quarterIndex]; calendar.set(1999, month, 5, 13, 25, 59); Date sample = calendar.getTime(); - return sdf.format(sample); + examples.add(sdf.format(sample)); } /** @@ -3035,7 +3042,8 @@ private String getLocaleDisplayPattern(String elementToOverride, String element, } } - private String handleDisplayNames(String xpath, XPathParts parts, String value) { + private void handleDisplayNames( + String xpath, XPathParts parts, String value, List examples) { String result = null; if (parts.contains("codePatterns")) { // ldml/localeDisplayNames/codePatterns/codePattern[@type="language"] @@ -3051,6 +3059,8 @@ private String handleDisplayNames(String xpath, XPathParts parts, String value) : type.equals("script") ? "Avst" : type.equals("territory") ? "057" : "CODE")); + examples.add(result); + return; } else if (parts.contains("localeDisplayPattern")) { // ldml/localeDisplayNames/localeDisplayPattern/localePattern // ldml/localeDisplayNames/localeDisplayPattern/localeSeparator @@ -3069,18 +3079,18 @@ private String handleDisplayNames(String xpath, XPathParts parts, String value) locales.add( element.equals("localeKeyTypePattern") ? "uz-Arab-u-tz-etadd" : "uz-Arab-AF"); locales.add("uz-Arab-AF-u-tz-etadd-nu-arab"); - String[] examples = new String[locales.size()]; + // String[] examples = new String[locales.size()]; for (int i = 0; i < locales.size(); i++) { - examples[i] = + examples.add( invertBackground( cldrFile.getName( locales.get(i), false, localeKeyTypePattern, localePattern, - localeSeparator)); + localeSeparator))); } - result = formatExampleList(examples); + return; } else if (parts.contains("languages") || parts.contains("scripts") || parts.contains("territories")) { @@ -3094,9 +3104,10 @@ private String handleDisplayNames(String xpath, XPathParts parts, String value) } else { result = cldrFile.getBaileyValue(xpath, null, null); } + examples.add(result); + return; } else { value = setBackground(value); - List examples = new ArrayList<>(); String nameType = parts.getElement(3); Map likely = supplementalDataInfo.getLikelySubtags(); @@ -3107,7 +3118,7 @@ private String handleDisplayNames(String xpath, XPathParts parts, String value) String tag = "language".equals(nameType) ? type : "und_" + type; String max = LikelySubtags.maximize(tag, likely); if (max == null) { - return null; + return; } LanguageTagParser ltp = new LanguageTagParser().set(max); String languageName = null; @@ -3216,10 +3227,9 @@ private String handleDisplayNames(String xpath, XPathParts parts, String value) + "\"]"); examples.add(invertBackground(format(codePattern, value))); } - result = formatExampleList(examples.toArray(new String[0])); + return; } } - return result; } private String formatExampleList(String[] examples) { @@ -3236,7 +3246,7 @@ private String formatExampleList(String[] examples) { * @param examples * @return */ - private String formatExampleList(Collection examples) { + public String formatExampleList(Collection examples) { if (examples == null || examples.isEmpty()) { return null; } diff --git a/tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestExampleGenerator.java b/tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestExampleGenerator.java index cdfb17560b0..121390a1be1 100644 --- a/tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestExampleGenerator.java +++ b/tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestExampleGenerator.java @@ -504,9 +504,10 @@ public void TestCompoundUnit() { private void checkCompoundUnits(String locale, String[][] tests) { ExampleGenerator exampleGenerator = getExampleGenerator(locale); for (String[] test : tests) { - String actual = - exampleGenerator.handleCompoundUnit( - UnitLength.valueOf(test[1]), test[0], Count.valueOf(test[2])); + List examples = new ArrayList<>(); + exampleGenerator.handleCompoundUnit( + UnitLength.valueOf(test[1]), test[0], Count.valueOf(test[2]), examples); + String actual = exampleGenerator.formatExampleList(examples); assertEquals("CompoundUnit", test[3], ExampleGenerator.simplify(actual, true)); } } @@ -566,10 +567,10 @@ public void TestCompoundUnit2() { for (String[] test : tests) { ExampleGenerator exampleGenerator = getExampleGenerator(test[0]); - - String actual = - exampleGenerator.handleCompoundUnit1( - UnitLength.valueOf(test[1]), Count.valueOf(test[2]), test[3]); + List examples = new ArrayList<>(); + exampleGenerator.handleCompoundUnit1( + UnitLength.valueOf(test[1]), Count.valueOf(test[2]), test[3], examples); + String actual = exampleGenerator.formatExampleList(examples); assertEquals("CompoundUnit", test[4], ExampleGenerator.simplify(actual, true)); } }