2 //#if defined(FOUNDATION10) || defined(J2SE13)
5 *******************************************************************************
6 * Copyright (C) 2002-2009, International Business Machines Corporation and *
7 * others. All Rights Reserved. *
8 *******************************************************************************
10 package com.ibm.icu.dev.test.util;
12 // TODO integrate this into the test framework
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;
21 import java.util.TreeSet;
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;
28 // TODO change to use test framework
29 public class TestBagFormatter {
31 static final void generatePropertyAliases(boolean showValues) {
32 generatePropertyAliases(showValues, ICUPropertyFactory.make());
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;
50 System.out.println(propAlias + "\t" + bf.join(up.getNameAliases()));
51 if (!showValues) continue;
53 if (type == UnicodeProperty.NUMERIC || type == UnicodeProperty.EXTENDED_NUMERIC) {
54 UnicodeMap um = new UnicodeMap();
56 System.out.println(um.toString(new NumberComparator()));
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)));
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;
81 public static void main(String[] args) throws Exception {
82 System.out.println("Start");
85 UnicodeProperty prop = ICUPropertyFactory.make().getProperty("Canonicalcombiningclass");
86 prop.getAvailableValues();
88 generatePropertyAliases(true);
90 BagFormatter bf = new BagFormatter();
92 UnicodeSet us = new UnicodeSet("[:gc=nd:]");
93 BagFormatter.CONSOLE.println("[:gc=nd:]");
94 bf.showSetNames(BagFormatter.CONSOLE,us);
96 us = new UnicodeSet("[:numeric_value=2:]");
97 BagFormatter.CONSOLE.println("[:numeric_value=2:]");
98 bf.showSetNames(BagFormatter.CONSOLE,us);
100 us = new UnicodeSet("[:numeric_type=numeric:]");
101 BagFormatter.CONSOLE.println("[:numeric_type=numeric:]");
102 bf.showSetNames(BagFormatter.CONSOLE,us);
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);
110 //showNames("Name", ".*MARK.*");
111 //showNames("NFD", "a.+");
112 //showNames("NFD", false);
113 //showNames("Lowercase_Mapping", false);
114 //TestUnicodePropertySource.test(true);
115 //showNames(".*\\ \\-.*");
121 //TestTokenizer.test();
122 //RandomCollator.generate("collationTest.txt", null);
130 TestCollator tc = new TestCollator();
131 tc.test(RuleBasedCollator.getInstance(),1000);
134 StringBuffer sb = new StringBuffer();
135 for (int i = 0; i < 100; ++i) {
138 System.out.println(sb);
142 System.out.println("End");
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;
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());
174 * Use the number of significant digits to round get a rounding value.
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]
182 static final UnicodeSet RTL = new UnicodeSet("[[:L:]&[[:bidi class=R:][:bidi class=AL:]]]");
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;
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());
198 Iterator it = s.iterator();
199 while (it.hasNext()) {
200 System.out.println(it.next());
204 static final Transliterator toHTML = Transliterator.createFromRules(
209 "'\"' > '"' ; ",
210 Transliterator.FORWARD);
211 static final Transliterator fromHTML = Transliterator.createFromRules(
213 "'<' < '&'[lL][Tt]';' ;" +
214 "'&' < '&'[aA][mM][pP]';' ;" +
215 "'>' < '&'[gG][tT]';' ;" +
216 "'\"' < '&'[qQ][uU][oO][tT]';' ; ",
217 Transliterator.REVERSE);
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()));
228 static void showNames(String propAlias, boolean matches) {
229 BagFormatter bf = new BagFormatter();
231 stuff = new UnicodePropertySource.ICU()
232 .setPropertyAlias(propAlias)
233 .getPropertySet(matches, null);
234 System.out.println(bf.showSetNames(propAlias + " with " + matches, stuff));
237 static void showNames(String propAlias, String pattern) {
238 BagFormatter bf = new BagFormatter();
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));