• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.unicode.cldr.util;
2 
3 import java.io.BufferedReader;
4 
5 import org.unicode.cldr.util.CldrUtility.VariableReplacer;
6 
7 /**
8  * Parses a file of regexes for use in RegexLookup or any other class that requires
9  * a list of regexes.
10  *
11  * @author jchye
12  */
13 public class RegexFileParser {
14     private RegexLineParser lineParser;
15     private VariableProcessor varProcessor = DEFAULT_VARIABLE_PROCESSOR;
16 
17     /**
18      * Parses a line given to it by the RegexFileParser.
19      */
20     public interface RegexLineParser {
parse(String line)21         public void parse(String line);
22     }
23 
24     /**
25      * Stores variables given to it by the RegexFileParser and performs replacements
26      * if needed.
27      */
28     public interface VariableProcessor {
add(String variable, String variableName)29         public void add(String variable, String variableName);
30 
replace(String str)31         public String replace(String str);
32     }
33 
34     private static final VariableProcessor DEFAULT_VARIABLE_PROCESSOR = new VariableProcessor() {
35         VariableReplacer variables = new VariableReplacer();
36 
37         @Override
38         public void add(String variableName, String value) {
39             variables.add(variableName, value);
40         }
41 
42         @Override
43         public String replace(String str) {
44             return variables.replace(str);
45         }
46     };
47 
setLineParser(RegexLineParser lineParser)48     public void setLineParser(RegexLineParser lineParser) {
49         this.lineParser = lineParser;
50     }
51 
setVariableProcessor(VariableProcessor varProcessor)52     public void setVariableProcessor(VariableProcessor varProcessor) {
53         this.varProcessor = varProcessor;
54     }
55 
56     /**
57      * Parses the specified text file.
58      *
59      * @param a
60      *            class relative to filename
61      * @param filename
62      *            the name of the text file to be parsed
63      */
parse(Class<?> baseClass, String filename)64     public void parse(Class<?> baseClass, String filename) {
65         BufferedReader reader = FileReaders.openFile(baseClass, filename);
66         String line = null;
67         int lineNumber = 0;
68         try {
69             while ((line = reader.readLine()) != null) {
70                 lineNumber++;
71                 line = line.trim();
72                 // Skip comments.
73                 if (line.length() == 0 || line.startsWith("#")) continue;
74                 // Read variables.
75                 if (line.charAt(0) == '%') {
76                     int pos = line.indexOf("=");
77                     if (pos < 0) {
78                         throw new IllegalArgumentException("Failed to read variable in " + filename + "\t\t("
79                             + lineNumber + ") " + line);
80                     }
81                     String varName = line.substring(0, pos).trim();
82                     String varValue = line.substring(pos + 1).trim();
83                     varProcessor.add(varName, varValue);
84                     continue;
85                 }
86                 if (line.contains("%")) {
87                     line = varProcessor.replace(line);
88                 }
89                 // Process a line in the input file for xpath conversion.
90                 lineParser.parse(line);
91             }
92         } catch (Exception e) {
93             System.err.println("Error reading " + filename + " at line " + lineNumber + ": " + line);
94             e.printStackTrace();
95         }
96     }
97 }