ICU-4162 fix transliterator failure

X-SVN-Rev: 16772
This commit is contained in:
Ram Viswanadha 2004-11-05 18:36:24 +00:00
parent c6e2c61f27
commit 274e90e66c
3 changed files with 96 additions and 78 deletions

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:d7c831175432e50f5a39a26adb8981cc32e3479a7a2887a29d96d02a05f46e56
size 2042601
oid sha256:f72be5fd8823056a1df406e1793bff3518d4c09e189cf9115f07ecc5da89d564
size 2042595

View File

@ -6,7 +6,6 @@
*/
package com.ibm.icu.text;
import com.ibm.icu.impl.data.ResourceReader;
import com.ibm.icu.impl.ICUResourceBundle;
import com.ibm.icu.impl.Utility;
import com.ibm.icu.impl.UtilityExtensions;
@ -1751,78 +1750,86 @@ public abstract class Transliterator {
String target) {
return registry.getAvailableVariants(source, target);
}
private static final String INDEX = "index",
RB_RULE_BASED_IDS ="RuleBasedTransliteratorIDs";
static {
registry = new TransliteratorRegistry();
// The display name cache starts out empty
displayNameCache = new Hashtable();
/* The following code parses the index table located in
* icu/data/translit/root.txt. The index is an n x 4 table
* that follows this format:
* <id>{
* file{
* resource{"<resource>"}
* direction{"<direction>"}
* }
* }
* <id>{
* internal{
* resource{"<resource>"}
* direction{"<direction"}
* }
* }
* <id>{
* alias{"<getInstanceArg"}
* }
* <id> is the ID of the system transliterator being defined. These
* are public IDs enumerated by Transliterator.getAvailableIDs(),
* unless the second field is "internal".
*
* <resource> is a ResourceReader resource name. Currently these refer
* to file names under com/ibm/text/resources. This string is passed
* directly to ResourceReader, together with <encoding>.
*
* <direction> is either "FORWARD" or "REVERSE".
*
* <getInstanceArg> is a string to be passed directly to
* Transliterator.getInstance(). The returned Transliterator object
* then has its ID changed to <id> and is returned.
*
* The extra blank field on "alias" lines is to make the array square.
*/
ICUResourceBundle bundle, transIDs, colBund;
bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_TRANSLIT_BASE_NAME, INDEX);
transIDs = bundle.get(RB_RULE_BASED_IDS);
// Read the index file and populate the registry. Each line
// of the index file is either blank, a '#' comment, or a
// colon-delimited line. In the latter case the format is one
// of the following:
//# <id>:file:<resource>:<encoding>:<direction>
//# <id>:internal:<resource>:<encoding>:<direction>
//# <id>:alias:<getInstanceArg>
ResourceReader r = new ResourceReader("Transliterator_index.txt");
for (;;) {
String line = null;
try {
line = r.readLine();
if (line == null) {
int row, maxRows;
maxRows = transIDs.getSize();
for (row = 0; row < maxRows; row++) {
colBund = transIDs.get(row);
String ID = colBund.getKey();
ICUResourceBundle res = colBund.get(0);
String type = res.getKey();
if (type.equals("file") || type.equals("internal")) {
// Rest of line is <resource>:<encoding>:<direction>
// pos colon c2
String resString = res.getString("resource");
int dir;
String direction = res.getString("direction");
switch (direction.charAt(0)) {
case 'F':
dir = FORWARD;
break;
case 'R':
dir = REVERSE;
break;
default:
throw new RuntimeException("Can't parse direction: " + direction);
}
// Skip over whitespace
int pos = 0;
while (pos < line.length() &&
UCharacterProperty.isRuleWhiteSpace(line.charAt(pos))) {
++pos;
}
// Ignore blank lines and comments
if (pos == line.length() || line.charAt(pos) == '#') {
continue;
}
// Parse colon-delimited line
int colon = line.indexOf(':', pos);
String ID = line.substring(pos, colon);
pos = colon+1;
colon = line.indexOf(':', pos);
String type = line.substring(pos, colon);
pos = colon+1;
if (type.equals("file") || type.equals("internal")) {
// Rest of line is <resource>:<encoding>:<direction>
// pos colon c2
colon = line.indexOf(':', pos);
int c2 = line.indexOf(':', colon+1);
int dir;
switch (line.charAt(c2+1)) {
case 'F':
dir = FORWARD;
break;
case 'R':
dir = REVERSE;
break;
default:
throw new RuntimeException("Can't parse line: " + line);
}
registry.put(ID,
line.substring(pos, colon), // resource
line.substring(colon+1, c2), // encoding
dir,
!type.equals("internal"));
} else if (type.equals("alias")) {
// Rest of line is the <getInstanceArg>
registry.put(ID, line.substring(pos), true);
} else {
// Unknown type
throw new RuntimeException("Can't parse line: " + line);
}
} catch (StringIndexOutOfBoundsException e) {
throw new RuntimeException("Can't parse line: " + line);
} catch (java.io.IOException e2) {
throw new RuntimeException("Can't read Transliterator_index.txt");
registry.put(ID,
resString, // resource
"UTF-16", // encoding
dir,
!type.equals("internal"));
} else if (type.equals("alias")) {
//'alias'; row[2]=createInstance argument
String resString = res.getString();
registry.put(ID, resString, true);
} else {
// Unknown type
throw new RuntimeException("Unknow type: " + type);
}
}

View File

@ -13,12 +13,11 @@ package com.ibm.icu.text;
//import com.ibm.icu.impl.ICULocaleData;
import com.ibm.icu.impl.ICUResourceBundle;
import com.ibm.icu.impl.LocaleUtility;
import com.ibm.icu.impl.data.ResourceReader;
import com.ibm.icu.lang.UScript;
import com.ibm.icu.util.CaseInsensitiveString;
import com.ibm.icu.util.UResourceBundle;
import java.io.UnsupportedEncodingException;
import java.io.*;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Locale;
@ -211,11 +210,11 @@ class TransliteratorRegistry {
//----------------------------------------------------------------------
static class ResourceEntry {
public String resourceName;
public String resource;
public String encoding;
public int direction;
public ResourceEntry(String n, String enc, int d) {
resourceName = n;
resource = n;
encoding = enc;
direction = d;
}
@ -832,16 +831,28 @@ class TransliteratorRegistry {
TransliteratorParser parser = new TransliteratorParser();
try {
ResourceEntry re = (ResourceEntry) entry;
ResourceReader r = null;
try {
r = new ResourceReader(re.resourceName, re.encoding);
} catch (UnsupportedEncodingException e) {
// This should never happen; UTF8 is always supported
throw new RuntimeException(e.getMessage());
/*
ResourceReader r = null;
try {
r = new ResourceReader(re.resourceName, re.encoding);
} catch (UnsupportedEncodingException e) {
// This should never happen; UTF8 is always supported
throw new RuntimeException(e.getMessage());
}
*/
OutputStreamWriter writer = new OutputStreamWriter( new FileOutputStream("\\work\\temp\\out.txt"), "UTF-8");
writer.write(re.resource);
writer.flush();
writer.close();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
parser.parse(r, re.direction);
parser.parse(re.resource, re.direction);
} catch (ClassCastException e) {
// If we pull a rule from a locale resource bundle it will
// be a LocaleEntry.