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