1 /* 2 * Copyright (c) 2003, 2012, 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.generatebreakiteratordata; 27 28 import java.util.Enumeration; 29 import java.util.ListResourceBundle; 30 import java.util.Locale; 31 import java.util.ResourceBundle; 32 33 /** 34 * Generates datafile for BreakIterator. 35 */ 36 public class GenerateBreakIteratorData { 37 38 /** 39 * Directory where generated data files are put in. 40 */ 41 private static String outputDir = "" ; 42 43 /** 44 * Unicode data file 45 */ 46 private static String unicodeData = "UnicodeData.txt"; 47 48 /** 49 * Rules file 50 */ 51 private static String rules = "sun.text.resources.BreakIteratorRules"; 52 53 /** 54 * Locale data 55 */ 56 private static String language = ""; 57 private static String country = ""; 58 private static String valiant = ""; 59 private static String localeName = ""; /* _language_country_valiant */ 60 61 62 public static void main(String[] args) { 63 /* Parse command-line options */ 64 processArgs(args); 65 66 /* Make categoryMap from Unicode data */ 67 CharacterCategory.makeCategoryMap(unicodeData); 68 69 /* Generate files */ 70 generateFiles(); 71 } 72 73 /** 74 * Generate data files whose names are included in 75 * sun.text.resources.BreakIteratorInfo+<localeName> 76 */ 77 private static void generateFiles() { 78 String[] classNames; 79 ResourceBundle rules, info; 80 81 info = ResourceBundle.getBundle("sun.text.resources.BreakIteratorInfo", 82 new Locale(language, country, valiant), 83 BreakIteratorRBControl.INSTANCE); 84 classNames = info.getStringArray("BreakIteratorClasses"); 85 86 rules = ResourceBundle.getBundle("sun.text.resources.BreakIteratorRules", 87 new Locale(language, country, valiant), 88 BreakIteratorRBControl.INSTANCE); 89 90 if (info.containsKey("CharacterData")) { 91 generateDataFile(info.getString("CharacterData"), 92 rules.getString("CharacterBreakRules"), 93 classNames[0]); 94 } 95 if (info.containsKey("WordData")) { 96 generateDataFile(info.getString("WordData"), 97 rules.getString("WordBreakRules"), 98 classNames[1]); 99 } 100 if (info.containsKey("LineData")) { 101 generateDataFile(info.getString("LineData"), 102 rules.getString("LineBreakRules"), 103 classNames[2]); 104 } 105 if (info.containsKey("SentenceData")) { 106 generateDataFile(info.getString("SentenceData"), 107 rules.getString("SentenceBreakRules"), 108 classNames[3]); 109 } 110 } 111 112 /** 113 * Generate a data file for break-iterator 114 */ 115 private static void generateDataFile(String datafile, String rule, String builder) { 116 RuleBasedBreakIteratorBuilder bld; 117 if (builder.equals("RuleBasedBreakIterator")) { 118 bld = new RuleBasedBreakIteratorBuilder(rule); 119 } else if (builder.equals("DictionaryBasedBreakIterator")) { 120 bld = new DictionaryBasedBreakIteratorBuilder(rule); 121 } else { 122 throw new IllegalArgumentException("Invalid break iterator class \"" + builder + "\""); 123 } 124 125 bld.makeFile(datafile); 126 } 127 128 /** 129 * Parses the specified arguments and sets up the variables. 130 */ 131 private static void processArgs(String[] args) { 132 for (int i = 0; i < args.length; i++) { 133 String arg = args[i]; 134 if (arg.equals("-o")) { 135 outputDir = args[++i]; 136 } else if (arg.equals("-spec")) { 137 unicodeData = args[++i]; 138 } else if (arg.equals("-language")) { 139 language = args[++i]; 140 } else if (arg.equals("-country")) { 141 country = args[++i]; 142 } else if (arg.equals("-valiant")) { 143 valiant = args[++i]; 144 } else { 145 usage(); 146 } 147 } 148 149 // Set locale name 150 localeName = getLocaleName(); 151 } 152 153 /** 154 * Make locale name ("_language_country_valiant") 155 */ 156 private static String getLocaleName() { 157 if (language.equals("")) { 158 if (!country.equals("") || !valiant.equals("")) { 159 language = "en"; 160 } else { 161 return ""; 162 } 163 } 164 165 StringBuffer sb = new StringBuffer(); 166 sb.append('_'); 167 sb.append(language); 168 if (!country.equals("") || !valiant.equals("")) { 169 sb.append('_'); 170 sb.append(country); 171 if (!valiant.equals("")) { 172 sb.append('_'); 173 sb.append(valiant); 174 } 175 } 176 177 return sb.toString(); 178 } 179 180 /** 181 * Usage: Displayed when an invalid command-line option is specified. 182 */ 183 private static void usage() { 184 System.err.println("Usage: GenerateBreakIteratorData [options]\n" + 185 " -o outputDir output directory name\n" + 186 " -spec specname unicode text filename\n" + 187 " and locale data:\n" + 188 " -lang language target language name\n" + 189 " -country country target country name\n" + 190 " -valiant valiant target valiant name\n" 191 ); 192 } 193 194 /** 195 * Return the path of output directory 196 */ 197 static String getOutputDirectory() { 198 return outputDir; 199 } 200 }