ICU-3477 added timezone test. Not wired in yet; wait for 3.0
X-SVN-Rev: 14224
This commit is contained in:
parent
201f1aab34
commit
6b67db0d74
@ -5,8 +5,8 @@
|
||||
*******************************************************************************
|
||||
*
|
||||
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/demo/chart/UnicodeChart.java,v $
|
||||
* $Date: 2003/11/27 02:31:48 $
|
||||
* $Revision: 1.2 $
|
||||
* $Date: 2003/12/29 19:48:58 $
|
||||
* $Revision: 1.3 $
|
||||
*
|
||||
*****************************************************************************************
|
||||
*/
|
||||
@ -21,7 +21,7 @@ import com.ibm.icu.impl.*;
|
||||
public class UnicodeChart {
|
||||
public static void main(String[] args) throws IOException {
|
||||
int rowWidth = 256;
|
||||
PrintWriter pw = BagFormatter.openUTF8Writer("", "UnicodeChart.html", BagFormatter.CONSOLE);
|
||||
PrintWriter pw = BagFormatter.openUTF8Writer("", "UnicodeChart.html");
|
||||
pw.println("<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'>");
|
||||
pw.println("<script type='text/javascript' src='UnicodeChart.js'></script>");
|
||||
pw.println("<link rel='stylesheet' type='text/css' href='UnicodeChart.css'>");
|
||||
|
@ -39,13 +39,15 @@ public class RandomCollator extends TestFmwk {
|
||||
|
||||
static final String POSITION = "{$$$}";
|
||||
|
||||
class Shower implements BagFormatter.Shower {
|
||||
public void println(String arg) {
|
||||
logln(arg);
|
||||
/*
|
||||
class Shower extends BagFormatter.Shower {
|
||||
public void print(String arg) {
|
||||
log(arg);
|
||||
}
|
||||
}
|
||||
|
||||
public Shower LOG = new Shower();
|
||||
*/
|
||||
|
||||
public void TestRandom() throws IOException {
|
||||
int year
|
||||
@ -54,8 +56,8 @@ public class RandomCollator extends TestFmwk {
|
||||
System.out.println("\nTestRandom skipped for 2003");
|
||||
return;
|
||||
}
|
||||
|
||||
PrintWriter pw = BagFormatter.openUTF8Writer("", "RandomCollationTestLog.txt", BagFormatter.CONSOLE);
|
||||
String fileName;
|
||||
PrintWriter pw = BagFormatter.openUTF8Writer("", "RandomCollationTestLog.txt");
|
||||
TestCollator tc = new TestCollator(chars);
|
||||
pw.println("Collation Test Run");
|
||||
pw.println("Note: For parse-exception, " + POSITION + " indicates the errorOffset");
|
||||
|
422
icu4j/src/com/ibm/icu/dev/test/timezone/TimeZoneAliasTest.java
Normal file
422
icu4j/src/com/ibm/icu/dev/test/timezone/TimeZoneAliasTest.java
Normal file
@ -0,0 +1,422 @@
|
||||
/*
|
||||
*******************************************************************************
|
||||
* Copyright (C) 2002-2004, International Business Machines Corporation and *
|
||||
* others. All Rights Reserved. *
|
||||
*******************************************************************************
|
||||
*
|
||||
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/timezone/TimeZoneAliasTest.java,v $
|
||||
* $Date: 2003/12/29 19:48:59 $
|
||||
* $Revision: 1.1 $
|
||||
*
|
||||
*******************************************************************************
|
||||
*/
|
||||
package com.ibm.icu.dev.test.timezone;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.SortedMap;
|
||||
import java.util.SortedSet;
|
||||
//import java.util.TimeZone;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
import java.util.Iterator;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.NumberFormat;
|
||||
|
||||
import com.ibm.icu.dev.test.*;
|
||||
|
||||
import com.ibm.icu.dev.test.util.BagFormatter;
|
||||
import com.ibm.icu.util.TimeZone;
|
||||
|
||||
|
||||
/**
|
||||
* Class for testing TimeZones for consistency
|
||||
* @author Davis
|
||||
*
|
||||
*/
|
||||
public class TimeZoneAliasTest extends TestFmwk {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
new TimeZoneAliasTest().run(args);
|
||||
}
|
||||
|
||||
/**
|
||||
* There are two things to check aliases for:<br>
|
||||
* 1. the alias set must be uniform: if a isAlias b, then aliasSet(a) == aliasSet(b)<br>
|
||||
* 2. all aliases must have the same offsets
|
||||
*/
|
||||
public void TestAliases() {
|
||||
Zone.Seconds seconds = new Zone.Seconds();
|
||||
for (Iterator it = Zone.getZoneSet().iterator(); it.hasNext(); ) {
|
||||
Zone zone = (Zone)it.next();
|
||||
String id = zone.id;
|
||||
if (id.indexOf('/') < 0 && (id.endsWith("ST") || id.endsWith("DT"))) {
|
||||
if (zone.minRecentOffset != zone.maxRecentOffset) {
|
||||
errln(
|
||||
"Standard or Daylight Time not constant: " + id
|
||||
+ ": " + zone.formatHours(zone.minRecentOffset)
|
||||
+ " != " + zone.formatHours(zone.maxRecentOffset));
|
||||
}
|
||||
}
|
||||
Set aliases = zone.getPurportedAliases();
|
||||
Set aliasesSet = new TreeSet(aliases);
|
||||
aliasesSet.add(id); // for comparison
|
||||
Iterator aliasIterator = aliases.iterator();
|
||||
while (aliasIterator.hasNext()) {
|
||||
String otherId = (String)aliasIterator.next();
|
||||
Zone otherZone = Zone.make(otherId);
|
||||
Set otherAliases = otherZone.getPurportedAliases();
|
||||
otherAliases.add(otherId); // for comparison
|
||||
if (!aliasesSet.equals(otherAliases)) {
|
||||
errln(
|
||||
"Aliases Unsymmetric: "
|
||||
+ id + " => " + join(aliasesSet, ", ")
|
||||
+ "; "
|
||||
+ otherId + " => " + join(otherAliases, ", "));
|
||||
}
|
||||
if (zone.findOffsetOrdering(otherZone, seconds) != 0) {
|
||||
errln("Aliases differ: " + id + ", " + otherId
|
||||
+ " differ at " + seconds);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* We check to see that every timezone that is not an alias is actually different!
|
||||
*/
|
||||
public void TestDifferences() {
|
||||
Zone last = null;
|
||||
Zone.Seconds diffDate = new Zone.Seconds();
|
||||
for (Iterator it = Zone.getZoneSet().iterator(); it.hasNext();) {
|
||||
Zone testZone = (Zone)it.next();
|
||||
if (last != null) {
|
||||
String common = testZone + " vs " + last + ":\t";
|
||||
int diff = testZone.findOffsetOrdering(last, diffDate);
|
||||
if (diff != 0) {
|
||||
logln("\t" + common + "difference at: " + diffDate
|
||||
+ ", " + Zone.formatHours(diff) + "hr");
|
||||
} else if (testZone.isRealAlias(last)) {
|
||||
logln("\t" + common + "alias, no difference");
|
||||
} else {
|
||||
errln(common + "NOT ALIAS BUT NO DIFFERENCE!");
|
||||
}
|
||||
}
|
||||
last = testZone;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility for printing out zones to be translated.
|
||||
*/
|
||||
public static void printZones() {
|
||||
int count = 1;
|
||||
for (Iterator it = Zone.getUniqueZoneSet().iterator(); it.hasNext();) {
|
||||
Zone zone = (Zone)it.next();
|
||||
System.out.println(zone.toString(count++));
|
||||
}
|
||||
}
|
||||
|
||||
/** Utility; ought to be someplace common
|
||||
*/
|
||||
|
||||
static String join(Collection c, String separator) {
|
||||
StringBuffer result = new StringBuffer();
|
||||
boolean isFirst = true;
|
||||
for (Iterator it = c.iterator(); it.hasNext(); ) {
|
||||
if (!isFirst) result.append(separator);
|
||||
else isFirst = false;
|
||||
result.append(it.next().toString());
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* The guts is in this subclass. It sucks in all the data from the zones,
|
||||
* and analyses it. It constructs some mappings for the unique ids,
|
||||
* etc.<br>
|
||||
* The main tricky bit is that for performance it pre-analyses all zones
|
||||
* for inflections points; the points in time where the offset changes.
|
||||
* The zones can then be sorted by those points, which allows us to
|
||||
* avoid expensive comparisons.
|
||||
* @author Davis
|
||||
*/
|
||||
static class Zone implements Comparable {
|
||||
// class fields
|
||||
static private final DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.US);
|
||||
static private final NumberFormat nf = NumberFormat.getInstance(Locale.US);
|
||||
static private final long HOUR = 1000*60*60;
|
||||
static private final double DHOUR = HOUR;
|
||||
static private final long DAY = 24*HOUR;
|
||||
static private final long GROSS_PERIOD = 30*DAY;
|
||||
static private final long EPSILON = HOUR/4;
|
||||
static private final int currentYear = new Date().getYear() + 1900;
|
||||
static private final long endDate = new Date((currentYear+1)-1900,0,1).getTime();
|
||||
static private final long endDate2 = new Date((currentYear+1)-1900,6,1).getTime();
|
||||
static private final long recentLimit = new Date((currentYear-1)-1900,6,1).getTime();
|
||||
static private final long startDate = new Date(1905-1900,0,1).getTime();
|
||||
|
||||
static private final Map idToZone = new HashMap();
|
||||
static private final Set zoneSet = new TreeSet();
|
||||
static private final Set uniqueZoneSet = new TreeSet();
|
||||
static private final Map idToRealAliases = new HashMap();
|
||||
|
||||
// build everything once.
|
||||
static {
|
||||
String [] foo = TimeZone.getAvailableIDs();
|
||||
for (int i = 0; i < foo.length; ++i) {
|
||||
zoneSet.add(Zone.make(foo[i]));
|
||||
}
|
||||
Zone last = null;
|
||||
Zone.Seconds diffDate = new Zone.Seconds();
|
||||
String lastUnique = "";
|
||||
for (Iterator it = Zone.getZoneSet().iterator(); it.hasNext();) {
|
||||
Zone testZone = (Zone)it.next();
|
||||
if (last == null) {
|
||||
uniqueZoneSet.add(testZone);
|
||||
lastUnique = testZone.id;
|
||||
} else {
|
||||
int diff = testZone.findOffsetOrdering(last, diffDate);
|
||||
if (diff != 0) {
|
||||
uniqueZoneSet.add(testZone);
|
||||
lastUnique = testZone.id;
|
||||
} else {
|
||||
Set aliases = (Set)idToRealAliases.get(lastUnique);
|
||||
if (aliases == null) {
|
||||
aliases = new TreeSet();
|
||||
idToRealAliases.put(lastUnique, aliases);
|
||||
}
|
||||
aliases.add(testZone.id);
|
||||
}
|
||||
}
|
||||
last = testZone;
|
||||
}
|
||||
}
|
||||
|
||||
static public Set getZoneSet() {
|
||||
return zoneSet;
|
||||
}
|
||||
|
||||
public static Set getUniqueZoneSet() {
|
||||
return uniqueZoneSet;
|
||||
}
|
||||
|
||||
static public Zone make(String id) {
|
||||
Zone result = (Zone)idToZone.get(id);
|
||||
if (result != null) return result;
|
||||
result = new Zone(id);
|
||||
idToZone.put(id, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
static public String formatHours(int hours) {
|
||||
return nf.format(hours/DHOUR);
|
||||
}
|
||||
|
||||
// utility class for date return, because Date is clunky.
|
||||
public static class Seconds {
|
||||
public long seconds = Long.MIN_VALUE;
|
||||
public String toString() {
|
||||
if (seconds == Long.MIN_VALUE) return "n/a";
|
||||
return df.format(new Date(seconds));
|
||||
}
|
||||
}
|
||||
|
||||
// instance fields
|
||||
// we keep min/max offsets not only over all time (that we care about)
|
||||
// but also separate ones for recent years.
|
||||
private String id;
|
||||
private TimeZone zone;
|
||||
// computed below
|
||||
private int minOffset;
|
||||
private int maxOffset;
|
||||
private int minRecentOffset;
|
||||
private int maxRecentOffset;
|
||||
private List inflectionPoints = new ArrayList();
|
||||
private Set purportedAliases = new TreeSet();
|
||||
|
||||
private Zone(String id) { // for interal use only; use make instead!
|
||||
zone = TimeZone.getTimeZone(id);
|
||||
this.id = id;
|
||||
|
||||
// get aliases
|
||||
int equivCount = TimeZone.countEquivalentIDs(id);
|
||||
for (int j = 0; j < equivCount; ++j) {
|
||||
String altID = TimeZone.getEquivalentID(id, j);
|
||||
if (altID.equals(id)) continue;
|
||||
purportedAliases.add(altID);
|
||||
}
|
||||
|
||||
// find inflexion points; times where the offset changed
|
||||
long lastDate = endDate;
|
||||
if (zone.getOffset(lastDate) < zone.getOffset(endDate2)) lastDate = endDate2;
|
||||
maxRecentOffset = minRecentOffset = minOffset = maxOffset = zone.getOffset(lastDate);
|
||||
|
||||
inflectionPoints.add(new Long(lastDate));
|
||||
int lastOffset = zone.getOffset(endDate);
|
||||
long lastInflection = endDate;
|
||||
|
||||
// we do a gross search, then narrow in when we find a difference from the last one
|
||||
for (long currentDate = endDate; currentDate >= startDate; currentDate -= GROSS_PERIOD) {
|
||||
int currentOffset = zone.getOffset(currentDate);
|
||||
if (currentOffset != lastOffset) { // Binary Search
|
||||
if (currentOffset < minOffset) minOffset = currentOffset;
|
||||
if (currentOffset > maxOffset) maxOffset = currentOffset;
|
||||
if (lastInflection >= recentLimit) {
|
||||
if (currentOffset < minRecentOffset) minRecentOffset = currentOffset;
|
||||
if (currentOffset > maxRecentOffset) maxRecentOffset = currentOffset;
|
||||
}
|
||||
long low = currentDate;
|
||||
long high = lastDate;
|
||||
while (low - high > EPSILON) {
|
||||
long mid = (high + low)/2;
|
||||
int midOffset = zone.getOffset(mid);
|
||||
if (midOffset == low) {
|
||||
low = mid;
|
||||
} else {
|
||||
high = mid;
|
||||
}
|
||||
}
|
||||
inflectionPoints.add(new Long(low));
|
||||
lastInflection = low;
|
||||
}
|
||||
lastOffset = currentOffset;
|
||||
}
|
||||
inflectionPoints.add(new Long(startDate)); // just to cap it off for comparisons.
|
||||
}
|
||||
|
||||
// we assume that places will not convert time zones then back within one day
|
||||
// so we go first by half
|
||||
public int findOffsetOrdering(Zone other, Seconds dateDiffFound) {
|
||||
//System.out.println("-diff: " + id + "\t" + other.id);
|
||||
int result = 0;
|
||||
long seconds = 0;
|
||||
int min = inflectionPoints.size();
|
||||
if (other.inflectionPoints.size() < min) min = other.inflectionPoints.size();
|
||||
main:
|
||||
{
|
||||
for (int i = 0; i < min; ++i) {
|
||||
long myIP = ((Long)inflectionPoints.get(i)).longValue();
|
||||
long otherIP = ((Long)other.inflectionPoints.get(i)).longValue();
|
||||
if (myIP > otherIP) { // take lowest, for transitivity (semi)
|
||||
long temp = myIP;
|
||||
myIP = otherIP;
|
||||
otherIP = temp;
|
||||
}
|
||||
result = zone.getOffset(myIP) - other.zone.getOffset(myIP);
|
||||
if (result != 0) {
|
||||
seconds = myIP;
|
||||
break main;
|
||||
}
|
||||
if (myIP == otherIP) continue; // test other if different
|
||||
myIP = otherIP;
|
||||
result = zone.getOffset(myIP) - other.zone.getOffset(myIP);
|
||||
if (result != 0) {
|
||||
seconds = myIP;
|
||||
break main;
|
||||
}
|
||||
}
|
||||
// if they are equal so far, we don't care about the rest
|
||||
result = 0;
|
||||
seconds = Long.MIN_VALUE;
|
||||
break main;
|
||||
}
|
||||
//System.out.println("+diff: " + (result/HOUR) + "\t" + dateDiffFound);
|
||||
if (dateDiffFound != null) dateDiffFound.seconds = seconds;
|
||||
return result;
|
||||
}
|
||||
|
||||
// internal buffer to avoid creation all the time.
|
||||
private Seconds diffDateReturn = new Seconds();
|
||||
|
||||
public int compareTo(Object o) {
|
||||
Zone other = (Zone)o;
|
||||
// first order by max and min offsets
|
||||
// min will usually correspond to standard time, max to daylight
|
||||
// unless there have been historical shifts
|
||||
if (minRecentOffset < other.minRecentOffset) return -1;
|
||||
if (minRecentOffset > other.minRecentOffset) return 1;
|
||||
if (maxRecentOffset < other.maxRecentOffset) return -1;
|
||||
if (maxRecentOffset > other.maxRecentOffset) return 1;
|
||||
// now check that all offsets are the same over history
|
||||
int diffDate = findOffsetOrdering(other, diffDateReturn);
|
||||
if (diffDate != 0) return diffDate;
|
||||
// choose longer name first!!
|
||||
if (id.length() != other.id.length()) {
|
||||
if (id.length() < other.id.length()) return 1;
|
||||
return -1;
|
||||
}
|
||||
return id.compareTo(other.id);
|
||||
}
|
||||
|
||||
public Set getPurportedAliases() {
|
||||
return new TreeSet(purportedAliases); // clone for safety
|
||||
}
|
||||
|
||||
public boolean isPurportedAlias(String id) {
|
||||
return purportedAliases.contains(id);
|
||||
}
|
||||
|
||||
public boolean isRealAlias(Zone z) {
|
||||
return purportedAliases.contains(z.id);
|
||||
}
|
||||
|
||||
public String getPurportedAliasesAsString() {
|
||||
Set s = getPurportedAliases();
|
||||
if (s.size() == 0) return "";
|
||||
return " {" + join(s,", ") + "}";
|
||||
}
|
||||
|
||||
public String getRealAliasesAsString() {
|
||||
Set s = (Set)idToRealAliases.get(id);
|
||||
if (s == null) return "";
|
||||
return " {" + join(s,", ") + "}";
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
int pos = id.lastIndexOf(('/'));
|
||||
String city = id.substring(pos+1);
|
||||
return city.replace('_',' ');
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return toString(-1);
|
||||
}
|
||||
|
||||
public String toString(int count) {
|
||||
String city = getCity();
|
||||
String hours = formatHours(minRecentOffset)
|
||||
+ (minRecentOffset != maxRecentOffset
|
||||
? "," + formatHours(maxRecentOffset)
|
||||
: "");
|
||||
if (count < 0) {
|
||||
return id + getPurportedAliasesAsString() + " (" + hours + ")";
|
||||
}
|
||||
|
||||
return "\t{\t\"" + id + "\"\t// [" + count + "] " + hours
|
||||
+ getPurportedAliasesAsString() + getRealAliasesAsString() + "\r\n"
|
||||
+ "\t\t// translate the following!!\r\n"
|
||||
+ (minRecentOffset != maxRecentOffset
|
||||
? "\t\t\"" + city + " Standard Time\"\r\n"
|
||||
+ "\t\t\"" + city + "-ST\"\r\n"
|
||||
+ "\t\t\"" + city + " Daylight Time\"\r\n"
|
||||
+ "\t\t\"" + city + "-DT\"\r\n"
|
||||
: "\t\t\"\"\r\n"
|
||||
+ "\t\t\"\"\r\n"
|
||||
+ "\t\t\"\"\r\n"
|
||||
+ "\t\t\"\"\r\n")
|
||||
+ "\t\t\"" + city + " Time\"\r\n"
|
||||
+ "\t\t\"" + city + "-T\"\r\n"
|
||||
+ "\t\t\"" + city + "\"\r\n"
|
||||
+ "\t}";
|
||||
}
|
||||
}
|
||||
}
|
@ -5,8 +5,8 @@
|
||||
*******************************************************************************
|
||||
*
|
||||
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/util/BagFormatter.java,v $
|
||||
* $Date: 2003/12/20 03:06:54 $
|
||||
* $Revision: 1.4 $
|
||||
* $Date: 2003/12/29 19:48:58 $
|
||||
* $Revision: 1.5 $
|
||||
*
|
||||
*****************************************************************************************
|
||||
*/
|
||||
@ -22,8 +22,11 @@ import java.util.*;
|
||||
import java.text.MessageFormat;
|
||||
|
||||
public class BagFormatter {
|
||||
public static final PrintWriter CONSOLE = new PrintWriter(System.out,true);
|
||||
|
||||
private static PrintWriter log = CONSOLE;
|
||||
|
||||
boolean abbreviated = false;
|
||||
private boolean abbreviated = false;
|
||||
|
||||
/**
|
||||
* Compare two UnicodeSets, and show the differences
|
||||
@ -39,10 +42,10 @@ public class BagFormatter {
|
||||
String name2,
|
||||
UnicodeSet set2) {
|
||||
|
||||
StringWriter sw = new StringWriter();
|
||||
showSetDifferences(new PrintWriter(sw), name1, set1, name2, set2);
|
||||
sw.flush();
|
||||
return sw.getBuffer().toString();
|
||||
StringWriter result = new StringWriter();
|
||||
showSetDifferences(new PrintWriter(result),name1,set1,name2,set2);
|
||||
result.flush();
|
||||
return result.getBuffer().toString();
|
||||
}
|
||||
|
||||
public String showSetDifferences(
|
||||
@ -51,10 +54,10 @@ public class BagFormatter {
|
||||
String name2,
|
||||
Collection set2) {
|
||||
|
||||
StringWriter sw = new StringWriter();
|
||||
showSetDifferences(new PrintWriter(sw), name1, set1, name2, set2);
|
||||
sw.flush();
|
||||
return sw.getBuffer().toString();
|
||||
StringWriter result = new StringWriter();
|
||||
showSetDifferences(new PrintWriter(result), name1, set1, name2, set2);
|
||||
result.flush();
|
||||
return result.getBuffer().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -71,7 +74,7 @@ public class BagFormatter {
|
||||
UnicodeSet set1,
|
||||
String name2,
|
||||
UnicodeSet set2) {
|
||||
|
||||
if (pw == null) pw = CONSOLE;
|
||||
String[] names = { name1, name2 };
|
||||
|
||||
UnicodeSet temp = new UnicodeSet(set1).removeAll(set2);
|
||||
@ -94,6 +97,7 @@ public class BagFormatter {
|
||||
String name2,
|
||||
Collection set2) {
|
||||
|
||||
if (pw == null) pw = CONSOLE;
|
||||
String[] names = { name1, name2 };
|
||||
// damn'd collection doesn't have a clone, so
|
||||
// we go with Set, even though that
|
||||
@ -116,14 +120,21 @@ public class BagFormatter {
|
||||
showSetNames(pw, inIn.format(names), temp);
|
||||
}
|
||||
|
||||
public String showSetNames(String title, Object set1) {
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter pw = new PrintWriter(sw);
|
||||
showSetNames(pw, title, set1);
|
||||
pw.flush();
|
||||
String result = sw.getBuffer().toString();
|
||||
pw.close();
|
||||
return result;
|
||||
/**
|
||||
* Returns a list of items in the collection, with each separated by the separator.
|
||||
* Each item must not be null; its toString() is called for a printable representation
|
||||
* @param c source collection
|
||||
* @param separator to be placed between any strings
|
||||
* @return
|
||||
* @internal
|
||||
*/
|
||||
public String showSetNames(String title, Object c) {
|
||||
StringWriter buffer = new StringWriter();
|
||||
PrintWriter output = new PrintWriter(buffer);
|
||||
output.println(title);
|
||||
mainVisitor.output = output;
|
||||
mainVisitor.doAt(c);
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -149,9 +160,11 @@ public class BagFormatter {
|
||||
* @internal
|
||||
*/
|
||||
public void showSetNames(String filename, String title, Object c) throws IOException {
|
||||
/* PrintWriter pw = */new PrintWriter(
|
||||
PrintWriter pw = new PrintWriter(
|
||||
new OutputStreamWriter(
|
||||
new FileOutputStream(filename),"utf-8"));
|
||||
showSetNames(log,title,c);
|
||||
pw.close();
|
||||
}
|
||||
|
||||
public String getAbbreviatedName(
|
||||
@ -251,6 +264,31 @@ public class BagFormatter {
|
||||
return hcp + "<reserved>";
|
||||
}
|
||||
|
||||
public String getName(String s) {
|
||||
if (s.length() == 1) return getName(s.charAt(0)); // optimize
|
||||
StringBuffer sb = new StringBuffer();
|
||||
int cp;
|
||||
for (int i = 0; i < s.length(); i+=UTF16.getCharCount(cp)) {
|
||||
cp = UTF16.charAt(s,i);
|
||||
if (i != 0) sb.append(separator);
|
||||
sb.append(getName(cp));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public String hex(String s) {
|
||||
if (s.length() == 1) return Utility.hex(s.charAt(0),4); // optimize
|
||||
StringBuffer sb = new StringBuffer();
|
||||
int cp;
|
||||
for (int i = 0; i < s.length(); i+=UTF16.getCharCount(cp)) {
|
||||
cp = UTF16.charAt(s,i);
|
||||
if (i != 0) sb.append(separator);
|
||||
sb.append(Utility.hex(cp,4));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
String separator = ",";
|
||||
UnicodePropertySource source;
|
||||
UnicodePropertySource labelSource;
|
||||
|
||||
@ -293,7 +331,7 @@ public class BagFormatter {
|
||||
private Visitor.Join labelVisitor = new Visitor.Join();
|
||||
|
||||
private boolean mergeRanges = true;
|
||||
private boolean literalCharacter = false;
|
||||
private Transliterator showLiteral = null;
|
||||
private boolean showSetAlso = false;
|
||||
|
||||
private RangeFinder rf = new RangeFinder();
|
||||
@ -404,7 +442,14 @@ public class BagFormatter {
|
||||
} else if (o instanceof Visitor.CodePointRange) {
|
||||
doAt((Visitor.CodePointRange) o);
|
||||
} else {
|
||||
output.print(o.toString());
|
||||
String thing = o.toString();
|
||||
output.println(
|
||||
singleTabber.process(
|
||||
hex(thing)
|
||||
+ " \t# "
|
||||
+ insertLiteral(thing)
|
||||
+ " \t"
|
||||
+ getName(thing)));
|
||||
}
|
||||
}
|
||||
|
||||
@ -419,10 +464,7 @@ public class BagFormatter {
|
||||
Utility.hex(cp, 4)
|
||||
+ " \t# "
|
||||
+ label
|
||||
+ (literalCharacter
|
||||
&& (cp >= 0x20)
|
||||
? " \t(" + UTF16.valueOf(cp) + ") "
|
||||
: "")
|
||||
+ insertLiteral(cp)
|
||||
+ " \t"
|
||||
+ getName(cp)));
|
||||
}
|
||||
@ -450,15 +492,7 @@ public class BagFormatter {
|
||||
+ " \t["
|
||||
+ nf.format(end - start + 1)
|
||||
+ "]"
|
||||
+ (literalCharacter
|
||||
&& (start >= 0x20)
|
||||
? " \t("
|
||||
+ UTF16.valueOf(start)
|
||||
+ ((start != end)
|
||||
? (".." + UTF16.valueOf(end))
|
||||
: "")
|
||||
+ ") "
|
||||
: "")
|
||||
+ insertLiteral(start, end)
|
||||
+ " \t"
|
||||
+ getName(start)
|
||||
+ ((start != end)
|
||||
@ -473,6 +507,25 @@ public class BagFormatter {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String insertLiteral(String thing) {
|
||||
return (showLiteral == null ? ""
|
||||
: " \t(" + showLiteral.transliterate(thing) + ") ");
|
||||
}
|
||||
|
||||
private String insertLiteral(int start, int end) {
|
||||
return (showLiteral == null ? "" :
|
||||
" \t(" + showLiteral.transliterate(UTF16.valueOf(start))
|
||||
+ ((start != end)
|
||||
? (".." + showLiteral.transliterate(UTF16.valueOf(end)))
|
||||
: "")
|
||||
+ ") ");
|
||||
}
|
||||
|
||||
private String insertLiteral(int cp) {
|
||||
return (showLiteral == null ? ""
|
||||
: " \t(" + showLiteral.transliterate(UTF16.valueOf(cp)) + ") ");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -578,33 +631,31 @@ public class BagFormatter {
|
||||
public static final Transliterator hex = Transliterator.getInstance(
|
||||
"[^\\u0021-\\u007E\\u00A0-\\u00FF] hex");
|
||||
|
||||
public interface Shower {
|
||||
public void println(String arg);
|
||||
public static BufferedReader openUTF8Reader(String dir, String filename) throws IOException {
|
||||
return openReader(dir,filename,"UTF-8");
|
||||
}
|
||||
|
||||
public static Shower CONSOLE = new Shower() {
|
||||
public void println(String arg) {
|
||||
System.out.println(arg);
|
||||
}
|
||||
};
|
||||
|
||||
public static BufferedReader openUTF8Reader(String dir, String filename, Shower shower) throws IOException {
|
||||
public static BufferedReader openReader(String dir, String filename, String encoding) throws IOException {
|
||||
File file = new File(dir + filename);
|
||||
if (shower != null) {
|
||||
shower.println("Creating File: "
|
||||
if (log != null) {
|
||||
log.println("Opening File: "
|
||||
+ file.getCanonicalPath());
|
||||
}
|
||||
return new BufferedReader(
|
||||
new InputStreamReader(
|
||||
new FileInputStream(file),
|
||||
"UTF-8"),
|
||||
encoding),
|
||||
4*1024);
|
||||
}
|
||||
|
||||
public static PrintWriter openUTF8Writer(String dir, String filename, Shower shower) throws IOException {
|
||||
public static PrintWriter openUTF8Writer(String dir, String filename) throws IOException {
|
||||
return openWriter(dir,filename,"UTF-8");
|
||||
}
|
||||
|
||||
public static PrintWriter openWriter(String dir, String filename, String encoding) throws IOException {
|
||||
File file = new File(dir + filename);
|
||||
if (shower != null) {
|
||||
shower.println("Creating File: "
|
||||
if (log != null) {
|
||||
log.println("Creating File: "
|
||||
+ file.getCanonicalPath());
|
||||
}
|
||||
//File parent = new File(file.getParent());
|
||||
@ -613,8 +664,26 @@ public class BagFormatter {
|
||||
new BufferedWriter(
|
||||
new OutputStreamWriter(
|
||||
new FileOutputStream(file),
|
||||
"UTF-8"),
|
||||
encoding),
|
||||
4*1024));
|
||||
}
|
||||
public static PrintWriter getLog() {
|
||||
return log;
|
||||
}
|
||||
public static void setLog(PrintWriter writer) {
|
||||
log = writer;
|
||||
}
|
||||
public String getSeparator() {
|
||||
return separator;
|
||||
}
|
||||
public void setSeparator(String string) {
|
||||
separator = string;
|
||||
}
|
||||
public Transliterator getShowLiteral() {
|
||||
return showLiteral;
|
||||
}
|
||||
public void setShowLiteral(Transliterator transliterator) {
|
||||
showLiteral = transliterator;
|
||||
}
|
||||
|
||||
}
|
@ -5,8 +5,8 @@
|
||||
*******************************************************************************
|
||||
*
|
||||
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/util/TestBagFormatter.java,v $
|
||||
* $Date: 2003/12/20 03:06:54 $
|
||||
* $Revision: 1.4 $
|
||||
* $Date: 2003/12/29 19:48:57 $
|
||||
* $Revision: 1.5 $
|
||||
*
|
||||
*****************************************************************************************
|
||||
*/
|
||||
@ -62,9 +62,9 @@ public class TestBagFormatter {
|
||||
BagFormatter bf = new BagFormatter();
|
||||
|
||||
UnicodeSet us = new UnicodeSet("[:numeric_value=2:]");
|
||||
System.out.println(bf.showSetNames("[:numeric_value=2:]", us));
|
||||
bf.showSetNames(bf.CONSOLE,"[:numeric_value=2:]", us);
|
||||
us = new UnicodeSet("[:numeric_type=numeric:]");
|
||||
System.out.println(bf.showSetNames("[:numeric_type=numeric:]", us));
|
||||
bf.showSetNames(bf.CONSOLE,"[:numeric_type=numeric:]", us);
|
||||
|
||||
if (true) return;
|
||||
//showNames("Name", ".*MARK.*");
|
||||
@ -121,7 +121,7 @@ public class TestBagFormatter {
|
||||
);
|
||||
}
|
||||
//CollectionFormatter cf = new CollectionFormatter();
|
||||
PrintWriter pw = BagFormatter.openUTF8Writer("", "countries.txt", BagFormatter.CONSOLE);
|
||||
PrintWriter pw = BagFormatter.openUTF8Writer("", "countries.txt");
|
||||
Iterator it = s.iterator();
|
||||
while (it.hasNext()) {
|
||||
pw.println(it.next());
|
||||
|
@ -6,14 +6,13 @@
|
||||
*******************************************************************************
|
||||
*
|
||||
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/util/UnicodePropertySource.java,v $
|
||||
* $Date: 2003/12/20 03:06:53 $
|
||||
* $Revision: 1.4 $
|
||||
* $Date: 2003/12/29 19:48:57 $
|
||||
* $Revision: 1.5 $
|
||||
*
|
||||
*****************************************************************************************
|
||||
*/
|
||||
package com.ibm.icu.dev.test.util;
|
||||
|
||||
//import java.util.regex.*;
|
||||
import java.util.Set;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
@ -88,7 +87,6 @@ public abstract class UnicodePropertySource implements Cloneable {
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
public UnicodeSet getPropertySet(String propertyValue, UnicodeSet result){
|
||||
if (result == null) result = new UnicodeSet();
|
||||
matchIterator.reset();
|
||||
@ -108,14 +106,16 @@ public abstract class UnicodePropertySource implements Cloneable {
|
||||
String value = filter.remap(getPropertyValue(matchIterator.codepoint));
|
||||
if (value == null)
|
||||
continue;
|
||||
matcher.reset(value);
|
||||
if (matcher.matches()) {
|
||||
if (matcher.matches(value)) {
|
||||
result.add(matchIterator.codepoint);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
*/
|
||||
|
||||
public interface Matcher {
|
||||
public boolean matches(String value);
|
||||
}
|
||||
|
||||
public int getNameChoice() {
|
||||
return nameChoice;
|
||||
|
Loading…
Reference in New Issue
Block a user