Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLDR-11155 Refactoring per comments on the previous commit #3539

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading