add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / icu / src / tools / java / org / apache / lucene / analysis / icu / GenerateJFlexSupplementaryMacros.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.text.DateFormat;
21 import java.util.Date;
22 import java.util.HashMap;
23 import java.util.Locale;
24 import java.util.TimeZone;
25
26 import com.ibm.icu.text.UnicodeSet;
27 import com.ibm.icu.text.UnicodeSetIterator;
28 import com.ibm.icu.util.VersionInfo;
29
30 /** creates a macro to augment jflex's unicode wordbreak support for > BMP */
31 public class GenerateJFlexSupplementaryMacros {
32   private static final UnicodeSet BMP = new UnicodeSet("[\u0000-\uFFFF]");
33   private static final String NL = System.getProperty("line.separator");
34   private static final DateFormat DATE_FORMAT = DateFormat.getDateTimeInstance
35     (DateFormat.FULL, DateFormat.FULL, Locale.US);
36   static {
37     DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
38   }
39   
40   private static final String APACHE_LICENSE 
41     = "/*" + NL
42       + " * Copyright 2010 The Apache Software Foundation." + NL
43       + " *" + NL
44       + " * Licensed under the Apache License, Version 2.0 (the \"License\");" + NL
45       + " * you may not use this file except in compliance with the License." + NL
46       + " * You may obtain a copy of the License at" + NL
47       + " *" + NL
48       + " *      http://www.apache.org/licenses/LICENSE-2.0" + NL
49       + " *" + NL
50       + " * Unless required by applicable law or agreed to in writing, software" + NL
51       + " * distributed under the License is distributed on an \"AS IS\" BASIS," + NL
52       + " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied." + NL
53       + " * See the License for the specific language governing permissions and" + NL
54       + " * limitations under the License." + NL
55       + " */" + NL + NL;
56     
57   
58   public static void main(String args[]) throws Exception {
59     outputHeader();
60     outputMacro("ALetterSupp",         "[:WordBreak=ALetter:]");
61     outputMacro("FormatSupp",          "[:WordBreak=Format:]");
62     outputMacro("ExtendSupp",          "[:WordBreak=Extend:]");
63     outputMacro("NumericSupp",         "[:WordBreak=Numeric:]");
64     outputMacro("KatakanaSupp",        "[:WordBreak=Katakana:]");
65     outputMacro("MidLetterSupp",       "[:WordBreak=MidLetter:]");
66     outputMacro("MidNumSupp",          "[:WordBreak=MidNum:]");
67     outputMacro("MidNumLetSupp",       "[:WordBreak=MidNumLet:]");
68     outputMacro("ExtendNumLetSupp",    "[:WordBreak=ExtendNumLet:]");
69     outputMacro("ExtendNumLetSupp",    "[:WordBreak=ExtendNumLet:]");
70     outputMacro("ComplexContextSupp",  "[:LineBreak=Complex_Context:]");
71     outputMacro("HanSupp",             "[:Script=Han:]");
72     outputMacro("HiraganaSupp",        "[:Script=Hiragana:]");
73   }
74   
75   static void outputHeader() {
76     System.out.print(APACHE_LICENSE);
77     System.out.print("// Generated using ICU4J " + VersionInfo.ICU_VERSION.toString() + " on ");
78     System.out.println(DATE_FORMAT.format(new Date()));
79     System.out.println("// by " + GenerateJFlexSupplementaryMacros.class.getName());
80     System.out.print(NL + NL);
81   }
82   
83   // we have to carefully output the possibilities as compact utf-16
84   // range expressions, or jflex will OOM!
85   static void outputMacro(String name, String pattern) {
86     UnicodeSet set = new UnicodeSet(pattern);
87     set.removeAll(BMP);
88     System.out.println(name + " = (");
89     // if the set is empty, we have to do this or jflex will barf
90     if (set.isEmpty()) {
91       System.out.println("\t  []");
92     }
93     
94     HashMap<Character,UnicodeSet> utf16ByLead = new HashMap<Character,UnicodeSet>();
95     for (UnicodeSetIterator it = new UnicodeSetIterator(set); it.next();) {    
96       char utf16[] = Character.toChars(it.codepoint);
97       UnicodeSet trails = utf16ByLead.get(utf16[0]);
98       if (trails == null) {
99         trails = new UnicodeSet();
100         utf16ByLead.put(utf16[0], trails);
101       }
102       trails.add(utf16[1]);
103     }
104     
105     boolean isFirst = true;
106     for (Character c : utf16ByLead.keySet()) {
107       UnicodeSet trail = utf16ByLead.get(c);
108       System.out.print( isFirst ? "\t  " : "\t| ");
109       isFirst = false;
110       System.out.println("([\\u" + Integer.toHexString(c) + "]" + trail.getRegexEquivalent() + ")");
111     }
112     System.out.println(")");
113   }
114 }