1 package org.unicode.cldr.icu; 2 3 import java.io.File; 4 5 import org.xml.sax.Attributes; 6 import org.xml.sax.SAXException; 7 8 /** 9 * Class for converting CLDR dayPeriods data into a format suitable for writing 10 * to ICU data. The regex-mapping method can't be used here because the special 11 * handling of sets. 12 * 13 * @author jchye 14 */ 15 public class DayPeriodsMapper { 16 private String supplementalDir; 17 DayPeriodsMapper(String supplementalDir)18 public DayPeriodsMapper(String supplementalDir) { 19 this.supplementalDir = supplementalDir; 20 } 21 22 /** 23 * @return CLDR data converted to an ICU-friendly format 24 */ fillFromCldr()25 public IcuData fillFromCldr() { 26 IcuData icuData = new IcuData("dayPeriods.xml", "dayPeriods", false); 27 DayPeriodsHandler handler = new DayPeriodsHandler(icuData); 28 File inputFile = new File(supplementalDir, "dayPeriods.xml"); 29 MapperUtils.parseFile(inputFile, handler); 30 return icuData; 31 } 32 33 private class DayPeriodsHandler extends MapperUtils.EmptyHandler { 34 private IcuData icuData; 35 private int setNum; 36 private String selection; 37 DayPeriodsHandler(IcuData icuData)38 public DayPeriodsHandler(IcuData icuData) { 39 this.icuData = icuData; 40 setNum = 0; 41 } 42 43 @Override startElement(String uri, String localName, String qName, Attributes attr)44 public void startElement(String uri, String localName, String qName, Attributes attr) throws SAXException { 45 // <dayPeriodRuleSet type="selection"> 46 if (qName.equals("dayPeriodRuleSet")) { 47 selection = attr.getValue("type"); 48 if (selection == null) { 49 selection = ""; 50 } else { 51 selection = "_" + selection; 52 } 53 } else if (qName.equals("dayPeriodRules")) { 54 setNum++; 55 String[] locales = attr.getValue("locales").split("\\s+"); 56 for (String locale : locales) { 57 icuData.add("/locales" + selection + "/" + locale, "set" + setNum); 58 } 59 } else if (qName.equals("dayPeriodRule")) { 60 String type = attr.getValue("type"); 61 String prefix = "/rules/set" + setNum + "/" + type + "/"; 62 // Possible attribute names: type, before, after, at. 63 for (int i = 0; i < attr.getLength(); i++) { 64 String attrName = attr.getLocalName(i); 65 if (!attrName.equals("type")) { 66 icuData.add(prefix + attrName, attr.getValue(i)); 67 } 68 } 69 } 70 } 71 } 72 } 73