1 /* 2 * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package build.tools.cldrconverter; 27 28 import java.io.File; 29 import java.io.IOException; 30 import java.text.DateFormatSymbols; 31 import java.util.ArrayList; 32 import java.util.HashMap; 33 import java.util.HashSet; 34 import java.util.List; 35 import java.util.Locale; 36 import java.util.Map; 37 import java.util.Set; 38 import org.xml.sax.Attributes; 39 import org.xml.sax.InputSource; 40 import org.xml.sax.SAXException; 41 42 /** 43 * Handles parsing of files in Locale Data Markup Language and produces a map 44 * that uses the keys and values of JRE locale data. 45 */ 46 class LDMLParseHandler extends AbstractLDMLHandler<Object> { 47 private String defaultNumberingSystem; 48 private String currentNumberingSystem = ""; 49 private CalendarType currentCalendarType; 50 private String zoneNameStyle; // "long" or "short" for time zone names 51 private String zonePrefix; 52 private final String id; 53 private String currentContext = ""; // "format"/"stand-alone" 54 private String currentWidth = ""; // "wide"/"narrow"/"abbreviated" 55 private String currentStyle = ""; // short, long for decimalFormat 56 private String compactCount = ""; // one or other for decimalFormat 57 58 LDMLParseHandler(String id) { 59 this.id = id; 60 } 61 62 @Override 63 public InputSource resolveEntity(String publicID, String systemID) throws IOException, SAXException { 64 // avoid HTTP traffic to unicode.org 65 if (systemID.startsWith(CLDRConverter.LDML_DTD_SYSTEM_ID)) { 66 return new InputSource((new File(CLDRConverter.LOCAL_LDML_DTD)).toURI().toString()); 67 } 68 return null; 69 } 70 71 @Override 72 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 73 switch (qName) { 74 // 75 // Generic information 76 // 77 case "identity": 78 // ignore this element - it has language and territory elements that aren't locale data 79 pushIgnoredContainer(qName); 80 break; 81 82 // for LocaleNames 83 // copy string 84 case "localeSeparator": 85 pushStringEntry(qName, attributes, 86 CLDRConverter.LOCALE_SEPARATOR); 87 break; 88 case "localeKeyTypePattern": 89 pushStringEntry(qName, attributes, 90 CLDRConverter.LOCALE_KEYTYPE); 91 break; 92 93 case "language": 94 case "script": 95 case "territory": 96 case "variant": 97 // for LocaleNames 98 // copy string 99 pushStringEntry(qName, attributes, 100 CLDRConverter.LOCALE_NAME_PREFIX + 101 (qName.equals("variant") ? "%%" : "") + 102 attributes.getValue("type")); 103 break; 104 105 case "key": 106 // for LocaleNames 107 // copy string 108 { 109 String key = convertOldKeyName(attributes.getValue("type")); 110 if (key.length() == 2) { 111 pushStringEntry(qName, attributes, 112 CLDRConverter.LOCALE_KEY_PREFIX + key); 113 } else { 114 pushIgnoredContainer(qName); 115 } 116 } 117 break; 118 119 case "type": 120 // for LocaleNames/CalendarNames 121 // copy string 122 { 123 String key = convertOldKeyName(attributes.getValue("key")); 124 if (key.length() == 2) { 125 pushStringEntry(qName, attributes, 126 CLDRConverter.LOCALE_TYPE_PREFIX + key + "." + 127 attributes.getValue("type")); 128 } else { 129 pushIgnoredContainer(qName); 130 } 131 } 132 break; 133 134 // 135 // Currency information 136 // 137 case "currency": 138 // for CurrencyNames 139 // stash away "type" value for nested <symbol> 140 pushKeyContainer(qName, attributes, attributes.getValue("type")); 141 break; 142 case "symbol": 143 // for CurrencyNames 144 // need to get the key from the containing <currency> element 145 pushStringEntry(qName, attributes, CLDRConverter.CURRENCY_SYMBOL_PREFIX 146 + getContainerKey()); 147 break; 148 149 // Calendar or currency 150 case "displayName": 151 { 152 if (currentContainer.getqName().equals("field")) { 153 pushStringEntry(qName, attributes, 154 (currentCalendarType != null ? currentCalendarType.keyElementName() : "") 155 + "field." + getContainerKey()); 156 } else { 157 // for CurrencyNames 158 // need to get the key from the containing <currency> element 159 // ignore if is has "count" attribute 160 String containerKey = getContainerKey(); 161 if (containerKey != null && attributes.getValue("count") == null) { 162 pushStringEntry(qName, attributes, 163 CLDRConverter.CURRENCY_NAME_PREFIX 164 + containerKey.toLowerCase(Locale.ROOT), 165 attributes.getValue("type")); 166 } else { 167 pushIgnoredContainer(qName); 168 } 169 } 170 } 171 break; 172 173 // 174 // Calendar information 175 // 176 case "calendar": 177 { 178 // mostly for FormatData (CalendarData items firstDay and minDays are also nested) 179 // use only if it's supported by java.util.Calendar. 180 String calendarName = attributes.getValue("type"); 181 currentCalendarType = CalendarType.forName(calendarName); 182 if (currentCalendarType != null) { 183 pushContainer(qName, attributes); 184 } else { 185 pushIgnoredContainer(qName); 186 } 187 } 188 break; 189 case "fields": 190 { 191 pushContainer(qName, attributes); 192 } 193 break; 194 case "field": 195 { 196 String type = attributes.getValue("type"); 197 switch (type) { 198 case "era": 199 case "year": 200 case "month": 201 case "week": 202 case "weekday": 203 case "dayperiod": 204 case "hour": 205 case "minute": 206 case "second": 207 case "zone": 208 pushKeyContainer(qName, attributes, type); 209 break; 210 default: 211 pushIgnoredContainer(qName); 212 break; 213 } 214 } 215 break; 216 case "monthContext": 217 { 218 // for FormatData 219 // need to keep stand-alone and format, to allow for inheritance in CLDR 220 String type = attributes.getValue("type"); 221 if ("stand-alone".equals(type) || "format".equals(type)) { 222 currentContext = type; 223 pushKeyContainer(qName, attributes, type); 224 } else { 225 pushIgnoredContainer(qName); 226 } 227 } 228 break; 229 case "monthWidth": 230 { 231 // for FormatData 232 // create string array for the two types that the JRE knows 233 // keep info about the context type so we can sort out inheritance later 234 if (currentCalendarType == null) { 235 pushIgnoredContainer(qName); 236 break; 237 } 238 String prefix = (currentCalendarType == null) ? "" : currentCalendarType.keyElementName(); 239 currentWidth = attributes.getValue("type"); 240 switch (currentWidth) { 241 case "wide": 242 pushStringArrayEntry(qName, attributes, prefix + "MonthNames/" + getContainerKey(), 13); 243 break; 244 case "abbreviated": 245 pushStringArrayEntry(qName, attributes, prefix + "MonthAbbreviations/" + getContainerKey(), 13); 246 break; 247 case "narrow": 248 pushStringArrayEntry(qName, attributes, prefix + "MonthNarrows/" + getContainerKey(), 13); 249 break; 250 default: 251 pushIgnoredContainer(qName); 252 break; 253 } 254 } 255 break; 256 case "month": 257 // for FormatData 258 // add to string array entry of monthWidth element 259 pushStringArrayElement(qName, attributes, Integer.parseInt(attributes.getValue("type")) - 1); 260 break; 261 case "dayContext": 262 { 263 // for FormatData 264 // need to keep stand-alone and format, to allow for multiple inheritance in CLDR 265 String type = attributes.getValue("type"); 266 if ("stand-alone".equals(type) || "format".equals(type)) { 267 currentContext = type; 268 pushKeyContainer(qName, attributes, type); 269 } else { 270 pushIgnoredContainer(qName); 271 } 272 } 273 break; 274 case "dayWidth": 275 { 276 // for FormatData 277 // create string array for the two types that the JRE knows 278 // keep info about the context type so we can sort out inheritance later 279 String prefix = (currentCalendarType == null) ? "" : currentCalendarType.keyElementName(); 280 currentWidth = attributes.getValue("type"); 281 switch (currentWidth) { 282 case "wide": 283 pushStringArrayEntry(qName, attributes, prefix + "DayNames/" + getContainerKey(), 7); 284 break; 285 case "abbreviated": 286 pushStringArrayEntry(qName, attributes, prefix + "DayAbbreviations/" + getContainerKey(), 7); 287 break; 288 case "narrow": 289 pushStringArrayEntry(qName, attributes, prefix + "DayNarrows/" + getContainerKey(), 7); 290 break; 291 default: 292 pushIgnoredContainer(qName); 293 break; 294 } 295 } 296 break; 297 case "day": 298 // for FormatData 299 // add to string array entry of monthWidth element 300 pushStringArrayElement(qName, attributes, Integer.parseInt(DAY_OF_WEEK_MAP.get(attributes.getValue("type"))) - 1); 301 break; 302 case "dayPeriodContext": 303 // for FormatData 304 // need to keep stand-alone and format, to allow for multiple inheritance in CLDR 305 // for FormatData 306 // need to keep stand-alone and format, to allow for multiple inheritance in CLDR 307 { 308 String type = attributes.getValue("type"); 309 if ("stand-alone".equals(type) || "format".equals(type)) { 310 currentContext = type; 311 pushKeyContainer(qName, attributes, type); 312 } else { 313 pushIgnoredContainer(qName); 314 } 315 } 316 break; 317 case "dayPeriodWidth": 318 // for FormatData 319 // create string array entry for am/pm. only keeping wide 320 currentWidth = attributes.getValue("type"); 321 switch (currentWidth) { 322 case "wide": 323 pushStringArrayEntry(qName, attributes, "AmPmMarkers/" + getContainerKey(), 2); 324 break; 325 case "narrow": 326 pushStringArrayEntry(qName, attributes, "narrow.AmPmMarkers/" + getContainerKey(), 2); 327 break; 328 case "abbreviated": 329 pushStringArrayEntry(qName, attributes, "abbreviated.AmPmMarkers/" + getContainerKey(), 2); 330 break; 331 default: 332 pushIgnoredContainer(qName); 333 break; 334 } 335 break; 336 case "dayPeriod": 337 // for FormatData 338 // add to string array entry of AmPmMarkers element 339 if (attributes.getValue("alt") == null) { 340 switch (attributes.getValue("type")) { 341 case "am": 342 pushStringArrayElement(qName, attributes, 0); 343 break; 344 case "pm": 345 pushStringArrayElement(qName, attributes, 1); 346 break; 347 default: 348 pushIgnoredContainer(qName); 349 break; 350 } 351 } else { 352 // discard alt values 353 pushIgnoredContainer(qName); 354 } 355 break; 356 case "eraNames": 357 // CLDR era names are inconsistent in terms of their lengths. For example, 358 // the full names of Japanese imperial eras are eraAbbr, while the full names 359 // of the Julian eras are eraNames. 360 if (currentCalendarType == null) { 361 assert currentContainer instanceof IgnoredContainer; 362 pushIgnoredContainer(qName); 363 } else { 364 String key = currentCalendarType.keyElementName() + "long.Eras"; // for now 365 pushStringArrayEntry(qName, attributes, key, currentCalendarType.getEraLength(qName)); 366 } 367 break; 368 case "eraAbbr": 369 // for FormatData 370 // create string array entry 371 if (currentCalendarType == null) { 372 assert currentContainer instanceof IgnoredContainer; 373 pushIgnoredContainer(qName); 374 } else { 375 String key = currentCalendarType.keyElementName() + "Eras"; 376 pushStringArrayEntry(qName, attributes, key, currentCalendarType.getEraLength(qName)); 377 } 378 break; 379 case "eraNarrow": 380 // mainly used for the Japanese imperial calendar 381 if (currentCalendarType == null) { 382 assert currentContainer instanceof IgnoredContainer; 383 pushIgnoredContainer(qName); 384 } else { 385 String key = currentCalendarType.keyElementName() + "narrow.Eras"; 386 pushStringArrayEntry(qName, attributes, key, currentCalendarType.getEraLength(qName)); 387 } 388 break; 389 case "era": 390 // for FormatData 391 // add to string array entry of eraAbbr element 392 if (currentCalendarType == null) { 393 assert currentContainer instanceof IgnoredContainer; 394 pushIgnoredContainer(qName); 395 } else { 396 int index = Integer.parseInt(attributes.getValue("type")); 397 index = currentCalendarType.normalizeEraIndex(index); 398 if (index >= 0) { 399 pushStringArrayElement(qName, attributes, index); 400 } else { 401 pushIgnoredContainer(qName); 402 } 403 if (currentContainer.getParent() == null) { 404 throw new InternalError("currentContainer: null parent"); 405 } 406 } 407 break; 408 case "quarterContext": 409 { 410 // for FormatData 411 // need to keep stand-alone and format, to allow for inheritance in CLDR 412 String type = attributes.getValue("type"); 413 if ("stand-alone".equals(type) || "format".equals(type)) { 414 currentContext = type; 415 pushKeyContainer(qName, attributes, type); 416 } else { 417 pushIgnoredContainer(qName); 418 } 419 } 420 break; 421 case "quarterWidth": 422 { 423 // for FormatData 424 // keep info about the context type so we can sort out inheritance later 425 String prefix = (currentCalendarType == null) ? "" : currentCalendarType.keyElementName(); 426 currentWidth = attributes.getValue("type"); 427 switch (currentWidth) { 428 case "wide": 429 pushStringArrayEntry(qName, attributes, prefix + "QuarterNames/" + getContainerKey(), 4); 430 break; 431 case "abbreviated": 432 pushStringArrayEntry(qName, attributes, prefix + "QuarterAbbreviations/" + getContainerKey(), 4); 433 break; 434 case "narrow": 435 pushStringArrayEntry(qName, attributes, prefix + "QuarterNarrows/" + getContainerKey(), 4); 436 break; 437 default: 438 pushIgnoredContainer(qName); 439 break; 440 } 441 } 442 break; 443 case "quarter": 444 // for FormatData 445 // add to string array entry of quarterWidth element 446 pushStringArrayElement(qName, attributes, Integer.parseInt(attributes.getValue("type")) - 1); 447 break; 448 449 // 450 // Time zone names 451 // 452 case "timeZoneNames": 453 pushContainer(qName, attributes); 454 break; 455 case "hourFormat": 456 pushStringEntry(qName, attributes, "timezone.hourFormat"); 457 break; 458 case "gmtFormat": 459 pushStringEntry(qName, attributes, "timezone.gmtFormat"); 460 break; 461 case "gmtZeroFormat": 462 pushStringEntry(qName, attributes, "timezone.gmtZeroFormat"); 463 break; 464 case "regionFormat": 465 { 466 String type = attributes.getValue("type"); 467 pushStringEntry(qName, attributes, "timezone.regionFormat" + 468 (type == null ? "" : "." + type)); 469 } 470 break; 471 case "zone": 472 { 473 String tzid = attributes.getValue("type"); // Olson tz id 474 zonePrefix = CLDRConverter.TIMEZONE_ID_PREFIX; 475 put(zonePrefix + tzid, new HashMap<String, String>()); 476 pushKeyContainer(qName, attributes, tzid); 477 } 478 break; 479 case "metazone": 480 { 481 String zone = attributes.getValue("type"); // LDML meta zone id 482 zonePrefix = CLDRConverter.METAZONE_ID_PREFIX; 483 put(zonePrefix + zone, new HashMap<String, String>()); 484 pushKeyContainer(qName, attributes, zone); 485 } 486 break; 487 case "long": 488 zoneNameStyle = "long"; 489 pushContainer(qName, attributes); 490 break; 491 case "short": 492 zoneNameStyle = "short"; 493 pushContainer(qName, attributes); 494 break; 495 case "generic": // generic name 496 case "standard": // standard time name 497 case "daylight": // daylight saving (summer) time name 498 pushStringEntry(qName, attributes, CLDRConverter.ZONE_NAME_PREFIX + qName + "." + zoneNameStyle); 499 break; 500 case "exemplarCity": 501 pushStringEntry(qName, attributes, CLDRConverter.EXEMPLAR_CITY_PREFIX); 502 break; 503 504 // 505 // Number format information 506 // 507 case "decimalFormatLength": 508 String type = attributes.getValue("type"); 509 if (null == type) { 510 // format data for decimal number format 511 pushStringEntry(qName, attributes, "NumberPatterns/decimal"); 512 currentStyle = type; 513 } else { 514 switch (type) { 515 case "short": 516 case "long": 517 // considering "short" and long for 518 // compact number formatting patterns 519 pushKeyContainer(qName, attributes, type); 520 currentStyle = type; 521 break; 522 default: 523 pushIgnoredContainer(qName); 524 break; 525 } 526 } 527 break; 528 case "decimalFormat": 529 if(currentStyle == null) { 530 pushContainer(qName, attributes); 531 } else { 532 switch (currentStyle) { 533 case "short": 534 pushStringListEntry(qName, attributes, 535 currentStyle+".CompactNumberPatterns"); 536 break; 537 case "long": 538 pushStringListEntry(qName, attributes, 539 currentStyle+".CompactNumberPatterns"); 540 break; 541 default: 542 pushIgnoredContainer(qName); 543 break; 544 } 545 } 546 break; 547 case "pattern": 548 String containerName = currentContainer.getqName(); 549 if (containerName.equals("decimalFormat")) { 550 if (currentStyle == null) { 551 pushContainer(qName, attributes); 552 } else { 553 // The compact number patterns parsing assumes that the order 554 // of patterns are always in the increasing order of their 555 // type attribute i.e. type = 1000... 556 // Between the inflectional forms for a type (e.g. 557 // count = "one" and count = "other" for type = 1000), it is 558 // assumed that the count = "one" always appears before 559 // count = "other" 560 switch (currentStyle) { 561 case "short": 562 case "long": 563 String count = attributes.getValue("count"); 564 // first pattern of count = "one" or count = "other" 565 if ((count.equals("one") || count.equals("other")) 566 && compactCount.equals("")) { 567 compactCount = count; 568 pushStringListElement(qName, attributes, 569 (int) Math.log10(Double.parseDouble(attributes.getValue("type")))); 570 } else if ((count.equals("one") || count.equals("other")) 571 && compactCount.equals(count)) { 572 // extract patterns with similar "count" 573 // attribute value 574 pushStringListElement(qName, attributes, 575 (int) Math.log10(Double.parseDouble(attributes.getValue("type")))); 576 } else { 577 pushIgnoredContainer(qName); 578 } 579 break; 580 default: 581 pushIgnoredContainer(qName); 582 break; 583 } 584 } 585 } else { 586 pushContainer(qName, attributes); 587 } 588 break; 589 case "currencyFormatLength": 590 if (attributes.getValue("type") == null) { 591 // skipping type="short" data 592 // for FormatData 593 pushContainer(qName, attributes); 594 } else { 595 pushIgnoredContainer(qName); 596 } 597 break; 598 case "currencyFormat": 599 { 600 // for FormatData 601 // copy string for later assembly into NumberPatterns 602 String cfStyle = attributes.getValue("type"); 603 if (cfStyle.equals("standard")) { 604 pushStringEntry(qName, attributes, "NumberPatterns/currency"); 605 } else if (cfStyle.equals("accounting")) { 606 pushStringEntry(qName, attributes, "NumberPatterns/accounting"); 607 } else { 608 pushIgnoredContainer(qName); 609 } 610 } 611 break; 612 case "percentFormat": 613 // for FormatData 614 // copy string for later assembly into NumberPatterns 615 if (attributes.getValue("type").equals("standard")) { 616 pushStringEntry(qName, attributes, "NumberPatterns/percent"); 617 } else { 618 pushIgnoredContainer(qName); 619 } 620 break; 621 case "defaultNumberingSystem": 622 // default numbering system if multiple numbering systems are used. 623 pushStringEntry(qName, attributes, "DefaultNumberingSystem"); 624 break; 625 case "symbols": 626 // for FormatData 627 // look up numberingSystems 628 symbols: { 629 String script = attributes.getValue("numberSystem"); 630 if (script == null) { 631 // Has no script. Just ignore. 632 pushIgnoredContainer(qName); 633 break; 634 } 635 636 // Use keys as <script>."NumberElements/<symbol>" 637 currentNumberingSystem = script + "."; 638 String digits = CLDRConverter.handlerNumbering.get(script); 639 if (digits == null) { 640 pushIgnoredContainer(qName); 641 break; 642 } 643 644 @SuppressWarnings("unchecked") 645 List<String> numberingScripts = (List<String>) get("numberingScripts"); 646 if (numberingScripts == null) { 647 numberingScripts = new ArrayList<>(); 648 put("numberingScripts", numberingScripts); 649 } 650 numberingScripts.add(script); 651 put(currentNumberingSystem + "NumberElements/zero", digits.substring(0, 1)); 652 pushContainer(qName, attributes); 653 } 654 break; 655 case "decimal": 656 // for FormatData 657 // copy string for later assembly into NumberElements 658 if (currentContainer.getqName().equals("symbols")) { 659 pushStringEntry(qName, attributes, currentNumberingSystem + "NumberElements/decimal"); 660 } else { 661 pushIgnoredContainer(qName); 662 } 663 break; 664 case "group": 665 // for FormatData 666 // copy string for later assembly into NumberElements 667 if (currentContainer.getqName().equals("symbols")) { 668 pushStringEntry(qName, attributes, currentNumberingSystem + "NumberElements/group"); 669 } else { 670 pushIgnoredContainer(qName); 671 } 672 break; 673 case "list": 674 // for FormatData 675 // copy string for later assembly into NumberElements 676 pushStringEntry(qName, attributes, currentNumberingSystem + "NumberElements/list"); 677 break; 678 case "percentSign": 679 // for FormatData 680 // copy string for later assembly into NumberElements 681 pushStringEntry(qName, attributes, currentNumberingSystem + "NumberElements/percent"); 682 break; 683 case "nativeZeroDigit": 684 // for FormatData 685 // copy string for later assembly into NumberElements 686 pushStringEntry(qName, attributes, currentNumberingSystem + "NumberElements/zero"); 687 break; 688 case "patternDigit": 689 // for FormatData 690 // copy string for later assembly into NumberElements 691 pushStringEntry(qName, attributes, currentNumberingSystem + "NumberElements/pattern"); 692 break; 693 case "plusSign": 694 // TODO: DecimalFormatSymbols doesn't support plusSign 695 pushIgnoredContainer(qName); 696 break; 697 case "minusSign": 698 // for FormatData 699 // copy string for later assembly into NumberElements 700 pushStringEntry(qName, attributes, currentNumberingSystem + "NumberElements/minus"); 701 break; 702 case "exponential": 703 // for FormatData 704 // copy string for later assembly into NumberElements 705 pushStringEntry(qName, attributes, currentNumberingSystem + "NumberElements/exponential"); 706 break; 707 case "perMille": 708 // for FormatData 709 // copy string for later assembly into NumberElements 710 pushStringEntry(qName, attributes, currentNumberingSystem + "NumberElements/permille"); 711 break; 712 case "infinity": 713 // for FormatData 714 // copy string for later assembly into NumberElements 715 pushStringEntry(qName, attributes, currentNumberingSystem + "NumberElements/infinity"); 716 break; 717 case "nan": 718 // for FormatData 719 // copy string for later assembly into NumberElements 720 pushStringEntry(qName, attributes, currentNumberingSystem + "NumberElements/nan"); 721 break; 722 case "timeFormatLength": 723 { 724 // for FormatData 725 // copy string for later assembly into DateTimePatterns 726 String prefix = (currentCalendarType == null) ? "" : currentCalendarType.keyElementName(); 727 pushStringEntry(qName, attributes, prefix + "DateTimePatterns/" + attributes.getValue("type") + "-time"); 728 } 729 break; 730 case "dateFormatLength": 731 { 732 // for FormatData 733 // copy string for later assembly into DateTimePatterns 734 String prefix = (currentCalendarType == null) ? "" : currentCalendarType.keyElementName(); 735 pushStringEntry(qName, attributes, prefix + "DateTimePatterns/" + attributes.getValue("type") + "-date"); 736 } 737 break; 738 case "dateTimeFormatLength": 739 { 740 // for FormatData 741 // copy string for later assembly into DateTimePatterns 742 String prefix = (currentCalendarType == null) ? "" : currentCalendarType.keyElementName(); 743 pushStringEntry(qName, attributes, prefix + "DateTimePatterns/" + attributes.getValue("type") + "-dateTime"); 744 } 745 break; 746 case "localizedPatternChars": 747 { 748 // for FormatData 749 // copy string for later adaptation to JRE use 750 String prefix = (currentCalendarType == null) ? "" : currentCalendarType.keyElementName(); 751 pushStringEntry(qName, attributes, prefix + "DateTimePatternChars"); 752 } 753 break; 754 755 // "alias" for root 756 case "alias": 757 { 758 if (id.equals("root") && !isIgnored(attributes) 759 && ((currentContainer.getqName().equals("decimalFormatLength")) 760 || (currentCalendarType != null && !currentCalendarType.lname().startsWith("islamic-")))) { // ignore islamic variants 761 pushAliasEntry(qName, attributes, attributes.getValue("path")); 762 } else { 763 pushIgnoredContainer(qName); 764 } 765 } 766 break; 767 768 default: 769 // treat anything else as a container 770 pushContainer(qName, attributes); 771 break; 772 } 773 } 774 775 private static final String[] CONTEXTS = {"stand-alone", "format"}; 776 private static final String[] WIDTHS = {"wide", "narrow", "abbreviated"}; 777 private static final String[] LENGTHS = {"full", "long", "medium", "short"}; 778 779 private void populateWidthAlias(String type, Set<String> keys) { 780 for (String context : CONTEXTS) { 781 for (String width : WIDTHS) { 782 String keyName = toJDKKey(type+"Width", context, width); 783 if (keyName.length() > 0) { 784 keys.add(keyName + "," + context + "," + width); 785 } 786 } 787 } 788 } 789 790 private void populateFormatLengthAlias(String type, Set<String> keys) { 791 for (String length: LENGTHS) { 792 String keyName = toJDKKey(type+"FormatLength", currentContext, length); 793 if (keyName.length() > 0) { 794 keys.add(keyName + "," + currentContext + "," + length); 795 } 796 } 797 } 798 799 private Set<String> populateAliasKeys(String qName, String context, String width) { 800 HashSet<String> ret = new HashSet<>(); 801 String keyName = qName; 802 803 switch (qName) { 804 case "monthWidth": 805 case "dayWidth": 806 case "quarterWidth": 807 case "dayPeriodWidth": 808 case "dateFormatLength": 809 case "timeFormatLength": 810 case "dateTimeFormatLength": 811 case "eraNames": 812 case "eraAbbr": 813 case "eraNarrow": 814 ret.add(toJDKKey(qName, context, width) + "," + context + "," + width); 815 break; 816 case "days": 817 populateWidthAlias("day", ret); 818 break; 819 case "months": 820 populateWidthAlias("month", ret); 821 break; 822 case "quarters": 823 populateWidthAlias("quarter", ret); 824 break; 825 case "dayPeriods": 826 populateWidthAlias("dayPeriod", ret); 827 break; 828 case "eras": 829 ret.add(toJDKKey("eraNames", context, width) + "," + context + "," + width); 830 ret.add(toJDKKey("eraAbbr", context, width) + "," + context + "," + width); 831 ret.add(toJDKKey("eraNarrow", context, width) + "," + context + "," + width); 832 break; 833 case "dateFormats": 834 populateFormatLengthAlias("date", ret); 835 break; 836 case "timeFormats": 837 populateFormatLengthAlias("time", ret); 838 break; 839 default: 840 break; 841 } 842 return ret; 843 } 844 845 private String translateWidthAlias(String qName, String context, String width) { 846 String keyName = qName; 847 String type = Character.toUpperCase(qName.charAt(0)) + qName.substring(1, qName.indexOf("Width")); 848 849 switch (width) { 850 case "wide": 851 keyName = type + "Names/" + context; 852 break; 853 case "abbreviated": 854 keyName = type + "Abbreviations/" + context; 855 break; 856 case "narrow": 857 keyName = type + "Narrows/" + context; 858 break; 859 default: 860 assert false; 861 } 862 863 return keyName; 864 } 865 866 private String toJDKKey(String containerqName, String context, String type) { 867 String keyName = containerqName; 868 869 switch (containerqName) { 870 case "monthWidth": 871 case "dayWidth": 872 case "quarterWidth": 873 keyName = translateWidthAlias(keyName, context, type); 874 break; 875 case "dayPeriodWidth": 876 switch (type) { 877 case "wide": 878 keyName = "AmPmMarkers/" + context; 879 break; 880 case "narrow": 881 keyName = "narrow.AmPmMarkers/" + context; 882 break; 883 case "abbreviated": 884 keyName = "abbreviated.AmPmMarkers/" + context; 885 break; 886 } 887 break; 888 case "dateFormatLength": 889 case "timeFormatLength": 890 case "dateTimeFormatLength": 891 keyName = "DateTimePatterns/" + 892 type + "-" + 893 keyName.substring(0, keyName.indexOf("FormatLength")); 894 break; 895 case "eraNames": 896 keyName = "long.Eras"; 897 break; 898 case "eraAbbr": 899 keyName = "Eras"; 900 break; 901 case "eraNarrow": 902 keyName = "narrow.Eras"; 903 break; 904 case "dateFormats": 905 case "timeFormats": 906 case "days": 907 case "months": 908 case "quarters": 909 case "dayPeriods": 910 case "eras": 911 break; 912 case "decimalFormatLength": // used for compact number formatting patterns 913 keyName = type + ".CompactNumberPatterns"; 914 break; 915 default: 916 keyName = ""; 917 break; 918 } 919 920 return keyName; 921 } 922 923 private String getTarget(String path, String calType, String context, String width) { 924 // Target qName 925 int lastSlash = path.lastIndexOf('/'); 926 String qName = path.substring(lastSlash+1); 927 int bracket = qName.indexOf('['); 928 if (bracket != -1) { 929 qName = qName.substring(0, bracket); 930 } 931 932 // calType 933 String typeKey = "/calendar[@type='"; 934 int start = path.indexOf(typeKey); 935 if (start != -1) { 936 calType = path.substring(start+typeKey.length(), path.indexOf("']", start)); 937 } 938 939 // context 940 typeKey = "Context[@type='"; 941 start = path.indexOf(typeKey); 942 if (start != -1) { 943 context = (path.substring(start+typeKey.length(), path.indexOf("']", start))); 944 } 945 946 // width 947 typeKey = "Width[@type='"; 948 start = path.indexOf(typeKey); 949 if (start != -1) { 950 width = path.substring(start+typeKey.length(), path.indexOf("']", start)); 951 } 952 953 // used for compact number formatting patterns aliases 954 typeKey = "decimalFormatLength[@type='"; 955 start = path.indexOf(typeKey); 956 if (start != -1) { 957 String style = path.substring(start + typeKey.length(), path.indexOf("']", start)); 958 return toJDKKey(qName, "", style); 959 } 960 961 return calType + "." + toJDKKey(qName, context, width); 962 } 963 964 @Override 965 public void endElement(String uri, String localName, String qName) throws SAXException { 966 assert qName.equals(currentContainer.getqName()) : "current=" + currentContainer.getqName() + ", param=" + qName; 967 switch (qName) { 968 case "calendar": 969 assert !(currentContainer instanceof Entry); 970 currentCalendarType = null; 971 break; 972 973 case "defaultNumberingSystem": 974 if (currentContainer instanceof StringEntry) { 975 defaultNumberingSystem = ((StringEntry) currentContainer).getValue(); 976 assert defaultNumberingSystem != null; 977 put(((StringEntry) currentContainer).getKey(), defaultNumberingSystem); 978 } else { 979 defaultNumberingSystem = null; 980 } 981 break; 982 983 case "timeZoneNames": 984 zonePrefix = null; 985 break; 986 987 case "generic": 988 case "standard": 989 case "daylight": 990 case "exemplarCity": 991 if (zonePrefix != null && (currentContainer instanceof Entry)) { 992 @SuppressWarnings("unchecked") 993 Map<String, String> valmap = (Map<String, String>) get(zonePrefix + getContainerKey()); 994 Entry<?> entry = (Entry<?>) currentContainer; 995 if (qName.equals("exemplarCity")) { 996 put(CLDRConverter.EXEMPLAR_CITY_PREFIX + getContainerKey(), (String) entry.getValue()); 997 } else { 998 valmap.put(entry.getKey(), (String) entry.getValue()); 999 } 1000 } 1001 break; 1002 1003 case "monthWidth": 1004 case "dayWidth": 1005 case "dayPeriodWidth": 1006 case "quarterWidth": 1007 currentWidth = ""; 1008 putIfEntry(); 1009 break; 1010 1011 case "monthContext": 1012 case "dayContext": 1013 case "dayPeriodContext": 1014 case "quarterContext": 1015 currentContext = ""; 1016 putIfEntry(); 1017 break; 1018 case "decimalFormatLength": 1019 currentStyle = ""; 1020 compactCount = ""; 1021 putIfEntry(); 1022 break; 1023 default: 1024 putIfEntry(); 1025 } 1026 currentContainer = currentContainer.getParent(); 1027 } 1028 1029 private void putIfEntry() { 1030 if (currentContainer instanceof AliasEntry) { 1031 Entry<?> entry = (Entry<?>) currentContainer; 1032 String containerqName = entry.getParent().getqName(); 1033 if (containerqName.equals("decimalFormatLength")) { 1034 String srcKey = toJDKKey(containerqName, "", currentStyle); 1035 String targetKey = getTarget(entry.getKey(), "", "", ""); 1036 CLDRConverter.aliases.put(srcKey, targetKey); 1037 } else { 1038 Set<String> keyNames = populateAliasKeys(containerqName, currentContext, currentWidth); 1039 if (!keyNames.isEmpty()) { 1040 for (String keyName : keyNames) { 1041 String[] tmp = keyName.split(",", 3); 1042 String calType = currentCalendarType.lname(); 1043 String src = calType+"."+tmp[0]; 1044 String target = getTarget( 1045 entry.getKey(), 1046 calType, 1047 tmp[1].length()>0 ? tmp[1] : currentContext, 1048 tmp[2].length()>0 ? tmp[2] : currentWidth); 1049 if (target.substring(target.lastIndexOf('.')+1).equals(containerqName)) { 1050 target = target.substring(0, target.indexOf('.'))+"."+tmp[0]; 1051 } 1052 CLDRConverter.aliases.put(src.replaceFirst("^gregorian.", ""), 1053 target.replaceFirst("^gregorian.", "")); 1054 } 1055 } 1056 } 1057 } else if (currentContainer instanceof Entry) { 1058 Entry<?> entry = (Entry<?>) currentContainer; 1059 Object value = entry.getValue(); 1060 if (value != null) { 1061 String key = entry.getKey(); 1062 // Tweak for MonthNames for the root locale, Needed for 1063 // SimpleDateFormat.format()/parse() roundtrip. 1064 if (id.equals("root") && key.startsWith("MonthNames")) { 1065 value = new DateFormatSymbols(Locale.US).getShortMonths(); 1066 } 1067 put(entry.getKey(), value); 1068 } 1069 } 1070 } 1071 1072 public String convertOldKeyName(String key) { 1073 // Explicitly obtained from "alias" attribute in each "key" element. 1074 switch (key) { 1075 case "calendar": 1076 return "ca"; 1077 case "currency": 1078 return "cu"; 1079 case "collation": 1080 return "co"; 1081 case "numbers": 1082 return "nu"; 1083 case "timezone": 1084 return "tz"; 1085 default: 1086 return key; 1087 } 1088 } 1089 }