001: //##header
002: /*
003: *******************************************************************************
004: * Copyright (C) 2002-2006, International Business Machines Corporation and *
005: * others. All Rights Reserved. *
006: *******************************************************************************
007: */
008: //#ifndef FOUNDATION
009: package com.ibm.icu.dev.test.util;
010:
011: // TODO integrate this into the test framework
012:
013: import java.io.IOException;
014: import java.io.PrintWriter;
015: import java.text.Collator;
016: import java.util.Comparator;
017: import java.util.Iterator;
018: import java.util.Locale;
019: import java.util.Set;
020: import java.util.TreeSet;
021:
022: import com.ibm.icu.lang.UProperty;
023: import com.ibm.icu.lang.UScript;
024: import com.ibm.icu.text.Transliterator;
025: import com.ibm.icu.text.UnicodeSet;
026:
027: // TODO change to use test framework
028: public class TestBagFormatter {
029:
030: static final void generatePropertyAliases(boolean showValues) {
031: generatePropertyAliases(showValues, ICUPropertyFactory.make());
032: }
033:
034: static final void generatePropertyAliases(boolean showValues,
035: UnicodeProperty.Factory ups) {
036: Collator order = Collator.getInstance(Locale.ENGLISH);
037: TreeSet props = new TreeSet(order);
038: TreeSet values = new TreeSet(order);
039: BagFormatter bf = new BagFormatter();
040: props.addAll(ups.getAvailableNames());
041: for (int i = UnicodeProperty.BINARY; i < UnicodeProperty.LIMIT_TYPE; ++i) {
042: System.out.println(UnicodeProperty.getTypeName(i));
043: Iterator it = props.iterator();
044: while (it.hasNext()) {
045: String propAlias = (String) it.next();
046: UnicodeProperty up = ups.getProperty(propAlias);
047: int type = up.getType();
048: if (type != i)
049: continue;
050: System.out.println();
051: System.out.println(propAlias + "\t"
052: + bf.join(up.getNameAliases()));
053: if (!showValues)
054: continue;
055: values.clear();
056: if (type == UnicodeProperty.NUMERIC
057: || type == UnicodeProperty.EXTENDED_NUMERIC) {
058: UnicodeMap um = new UnicodeMap();
059: um.putAll(up);
060: System.out.println(um
061: .toString(new NumberComparator()));
062: continue;
063: }
064: values.clear();
065: values.addAll(up.getAvailableValues());
066: Iterator it2 = values.iterator();
067: while (it2.hasNext()) {
068: String valueAlias = (String) it2.next();
069: System.out.println("\t"
070: + bf.join(valueAlias + "\t"
071: + up.getValueAliases(valueAlias)));
072: }
073: }
074: }
075: }
076:
077: static class NumberComparator implements Comparator {
078: public int compare(Object o1, Object o2) {
079: if (o1 == o2)
080: return 0;
081: if (o1 == null)
082: return 1;
083: if (o2 == null)
084: return -1;
085: double n1 = Double.parseDouble((String) o1);
086: double n2 = Double.parseDouble((String) o2);
087: return n1 < n2 ? -1 : n1 > n2 ? 1 : 0;
088: }
089: }
090:
091: public static void main(String[] args) throws Exception {
092: System.out.println("Start");
093: try {
094: //readCharacters();
095: UnicodeProperty prop = ICUPropertyFactory.make()
096: .getProperty("Canonicalcombiningclass");
097: prop.getAvailableValues();
098:
099: generatePropertyAliases(true);
100:
101: BagFormatter bf = new BagFormatter();
102:
103: UnicodeSet us = new UnicodeSet("[:gc=nd:]");
104: BagFormatter.CONSOLE.println("[:gc=nd:]");
105: bf.showSetNames(BagFormatter.CONSOLE, us);
106:
107: us = new UnicodeSet("[:numeric_value=2:]");
108: BagFormatter.CONSOLE.println("[:numeric_value=2:]");
109: bf.showSetNames(BagFormatter.CONSOLE, us);
110:
111: us = new UnicodeSet("[:numeric_type=numeric:]");
112: BagFormatter.CONSOLE.println("[:numeric_type=numeric:]");
113: bf.showSetNames(BagFormatter.CONSOLE, us);
114:
115: UnicodeProperty.Factory ups = ICUPropertyFactory.make();
116: us = ups.getSet("gc=mn", null, null);
117: BagFormatter.CONSOLE.println("gc=mn");
118: bf.showSetNames(BagFormatter.CONSOLE, us);
119:
120: if (true)
121: return;
122: //showNames("Name", ".*MARK.*");
123: //showNames("NFD", "a.+");
124: //showNames("NFD", false);
125: //showNames("Lowercase_Mapping", false);
126: //TestUnicodePropertySource.test(true);
127: //showNames(".*\\ \\-.*");
128:
129: //checkHTML();
130: //testIsRTL();
131:
132: //TestTokenizer.test();
133: //RandomCollator.generate("collationTest.txt", null);
134:
135: //TestPick.test();
136: //printRandoms();
137: //if (true) return;
138: //testLocales();
139: //if (true) return;
140: /*
141: TestCollator tc = new TestCollator();
142: tc.test(RuleBasedCollator.getInstance(),1000);
143: */
144: /*
145: StringBuffer sb = new StringBuffer();
146: for (int i = 0; i < 100; ++i) {
147: sb.setLength(0);
148: rc.nextRule(sb);
149: System.out.println(sb);
150: }
151: */
152: } finally {
153: System.out.println("End");
154: }
155:
156: }
157:
158: static void testLocales() throws IOException {
159: Locale[] locales = Collator.getAvailableLocales();
160: Set s = new TreeSet(Collator.getInstance());
161: for (int i = 0; i < locales.length; ++i) {
162: String lang = locales[i].getLanguage();
163: String dlang = locales[i].getDisplayLanguage();
164: String country = locales[i].getCountry();
165: String dcountry = locales[i].getDisplayCountry();
166: if (country.equals(""))
167: continue;
168: s.add("" + "\t" + dcountry + "\t" + country + "\t" + dlang
169: + "\t" + lang);
170: }
171: //CollectionFormatter cf = new CollectionFormatter();
172: PrintWriter pw = BagFormatter.openUTF8Writer("",
173: "countries.txt");
174: Iterator it = s.iterator();
175: while (it.hasNext()) {
176: pw.println(it.next());
177: }
178: pw.close();
179: }
180:
181: /*
182: * Use the number of significant digits to round get a rounding value.
183: */
184: static final double LOG10 = Math.log(10);
185:
186: public static void useSignificantDigits(double value, int digits) {
187: double log10 = Math.log(value) / LOG10; // log[e]
188:
189: }
190:
191: static final UnicodeSet RTL = new UnicodeSet(
192: "[[:L:]&[[:bidi class=R:][:bidi class=AL:]]]");
193:
194: static boolean isRTL(Locale loc) {
195: // in 2.8 we can use the exemplar characters, but for 2.6 we have to work around it
196: int[] scripts = UScript.getCode(loc);
197: return new UnicodeSet().applyIntPropertyValue(UProperty.SCRIPT,
198: scripts == null ? UScript.LATIN : scripts[0])
199: .retainAll(RTL).size() != 0;
200: }
201:
202: static void testIsRTL() {
203: Locale[] locales = Locale.getAvailableLocales();
204: Set s = new TreeSet();
205: for (int i = 0; i < locales.length; ++i) {
206: s.add((isRTL(locales[i]) ? "R " : "L ")
207: + locales[i].getDisplayName());
208: }
209: Iterator it = s.iterator();
210: while (it.hasNext()) {
211: System.out.println(it.next());
212: }
213: }
214:
215: static final Transliterator toHTML = Transliterator
216: .createFromRules("any-html", "'<' > '<' ;"
217: + "'&' > '&' ;" + "'>' > '>' ;"
218: + "'\"' > '"' ; ", Transliterator.FORWARD);
219: static final Transliterator fromHTML = Transliterator
220: .createFromRules("html-any", "'<' < '&'[lL][Tt]';' ;"
221: + "'&' < '&'[aA][mM][pP]';' ;"
222: + "'>' < '&'[gG][tT]';' ;"
223: + "'\"' < '&'[qQ][uU][oO][tT]';' ; ",
224: Transliterator.REVERSE);
225:
226: static void checkHTML() {
227: String foo = "& n < b < \"ab\"";
228: String fii = toHTML.transliterate(foo);
229: System.out.println("in: " + foo);
230: System.out.println("out: " + fii);
231: System.out.println("in*: " + fromHTML.transliterate(fii));
232: System.out.println("IN*: "
233: + fromHTML.transliterate(fii.toUpperCase()));
234: }
235: /*
236: static void showNames(String propAlias, boolean matches) {
237: BagFormatter bf = new BagFormatter();
238: UnicodeSet stuff;
239: stuff = new UnicodePropertySource.ICU()
240: .setPropertyAlias(propAlias)
241: .getPropertySet(matches, null);
242: System.out.println(bf.showSetNames(propAlias + " with " + matches, stuff));
243: }
244:
245: static void showNames(String propAlias, String pattern) {
246: BagFormatter bf = new BagFormatter();
247: UnicodeSet stuff;
248: stuff = new UnicodePropertySource.ICU()
249: .setPropertyAlias(propAlias)
250: .getPropertySet(Pattern.compile(pattern).matcher(""), null);
251: System.out.println(bf.showSetNames(propAlias + "with " + pattern, stuff));
252: }
253: */
254: }
255: //#endif
|