Java Code Examples for com.ibm.icu.text.RuleBasedBreakIterator#compileRules()

The following examples show how to use com.ibm.icu.text.RuleBasedBreakIterator#compileRules() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RBBIRuleCompiler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
static void compile(File srcDir, File destDir) throws Exception {
  File files[] = srcDir.listFiles(new FilenameFilter() {
    public boolean accept(File dir, String name) {
      return name.endsWith("rbbi");
    }});
  if (files == null) throw new IOException("Path does not exist: " + srcDir);
  for (int i = 0; i < files.length; i++) {
    File file = files[i];
    File outputFile = new File(destDir, 
        file.getName().replaceAll("rbbi$", "brk"));
    String rules = getRules(file);
    System.err.print("Compiling " + file.getName() + " to "
        + outputFile.getName() + ": ");
    /*
     * if there is a syntax error, compileRules() may succeed. the way to
     * check is to try to instantiate from the string. additionally if the
     * rules are invalid, you can get a useful syntax error.
     */
    try {
      new RuleBasedBreakIterator(rules);
    } catch (IllegalArgumentException e) {
      /*
       * do this intentionally, so you don't get a massive stack trace
       * instead, get a useful syntax error!
       */
      System.err.println(e.getMessage());
      System.exit(1);
    }
    FileOutputStream os = new FileOutputStream(outputFile);
    RuleBasedBreakIterator.compileRules(rules, os);
    os.close();
    System.err.println(outputFile.length() + " bytes.");
  }
}
 
Example 2
Source File: RBBIRuleCompiler.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 5 votes vote down vote up
public void compile(InputStream inputStream, OutputStream outputStream) throws IOException {
    String rules = getRules(inputStream);
    try (OutputStream os = outputStream) {
        new RuleBasedBreakIterator(rules);
        RuleBasedBreakIterator.compileRules(rules, os);
    } catch (IllegalArgumentException e) {
        logger.error(e.getMessage(), e);
    }
}