pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / icu / src / tools / java / org / apache / lucene / analysis / icu / RBBIRuleCompiler.java
1 package org.apache.lucene.analysis.icu;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.io.BufferedReader;
21 import java.io.File;
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;
28
29 import com.ibm.icu.text.RuleBasedBreakIterator;
30
31 /**
32  * Command-line utility to converts RuleBasedBreakIterator (.rbbi) files into
33  * binary compiled form (.brk).
34  */
35 public class RBBIRuleCompiler {
36   
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"));
41     String line = null;
42     while ((line = cin.readLine()) != null) {
43       if (!line.startsWith("#"))
44         rules.append(line);
45       rules.append('\n');
46     }
47     cin.close();
48     in.close();
49     return rules.toString();
50   }
51   
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");
56       }});
57     if (files == null) throw new IOException("Path does not exist: " + srcDir);
58     for (int i = 0; i < files.length; i++) {
59       File file = files[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() + ": ");
65       /*
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.
69        */
70       try {
71         new RuleBasedBreakIterator(rules);
72       } catch (IllegalArgumentException e) {
73         /*
74          * do this intentionally, so you don't get a massive stack trace
75          * instead, get a useful syntax error!
76          */
77         System.err.println(e.getMessage());
78         System.exit(1);
79       }
80       FileOutputStream os = new FileOutputStream(outputFile);
81       RuleBasedBreakIterator.compileRules(rules, os);
82       os.close();
83       System.err.println(outputFile.length() + " bytes.");
84     }
85   }
86   
87   public static void main(String args[]) throws Exception {
88     if (args.length < 2) {
89       System.err.println("Usage: RBBIRuleComputer <sourcedir> <destdir>");
90       System.exit(1);
91     }
92     compile(new File(args[0]), new File(args[1]));
93     System.exit(0);
94   }
95 }