• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.unicode.cldr.icu;
2 
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.ArrayList;
7 import java.util.Collection;
8 import java.util.List;
9 import java.util.regex.Matcher;
10 import java.util.regex.Pattern;
11 
12 import org.unicode.cldr.util.InputStreamFactory;
13 import org.unicode.cldr.util.PatternCache;
14 import org.unicode.cldr.util.RegexUtilities;
15 import org.unicode.cldr.util.XMLFileReader;
16 import org.xml.sax.ContentHandler;
17 import org.xml.sax.InputSource;
18 import org.xml.sax.Locator;
19 import org.xml.sax.SAXException;
20 import org.xml.sax.XMLReader;
21 
22 /**
23  * Utility class for NewLdml2IcuConverter mappers.
24  *
25  * @author jchye
26  */
27 public class MapperUtils {
28     private static final Pattern VERSION_PATTERN = PatternCache.get("\\$Revision:\\s*([\\d.]+)\\s*\\$");
29 
30     /**
31      * Parses an XML file.
32      *
33      * @param inputFile
34      *            the file to be parsed
35      * @param handler
36      *            the XML parser to be used
37      */
parseFile(File inputFile, ContentHandler handler)38     public static void parseFile(File inputFile, ContentHandler handler) {
39         XMLReader xmlReader = XMLFileReader.createXMLReader(true);
40         xmlReader.setContentHandler(handler);
41         if (inputFile == null) {
42             System.err.println("Please call with non-null input file");
43             return;
44         }
45         try (InputStream fis = InputStreamFactory.createInputStream(inputFile)) {
46 //            FileInputStream fis = new FileInputStream(inputFile);
47             InputSource is = new InputSource(fis);
48             // Set the system ID so the parser knows where to find the dtd.
49             is.setSystemId(inputFile.toString());
50             xmlReader.parse(is);
51 //            fis.close();
52         } catch (IOException | SAXException e) {
53             System.err.println("Error loading " + inputFile.getAbsolutePath());
54             e.printStackTrace();
55         }
56     }
57 
formatVersion(String value)58     public static String formatVersion(String value) {
59         Matcher versionMatcher = VERSION_PATTERN.matcher(value);
60         int versionNum;
61         if (!versionMatcher.find()) {
62             int failPoint = RegexUtilities.findMismatch(versionMatcher, value);
63             String show = value.substring(0, failPoint) + "☹" + value.substring(failPoint);
64             System.err.println("Warning: no version match with: " + show);
65             versionNum = 0;
66         } else {
67             String rawVersion = versionMatcher.group(1);
68             // No further processing needed, e.g. "1.1"
69             if (rawVersion.contains(".")) {
70                 return rawVersion;
71             }
72             versionNum = Integer.parseInt(rawVersion);
73         }
74         String version = "";
75         int numDots = 0;
76         while (versionNum > 0) {
77             version = "." + versionNum % 100 + version;
78             versionNum /= 100;
79             numDots++;
80         }
81         return (numDots > 2 ? "2" : "2.0") + version;
82     }
83 
84     /**
85      * Empty convenience superclass for all XML parsers used in mappers.
86      *
87      * @author jchye
88      *
89      */
90     public static abstract class EmptyHandler implements ContentHandler {
91         @Override
characters(char[] ch, int start, int length)92         public void characters(char[] ch, int start, int length) throws SAXException {
93         }
94 
95         @Override
endElement(String uri, String localName, String qName)96         public void endElement(String uri, String localName, String qName) throws SAXException {
97         }
98 
99         @Override
startPrefixMapping(String arg0, String arg1)100         public void startPrefixMapping(String arg0, String arg1) throws SAXException {
101         }
102 
103         @Override
endPrefixMapping(String arg0)104         public void endPrefixMapping(String arg0) throws SAXException {
105         }
106 
107         @Override
ignorableWhitespace(char[] arg0, int arg1, int arg2)108         public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException {
109         }
110 
111         @Override
processingInstruction(String arg0, String arg1)112         public void processingInstruction(String arg0, String arg1) throws SAXException {
113         }
114 
115         @Override
setDocumentLocator(Locator arg0)116         public void setDocumentLocator(Locator arg0) {
117         }
118 
119         @Override
skippedEntity(String arg0)120         public void skippedEntity(String arg0) throws SAXException {
121         }
122 
123         @Override
startDocument()124         public void startDocument() throws SAXException {
125         }
126 
127         @Override
endDocument()128         public void endDocument() throws SAXException {
129         }
130     }
131 
toArray(Collection<IcuData> collection)132     public static IcuData[] toArray(Collection<IcuData> collection) {
133         IcuData[] dataList = new IcuData[collection.size()];
134         return collection.toArray(dataList);
135     }
136 
137     /**
138      * Returns a list of names of XML files in the specified directory.
139      * @param sourceDir
140      * @return
141      */
getNames(String sourceDir)142     public static List<String> getNames(String sourceDir) {
143         return getNames(new File(sourceDir));
144     }
145 
146     /**
147      * Returns a list of names of XML files in the specified directory.
148      * @param sourceDir
149      * @return
150      */
getNames(File sourceDir)151     public static List<String> getNames(File sourceDir) {
152         List<String> locales = new ArrayList<String>();
153         for (String filename : sourceDir.list()) {
154             if (!filename.endsWith(".xml")) continue;
155             String locale = filename.substring(0, filename.length() - 4);
156             locales.add(locale);
157         }
158         return locales;
159     }
160 }
161