1 /* 2 * Copyright (c) 2003, 2011, 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 classNames = info.getStringArray("BreakIteratorClasses"); 84 85 rules = ResourceBundle.getBundle("sun.text.resources.BreakIteratorRules", 86 new Locale(language, country, valiant)); 87 88 /* 89 * Fallback is not necessary here.... So, cannot use getBundle(). 90 */ 91 try { 92 info = (ResourceBundle)Class.forName("sun.text.resources.BreakIteratorInfo" + localeName).newInstance(); 93 94 Enumeration<String> keys = info.getKeys(); 95 while (keys.hasMoreElements()) { 96 String key = keys.nextElement(); 97 98 if (key.equals("CharacterData")) { 99 generateDataFile(info.getString(key), 100 rules.getString("CharacterBreakRules"), 101 classNames[0]); 102 } else if (key.endsWith("WordData")) { 103 generateDataFile(info.getString(key), 104 rules.getString("WordBreakRules"), 105 classNames[1]); 106 } else if (key.endsWith("LineData")) { 107 generateDataFile(info.getString(key), 108 rules.getString("LineBreakRules"), 109 classNames[2]); 110 } else if (key.endsWith("SentenceData")) { 111 generateDataFile(info.getString(key), 112 rules.getString("SentenceBreakRules"), 113 classNames[3]); 114 } 115 } 116 } 117 catch (Exception e) { 118 throw new InternalError(e.toString()); 119 } 120 } 121 122 /** 123 * Generate a data file for break-iterator 124 */ 125 private static void generateDataFile(String datafile, String rule, String builder) { 126 RuleBasedBreakIteratorBuilder bld; 127 if (builder.equals("RuleBasedBreakIterator")) { 128 bld = new RuleBasedBreakIteratorBuilder(rule); 129 } else if (builder.equals("DictionaryBasedBreakIterator")) { 130 bld = new DictionaryBasedBreakIteratorBuilder(rule); 131 } else { 132 throw new IllegalArgumentException("Invalid break iterator class \"" + builder + "\""); 133 } 134 135 bld.makeFile(datafile); 136 } 137 138 /** 139 * Parses the specified arguments and sets up the variables. 140 */ 141 private static void processArgs(String[] args) { 142 for (int i = 0; i < args.length; i++) { 143 String arg = args[i]; 144 if (arg.equals("-o")) { 145 outputDir = args[++i]; 146 } else if (arg.equals("-spec")) { 147 unicodeData = args[++i]; 148 } else if (arg.equals("-language")) { 149 language = args[++i]; 150 } else if (arg.equals("-country")) { 151 country = args[++i]; 152 } else if (arg.equals("-valiant")) { 153 valiant = args[++i]; 154 } else { 155 usage(); 156 } 157 } 158 159 // Set locale name 160 localeName = getLocaleName(); 161 } 162 163 /** 164 * Make locale name ("_language_country_valiant") 165 */ 166 private static String getLocaleName() { 167 if (language.equals("")) { 168 if (!country.equals("") || !valiant.equals("")) { 169 language = "en"; 170 } else { 171 return ""; 172 } 173 } 174 175 StringBuffer sb = new StringBuffer(); 176 sb.append('_'); 177 sb.append(language); 178 if (!country.equals("") || !valiant.equals("")) { 179 sb.append('_'); 180 sb.append(country); 181 if (!valiant.equals("")) { 182 sb.append('_'); 183 sb.append(valiant); 184 } 185 } 186 187 return sb.toString(); 188 } 189 190 /** 191 * Usage: Displayed when an invalid command-line option is specified. 192 */ 193 private static void usage() { 194 System.err.println("Usage: GenerateBreakIteratorData [options]\n" + 195 " -o outputDir output directory name\n" + 196 " -spec specname unicode text filename\n" + 197 " and locale data:\n" + 198 " -lang language target language name\n" + 199 " -country country target country name\n" + 200 " -valiant valiant target valiant name\n" 201 ); 202 } 203 204 /** 205 * Return the path of output directory 206 */ 207 static String getOutputDirectory() { 208 return outputDir; 209 } 210 }