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