]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/translit/src/com/ibm/icu/dev/test/util/TestBagFormatter.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / translit / src / com / ibm / icu / dev / test / util / TestBagFormatter.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2002-2009, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.dev.test.util;\r
8 \r
9 // TODO integrate this into the test framework\r
10 \r
11 import java.io.IOException;\r
12 import java.io.PrintWriter;\r
13 import java.text.Collator;\r
14 import java.util.Comparator;\r
15 import java.util.Iterator;\r
16 import java.util.Locale;\r
17 import java.util.Set;\r
18 import java.util.TreeSet;\r
19 \r
20 import com.ibm.icu.lang.UProperty;\r
21 import com.ibm.icu.lang.UScript;\r
22 import com.ibm.icu.text.Transliterator;\r
23 import com.ibm.icu.text.UnicodeSet;\r
24 \r
25 // TODO change to use test framework\r
26 public class TestBagFormatter {\r
27     \r
28     static final void generatePropertyAliases(boolean showValues) {\r
29         generatePropertyAliases(showValues, ICUPropertyFactory.make());\r
30     }\r
31     \r
32     static final void generatePropertyAliases(boolean showValues, UnicodeProperty.Factory ups) {\r
33         Collator order = Collator.getInstance(Locale.ENGLISH);\r
34         TreeSet props = new TreeSet(order);\r
35         TreeSet values = new TreeSet(order);\r
36         BagFormatter bf = new BagFormatter();\r
37         props.addAll(ups.getAvailableNames());\r
38         for (int i = UnicodeProperty.BINARY; i < UnicodeProperty.LIMIT_TYPE; ++i) {\r
39             System.out.println(UnicodeProperty.getTypeName(i));\r
40             Iterator it = props.iterator();\r
41             while (it.hasNext()) {\r
42                 String propAlias = (String)it.next();\r
43                 UnicodeProperty up = ups.getProperty(propAlias);\r
44                 int type = up.getType();\r
45                 if (type != i) continue;                \r
46                 System.out.println();\r
47                 System.out.println(propAlias + "\t" + bf.join(up.getNameAliases()));\r
48                 if (!showValues) continue;\r
49                 values.clear();\r
50                 if (type == UnicodeProperty.NUMERIC || type == UnicodeProperty.EXTENDED_NUMERIC) {\r
51                     UnicodeMap um = new UnicodeMap();\r
52                     um.putAll(up.getUnicodeMap());\r
53                     System.out.println(um.toString(new NumberComparator()));\r
54                     continue;\r
55                 }\r
56                 values.clear();\r
57                 values.addAll(up.getAvailableValues());\r
58                 Iterator it2 = values.iterator();\r
59                 while (it2.hasNext()) {\r
60                     String valueAlias = (String)it2.next();\r
61                     System.out.println("\t" + bf.join(valueAlias + "\t" + up.getValueAliases(valueAlias)));\r
62                 }\r
63             }\r
64         }\r
65     }\r
66     \r
67     static class NumberComparator implements Comparator {\r
68         public int compare(Object o1, Object o2) {\r
69             if (o1 == o2) return 0;\r
70             if (o1 == null) return 1;\r
71             if (o2 == null) return -1;\r
72             double n1 = Double.parseDouble((String)o1);\r
73             double n2 = Double.parseDouble((String)o2);\r
74             return n1 < n2 ? -1 : n1 > n2 ? 1 : 0;\r
75         }\r
76     }\r
77 \r
78     public static void main(String[] args) throws Exception {\r
79         System.out.println("Start");\r
80         try {\r
81             //readCharacters();\r
82             UnicodeProperty prop = ICUPropertyFactory.make().getProperty("Canonicalcombiningclass");\r
83             prop.getAvailableValues();\r
84             \r
85             generatePropertyAliases(true);\r
86             \r
87             BagFormatter bf = new BagFormatter();\r
88 \r
89             UnicodeSet us = new UnicodeSet("[:gc=nd:]");  \r
90             BagFormatter.CONSOLE.println("[:gc=nd:]");\r
91             bf.showSetNames(BagFormatter.CONSOLE,us);\r
92 \r
93             us = new UnicodeSet("[:numeric_value=2:]");  \r
94             BagFormatter.CONSOLE.println("[:numeric_value=2:]");\r
95             bf.showSetNames(BagFormatter.CONSOLE,us);\r
96             \r
97             us = new UnicodeSet("[:numeric_type=numeric:]");   \r
98             BagFormatter.CONSOLE.println("[:numeric_type=numeric:]");\r
99             bf.showSetNames(BagFormatter.CONSOLE,us);\r
100             \r
101             UnicodeProperty.Factory ups = ICUPropertyFactory.make();\r
102             us = ups.getSet("gc=mn", null, null); \r
103             BagFormatter.CONSOLE.println("gc=mn");\r
104             bf.showSetNames(BagFormatter.CONSOLE, us);\r
105             \r
106             if (true) return;\r
107             //showNames("Name", ".*MARK.*");\r
108             //showNames("NFD", "a.+");\r
109             //showNames("NFD", false);\r
110             //showNames("Lowercase_Mapping", false);\r
111             //TestUnicodePropertySource.test(true);\r
112             //showNames(".*\\ \\-.*");\r
113 \r
114 \r
115             //checkHTML();\r
116             //testIsRTL();\r
117            \r
118             //TestTokenizer.test();\r
119             //RandomCollator.generate("collationTest.txt", null);\r
120             \r
121             //TestPick.test();\r
122             //printRandoms();\r
123             //if (true) return;\r
124             //testLocales();\r
125             //if (true) return;\r
126             /*\r
127             TestCollator tc = new TestCollator();\r
128             tc.test(RuleBasedCollator.getInstance(),1000);\r
129             */\r
130             /*\r
131             StringBuffer sb = new StringBuffer();\r
132             for (int i = 0; i < 100; ++i) {\r
133                 sb.setLength(0);\r
134                 rc.nextRule(sb);\r
135                 System.out.println(sb);\r
136             }\r
137             */\r
138         } finally {\r
139             System.out.println("End");\r
140        }\r
141 \r
142     }\r
143     \r
144     static void testLocales() throws IOException {\r
145         Locale[] locales = Collator.getAvailableLocales();\r
146         Set s = new TreeSet(Collator.getInstance());\r
147         for (int i = 0; i < locales.length; ++i) {\r
148             String lang = locales[i].getLanguage();\r
149             String dlang = locales[i].getDisplayLanguage();\r
150             String country = locales[i].getCountry();\r
151             String dcountry = locales[i].getDisplayCountry();\r
152             if (country.equals("")) continue;\r
153             s.add(""\r
154                 + "\t" + dcountry \r
155                 + "\t" + country \r
156                 + "\t" + dlang\r
157                 + "\t" + lang \r
158             );\r
159         }\r
160         //CollectionFormatter cf = new CollectionFormatter();\r
161         PrintWriter pw = BagFormatter.openUTF8Writer("", "countries.txt");\r
162         Iterator it = s.iterator();\r
163         while (it.hasNext()) {\r
164             pw.println(it.next());\r
165         }\r
166         pw.close();\r
167     }\r
168     \r
169     \r
170     /*\r
171      * Use the number of significant digits to round get a rounding value.\r
172      */\r
173 /*    static final double LOG10 = Math.log(10);\r
174     public static void useSignificantDigits(double value, int digits) {\r
175         double log10 = Math.log(value)/LOG10; // log[e]\r
176         \r
177     }*/\r
178     \r
179     static final UnicodeSet RTL = new UnicodeSet("[[:L:]&[[:bidi class=R:][:bidi class=AL:]]]");\r
180     \r
181     static boolean isRTL(Locale loc) {        \r
182         // in 2.8 we can use the exemplar characters, but for 2.6 we have to work around it\r
183         int[] scripts = UScript.getCode(loc);\r
184         return new UnicodeSet()\r
185             .applyIntPropertyValue(UProperty.SCRIPT, scripts == null ? UScript.LATIN : scripts[0])\r
186             .retainAll(RTL).size() != 0;\r
187     }\r
188     \r
189     static void testIsRTL() {\r
190         Locale[] locales = Locale.getAvailableLocales();\r
191         Set s = new TreeSet();\r
192         for (int i = 0; i < locales.length; ++i) {\r
193             s.add((isRTL(locales[i]) ? "R " : "L ") + locales[i].getDisplayName());\r
194         }\r
195         Iterator it = s.iterator();\r
196         while (it.hasNext()) {\r
197             System.out.println(it.next());\r
198         }\r
199     }\r
200 \r
201     static final Transliterator toHTML = Transliterator.createFromRules(\r
202         "any-html",        \r
203             "'<' > '&lt;' ;" +\r
204             "'&' > '&amp;' ;" +\r
205             "'>' > '&gt;' ;" +\r
206             "'\"' > '&quot;' ; ",\r
207         Transliterator.FORWARD);\r
208     static final Transliterator fromHTML = Transliterator.createFromRules(\r
209         "html-any",        \r
210             "'<' < '&'[lL][Tt]';' ;" +\r
211             "'&' < '&'[aA][mM][pP]';' ;" +\r
212             "'>' < '&'[gG][tT]';' ;" +\r
213             "'\"' < '&'[qQ][uU][oO][tT]';' ; ",\r
214         Transliterator.REVERSE);\r
215         \r
216     static void checkHTML() {\r
217         String foo = "& n < b < \"ab\"";\r
218         String fii = toHTML.transliterate(foo);\r
219         System.out.println("in: " + foo);\r
220         System.out.println("out: " + fii);\r
221         System.out.println("in*: " + fromHTML.transliterate(fii));\r
222         System.out.println("IN*: " + fromHTML.transliterate(fii.toUpperCase()));\r
223     }\r
224     /*\r
225     static void showNames(String propAlias, boolean matches) {\r
226         BagFormatter bf = new BagFormatter();\r
227         UnicodeSet stuff;\r
228         stuff = new UnicodePropertySource.ICU()\r
229             .setPropertyAlias(propAlias)\r
230             .getPropertySet(matches, null);\r
231         System.out.println(bf.showSetNames(propAlias + " with " + matches, stuff));\r
232     }\r
233     \r
234     static void showNames(String propAlias, String pattern) {\r
235         BagFormatter bf = new BagFormatter();\r
236         UnicodeSet stuff;\r
237         stuff = new UnicodePropertySource.ICU()\r
238             .setPropertyAlias(propAlias)\r
239             .getPropertySet(Pattern.compile(pattern).matcher(""), null);\r
240         System.out.println(bf.showSetNames(propAlias + "with " + pattern, stuff));\r
241     }\r
242     */\r
243 }\r