2 *******************************************************************************
\r
3 * Copyright (C) 2002-2009, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.util;
\r
9 // TODO integrate this into the test framework
\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
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
25 // TODO change to use test framework
\r
26 public class TestBagFormatter {
\r
28 static final void generatePropertyAliases(boolean showValues) {
\r
29 generatePropertyAliases(showValues, ICUPropertyFactory.make());
\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
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
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
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
78 public static void main(String[] args) throws Exception {
\r
79 System.out.println("Start");
\r
82 UnicodeProperty prop = ICUPropertyFactory.make().getProperty("Canonicalcombiningclass");
\r
83 prop.getAvailableValues();
\r
85 generatePropertyAliases(true);
\r
87 BagFormatter bf = new BagFormatter();
\r
89 UnicodeSet us = new UnicodeSet("[:gc=nd:]");
\r
90 BagFormatter.CONSOLE.println("[:gc=nd:]");
\r
91 bf.showSetNames(BagFormatter.CONSOLE,us);
\r
93 us = new UnicodeSet("[:numeric_value=2:]");
\r
94 BagFormatter.CONSOLE.println("[:numeric_value=2:]");
\r
95 bf.showSetNames(BagFormatter.CONSOLE,us);
\r
97 us = new UnicodeSet("[:numeric_type=numeric:]");
\r
98 BagFormatter.CONSOLE.println("[:numeric_type=numeric:]");
\r
99 bf.showSetNames(BagFormatter.CONSOLE,us);
\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
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
118 //TestTokenizer.test();
\r
119 //RandomCollator.generate("collationTest.txt", null);
\r
123 //if (true) return;
\r
125 //if (true) return;
\r
127 TestCollator tc = new TestCollator();
\r
128 tc.test(RuleBasedCollator.getInstance(),1000);
\r
131 StringBuffer sb = new StringBuffer();
\r
132 for (int i = 0; i < 100; ++i) {
\r
135 System.out.println(sb);
\r
139 System.out.println("End");
\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
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
171 * Use the number of significant digits to round get a rounding value.
\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
179 static final UnicodeSet RTL = new UnicodeSet("[[:L:]&[[:bidi class=R:][:bidi class=AL:]]]");
\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
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
195 Iterator it = s.iterator();
\r
196 while (it.hasNext()) {
\r
197 System.out.println(it.next());
\r
201 static final Transliterator toHTML = Transliterator.createFromRules(
\r
204 "'&' > '&' ;" +
\r
206 "'\"' > '"' ; ",
\r
207 Transliterator.FORWARD);
\r
208 static final Transliterator fromHTML = Transliterator.createFromRules(
\r
210 "'<' < '&'[lL][Tt]';' ;" +
\r
211 "'&' < '&'[aA][mM][pP]';' ;" +
\r
212 "'>' < '&'[gG][tT]';' ;" +
\r
213 "'\"' < '&'[qQ][uU][oO][tT]';' ; ",
\r
214 Transliterator.REVERSE);
\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
225 static void showNames(String propAlias, boolean matches) {
\r
226 BagFormatter bf = new BagFormatter();
\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
234 static void showNames(String propAlias, String pattern) {
\r
235 BagFormatter bf = new BagFormatter();
\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