1 package org.unicode.cldr.util; 2 3 import java.util.Collections; 4 import java.util.EnumSet; 5 import java.util.Set; 6 7 import org.unicode.cldr.util.GrammarInfo.GrammaticalFeature; 8 9 import com.google.common.collect.ImmutableMultimap; 10 import com.google.common.collect.ImmutableSet; 11 import com.google.common.collect.Multimap; 12 13 public enum UnitPathType { 14 // we may add features over time 15 unit("kilometer", EnumSet.of( 16 GrammarInfo.GrammaticalFeature.grammaticalNumber, 17 GrammarInfo.GrammaticalFeature.grammaticalCase), 18 null), 19 perUnit("minute", null, null), 20 times(null, null, ImmutableMultimap.<String,String>builder() 21 .put("", "newton-meter") 22 .put("", "kilowatt-hour") 23 .build()), 24 per(null, null, ImmutableMultimap.<String,String>builder() 25 .put("", "meter-per-second") 26 .put("", "mile-per-gallon") 27 .build()), 28 prefix(null, null, ImmutableMultimap.<String,String>builder() 29 .put("10p2", "hectopascal") 30 .put("10p3", "kilometer") 31 .put("10p6", "megabyte") 32 .put("10p9", "gigahertz") 33 .put("10p12", "terabyte") 34 .put("10p15", "petabyte") 35 .put("10p-1", "deciliter") 36 .put("10p-2", "centimeter") 37 .put("10p-3", "milligram") 38 .put("10p-6", "microsecond") 39 .put("10p-9", "nanosecond") 40 .put("10p-12", "picometer") 41 .build()), 42 power("power2", 43 EnumSet.of( 44 GrammarInfo.GrammaticalFeature.grammaticalNumber, 45 GrammarInfo.GrammaticalFeature.grammaticalCase, 46 GrammarInfo.GrammaticalFeature.grammaticalGender), 47 ImmutableMultimap.<String,String>builder() 48 .put("power2", "square-meter") 49 .put("power2", "square-second") 50 .put("power3", "cubic-meter") 51 .put("power3", "cubic-second") 52 .build()), 53 duration(null, null, null), 54 gender(null,null, null), 55 coordinate(null, null, null), 56 displayName(null, null, null) 57 ; 58 59 public final Set<GrammaticalFeature> features; 60 public final Set<String> sampleShortUnitType; 61 public final ImmutableMultimap<String,String> sampleComposedShortUnitIds; 62 UnitPathType(String sampleType, Set<GrammarInfo.GrammaticalFeature> features, ImmutableMultimap<String,String> sampleComposedLongUnits)63 private UnitPathType(String sampleType, Set<GrammarInfo.GrammaticalFeature> features, ImmutableMultimap<String,String> sampleComposedLongUnits) { 64 this.sampleShortUnitType = Collections.singleton(sampleType); 65 this.sampleComposedShortUnitIds = sampleComposedLongUnits; 66 this.features = features == null ? Collections.emptySet() : ImmutableSet.copyOf(features); 67 } 68 getPathType(XPathParts parts)69 public static UnitPathType getPathType(XPathParts parts) { 70 if (!parts.getElement(2).equals("unitLength")) { 71 return null; 72 } 73 switch (parts.getElement(-1)) { 74 case "compoundUnitPattern": return "times".equals(parts.getAttributeValue(-2, "type")) ? UnitPathType.times : UnitPathType.per; 75 case "unitPrefixPattern": return UnitPathType.prefix; 76 case "compoundUnitPattern1": return UnitPathType.power; 77 case "unitPattern": return UnitPathType.unit; 78 case "perUnitPattern": return UnitPathType.perUnit; 79 case "prefix": return UnitPathType.prefix; 80 case "gender": return UnitPathType.gender; 81 case "coordinateUnitPattern": return UnitPathType.coordinate; 82 case "durationUnit": return UnitPathType.duration; 83 case "alias": return null; 84 case "displayName": return UnitPathType.displayName; 85 } 86 throw new IllegalArgumentException("PathType: " + parts); 87 } 88 getTranslationPath(LocaleStringProvider resolvedFile, String width, String shortUnitId, String pluralCategory, String caseVariant, String genderVariant)89 public String getTranslationPath(LocaleStringProvider resolvedFile, String width, String shortUnitId, String pluralCategory, 90 String caseVariant, String genderVariant) { 91 UnitPathType pathType = this; 92 final String pathPrefix = "//ldml/units/unitLength[@type=\"" + width + "\"]/"; 93 String longUnitId; 94 GrammarInfo grammarInfo1; 95 UnitConverter uc = CLDRConfig.getInstance().getSupplementalDataInfo().getUnitConverter(); 96 97 String grammaticalAttributes; 98 switch (pathType) { 99 case times: 100 return pathPrefix + "compoundUnit[@type=\"" + "times" + "\"]/compoundUnitPattern"; 101 case per: 102 return pathPrefix + "compoundUnit[@type=\"" + "per" + "\"]/compoundUnitPattern"; 103 case prefix: 104 longUnitId = CLDRConfig.getInstance().getSupplementalDataInfo().getUnitConverter().getLongId(shortUnitId); 105 return pathPrefix + "compoundUnit[@type=\"" + longUnitId + "\"]/unitPrefixPattern"; 106 case power: 107 longUnitId = uc.getLongId(shortUnitId); 108 grammarInfo1 = CLDRConfig.getInstance().getSupplementalDataInfo().getGrammarInfo(resolvedFile.getLocaleID()); 109 grammaticalAttributes = GrammarInfo.getGrammaticalInfoAttributes(grammarInfo1, pathType, pluralCategory, genderVariant, caseVariant); 110 return pathPrefix + "compoundUnit[@type=\"" + longUnitId + "\"]/compoundUnitPattern1" + grammaticalAttributes; 111 case unit: 112 longUnitId = uc.getLongId(shortUnitId); 113 grammarInfo1 = CLDRConfig.getInstance().getSupplementalDataInfo().getGrammarInfo(resolvedFile.getLocaleID()); 114 grammaticalAttributes = GrammarInfo.getGrammaticalInfoAttributes(grammarInfo1, pathType, pluralCategory, genderVariant, caseVariant); 115 return pathPrefix + "unit[@type=\"" + longUnitId + "\"]/unitPattern" + grammaticalAttributes; 116 case displayName: 117 longUnitId = uc.getLongId(shortUnitId); 118 return pathPrefix + "unit[@type=\"" + longUnitId + "\"]/displayName"; 119 case perUnit: 120 longUnitId = uc.getLongId(shortUnitId); 121 return pathPrefix + "unit[@type=\"" + longUnitId + "\"]/perUnitPattern"; 122 case gender: 123 if (!width.equals("long")) { 124 throw new IllegalArgumentException("illegal width for gender: "); 125 } 126 longUnitId = uc.getLongId(shortUnitId); 127 return pathPrefix + "unit[@type=\"" + uc.getLongId(shortUnitId) + "\"]/gender"; 128 case coordinate: 129 return pathPrefix + "coordinateUnit/coordinateUnitPattern[@type=\"" + shortUnitId + "\"]"; 130 case duration: 131 return "//ldml/units/durationUnit[@type=\"" + shortUnitId + "\"]/durationUnitPattern"; 132 } 133 throw new IllegalArgumentException("PathType: " + pathType); 134 } 135 getTrans(LocaleStringProvider resolvedFile, String width, String shortUnitId, String pluralCategory, String caseVariant, String genderVariant, Multimap<UnitPathType, String> partsUsed)136 public String getTrans(LocaleStringProvider resolvedFile, String width, String shortUnitId, String pluralCategory, String caseVariant, String genderVariant, Multimap<UnitPathType, String> partsUsed) { 137 UnitPathType pathType = this; 138 String path = pathType.getTranslationPath(resolvedFile, width, shortUnitId, pluralCategory, caseVariant, genderVariant); 139 String result = resolvedFile.getStringValue(path); 140 if (result == null) { 141 int debug = 0; 142 } 143 144 if (partsUsed != null) { 145 CLDRFile.Status status = new CLDRFile.Status(); 146 String foundLocale = resolvedFile.getSourceLocaleID(path, status ); 147 partsUsed.put(pathType, 148 (result != null ? "«" + result + "»": "∅") 149 + (foundLocale.equals(resolvedFile.getLocaleID()) ? "" : "\n\t\tactualLocale: " + foundLocale) 150 + (status.pathWhereFound.equals(path) ? "" : "\n\t\trequestPath: " + path + "\n\t\tactualPath: " + status.pathWhereFound) 151 ); 152 } 153 return result; 154 } 155 }