ICU-1566 make getDisplayName() grok variants

X-SVN-Rev: 7182
This commit is contained in:
Alan Liu 2001-11-29 23:01:51 +00:00
parent cbfc4b4c7a
commit 04d996482a
4 changed files with 232 additions and 270 deletions

View File

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/translit/TransliteratorTest.java,v $
* $Date: 2001/11/29 18:58:33 $
* $Revision: 1.86 $
* $Date: 2001/11/29 23:01:51 $
* $Revision: 1.87 $
*
*****************************************************************************************
*/
@ -323,7 +323,7 @@ public class TransliteratorTest extends TestFmwk {
log = new StringBuffer(s.toString() + " => ");
t.finishTransliteration(s, index);
}
formatInput(log, s, index);
Utility.formatInput(log, s, index);
if (s.toString().equals(DATA[i+1])) {
logln(log.toString());
} else {
@ -2126,6 +2126,46 @@ public class TransliteratorTest extends TestFmwk {
"\\x{41}\\x{10BEEF}\\x{FEED}");
}
/**
* Make sure display names of variants look reasonable.
*/
public void TestDisplayName() {
String DATA[] = {
// ID, forward name, reverse name
// Update the text as necessary -- the important thing is
// not the text itself, but how various cases are handled.
// Basic test
"Any-Hex", "Any to Hex Escape", "Hex Escape to Any",
// Variants
"Any-Hex/Perl", "Any to Hex Escape/Perl", "Hex Escape to Any/Perl",
// Target-only IDs
"NFC", "Any to NFC", "Any to NFD",
};
Locale US = Locale.US;
for (int i=0; i<DATA.length; i+=3) {
String name = Transliterator.getDisplayName(DATA[i], US);
if (!name.equals(DATA[i+1])) {
errln("FAIL: " + DATA[i] + ".getDisplayName() => " +
name + ", expected " + DATA[i+1]);
} else {
logln("Ok: " + DATA[i] + ".getDisplayName() => " + name);
}
Transliterator t = Transliterator.getInstance(DATA[i], Transliterator.REVERSE);
name = Transliterator.getDisplayName(t.getID(), US);
if (!name.equals(DATA[i+2])) {
errln("FAIL: " + t.getID() + ".getDisplayName() => " +
name + ", expected " + DATA[i+2]);
} else {
logln("Ok: " + t.getID() + ".getDisplayName() => " + name);
}
}
}
//======================================================================
// icu4j ONLY
// These tests are not mirrored (yet) in icu4c at
@ -2282,7 +2322,6 @@ public class TransliteratorTest extends TestFmwk {
}
return test.toString();
}
//======================================================================
// Support methods
@ -2356,16 +2395,16 @@ public class TransliteratorTest extends TestFmwk {
rsource.replace(0, rsource.length(), "");
if (pos != null) {
rsource.replace(0, 0, source);
v.add(formatInput(rsource, index));
v.add(Utility.formatInput(rsource, index));
t.transliterate(rsource, index);
v.add(formatInput(rsource, index));
v.add(Utility.formatInput(rsource, index));
} else {
for (int i=0; i<source.length(); ++i) {
//v.add(i == 0 ? "" : " + " + source.charAt(i) + "");
//log.append(source.charAt(i)).append(" -> "));
t.transliterate(rsource, index, source.charAt(i));
//v.add(formatInput(rsource, index) + source.substring(i+1));
v.add(formatInput(rsource, index) +
//v.add(Utility.formatInput(rsource, index) + source.substring(i+1));
v.add(Utility.formatInput(rsource, index) +
((i<source.length()-1)?(" + '" + source.charAt(i+1) + "' ->"):" =>"));
}
}
@ -2385,46 +2424,6 @@ public class TransliteratorTest extends TestFmwk {
expectedResult);
}
String formatInput(final ReplaceableString input,
final Transliterator.Position pos) {
return formatInput(new StringBuffer(), input, pos).toString();
}
/**
* @param appendTo result is appended to this param.
* @param input the string being transliterated
* @param pos the index struct
*/
StringBuffer formatInput(StringBuffer appendTo,
final ReplaceableString input,
final Transliterator.Position pos) {
// Output a string of the form aaa{bbb|ccc|ddd}eee, where
// the {} indicate the context start and limit, and the ||
// indicate the start and limit.
if (0 <= pos.contextStart &&
pos.contextStart <= pos.start &&
pos.start <= pos.limit &&
pos.limit <= pos.contextLimit &&
pos.contextLimit <= input.length()) {
String a, b, c, d, e;
a = input.substring(0, pos.contextStart);
b = input.substring(pos.contextStart, pos.start);
c = input.substring(pos.start, pos.limit);
d = input.substring(pos.limit, pos.contextLimit);
e = input.substring(pos.contextLimit, input.length());
appendTo.append(a).append('{').append(b).
append('|').append(c).append('|').append(d).
append('}').append(e);
} else {
appendTo.append("INVALID Transliterator.Position {cs=" +
pos.contextStart + ", s=" + pos.start + ", l=" +
pos.limit + ", cl=" + pos.contextLimit + "} on " +
input);
}
return appendTo;
}
boolean expectAux(String tag, String source,
String result, String expectedResult) {
return expectAux(tag, new String[] {source, result},

View File

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/Transliterator.java,v $
* $Date: 2001/11/29 22:31:18 $
* $Revision: 1.70 $
* $Date: 2001/11/29 22:59:49 $
* $Revision: 1.71 $
*
*****************************************************************************************
*/
@ -242,7 +242,7 @@ import com.ibm.util.Utility;
* <p>Copyright &copy; IBM Corporation 1999. All rights reserved.
*
* @author Alan Liu
* @version $RCSfile: Transliterator.java,v $ $Revision: 1.70 $ $Date: 2001/11/29 22:31:18 $
* @version $RCSfile: Transliterator.java,v $ $Revision: 1.71 $ $Date: 2001/11/29 22:59:49 $
*/
public abstract class Transliterator {
/**
@ -1090,10 +1090,18 @@ public abstract class Transliterator {
* localized.
* @see java.text.MessageFormat
*/
public static String getDisplayName(String ID, Locale inLocale) {
public static String getDisplayName(String id, Locale inLocale) {
ResourceBundle bundle = ResourceBundle.getBundle(
RB_LOCALE_ELEMENTS, inLocale);
// Normalize the ID
String stv[] = IDtoSTV(id);
if (stv[1].length() < 1) {
// No target; malformed id
return "";
}
String ID = stv[0] + '-' + stv[1] + stv[2];
// Use the registered display name, if any
String n = (String) displayNameCache.get(new CaseInsensitiveString(ID));
if (n != null) {
@ -1112,14 +1120,10 @@ public abstract class Transliterator {
MessageFormat format = new MessageFormat(
bundle.getString(RB_DISPLAY_NAME_PATTERN));
// Construct the argument array
int i = ID.indexOf('-');
Object[] args = (i < 0)
? new Object[] { new Integer(1), ID }
: new Object[] { new Integer(2), ID.substring(0, i),
ID.substring(i+1) };
Object[] args = new Object[] { new Integer(2), stv[0], stv[1] };
// Use display names for the scripts, if they exist
for (int j=1; j<=((i<0)?1:2); ++j) {
for (int j=1; j<=2; ++j) {
try {
args[j] = bundle.getString(RB_SCRIPT_DISPLAY_NAME_PREFIX +
(String) args[j]);
@ -1127,7 +1131,7 @@ public abstract class Transliterator {
}
// Format it using the pattern in the resource
return format.format(args);
return format.format(args) + stv[2];
} catch (MissingResourceException e2) {}
// We should not reach this point unless there is something
@ -1388,50 +1392,55 @@ public abstract class Transliterator {
}
}
// /**
// * Parse an ID into pieces. Take IDs of the form T, T/V, S-T, S-T/V, or S/V-T.
// * If the source is missing, return a source of ANY.
// * @param id the id string, in any of several forms
// * @return an array of 4 strings: source, target, variant, and isSourcePresent.
// * Source and
// */
// static String[] IDtoSTV(String id) {
// String source = ANY;
// String target = null;
// String variant = ""; // Variant INCLUDING "/"
// int sep = id.indexOf(ID_SEP);
// int var = id.indexOf(VARIANT_SEP);
// if (var < 0) {
// var = id.length();
// }
// boolean isSourcePresent = false;
//
// if (sep < 0) {
// // Form: T/V or T (or /V)
// target = id.substring(0, var);
// variant = id.substring(var);
// } else if (sep < var) {
// // Form: S-T/V or S-T (or -T/V or -T)
// if (sep > 0) {
// source = id.substring(0, sep);
// isSourcePresent = true;
// }
// target = id.substring(++sep, var);
// variant = id.substring(var);
// } else {
// // Form: (S/V-T or /V-T)
// if (var > 0) {
// source = id.substring(0, var);
// isSourcePresent = true;
// }
// variant = id.substring(var, sep++);
// target = id.substring(sep);
// }
// return new String[] { source, target, variant,
// isSourcePresent ? "1" : "" };
// }
/**
* Parse an ID into pieces. Take IDs of the form T, T/V, S-T,
* S-T/V, or S/V-T. If the source is missing, return a source of
* ANY.
* @param id the id string, in any of several forms
* @return an array of 4 strings: source, target, variant, and
* isSourcePresent. If the source is not present, ANY will be
* given as the source, and isSourcePresent will be "". Otherwise
* isSourcePresent will be != "". The target may be empty if the
* id is not well-formed. The variant may be empty; if it is not,
* it will contain a leading '/'.
*/
static String[] IDtoSTV(String id) {
String source = ANY;
String target = null;
String variant = ""; // Variant INCLUDING "/"
int sep = id.indexOf(ID_SEP);
int var = id.indexOf(VARIANT_SEP);
if (var < 0) {
var = id.length();
}
boolean isSourcePresent = false;
if (sep < 0) {
// Form: T/V or T (or /V)
target = id.substring(0, var);
variant = id.substring(var);
} else if (sep < var) {
// Form: S-T/V or S-T (or -T/V or -T)
if (sep > 0) {
source = id.substring(0, sep);
isSourcePresent = true;
}
target = id.substring(++sep, var);
variant = id.substring(var);
} else {
// Form: (S/V-T or /V-T)
if (var > 0) {
source = id.substring(0, var);
isSourcePresent = true;
}
variant = id.substring(var, sep++);
target = id.substring(sep);
}
return new String[] { source, target, variant,
isSourcePresent ? "1" : "" };
}
/**
* Parse a single ID, possibly including an inline filter, and return
@ -1563,39 +1572,12 @@ public abstract class Transliterator {
// produces T-S/V, with a default S of "Any". If the ID has a special
// non-canonical inverse, look it up (e.g., NFC -> NFD, Null -> Null).
if (id.length() > 0) { // We handle empty IDs below
String source = ANY;
String target = null;
String variant = ""; // Variant INCLUDING "/"
String[] stv = IDtoSTV(id.toString());
String source = stv[0];
String target = stv[1];
String variant = stv[2];
boolean isSourcePresent = stv[3].length() != 0;
String idSTR = id.toString();
int sep = idSTR.indexOf(ID_SEP);
int var = idSTR.indexOf(VARIANT_SEP);
if (var < 0) {
var = id.length();
}
boolean isSourcePresent = false;
if (sep < 0) {
// Form: T/V or T (or /V)
target = id.substring(0, var);
variant = id.substring(var);
} else if (sep < var) {
// Form: S-T/V or S-T (or -T/V or -T)
if (sep > 0) {
source = id.substring(0, sep);
isSourcePresent = true;
}
target = id.substring(++sep, var);
variant = id.substring(var);
} else {
// Form: (S/V-T or /V-T)
if (var > 0) {
source = id.substring(0, var);
isSourcePresent = true;
}
variant = id.substring(var, sep++);
target = id.substring(sep);
}
id.setLength(0);
// Source and variant may be empty, but target may not be.
if (target.length() == 0) {

View File

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/test/translit/Attic/TransliteratorTest.java,v $
* $Date: 2001/11/29 18:58:33 $
* $Revision: 1.86 $
* $Date: 2001/11/29 23:01:51 $
* $Revision: 1.87 $
*
*****************************************************************************************
*/
@ -323,7 +323,7 @@ public class TransliteratorTest extends TestFmwk {
log = new StringBuffer(s.toString() + " => ");
t.finishTransliteration(s, index);
}
formatInput(log, s, index);
Utility.formatInput(log, s, index);
if (s.toString().equals(DATA[i+1])) {
logln(log.toString());
} else {
@ -2126,6 +2126,46 @@ public class TransliteratorTest extends TestFmwk {
"\\x{41}\\x{10BEEF}\\x{FEED}");
}
/**
* Make sure display names of variants look reasonable.
*/
public void TestDisplayName() {
String DATA[] = {
// ID, forward name, reverse name
// Update the text as necessary -- the important thing is
// not the text itself, but how various cases are handled.
// Basic test
"Any-Hex", "Any to Hex Escape", "Hex Escape to Any",
// Variants
"Any-Hex/Perl", "Any to Hex Escape/Perl", "Hex Escape to Any/Perl",
// Target-only IDs
"NFC", "Any to NFC", "Any to NFD",
};
Locale US = Locale.US;
for (int i=0; i<DATA.length; i+=3) {
String name = Transliterator.getDisplayName(DATA[i], US);
if (!name.equals(DATA[i+1])) {
errln("FAIL: " + DATA[i] + ".getDisplayName() => " +
name + ", expected " + DATA[i+1]);
} else {
logln("Ok: " + DATA[i] + ".getDisplayName() => " + name);
}
Transliterator t = Transliterator.getInstance(DATA[i], Transliterator.REVERSE);
name = Transliterator.getDisplayName(t.getID(), US);
if (!name.equals(DATA[i+2])) {
errln("FAIL: " + t.getID() + ".getDisplayName() => " +
name + ", expected " + DATA[i+2]);
} else {
logln("Ok: " + t.getID() + ".getDisplayName() => " + name);
}
}
}
//======================================================================
// icu4j ONLY
// These tests are not mirrored (yet) in icu4c at
@ -2282,7 +2322,6 @@ public class TransliteratorTest extends TestFmwk {
}
return test.toString();
}
//======================================================================
// Support methods
@ -2356,16 +2395,16 @@ public class TransliteratorTest extends TestFmwk {
rsource.replace(0, rsource.length(), "");
if (pos != null) {
rsource.replace(0, 0, source);
v.add(formatInput(rsource, index));
v.add(Utility.formatInput(rsource, index));
t.transliterate(rsource, index);
v.add(formatInput(rsource, index));
v.add(Utility.formatInput(rsource, index));
} else {
for (int i=0; i<source.length(); ++i) {
//v.add(i == 0 ? "" : " + " + source.charAt(i) + "");
//log.append(source.charAt(i)).append(" -> "));
t.transliterate(rsource, index, source.charAt(i));
//v.add(formatInput(rsource, index) + source.substring(i+1));
v.add(formatInput(rsource, index) +
//v.add(Utility.formatInput(rsource, index) + source.substring(i+1));
v.add(Utility.formatInput(rsource, index) +
((i<source.length()-1)?(" + '" + source.charAt(i+1) + "' ->"):" =>"));
}
}
@ -2385,46 +2424,6 @@ public class TransliteratorTest extends TestFmwk {
expectedResult);
}
String formatInput(final ReplaceableString input,
final Transliterator.Position pos) {
return formatInput(new StringBuffer(), input, pos).toString();
}
/**
* @param appendTo result is appended to this param.
* @param input the string being transliterated
* @param pos the index struct
*/
StringBuffer formatInput(StringBuffer appendTo,
final ReplaceableString input,
final Transliterator.Position pos) {
// Output a string of the form aaa{bbb|ccc|ddd}eee, where
// the {} indicate the context start and limit, and the ||
// indicate the start and limit.
if (0 <= pos.contextStart &&
pos.contextStart <= pos.start &&
pos.start <= pos.limit &&
pos.limit <= pos.contextLimit &&
pos.contextLimit <= input.length()) {
String a, b, c, d, e;
a = input.substring(0, pos.contextStart);
b = input.substring(pos.contextStart, pos.start);
c = input.substring(pos.start, pos.limit);
d = input.substring(pos.limit, pos.contextLimit);
e = input.substring(pos.contextLimit, input.length());
appendTo.append(a).append('{').append(b).
append('|').append(c).append('|').append(d).
append('}').append(e);
} else {
appendTo.append("INVALID Transliterator.Position {cs=" +
pos.contextStart + ", s=" + pos.start + ", l=" +
pos.limit + ", cl=" + pos.contextLimit + "} on " +
input);
}
return appendTo;
}
boolean expectAux(String tag, String source,
String result, String expectedResult) {
return expectAux(tag, new String[] {source, result},

View File

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/text/Attic/Transliterator.java,v $
* $Date: 2001/11/29 22:31:18 $
* $Revision: 1.70 $
* $Date: 2001/11/29 22:59:49 $
* $Revision: 1.71 $
*
*****************************************************************************************
*/
@ -242,7 +242,7 @@ import com.ibm.util.Utility;
* <p>Copyright &copy; IBM Corporation 1999. All rights reserved.
*
* @author Alan Liu
* @version $RCSfile: Transliterator.java,v $ $Revision: 1.70 $ $Date: 2001/11/29 22:31:18 $
* @version $RCSfile: Transliterator.java,v $ $Revision: 1.71 $ $Date: 2001/11/29 22:59:49 $
*/
public abstract class Transliterator {
/**
@ -1090,10 +1090,18 @@ public abstract class Transliterator {
* localized.
* @see java.text.MessageFormat
*/
public static String getDisplayName(String ID, Locale inLocale) {
public static String getDisplayName(String id, Locale inLocale) {
ResourceBundle bundle = ResourceBundle.getBundle(
RB_LOCALE_ELEMENTS, inLocale);
// Normalize the ID
String stv[] = IDtoSTV(id);
if (stv[1].length() < 1) {
// No target; malformed id
return "";
}
String ID = stv[0] + '-' + stv[1] + stv[2];
// Use the registered display name, if any
String n = (String) displayNameCache.get(new CaseInsensitiveString(ID));
if (n != null) {
@ -1112,14 +1120,10 @@ public abstract class Transliterator {
MessageFormat format = new MessageFormat(
bundle.getString(RB_DISPLAY_NAME_PATTERN));
// Construct the argument array
int i = ID.indexOf('-');
Object[] args = (i < 0)
? new Object[] { new Integer(1), ID }
: new Object[] { new Integer(2), ID.substring(0, i),
ID.substring(i+1) };
Object[] args = new Object[] { new Integer(2), stv[0], stv[1] };
// Use display names for the scripts, if they exist
for (int j=1; j<=((i<0)?1:2); ++j) {
for (int j=1; j<=2; ++j) {
try {
args[j] = bundle.getString(RB_SCRIPT_DISPLAY_NAME_PREFIX +
(String) args[j]);
@ -1127,7 +1131,7 @@ public abstract class Transliterator {
}
// Format it using the pattern in the resource
return format.format(args);
return format.format(args) + stv[2];
} catch (MissingResourceException e2) {}
// We should not reach this point unless there is something
@ -1388,50 +1392,55 @@ public abstract class Transliterator {
}
}
// /**
// * Parse an ID into pieces. Take IDs of the form T, T/V, S-T, S-T/V, or S/V-T.
// * If the source is missing, return a source of ANY.
// * @param id the id string, in any of several forms
// * @return an array of 4 strings: source, target, variant, and isSourcePresent.
// * Source and
// */
// static String[] IDtoSTV(String id) {
// String source = ANY;
// String target = null;
// String variant = ""; // Variant INCLUDING "/"
// int sep = id.indexOf(ID_SEP);
// int var = id.indexOf(VARIANT_SEP);
// if (var < 0) {
// var = id.length();
// }
// boolean isSourcePresent = false;
//
// if (sep < 0) {
// // Form: T/V or T (or /V)
// target = id.substring(0, var);
// variant = id.substring(var);
// } else if (sep < var) {
// // Form: S-T/V or S-T (or -T/V or -T)
// if (sep > 0) {
// source = id.substring(0, sep);
// isSourcePresent = true;
// }
// target = id.substring(++sep, var);
// variant = id.substring(var);
// } else {
// // Form: (S/V-T or /V-T)
// if (var > 0) {
// source = id.substring(0, var);
// isSourcePresent = true;
// }
// variant = id.substring(var, sep++);
// target = id.substring(sep);
// }
// return new String[] { source, target, variant,
// isSourcePresent ? "1" : "" };
// }
/**
* Parse an ID into pieces. Take IDs of the form T, T/V, S-T,
* S-T/V, or S/V-T. If the source is missing, return a source of
* ANY.
* @param id the id string, in any of several forms
* @return an array of 4 strings: source, target, variant, and
* isSourcePresent. If the source is not present, ANY will be
* given as the source, and isSourcePresent will be "". Otherwise
* isSourcePresent will be != "". The target may be empty if the
* id is not well-formed. The variant may be empty; if it is not,
* it will contain a leading '/'.
*/
static String[] IDtoSTV(String id) {
String source = ANY;
String target = null;
String variant = ""; // Variant INCLUDING "/"
int sep = id.indexOf(ID_SEP);
int var = id.indexOf(VARIANT_SEP);
if (var < 0) {
var = id.length();
}
boolean isSourcePresent = false;
if (sep < 0) {
// Form: T/V or T (or /V)
target = id.substring(0, var);
variant = id.substring(var);
} else if (sep < var) {
// Form: S-T/V or S-T (or -T/V or -T)
if (sep > 0) {
source = id.substring(0, sep);
isSourcePresent = true;
}
target = id.substring(++sep, var);
variant = id.substring(var);
} else {
// Form: (S/V-T or /V-T)
if (var > 0) {
source = id.substring(0, var);
isSourcePresent = true;
}
variant = id.substring(var, sep++);
target = id.substring(sep);
}
return new String[] { source, target, variant,
isSourcePresent ? "1" : "" };
}
/**
* Parse a single ID, possibly including an inline filter, and return
@ -1563,39 +1572,12 @@ public abstract class Transliterator {
// produces T-S/V, with a default S of "Any". If the ID has a special
// non-canonical inverse, look it up (e.g., NFC -> NFD, Null -> Null).
if (id.length() > 0) { // We handle empty IDs below
String source = ANY;
String target = null;
String variant = ""; // Variant INCLUDING "/"
String[] stv = IDtoSTV(id.toString());
String source = stv[0];
String target = stv[1];
String variant = stv[2];
boolean isSourcePresent = stv[3].length() != 0;
String idSTR = id.toString();
int sep = idSTR.indexOf(ID_SEP);
int var = idSTR.indexOf(VARIANT_SEP);
if (var < 0) {
var = id.length();
}
boolean isSourcePresent = false;
if (sep < 0) {
// Form: T/V or T (or /V)
target = id.substring(0, var);
variant = id.substring(var);
} else if (sep < var) {
// Form: S-T/V or S-T (or -T/V or -T)
if (sep > 0) {
source = id.substring(0, sep);
isSourcePresent = true;
}
target = id.substring(++sep, var);
variant = id.substring(var);
} else {
// Form: (S/V-T or /V-T)
if (var > 0) {
source = id.substring(0, var);
isSourcePresent = true;
}
variant = id.substring(var, sep++);
target = id.substring(sep);
}
id.setLength(0);
// Source and variant may be empty, but target may not be.
if (target.length() == 0) {