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