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