ICU-11479 do not ask the Java system what the canonical name should be, just figure it out from ICU data

X-SVN-Rev: 36982
This commit is contained in:
Markus Scherer 2015-01-22 20:40:30 +00:00
parent 7dff411df7
commit 0acf60d3e0
2 changed files with 5 additions and 86 deletions

View file

@ -12,11 +12,9 @@ import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.nio.charset.spi.CharsetProvider;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import com.ibm.icu.impl.InvalidFormatException;
@ -29,50 +27,10 @@ import com.ibm.icu.impl.InvalidFormatException;
public final class CharsetProviderICU extends CharsetProvider{
/**
* List of available ICU Charsets, empty during static initialization.
* Not a Set or Map, so that we can add different Charset objects with the same name(),
* which means that they are .equals(). See ICU ticket #11493.
*/
private static List<Charset> icuCharsets = Collections.<Charset>emptyList();
/**
* Maps uppercased Java charset names and aliases to canonical Java charset names.
*/
private static final Map<String, String> javaNamesMap = new HashMap<String, String>();
static {
// This loop will exclude ICU charsets because Charset.availableCharsets() calls
// our charsets() which returns an empty iterator
// until we have tried to open all of the ICU charsets and built icuCharsets.
// We can only open ICU charsets when we have the javaNamesMap,
// for getting the Java canonical name.
for (Map.Entry<String, Charset> nameAndCharset : Charset.availableCharsets().entrySet()) {
String canonicalName = nameAndCharset.getKey();
javaNamesMap.put(ASCII.toUpperCase(canonicalName), canonicalName);
for (String alias : nameAndCharset.getValue().aliases()) {
javaNamesMap.put(ASCII.toUpperCase(alias), canonicalName);
}
}
}
/**
* Simpler/faster methods for ASCII than ones based on Unicode data.
* TODO: There should be code like this somewhere already??
*/
private static final class ASCII {
static String toUpperCase(String s) {
for (int i = 0; i < s.length(); ++i) {
char c = s.charAt(i);
if ('a' <= c && c <= 'z') {
StringBuilder sb = new StringBuilder(s.length());
sb.append(s, 0, i).append((char)(c - 0x20));
while (++i < s.length()) {
c = s.charAt(i);
if ('a' <= c && c <= 'z') { c = (char)(c - 0x20); }
sb.append(c);
}
return sb.toString();
}
}
return s;
}
}
/**
* Default constructor
@ -259,20 +217,6 @@ public final class CharsetProviderICU extends CharsetProvider{
cName = "x-"+ name;
}
}
/* After getting the Java canonical name from the ICU alias table, get the
* Java canonical name from the current JDK. This is necessary because
* different versions of the JVM (Sun and IBM) may have a different
* canonical name than the one given by ICU. So the Java canonical name
* will depend on the current JVM. Since Java cannot use the ICU canonical name
* we have to try to use a Java compatible name.
*/
if (cName != null) {
String testName = javaNamesMap.get(ASCII.toUpperCase(cName));
if (testName != null && !testName.equals(cName) &&
getICUCanonicalName(testName).length() > 0) {
cName = testName;
}
}
return cName;
}catch (IOException ex){
@ -299,7 +243,7 @@ public final class CharsetProviderICU extends CharsetProvider{
aliasNum = UConverterAlias.countAliases(encName);
for(i=0,j=0;i<aliasNum;i++){
String name = UConverterAlias.getAlias(encName,i);
if(name.indexOf('+')==-1 && name.indexOf(',')==-1){
if(name.indexOf(',')==-1){
aliasArray[j++]= name;
}
}
@ -319,9 +263,7 @@ public final class CharsetProviderICU extends CharsetProvider{
* were cheap enough. See ICU ticket #11481.
*/
private static final synchronized void loadAvailableICUCharsets() {
// The Java names Map is empty during static initialization when we are
// just about to build it.
if (!icuCharsets.isEmpty() || javaNamesMap.isEmpty()) {
if (!icuCharsets.isEmpty()) {
return;
}
List<Charset> icucs = new LinkedList<Charset>();

View file

@ -1610,30 +1610,7 @@ public class TestCharset extends TestFmwk {
}
logln("Total Number of chasets = " + map.size());
}
/* ticket 5580 */
public void TestJavaCanonicalNameOnAvailableCharsets() {
CharsetProviderICU provider = new CharsetProviderICU();
Iterator allCharsets = provider.charsets();
String errorMessage = null;
while (allCharsets.hasNext()) {
Charset _chset = (Charset)allCharsets.next();
Charset chset = Charset.forName(_chset.name());
if (!chset.name().equals(_chset.name())) {
if (errorMessage == null) {
errorMessage = new String("Error: Charset.forName( " + _chset.name() + " ) returned " + chset + " instead of " + _chset);
} else {
errorMessage = errorMessage + "\nError: Charset.forName( " + _chset.name() + " ) returned " + chset + " instead of " + _chset;
}
}
}
if (errorMessage != null) {
errln(errorMessage);
}
}
public void TestWindows936(){
CharsetProviderICU icu = new CharsetProviderICU();
Charset cs = icu.charsetForName("windows-936-2000");