Skip to content

Commit

Permalink
CLDR-11155 Refactoring per comments on the previous commit (#3539)
Browse files Browse the repository at this point in the history
-Throw InternalCldrException instead of RuntimeException

-Call getVolumePageId() from fix() instead of from makePathHeader()
  • Loading branch information
btangmu authored Feb 28, 2024
1 parent b873ba0 commit b0fa80d
Showing 1 changed file with 26 additions and 29 deletions.
55 changes: 26 additions & 29 deletions tools/cldr-code/src/main/java/org/unicode/cldr/util/PathHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -755,13 +755,7 @@ private PathHeader makePathHeader(RawData data, String path, String alt) {
// Caution: each call to PathHeader.Factory.fix changes the value of
// PathHeader.Factory.order
SectionId newSectionId = SectionId.forString(fix(data.section, 0));
String pageIdName = fix(data.page, 0);
PageId newPageId;
if ("Volume".equals(pageIdName)) {
newPageId = getVolumePageId(path);
} else {
newPageId = PageId.forString(pageIdName);
}
PageId newPageId = PageId.forString(fix(data.page, 0));
String newHeader = fix(data.header, data.headerOrder);
int newHeaderOrder = (int) order;
String codeDashAlt = data.code + (alt == null ? "" : ("-" + alt));
Expand All @@ -779,28 +773,6 @@ private PathHeader makePathHeader(RawData data, String path, String alt) {
path);
}

private static Set<UnitConverter.UnitSystem> METRIC =
Set.of(UnitConverter.UnitSystem.metric, UnitConverter.UnitSystem.metric_adjacent);

private PageId getVolumePageId(String path) {
// Extract the unit from the path. For example, if path is
// //ldml/units/unitLength[@type="narrow"]/unit[@type="volume-cubic-kilometer"]/displayName
// then extract "volume-cubic-kilometer" which is the long unit id
final String longUnitId =
XPathParts.getFrozenInstance(path).findAttributeValue("unit", "type");
if (longUnitId == null) {
throw new RuntimeException("Missing unit in path " + path);
}
final UnitConverter uc = supplementalDataInfo.getUnitConverter();
// Convert, for example, "volume-cubic-kilometer" to "cubic-kilometer"
final String shortUnitId = uc.getShortId(longUnitId);
if (!Collections.disjoint(METRIC, uc.getSystemsEnum(shortUnitId))) {
return PageId.Volume_Metric;
} else {
return PageId.Volume_Other;
}
}

private static class SectionPage implements Comparable<SectionPage> {
private final SectionId sectionId;
private final PageId pageId;
Expand Down Expand Up @@ -2267,6 +2239,9 @@ private static String fix(String input, int orderIn) {
while (true) {
int functionStart = input.indexOf('&', pos);
if (functionStart < 0) {
if ("Volume".equals(input)) {
return getVolumePageId(args.value[0] /* path */).toString();
}
return input;
}
int functionEnd = input.indexOf('(', functionStart);
Expand All @@ -2287,6 +2262,28 @@ private static String fix(String input, int orderIn) {
}
}

private static Set<UnitConverter.UnitSystem> METRIC =
Set.of(UnitConverter.UnitSystem.metric, UnitConverter.UnitSystem.metric_adjacent);

private static PageId getVolumePageId(String path) {
// Extract the unit from the path. For example, if path is
// //ldml/units/unitLength[@type="narrow"]/unit[@type="volume-cubic-kilometer"]/displayName
// then extract "volume-cubic-kilometer" which is the long unit id
final String longUnitId =
XPathParts.getFrozenInstance(path).findAttributeValue("unit", "type");
if (longUnitId == null) {
throw new InternalCldrException("Missing unit in path " + path);
}
final UnitConverter uc = supplementalDataInfo.getUnitConverter();
// Convert, for example, "volume-cubic-kilometer" to "cubic-kilometer"
final String shortUnitId = uc.getShortId(longUnitId);
if (!Collections.disjoint(METRIC, uc.getSystemsEnum(shortUnitId))) {
return PageId.Volume_Metric;
} else {
return PageId.Volume_Other;
}
}

/**
* Collect all the paths for a CLDRFile, and make sure that they have cached PathHeaders
*
Expand Down

0 comments on commit b0fa80d

Please sign in to comment.