• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.unicode.cldr.util;
2 
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.EnumSet;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.Map.Entry;
10 import java.util.Set;
11 import java.util.TreeSet;
12 import java.util.regex.Matcher;
13 import java.util.regex.Pattern;
14 
15 import org.unicode.cldr.util.PatternPlaceholders.PlaceholderInfo;
16 
17 import com.ibm.icu.text.MessageFormat;
18 import com.ibm.icu.util.Output;
19 
20 public class PathDescription {
21 
22     public enum ErrorHandling {
23         SKIP, CONTINUE
24     }
25 
26     // BE sure to sync with the list in xmbSkip!
27     public static final Set<String> EXTRA_LANGUAGES = new TreeSet<>(
28         Arrays
29             .asList(
30                 "ach|af|ak|ak|am|ar|az|be|bem|bg|bh|bn|br|bs|ca|chr|ckb|co|crs|cs|cy|da|de|de_AT|de_CH|ee|el|en|en_AU|en_CA|en_GB|en_US|eo|es|es_419|es_ES|et|eu|fa|fi|fil|fo|fr|fr_CA|fr_CH|fy|ga|gaa|gd|gl|gn|gsw|gu|ha|haw|he|hi|hr|ht|hu|hy|ia|id|ig|io|is|it|ja|jv|ka|kg|kk|km|kn|ko|kri|ku|ky|la|lg|ln|lo|loz|lt|lua|lv|mfe|mg|mi|mk|ml|mn|mr|ms|mt|my|nb|ne|nl|nl_BE|nn|nso|ny|nyn|oc|om|or|pa|pcm|pl|ps|pt|pt_BR|pt_PT|qu|rm|rn|ro|ro|ro_MD|ru|rw|sd|si|sk|sl|sn|so|sq|sr|sr_Latn|sr_ME|st|su|sv|sw|ta|te|tg|th|ti|tk|tlh|tn|to|tr|tt|tum|ug|uk|und|ur|uz|vi|wo|xh|yi|yo|zh|zh_Hans|zh_Hant|zh_HK|zu|zxx"
31                     .split("|")));
32 
33     private static final Pattern METAZONE_PATTERN = Pattern
34         .compile("//ldml/dates/timeZoneNames/metazone\\[@type=\"([^\"]*)\"]/(.*)/(.*)");
35     private static final Pattern STAR_ATTRIBUTE_PATTERN = PatternCache.get("=\"([^\"]*)\"");
36 
37     private static final StandardCodes STANDARD_CODES = StandardCodes.make();
38     private static Map<String, String> ZONE2COUNTRY = STANDARD_CODES.getZoneToCounty();
39     private static RegexLookup<String> pathHandling = new RegexLookup<String>().loadFromFile(PathDescription.class,
40         "data/PathDescription.txt");
41 
42     // set in construction
43 
44     private final CLDRFile english;
45     private final Map<String, String> extras;
46     private final ErrorHandling errorHandling;
47     private final Map<String, List<Set<String>>> starredPaths;
48     private final Set<String> allMetazones;
49 
50     // used on instance
51 
52     private Matcher metazoneMatcher = METAZONE_PATTERN.matcher("");
53     private String starredPathOutput;
54     private Output<String[]> pathArguments = new Output<>();
55     private EnumSet<Status> status = EnumSet.noneOf(Status.class);
56 
57     public static final String MISSING_DESCRIPTION = "Before translating, please see http://cldr.org/translation.";
58 
PathDescription(SupplementalDataInfo supplementalDataInfo, CLDRFile english, Map<String, String> extras, Map<String, List<Set<String>>> starredPaths, ErrorHandling errorHandling)59     public PathDescription(SupplementalDataInfo supplementalDataInfo,
60         CLDRFile english,
61         Map<String, String> extras,
62         Map<String, List<Set<String>>> starredPaths,
63         ErrorHandling errorHandling) {
64         this.english = english;
65         this.extras = extras == null ? new HashMap<>() : extras;
66         this.starredPaths = starredPaths == null ? new HashMap<>() : starredPaths;
67         allMetazones = supplementalDataInfo.getAllMetazones();
68         this.errorHandling = errorHandling;
69     }
70 
getStarredPathOutput()71     public String getStarredPathOutput() {
72         return starredPathOutput;
73     }
74 
getStatus()75     public EnumSet<Status> getStatus() {
76         return status;
77     }
78 
79     public enum Status {
80         SKIP, NULL_VALUE, EMPTY_CONTENT, NOT_REQUIRED
81     }
82 
getRawDescription(String path, String value, Object context)83     public String getRawDescription(String path, String value, Object context) {
84         status.clear();
85         return pathHandling.get(path, context, pathArguments);
86     }
87 
getDescription(String path, String value, Level level, Object context)88     public String getDescription(String path, String value, Level level, Object context) {
89         status.clear();
90 
91         String description = pathHandling.get(path, context, pathArguments);
92         if (description == null) {
93             description = MISSING_DESCRIPTION;
94         } else if ("SKIP".equals(description)) {
95             status.add(Status.SKIP);
96             if (errorHandling == ErrorHandling.SKIP) {
97                 return null;
98             }
99         }
100 
101         // String localeWhereFound = english.getSourceLocaleID(path, status);
102         // if (!status.pathWhereFound.equals(path)) {
103         // reasonsToPaths.put("alias", path + "  " + value);
104         // continue;
105         // }
106         if (value == null) { // a count item?
107             String xpath = extras.get(path);
108             if (xpath != null) {
109                 value = english.getStringValue(xpath);
110             } else if (path.contains("/metazone")) {
111                 if (metazoneMatcher.reset(path).matches()) {
112                     String name = metazoneMatcher.group(1);
113                     String type = metazoneMatcher.group(3);
114                     value = name.replace('_', ' ')
115                         + (type.equals("generic") ? "" : type.equals("daylight") ? " Summer" : " Winter") + " Time";
116                     // System.out.println("Missing:    " + path + " :    " + value);
117                 }
118             }
119             if (value == null) {
120                 status.add(Status.NULL_VALUE);
121                 if (errorHandling == ErrorHandling.SKIP) {
122                     return null;
123                 }
124             }
125         }
126         if (value != null && value.length() == 0) {
127             status.add(Status.EMPTY_CONTENT);
128             if (errorHandling == ErrorHandling.SKIP) {
129                 return null;
130             }
131         }
132         // if (GenerateXMB.contentMatcher != null && !GenerateXMB.contentMatcher.reset(value).find()) {
133         // PathDescription.addSkipReasons(reasonsToPaths, "content-parameter", level, path, value);
134         // return null;
135         // }
136 
137         List<String> attributes = addStarredInfo(starredPaths, path);
138 
139         // In special cases, only use if there is a root value (languageNames, ...
140         if (description.startsWith("ROOT")) {
141             int typeEnd = description.indexOf(';');
142             String type = description.substring(4, typeEnd).trim();
143             description = description.substring(typeEnd + 1).trim();
144 
145             boolean isMetazone = type.equals("metazone");
146             String code = attributes.get(0);
147             boolean isRootCode = isRootCode(code, allMetazones, type, isMetazone);
148             if (!isRootCode) {
149                 status.add(Status.NOT_REQUIRED);
150                 if (errorHandling == ErrorHandling.SKIP) {
151                     return null;
152                 }
153             }
154             if (isMetazone) {
155                 XPathParts parts = XPathParts.getFrozenInstance(path);
156                 String daylightType = parts.getElement(-1);
157                 daylightType = daylightType.equals("daylight") ? "summer" : daylightType.equals("standard") ? "winter"
158                     : daylightType;
159                 String length = parts.getElement(-2);
160                 length = length.equals("long") ? "" : "abbreviated ";
161                 code = code + ", " + length + daylightType + " form";
162             } else if (type.equals("timezone")) {
163                 String country = ZONE2COUNTRY.get(code);
164                 int lastSlash = code.lastIndexOf('/');
165                 String codeName = lastSlash < 0 ? code : code.substring(lastSlash + 1).replace('_', ' ');
166 
167                 boolean found = false;
168                 if ("001".equals(country)) {
169                     code = "the timezone “" + codeName + "”";
170                     found = true;
171                 } else if (country != null) {
172                     String countryName = english.getName("territory", country);
173                     if (countryName != null) {
174                         if (!codeName.equals(countryName)) {
175                             code = "the city “" + codeName + "” (in " + countryName + ")";
176                         } else {
177                             code = "the country “" + codeName + "”";
178                         }
179                         found = true;
180                     }
181                 }
182                 if (!found) {
183                     System.out.println("Missing country for timezone " + code);
184                 }
185             }
186             description = MessageFormat.format(MessageFormat.autoQuoteApostrophe(description), new Object[] { code });
187         } else if (path.contains("exemplarCity")) {
188             String regionCode = ZONE2COUNTRY.get(attributes.get(0));
189             String englishRegionName = english.getName(CLDRFile.TERRITORY_NAME, regionCode);
190             description = MessageFormat.format(MessageFormat.autoQuoteApostrophe(description),
191                 new Object[] { englishRegionName });
192         } else if (description != MISSING_DESCRIPTION) {
193             description = MessageFormat.format(MessageFormat.autoQuoteApostrophe(description),
194                 (Object[]) pathArguments.value);
195         }
196 
197         return description;
198     }
199 
200     /**
201      * Creates an escaped HTML string of placeholder information.
202      *
203      * @param path
204      *            the xpath to specify placeholder information for
205      * @return a HTML string, or an empty string if there was no placeholder information
206      */
207     public String getPlaceholderDescription(String path) {
208         Map<String, PlaceholderInfo> placeholders = PatternPlaceholders.getInstance().get(path);
209         if (placeholders != null && placeholders.size() > 0) {
210             StringBuffer buffer = new StringBuffer();
211             buffer.append("<table>");
212             buffer.append("<tr><th>Placeholder</th><th>Meaning</th><th>Example</th></tr>");
213             for (Entry<String, PlaceholderInfo> entry : placeholders.entrySet()) {
214                 PlaceholderInfo info = entry.getValue();
215                 buffer.append("<tr>");
216                 buffer.append("<td>").append(entry.getKey()).append("</td>");
217                 buffer.append("<td>").append(info.name).append("</td>");
218                 buffer.append("<td>").append(info.example).append("</td>");
219                 buffer.append("</tr>");
220             }
221             buffer.append("</table>");
222             return buffer.toString();
223         }
224         return "";
225     }
226 
isRootCode(String code, Set<String> allMetazones, String type, boolean isMetazone)227     private static boolean isRootCode(String code, Set<String> allMetazones, String type, boolean isMetazone) {
228         Set<String> codes = isMetazone ? allMetazones
229             : type.equals("timezone") ? STANDARD_CODES.getCanonicalTimeZones()
230                 : STANDARD_CODES.getSurveyToolDisplayCodes(type);
231         // end
232         boolean isRootCode = codes.contains(code) || code.contains("_");
233         if (!isRootCode && type.equals("language")
234             && EXTRA_LANGUAGES.contains(code)) {
235             isRootCode = true;
236         }
237         return isRootCode;
238     }
239 
addStarredInfo(Map<String, List<Set<String>>> starredPaths, String path)240     private List<String> addStarredInfo(Map<String, List<Set<String>>> starredPaths, String path) {
241         Matcher starAttributeMatcher = STAR_ATTRIBUTE_PATTERN.matcher(path);
242         StringBuilder starredPath = new StringBuilder();
243         List<String> attributes = new ArrayList<>();
244         int lastEnd = 0;
245         while (starAttributeMatcher.find()) {
246             int start = starAttributeMatcher.start(1);
247             int end = starAttributeMatcher.end(1);
248             starredPath.append(path.substring(lastEnd, start));
249             starredPath.append(".*");
250 
251             attributes.add(path.substring(start, end));
252             lastEnd = end;
253         }
254         starredPath.append(path.substring(lastEnd));
255         String starredPathString = starredPath.toString().intern();
256         starredPathOutput = starredPathString;
257 
258         List<Set<String>> attributeList = starredPaths.get(starredPathString);
259         if (attributeList == null) {
260             starredPaths.put(starredPathString, attributeList = new ArrayList<>());
261         }
262         int i = 0;
263         for (String attribute : attributes) {
264             if (attributeList.size() <= i) {
265                 TreeSet<String> subset = new TreeSet<>();
266                 subset.add(attribute);
267                 attributeList.add(subset);
268             } else {
269                 Set<String> subset = attributeList.get(i);
270                 subset.add(attribute);
271             }
272             ++i;
273         }
274         return attributes;
275     }
276 }
277