1 package org.apache.lucene.analysis.icu;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.BufferedReader;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.FilenameFilter;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
29 import com.ibm.icu.text.RuleBasedBreakIterator;
32 * Command-line utility to converts RuleBasedBreakIterator (.rbbi) files into
33 * binary compiled form (.brk).
35 public class RBBIRuleCompiler {
37 static String getRules(File ruleFile) throws IOException {
38 StringBuilder rules = new StringBuilder();
39 InputStream in = new FileInputStream(ruleFile);
40 BufferedReader cin = new BufferedReader(new InputStreamReader(in, "UTF-8"));
42 while ((line = cin.readLine()) != null) {
43 if (!line.startsWith("#"))
49 return rules.toString();
52 static void compile(File srcDir, File destDir) throws Exception {
53 File files[] = srcDir.listFiles(new FilenameFilter() {
54 public boolean accept(File dir, String name) {
55 return name.endsWith("rbbi");
57 if (files == null) throw new IOException("Path does not exist: " + srcDir);
58 for (int i = 0; i < files.length; i++) {
60 File outputFile = new File(destDir,
61 file.getName().replaceAll("rbbi$", "brk"));
62 String rules = getRules(file);
63 System.err.print("Compiling " + file.getName() + " to "
64 + outputFile.getName() + ": ");
66 * if there is a syntax error, compileRules() may succeed. the way to
67 * check is to try to instantiate from the string. additionally if the
68 * rules are invalid, you can get a useful syntax error.
71 new RuleBasedBreakIterator(rules);
72 } catch (IllegalArgumentException e) {
74 * do this intentionally, so you don't get a massive stack trace
75 * instead, get a useful syntax error!
77 System.err.println(e.getMessage());
80 FileOutputStream os = new FileOutputStream(outputFile);
81 RuleBasedBreakIterator.compileRules(rules, os);
83 System.err.println(outputFile.length() + " bytes.");
87 public static void main(String args[]) throws Exception {
88 if (args.length < 2) {
89 System.err.println("Usage: RBBIRuleComputer <sourcedir> <destdir>");
92 compile(new File(args[0]), new File(args[1]));