diff --git a/.gitattributes b/.gitattributes index 2f8ee3a59d0..d6010debba2 100644 --- a/.gitattributes +++ b/.gitattributes @@ -111,6 +111,7 @@ icu4j/src/com/ibm/icu/dev/tool/docs/icu4j342.api.gz -text icu4j/src/com/ibm/icu/dev/tool/docs/icu4j343.api.gz -text icu4j/src/com/ibm/icu/impl/data/icudata.jar -text icu4j/src/com/ibm/icu/impl/data/th.brk -text +icu4j/src/com/ibm/icu/util/UResourceBundleIterator.java -text icu4j/src/com/ibm/richtext/textapps/resources/unicode.arabic.red -text icu4j/src/com/ibm/richtext/textapps/resources/unicode.hebrew.red -text tools/unicodetools/com/ibm/rbm/docs/images/TitleLogo_transparent.gif -text diff --git a/icu4j/src/com/ibm/icu/dev/test/ResourceModule.java b/icu4j/src/com/ibm/icu/dev/test/ResourceModule.java index 07ace26fb25..46eef9c5d38 100644 --- a/icu4j/src/com/ibm/icu/dev/test/ResourceModule.java +++ b/icu4j/src/com/ibm/icu/dev/test/ResourceModule.java @@ -1,7 +1,7 @@ //##header /* ********************************************************************** - * Copyright (c) 2006, International Business Machines + * Copyright (c) 2006-2007, International Business Machines * Corporation and others. All Rights Reserved. ********************************************************************** * Created on 2006-4-21 @@ -17,8 +17,8 @@ import java.util.MissingResourceException; import java.util.NoSuchElementException; import com.ibm.icu.impl.ICUResourceBundle; -import com.ibm.icu.impl.ICUResourceBundleIterator; import com.ibm.icu.util.UResourceBundle; +import com.ibm.icu.util.UResourceBundleIterator; import com.ibm.icu.util.UResourceTypeMismatchException; /** @@ -71,20 +71,20 @@ class ResourceModule implements TestDataModule { private static final String DATA = "Cases"; - ICUResourceBundle res; - ICUResourceBundle info; - ICUResourceBundle defaultHeader; - ICUResourceBundle testData; + UResourceBundle res; + UResourceBundle info; + UResourceBundle defaultHeader; + UResourceBundle testData; ResourceModule(String baseName, String localeName) throws DataModuleFormatError{ - res = (ICUResourceBundle) UResourceBundle.getBundleInstance(baseName, localeName); - info = getFromTable(res, INFO, ICUResourceBundle.TABLE); - testData = getFromTable(res, TEST_DATA, ICUResourceBundle.TABLE); + res = (UResourceBundle) UResourceBundle.getBundleInstance(baseName, localeName); + info = getFromTable(res, INFO, UResourceBundle.TABLE); + testData = getFromTable(res, TEST_DATA, UResourceBundle.TABLE); try { // unfortunately, actually, data can be either ARRAY or STRING - defaultHeader = getFromTable(info, HEADER, new int[]{ICUResourceBundle.ARRAY, ICUResourceBundle.STRING}); + defaultHeader = getFromTable(info, HEADER, new int[]{UResourceBundle.ARRAY, UResourceBundle.STRING}); } catch (MissingResourceException e){ defaultHeader = null; } @@ -104,35 +104,35 @@ class ResourceModule implements TestDataModule { public Iterator getTestDataIterator() { return new IteratorAdapter(testData){ - protected Object prepareNext(ICUResourceBundle nextRes) throws DataModuleFormatError { + protected Object prepareNext(UResourceBundle nextRes) throws DataModuleFormatError { return new UResourceTestData(defaultHeader, nextRes); } }; } /** - * To make ICUResourceBundleIterator works like Iterator + * To make UResourceBundleIterator works like Iterator * and return various data-driven test object for next() call * * @author Raymond Yang */ private abstract static class IteratorAdapter implements Iterator{ - private ICUResourceBundle res; - private ICUResourceBundleIterator itr; + private UResourceBundle res; + private UResourceBundleIterator itr; private Object preparedNextElement = null; - // fix a strange behavior for ICUResourceBundleIterator for - // ICUResourceBundle.STRING. It support hasNext(), but does + // fix a strange behavior for UResourceBundleIterator for + // UResourceBundle.STRING. It support hasNext(), but does // not support next() now. // // Use the iterated resource itself as the result from next() call private boolean isStrRes = false; private boolean isStrResPrepared = false; // for STRING resouce, we only prepare once - IteratorAdapter(ICUResourceBundle theRes) { + IteratorAdapter(UResourceBundle theRes) { assert_not (theRes == null); res = theRes; - itr = res.getIterator(); - isStrRes = res.getType() == ICUResourceBundle.STRING; + itr = ((ICUResourceBundle)res).getIterator(); + isStrRes = res.getType() == UResourceBundle.STRING; } public void remove() { @@ -163,7 +163,7 @@ class ResourceModule implements TestDataModule { if (isStrRes) return hasNextForStrRes(); if (preparedNextElement != null) return true; - ICUResourceBundle t = null; + UResourceBundle t = null; if (itr.hasNext()) { // Notice, other RuntimeException may be throwed t = itr.next(); @@ -197,7 +197,7 @@ class ResourceModule implements TestDataModule { /** * To prepare data-driven test object for next() call, should not return null */ - abstract protected Object prepareNext(ICUResourceBundle nextRes) throws DataModuleFormatError; + abstract protected Object prepareNext(UResourceBundle nextRes) throws DataModuleFormatError; } @@ -234,13 +234,13 @@ class ResourceModule implements TestDataModule { * silently also. The behavior is modified because some resource are * optional and can be missed. */ - static ICUResourceBundle getFromTable(ICUResourceBundle res, String key, int expResType) throws DataModuleFormatError{ + static UResourceBundle getFromTable(UResourceBundle res, String key, int expResType) throws DataModuleFormatError{ return getFromTable(res, key, new int[]{expResType}); } - static ICUResourceBundle getFromTable(ICUResourceBundle res, String key, int[] expResTypes) throws DataModuleFormatError{ - assert_is (res != null && key != null && res.getType() == ICUResourceBundle.TABLE); - ICUResourceBundle t = res.get(key); + static UResourceBundle getFromTable(UResourceBundle res, String key, int[] expResTypes) throws DataModuleFormatError{ + assert_is (res != null && key != null && res.getType() == UResourceBundle.TABLE); + UResourceBundle t = res.get(key); assert_not (t ==null); int type = t.getType(); @@ -257,21 +257,21 @@ class ResourceModule implements TestDataModule { } /** - * Unfortunately, ICUResourceBundle is unable to treat one string as string array. - * This function return a String[] from ICUResourceBundle, regardless it is an array or a string + * Unfortunately, UResourceBundle is unable to treat one string as string array. + * This function return a String[] from UResourceBundle, regardless it is an array or a string */ - static String[] getStringArrayHelper(ICUResourceBundle res, String key) throws DataModuleFormatError{ - ICUResourceBundle t = getFromTable(res, key, new int[]{ICUResourceBundle.ARRAY, ICUResourceBundle.STRING}); + static String[] getStringArrayHelper(UResourceBundle res, String key) throws DataModuleFormatError{ + UResourceBundle t = getFromTable(res, key, new int[]{UResourceBundle.ARRAY, UResourceBundle.STRING}); return getStringArrayHelper(t); } - static String[] getStringArrayHelper(ICUResourceBundle res) throws DataModuleFormatError{ + static String[] getStringArrayHelper(UResourceBundle res) throws DataModuleFormatError{ try{ int type = res.getType(); switch (type) { - case ICUResourceBundle.ARRAY: + case UResourceBundle.ARRAY: return res.getStringArray(); - case ICUResourceBundle.STRING: + case UResourceBundle.STRING: return new String[]{res.getString()}; default: throw new UResourceTypeMismatchException("Only accept ARRAY and STRING types."); @@ -299,24 +299,24 @@ class ResourceModule implements TestDataModule { } private static class UResourceTestData implements TestData{ - private ICUResourceBundle res; - private ICUResourceBundle info; - private ICUResourceBundle settings; - private ICUResourceBundle header; - private ICUResourceBundle data; + private UResourceBundle res; + private UResourceBundle info; + private UResourceBundle settings; + private UResourceBundle header; + private UResourceBundle data; - UResourceTestData(ICUResourceBundle defaultHeader, ICUResourceBundle theRes) throws DataModuleFormatError{ + UResourceTestData(UResourceBundle defaultHeader, UResourceBundle theRes) throws DataModuleFormatError{ - assert_is (theRes != null && theRes.getType() == ICUResourceBundle.TABLE); + assert_is (theRes != null && theRes.getType() == UResourceBundle.TABLE); res = theRes; // unfortunately, actually, data can be either ARRAY or STRING - data = getFromTable(res, DATA, new int[]{ICUResourceBundle.ARRAY, ICUResourceBundle.STRING}); + data = getFromTable(res, DATA, new int[]{UResourceBundle.ARRAY, UResourceBundle.STRING}); try { // unfortunately, actually, data can be either ARRAY or STRING - header = getFromTable(res, HEADER, new int[]{ICUResourceBundle.ARRAY, ICUResourceBundle.STRING}); + header = getFromTable(res, HEADER, new int[]{UResourceBundle.ARRAY, UResourceBundle.STRING}); } catch (MissingResourceException e){ if (defaultHeader == null) { throw new DataModuleFormatError("Unable to find a header for test data '" + res.getKey() + "' and no default header exist."); @@ -325,8 +325,8 @@ class ResourceModule implements TestDataModule { } } try{ - settings = getFromTable(res, SETTINGS, ICUResourceBundle.ARRAY); - info = getFromTable(res, INFO, ICUResourceBundle.TABLE); + settings = getFromTable(res, SETTINGS, UResourceBundle.ARRAY); + info = getFromTable(res, INFO, UResourceBundle.TABLE); } catch (MissingResourceException e){ // do nothing, left them null; settings = data; @@ -342,9 +342,9 @@ class ResourceModule implements TestDataModule { } public Iterator getSettingsIterator() { - assert_is (settings.getType() == ICUResourceBundle.ARRAY); + assert_is (settings.getType() == UResourceBundle.ARRAY); return new IteratorAdapter(settings){ - protected Object prepareNext(ICUResourceBundle nextRes) throws DataModuleFormatError { + protected Object prepareNext(UResourceBundle nextRes) throws DataModuleFormatError { return new UTableResource(nextRes); } }; @@ -352,10 +352,10 @@ class ResourceModule implements TestDataModule { public Iterator getDataIterator() { // unfortunately, - assert_is (data.getType() == ICUResourceBundle.ARRAY - || data.getType() == ICUResourceBundle.STRING); + assert_is (data.getType() == UResourceBundle.ARRAY + || data.getType() == UResourceBundle.STRING); return new IteratorAdapter(data){ - protected Object prepareNext(ICUResourceBundle nextRes) throws DataModuleFormatError { + protected Object prepareNext(UResourceBundle nextRes) throws DataModuleFormatError { return new UArrayResource(header, nextRes); } }; @@ -363,9 +363,9 @@ class ResourceModule implements TestDataModule { } private static class UTableResource implements DataMap{ - private ICUResourceBundle res; + private UResourceBundle res; - UTableResource(ICUResourceBundle theRes){ + UTableResource(UResourceBundle theRes){ res = theRes; } public String getString(String key) { @@ -385,7 +385,7 @@ class ResourceModule implements TestDataModule { private static class UArrayResource implements DataMap{ private Map theMap; - UArrayResource(ICUResourceBundle theHeader, ICUResourceBundle theData) throws DataModuleFormatError{ + UArrayResource(UResourceBundle theHeader, UResourceBundle theData) throws DataModuleFormatError{ assert_is (theHeader != null && theData != null); String[] header; @@ -394,9 +394,9 @@ class ResourceModule implements TestDataModule { throw new DataModuleFormatError("The count of Header and Data is mismatch."); theMap = new HashMap(); for (int i = 0; i < header.length; i++) { - if(theData.getType()==ICUResourceBundle.ARRAY){ + if(theData.getType()==UResourceBundle.ARRAY){ theMap.put(header[i], theData.get(i)); - }else if(theData.getType()==ICUResourceBundle.STRING){ + }else if(theData.getType()==UResourceBundle.STRING){ theMap.put(header[i], theData.getString()); }else{ throw new DataModuleFormatError("Did not get the expected data!"); diff --git a/icu4j/src/com/ibm/icu/dev/test/format/NumberRegression.java b/icu4j/src/com/ibm/icu/dev/test/format/NumberRegression.java index 5948269a183..27b92d29515 100755 --- a/icu4j/src/com/ibm/icu/dev/test/format/NumberRegression.java +++ b/icu4j/src/com/ibm/icu/dev/test/format/NumberRegression.java @@ -1,7 +1,7 @@ //##header /***************************************************************************************** * - * Copyright (C) 1996-2006, International Business Machines + * Copyright (C) 1996-2007, International Business Machines * Corporation and others. All Rights Reserved. **/ @@ -953,14 +953,14 @@ public class NumberRegression extends com.ibm.icu.dev.test.TestFmwk { Locale[] locales = NumberFormat.getAvailableLocales(); for (int i = 0; i < locales.length; i++) { - ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,locales[i]); + UResourceBundle rb = UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,locales[i]); // // Get the currency pattern for this locale. We have to fish it // out of the ResourceBundle directly, since DecimalFormat.toPattern // will return the localized symbol, not \00a4 // - ICUResourceBundle numPatterns = rb.get("NumberPatterns"); + UResourceBundle numPatterns = rb.get("NumberPatterns"); String pattern = numPatterns.getString(1); if (pattern.indexOf('\u00A4') == -1 ) { // 'x' not "x" -- workaround bug in IBM JDK 1.4.1 diff --git a/icu4j/src/com/ibm/icu/dev/test/perf/ResourceBundlePerf.java b/icu4j/src/com/ibm/icu/dev/test/perf/ResourceBundlePerf.java index 6bae8042737..fe1fbfde065 100644 --- a/icu4j/src/com/ibm/icu/dev/test/perf/ResourceBundlePerf.java +++ b/icu4j/src/com/ibm/icu/dev/test/perf/ResourceBundlePerf.java @@ -1,6 +1,6 @@ /* ********************************************************************** -* Copyright (c) 2006, International Business Machines +* Copyright (c) 2006-2007, International Business Machines * Corporation and others. All Rights Reserved. ********************************************************************** */ @@ -16,7 +16,7 @@ import com.ibm.icu.util.UResourceBundle; public class ResourceBundlePerf extends PerfTest { - private ICUResourceBundle icuRes = null; + private UResourceBundle icuRes = null; private ResourceBundle javaRes = null; public static void main(String[] org_args) throws Exception { @@ -24,7 +24,7 @@ public class ResourceBundlePerf extends PerfTest { } protected void setup(String[] args) { - icuRes = (ICUResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "testtypes"); + icuRes = UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "testtypes"); javaRes = ICULocaleData.getResourceBundle("com.ibm.icu.dev.data","TestDataElements","testtypes"); } @@ -132,7 +132,7 @@ public class ResourceBundlePerf extends PerfTest { this.expected = expected; } public void call() { - ICUResourceBundle temp = icuRes.get(key); + UResourceBundle temp = icuRes.get(key); int t = temp.getInt(); if (t != expected) throw new Error("not equal"); } @@ -190,7 +190,7 @@ public class ResourceBundlePerf extends PerfTest { PerfTest.Function TestGetMinusOneUintICU(){ return new PerfTest.Function(){ public void call(){ - ICUResourceBundle sub = icuRes.get("minusone"); + UResourceBundle sub = icuRes.get("minusone"); int t = sub.getUInt(); if (t != 0xFFFFFFF) throw new Error("not equal"); } @@ -221,7 +221,7 @@ public class ResourceBundlePerf extends PerfTest { this.expected = expected; } public void call() { - ICUResourceBundle temp = icuRes.get(key); + UResourceBundle temp = icuRes.get(key); int[] iv = temp.getIntVector(); for (int i = 0; i < iv.length; i++){ if (expected[i] != iv[i]) throw new Error("not equal"); @@ -254,7 +254,7 @@ public class ResourceBundlePerf extends PerfTest { this.expected_len = expected_len; } public void call() { - ICUResourceBundle temp = icuRes.get(key); + UResourceBundle temp = icuRes.get(key); ByteBuffer got = temp.getBinary(); if(got.remaining() != expected_len) throw new Error("not the expected len"); for(int i=0; i< got.remaining(); i++){ @@ -327,16 +327,16 @@ public class ResourceBundlePerf extends PerfTest { } public void call() { int p = 0; - ICUResourceBundle menus = icuRes.get(key); + UResourceBundle menus = icuRes.get(key); int sizei = menus.getSize(); for (int i=0; i0){ logln("Alias mechanism works"); }else{ errln("Alias mechanism failed for fr_BE SpelloutRules"); } - rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME,"zh_TW"); - ICUResourceBundle b = (ICUResourceBundle) rb.getObject("collations"); + rb = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME,"zh_TW"); + UResourceBundle b = (UResourceBundle) rb.getObject("collations"); if(b != null){ if(b.get(0).getKey().equals( "default")){ logln("Alias mechanism works"); @@ -596,8 +594,8 @@ public final class ICUResourceBundleTest extends TestFmwk { } public void TestAlias(){ logln("Testing %%ALIAS"); - ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,"iw_IL"); - ICUResourceBundle b = rb.get("NumberPatterns"); + UResourceBundle rb = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,"iw_IL"); + UResourceBundle b = rb.get("NumberPatterns"); if(b != null){ if(b.getSize()>0){ logln("%%ALIAS mechanism works"); @@ -609,8 +607,8 @@ public final class ICUResourceBundleTest extends TestFmwk { } } public void TestXPathAlias(){ - ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","te_IN",testLoader); - ICUResourceBundle b = rb.get("aliasClient"); + UResourceBundle rb = (UResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","te_IN",testLoader); + UResourceBundle b = rb.get("aliasClient"); String result = b.getString(); String expResult= "correct"; @@ -618,7 +616,7 @@ public final class ICUResourceBundleTest extends TestFmwk { errln("Did not get the expected result for XPath style alias"); } try{ - ICUResourceBundle c = rb.get("rootAliasClient"); + UResourceBundle c = rb.get("rootAliasClient"); result = c.getString(); expResult = "correct"; if(!result.equals(expResult)){ @@ -630,8 +628,8 @@ public final class ICUResourceBundleTest extends TestFmwk { } public void TestCircularAliases(){ try{ - ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader); - ICUResourceBundle sub = rb.get("aaa"); + UResourceBundle rb = (UResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","testaliases",testLoader); + UResourceBundle sub = rb.get("aaa"); String s1 = sub.getString(); if(s1!=null){ errln("Did not get the expected exception"); @@ -646,7 +644,7 @@ public final class ICUResourceBundleTest extends TestFmwk { public void TestGetWithFallback(){ /* - ICUResourceBundle bundle =(ICUResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","te_IN"); + UResourceBundle bundle =(UResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","te_IN"); String key = bundle.getStringWithFallback("Keys/collation"); if(!key.equals("COLLATION")){ errln("Did not get the expected result from getStringWithFallback method."); @@ -837,8 +835,8 @@ public final class ICUResourceBundleTest extends TestFmwk { public void TestNorwegian(){ try{ - ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "no_NO_NY"); - ICUResourceBundle sub = rb.get("Countries"); + UResourceBundle rb = UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "no_NO_NY"); + UResourceBundle sub = rb.get("Countries"); String s1 = sub.getString("NO"); if(s1.equals("Noreg")){ logln("got expected output "); @@ -851,7 +849,7 @@ public final class ICUResourceBundleTest extends TestFmwk { } public void TestJB4102(){ try { - ICUResourceBundle root =(ICUResourceBundle) ICUResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "root"); + ICUResourceBundle root =(ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "root"); ICUResourceBundle t = null; try{ t = root.getWithFallback("calendar/islamic-civil/AmPmMarkers"); @@ -880,12 +878,12 @@ public final class ICUResourceBundleTest extends TestFmwk { logln("Testing CLDR style aliases......\n"); - ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "te_IN_REVISED",testLoader); - ICUResourceBundle alias = rb.get("a"); + UResourceBundle rb = UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "te_IN_REVISED",testLoader); + ICUResourceBundle alias = (ICUResourceBundle)rb.get("a"); for(int i = 1; i < 5 ; i++) { String resource="a"+i; - ICUResourceBundle a = ((ICUResourceBundle)alias).getWithFallback(resource); + UResourceBundle a = (alias).getWithFallback(resource); result = a.getString(); if(result.equals(expected)) { errln("CLDR style aliases failed resource with name "+resource+"resource, exp "+expects[i] +" , got " + result); @@ -923,22 +921,22 @@ public final class ICUResourceBundleTest extends TestFmwk { logln("Test to verify loading status of get(String)"); bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "te_IN"); - ICUResourceBundle countries = bundle.get("Countries"); - status =countries.getLoadingStatus(); + UResourceBundle countries = bundle.get("Countries"); + status = ((ICUResourceBundle)countries).getLoadingStatus(); if(status != ICUResourceBundle.FROM_FALLBACK){ errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_FALLBACK) + " Got: " + getLSString(status)); } /* - ICUResourceBundle auxExemplar = bundle.get("AuxExemplarCharacters"); + UResourceBundle auxExemplar = bundle.get("AuxExemplarCharacters"); status = auxExemplar.getLoadingStatus(); - if(status != ICUResourceBundle.FROM_ROOT){ - errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_ROOT) + if(status != UResourceBundle.FROM_ROOT){ + errln("Did not get the expected value for loading status. Expected "+ getLSString(UResourceBundle.FROM_ROOT) + " Got: " + getLSString(status)); } */ logln("Test to verify loading status of get(int)"); - ICUResourceBundle ms = bundle.get("MeasurementSystem"); + ICUResourceBundle ms = (ICUResourceBundle)bundle.get("MeasurementSystem"); status = ms.getLoadingStatus(); if(status != ICUResourceBundle.FROM_ROOT){ errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_ROOT) @@ -947,7 +945,7 @@ public final class ICUResourceBundleTest extends TestFmwk { logln("Test to verify loading status of getwithFallback"); bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "sh_YU",testLoader); - ICUResourceBundle temp = bundle.getWithFallback("a/a2"); + ICUResourceBundle temp = (ICUResourceBundle)bundle.getWithFallback("a/a2"); status = temp.getLoadingStatus(); if(status != ICUResourceBundle.FROM_LOCALE){ errln("Did not get the expected value for loading status. Expected "+ getLSString(ICUResourceBundle.FROM_LOCALE) diff --git a/icu4j/src/com/ibm/icu/impl/ICUResourceBundle.java b/icu4j/src/com/ibm/icu/impl/ICUResourceBundle.java index 28bf80b73dc..c544663332a 100644 --- a/icu4j/src/com/ibm/icu/impl/ICUResourceBundle.java +++ b/icu4j/src/com/ibm/icu/impl/ICUResourceBundle.java @@ -1,7 +1,7 @@ //##header /* * ***************************************************************************** - * Copyright (C) 2005-2006, International Business Machines Corporation and * others. + * Copyright (C) 2005-2007, International Business Machines Corporation and * others. * All Rights Reserved. * * ***************************************************************************** */ @@ -23,22 +23,17 @@ import java.util.HashSet; import java.util.Locale; import java.util.Map; import java.util.MissingResourceException; +import java.util.ResourceBundle; import java.util.Set; -import java.util.Vector; -//#ifndef FOUNDATION -import java.nio.ByteBuffer; -//#else -//##import com.ibm.icu.impl.ByteBuffer; -//#endif import com.ibm.icu.impl.URLHandler.URLVisitor; import com.ibm.icu.util.StringTokenizer; import com.ibm.icu.util.ULocale; import com.ibm.icu.util.UResourceBundle; -import com.ibm.icu.util.UResourceTypeMismatchException; +import com.ibm.icu.util.UResourceBundleIterator; import com.ibm.icu.util.VersionInfo; -public abstract class ICUResourceBundle extends UResourceBundle { +public class ICUResourceBundle extends UResourceBundle { /** * The data path to be used with getBundleInstance API * @draft ICU 3.0 @@ -85,7 +80,14 @@ public abstract class ICUResourceBundle extends UResourceBundle { * @draft ICU 3.0 */ public static final String ICU_TRANSLIT_BASE_NAME = ICU_BASE_NAME + "/translit"; - + + /** + * The actual path of the resource + */ + protected String resPath; + + protected static final long UNSIGNED_INT_MASK = 0xffffffffL; + /** * The class loader constant to be used with getBundleInstance API * @draft ICU 3.0 @@ -105,71 +107,7 @@ public abstract class ICUResourceBundle extends UResourceBundle { */ protected static final String INSTALLED_LOCALES = "InstalledLocales"; - /** - * Resource type constant for "no resource". - * @draft ICU 3.0 - */ - public static final int NONE = -1; - - /** - * Resource type constant for strings. - * @draft ICU 3.0 - */ - public static final int STRING = 0; - - /** - * Resource type constant for binary data. - * @draft ICU 3.0 - */ - public static final int BINARY = 1; - - /** - * Resource type constant for tables of key-value pairs. - * @draft ICU 3.0 - */ - public static final int TABLE = 2; - - /** - * Resource type constant for aliases; - * internally stores a string which identifies the actual resource - * storing the data (can be in a different resource bundle). - * Resolved internally before delivering the actual resource through the API. - * @draft ICU 3.0 - * @internal - */ - protected static final int ALIAS = 3; - - /** - * Internal use only. - * Alternative resource type constant for tables of key-value pairs. - * Never returned by getType(). - * @internal - * @draft ICU 3.0 - */ - protected static final int TABLE32 = 4; - - /** - * Resource type constant for a single 28-bit integer, interpreted as - * signed or unsigned by the getInt() function. - * @see #getInt - * @draft ICU 3.0 - */ - public static final int INT = 7; - - /** - * Resource type constant for arrays of resources. - * @draft ICU 3.0 - */ - public static final int ARRAY = 8; - - /** - * Resource type constant for vectors of 32-bit integers. - * @see #getIntVector - * @draft ICU 3.0 - */ - public static final int INT_VECTOR = 14; - - public static final int FROM_FALLBACK = 1, FROM_ROOT = 2, FROM_DEFAULT = 3, FROM_LOCALE = 4; + public static final int FROM_FALLBACK = 1, FROM_ROOT = 2, FROM_DEFAULT = 3, FROM_LOCALE = 4; private int loadingStatus = -1; @@ -187,289 +125,29 @@ public abstract class ICUResourceBundle extends UResourceBundle { return loadingStatus; } - /** - * Get the noFallback flag specified in the loaded bundle. - * @return The noFallback flag. - */ - protected boolean getNoFallback() { - return false; - } - - /** - * Return the version number associated with this UResourceBundle as an - * VersionInfo object. - * @return VersionInfo object containing the version of the bundle - * @draft ICU 3.0 - */ - public VersionInfo getVersion() { - return null; - } - - /** - * Returns a string from a string resource type - * - * @return a string - * @see #getBinary() - * @see #getIntVector - * @see #getInt - * @throws MissingResourceException - * @throws UResourceTypeMismatchException - * @draft ICU 3.0 - */ - public String getString() { - throw new UResourceTypeMismatchException(""); - } - - /** - * @internal ICU 3.0 - */ - public String[] getStringArray() { - throw new UResourceTypeMismatchException(""); - } - - /* - * Returns a string from a string resource type - * @param key The key whose values needs to be fetched - * @return a string - * @see #getBinary - * @see #getIntVector - * @see #getInt - * @throws MissingResourceException - * @throws UResourceTypeMismatchException - * @draft ICU 3.0 - */ - // public String getString(String key) { - // throw new UResourceTypeMismatchException(""); - // } - - /** - * Returns a binary data from a binary resource. - * - * @return a pointer to a chuck of unsigned bytes which live in a memory mapped/DLL file. - * @see #getIntVector - * @see #getInt - * @throws MissingResourceException - * @throws UResourceTypeMismatchException - * @draft ICU 3.0 - */ - public ByteBuffer getBinary() { - throw new UResourceTypeMismatchException(""); - } - - /** - * Returns a binary data from a binary resource. - * - * @param ba Establish the return type from this function. The value of the parameter - * is not used; a null variable is OK. The thing that matters is the type - * of the parameter - * @return an array unsigned bytes containing the binary data from the resource. - * @see #getIntVector - * @see #getInt - * @throws MissingResourceException - * @throws UResourceTypeMismatchException - * @draft ICU 3.6 - */ - public byte [] getBinary(byte []ba) { - throw new UResourceTypeMismatchException(""); - } + public static void setLoadingStatus(UResourceBundle b, String requestedLocale){ + ICUResourceBundle bundle = (ICUResourceBundle) b; + String locale = bundle.getLocaleID(); + if(locale.equals("root")){ + bundle.setLoadingStatus(FROM_ROOT); + return; + } + if(locale.equals(requestedLocale)){ + bundle.setLoadingStatus(FROM_LOCALE); + }else{ + bundle.setLoadingStatus(FROM_FALLBACK); + } + } + /** - * Returns a 32 bit integer array from a resource. - * - * @return a pointer to a chunk of unsigned bytes which live in a memory mapped/DLL file. - * @see #getBinary() - * @see #getInt - * @throws MissingResourceException - * @throws UResourceTypeMismatchException - * @draft ICU 3.0 + * Returns the respath of this bundle + * @return */ - public int[] getIntVector() { - throw new UResourceTypeMismatchException(""); + public String getResPath(){ + return resPath; } - /** - * Returns a signed integer from a resource. - * - * @return an integer value - * @see #getIntVector - * @see #getBinary() - * @throws MissingResourceException - * @throws UResourceTypeMismatchException - * @stable ICU 2.0 - */ - public int getInt() { - throw new UResourceTypeMismatchException(""); - } - - /** - * Returns a unsigned integer from a resource. - * This integer is originally 28 bit and the sign gets propagated. - * - * @return an integer value - * @see #getIntVector - * @see #getBinary() - * @throws MissingResourceException - * @throws UResourceTypeMismatchException - * @stable ICU 2.0 - */ - public int getUInt() { - throw new UResourceTypeMismatchException(""); - } - /** - * Returns the size of a resource. Size for scalar types is always 1, - * and for vector/table types is the number of child resources. - *
Warning: Integer array is treated as a scalar type. There are no - * APIs to access individual members of an integer array. It - * is always returned as a whole. - * @return number of resources in a given resource. - * @draft ICU 3.0 - */ - public int getSize() { - return size; - } - - /** - * Returns the type of a resource. - * Available types are {@link #INT INT}, {@link #ARRAY ARRAY}, - * {@link #BINARY BINARY}, {@link #INT_VECTOR INT_VECTOR}, - * {@link #STRING STRING}, {@link #TABLE TABLE}. - * - * @return type of the given resource. - * @draft ICU 3.0 - */ - public int getType() { - int type = ICUResourceBundleImpl.RES_GET_TYPE(resource); - if(type==TABLE32){ - return TABLE; //Mask the table32's real type - } - return type; - } - - /** - * Returns the key associated with a given resource. Not all the resources have a key - only - * those that are members of a table. - * @return a key associated to this resource, or NULL if it doesn't have a key - * @draft ICU 3.0 - */ - public String getKey() { - return key; - } - - /** - * Returns the iterator which iterates over this - * resource bundle - * @draft ICU 3.0 - */ - public ICUResourceBundleIterator getIterator() { - return new ICUResourceBundleIterator(this); - } - - /** - * Returns the resource in a given resource at the specified index. - * - * @param index an index to the wanted resource. - * @return the sub resource UResourceBundle object - * @throws IndexOutOfBoundsException - * @draft ICU 3.0 - */ - public ICUResourceBundle get(int index) { - return getImpl(index, null, this); - } - protected ICUResourceBundle getImpl(int index, HashMap table, - ICUResourceBundle requested) { - ICUResourceBundle obj = handleGet(index, table, requested); - if (obj == null) { - obj = (ICUResourceBundle) getParent(); - if (obj != null) { - obj = obj.getImpl(index, table, requested); - } - if (obj == null) - throw new MissingResourceException( - "Can't find resource for bundle " - + this.getClass().getName() + ", key " - + getKey(), this.getClass().getName(), getKey()); - } - setLoadingStatus(obj, requested.getLocaleID()); - return obj; - } - // abstract UResourceBundle handleGetInt(int index); - - /** - * Returns a resource in a given resource that has a given key. - * - * @param key a key associated with the wanted resource - * @return a resource bundle object representing rhe resource - * @throws MissingResourceException - * @draft ICU 3.0 - */ - public ICUResourceBundle get(String key) { - return getImpl(key, null, this); - } - protected ICUResourceBundle getImpl(String key, HashMap table, - ICUResourceBundle requested) { - ICUResourceBundle obj = handleGet(key, table, requested); - if (obj == null) { - obj = (ICUResourceBundle) getParent(); - if (obj != null) { - //call the get method to recursively fetch the resource - obj = obj.getImpl(key, table, requested); - } - if (obj == null) { - String fullName = ICUResourceBundleReader.getFullName( - getBaseName(), getLocaleID()); - throw new MissingResourceException( - "Can't find resource for bundle " + fullName + ", key " - + key, this.getClass().getName(), key); - } - } - setLoadingStatus(obj, requested.getLocaleID()); - return obj; - } - - private static void setLoadingStatus(ICUResourceBundle bundle, String requestedLocale){ - String locale = bundle.getLocaleID(); - if(locale.equals("root")){ - bundle.setLoadingStatus(FROM_ROOT); - return; - } - if(locale.equals(requestedLocale)){ - bundle.setLoadingStatus(FROM_LOCALE); - }else{ - bundle.setLoadingStatus(FROM_FALLBACK); - } - } - - /** - * Returns the string in a given resource at the specified index. - * - * @param index an index to the wanted string. - * @return a string which lives in the resource. - * @throws IndexOutOfBoundsException - * @throws UResourceTypeMismatchException - * @draft ICU 3.0 - */ - public String getString(int index) { - ICUResourceBundle temp = get(index); - if (temp.getType() == STRING) { - return temp.getString(); - } - throw new UResourceTypeMismatchException(""); - } - - /** - * Returns the parent bundle of this bundle - * @return UResourceBundle the parent of this bundle. Returns null if none - * @draft ICU 3.0 - */ - public abstract UResourceBundle getParent(); - - /** - * Returns the locale id of this bundle as String - * @return String locale id - * @draft ICU 3.4 - */ - protected abstract String getLocaleID(); - /** * Returns a functionally equivalent locale, considering keywords as well, for the specified keyword. * @param baseName resource specifier @@ -518,7 +196,7 @@ public abstract class ICUResourceBundle extends UResourceBundle { // determine in which locale (if any) the currently relevant 'default' is do { try { - ICUResourceBundle irb = r.get(resName); + ICUResourceBundle irb = (ICUResourceBundle) r.get(resName); defStr = irb.getString(DEFAULT_TAG); if (lookForDefault == true) { kwVal = defStr; @@ -540,7 +218,7 @@ public abstract class ICUResourceBundle extends UResourceBundle { // determine in which locale (if any) the named resource is located do { try { - ICUResourceBundle irb = r.get(resName); + ICUResourceBundle irb = (ICUResourceBundle)r.get(resName); /* UResourceBundle urb = */irb.get(kwVal); fullBase = irb.getULocale(); // If the get() completed, we have the full base locale @@ -571,7 +249,7 @@ public abstract class ICUResourceBundle extends UResourceBundle { // determine in which locale (if any) the named resource is located do { try { - ICUResourceBundle irb = r.get(resName); + ICUResourceBundle irb = (ICUResourceBundle)r.get(resName); UResourceBundle urb = irb.get(kwVal); // if we didn't fail before this.. @@ -780,67 +458,6 @@ public abstract class ICUResourceBundle extends UResourceBundle { return (Locale[]) list.toArray(new Locale[list.size()]); } - public Enumeration getKeys() { - initKeysVector(); - return keys.elements(); - } - private Vector keys = null; - private synchronized void initKeysVector(){ - if(keys!=null){ - return; - } - //ICUResourceBundle current = this; - keys = new Vector(); - //while(current!=null){ - Enumeration e = this.handleGetKeys(); - while(e.hasMoreElements()){ - String elem = (String)e.nextElement(); - if(!keys.contains(elem)){ - keys.add(elem); - } - } - // current = (ICUResourceBundle)current.getParent(); - //} - } - protected Enumeration handleGetKeys(){ - Vector keys = new Vector(); - ICUResourceBundle item = null; - for (int i = 0; i < size; i++) { - item = get(i); - keys.add(item.getKey()); - } - return keys.elements(); - } - - public static ICUResourceBundle createBundle(String baseName, - String localeID, ClassLoader root) { - ICUResourceBundle b = ICUResourceBundleImpl.createBundle(baseName, localeID, root); - if(b==null){ - throw new MissingResourceException("Could not find the bundle "+ baseName+"/"+ localeID+".res","",""); - } - return b; - } - - //====== protected members ============== - protected String key; - protected int size = 1; - protected String resPath; - protected long resource = RES_BOGUS; - protected boolean isTopLevel = false; - - protected static final long UNSIGNED_INT_MASK = 0xffffffffL; - - protected static final long RES_BOGUS = 0xffffffff; - - protected ICUResourceBundle handleGet(String key, HashMap table, - ICUResourceBundle requested) { - throw new UResourceTypeMismatchException(""); - } - protected ICUResourceBundle handleGet(int index, HashMap table, - ICUResourceBundle requested) { - throw new UResourceTypeMismatchException(""); - } - /** * Returns the locale of this resource bundle. This method can be used after * a call to getBundle() to determine whether the resource bundle returned @@ -852,66 +469,6 @@ public abstract class ICUResourceBundle extends UResourceBundle { return getULocale().toLocale(); } - // this method is declared in ResourceBundle class - // so cannot change the signature - protected Object handleGetObject(String key) { - return handleGetObjectImpl(key, this); - } - - // To facilitate XPath style aliases we need a way to pass the reference - // to requested locale. The only way I could figure out is to implement - // the look up logic here. This has a disadvantage that if the client - // loads an ICUResourceBundle, calls ResourceBundle.getObject method - // with a key that does not exist in the bundle then the lookup is - // done twice before throwing a MissingResourceExpection. - private Object handleGetObjectImpl(String key, ICUResourceBundle requested) { - Object obj = resolveObject(key, requested); - if (obj == null) { - ICUResourceBundle parent = (ICUResourceBundle) getParent(); - if (parent != null) { - obj = parent.handleGetObjectImpl(key, requested); - } - if (obj == null) - throw new MissingResourceException( - "Can't find resource for bundle " - + this.getClass().getName() + ", key " + key, - this.getClass().getName(), key); - } - return obj; - } - - private Object resolveObject(String key, ICUResourceBundle requested) { - if (getType() == STRING) { - return getString(); - } - ICUResourceBundle obj = handleGet(key, requested); - if (obj != null) { - if (obj.getType() == STRING) { - return obj.getString(); - } - try { - if (obj.getType() == ARRAY) { - return obj.handleGetStringArray(); - } - } catch (UResourceTypeMismatchException ex) { - return obj; - } - } - return obj; - } - - protected ICUResourceBundle handleGet(int index, ICUResourceBundle requested) { - return null; - } - - protected ICUResourceBundle handleGet(String key, - ICUResourceBundle requested) { - return null; - } - - protected String[] handleGetStringArray() { - return null; - } // ========== privates ========== private static final String ICU_RESOURCE_INDEX = "res_index"; @@ -932,11 +489,11 @@ public abstract class ICUResourceBundle extends UResourceBundle { // and it's embedded in our binary resources. ICUResourceBundle bundle = (ICUResourceBundle) UResourceBundle.instantiateBundle(baseName, ICU_RESOURCE_INDEX, root, true); - bundle = bundle.get(INSTALLED_LOCALES); + bundle = (ICUResourceBundle)bundle.get(INSTALLED_LOCALES); int length = bundle.getSize(); int i = 0; ULocale[] locales = new ULocale[length]; - ICUResourceBundleIterator iter = bundle.getIterator(); + UResourceBundleIterator iter = bundle.getIterator(); iter.reset(); while (iter.hasNext()) { locales[i++] = new ULocale(iter.next().getKey()); @@ -953,11 +510,11 @@ public abstract class ICUResourceBundle extends UResourceBundle { private static final String[] createLocaleNameArray(String baseName, ClassLoader root) { ICUResourceBundle bundle = (ICUResourceBundle) UResourceBundle.instantiateBundle( baseName, ICU_RESOURCE_INDEX, root, true); - bundle = bundle.get(INSTALLED_LOCALES); + bundle = (ICUResourceBundle)bundle.get(INSTALLED_LOCALES); int length = bundle.getSize(); int i = 0; String[] locales = new String[length]; - ICUResourceBundleIterator iter = bundle.getIterator(); + UResourceBundleIterator iter = bundle.getIterator(); iter.reset(); while (iter.hasNext()) { locales[i++] = iter.next().getKey(); @@ -1115,17 +672,17 @@ public abstract class ICUResourceBundle extends UResourceBundle { } protected static final ICUResourceBundle findResourceWithFallback(String path, - ICUResourceBundle actualBundle, ICUResourceBundle requested) { + UResourceBundle actualBundle, UResourceBundle requested) { ICUResourceBundle sub = null; if (requested == null) { requested = actualBundle; } while (actualBundle != null) { StringTokenizer st = new StringTokenizer(path, "/"); - ICUResourceBundle current = actualBundle; + ICUResourceBundle current = (ICUResourceBundle) actualBundle; while (st.hasMoreTokens()) { String subKey = st.nextToken(); - sub = current.handleGet(subKey, requested); + sub = (ICUResourceBundle)current.handleGet(subKey, requested); if (sub == null) { break; } @@ -1135,15 +692,15 @@ public abstract class ICUResourceBundle extends UResourceBundle { //we found it break; } - if (actualBundle.resPath.length() != 0) { - path = actualBundle.resPath + "/" + path; + if (((ICUResourceBundle)actualBundle).resPath.length() != 0) { + path = ((ICUResourceBundle)actualBundle).resPath + "/" + path; } // if not try the parent bundle - actualBundle = (ICUResourceBundle) actualBundle.getParent(); + actualBundle = ((ICUResourceBundle) actualBundle).getParent(); } if(sub != null){ - setLoadingStatus(sub, requested.getLocaleID()); + setLoadingStatus(sub, ((ICUResourceBundle)requested).getLocaleID()); } return sub; } @@ -1192,7 +749,7 @@ public abstract class ICUResourceBundle extends UResourceBundle { } if(DEBUG) System.out.println("Creating "+fullName+ " currently b is "+b); if (b == null) { - b = ICUResourceBundleImpl.createBundle(baseName, localeName, root); + b = ICUResourceBundle.createBundle(baseName, localeName, root); if(DEBUG)System.out.println("The bundle created is: "+b+" and disableFallback="+disableFallback+" and bundle.getNoFallback="+(b!=null && b.getNoFallback())); if(disableFallback || (b!=null && b.getNoFallback())){ @@ -1217,7 +774,7 @@ public abstract class ICUResourceBundle extends UResourceBundle { b.setLoadingStatus(ICUResourceBundle.FROM_DEFAULT); } }else if(rootLocale.length()!=0){ - b = ICUResourceBundleImpl.createBundle(baseName, rootLocale, root); + b = ICUResourceBundle.createBundle(baseName, rootLocale, root); if(b!=null){ b.setLoadingStatus(ICUResourceBundle.FROM_ROOT); } @@ -1243,4 +800,359 @@ public abstract class ICUResourceBundle extends UResourceBundle { } return b; } + UResourceBundle get(String key, HashMap table, UResourceBundle requested) { + ICUResourceBundle obj = (ICUResourceBundle)handleGet(key, table, requested); + if (obj == null) { + obj = (ICUResourceBundle)getParent(); + if (obj != null) { + //call the get method to recursively fetch the resource + obj = (ICUResourceBundle)obj.get(key, table, requested); + } + if (obj == null) { + String fullName = ICUResourceBundleReader.getFullName( + getBaseName(), getLocaleID()); + throw new MissingResourceException( + "Can't find resource for bundle " + fullName + ", key " + + key, this.getClass().getName(), key); + } + } + ICUResourceBundle.setLoadingStatus(obj, ((ICUResourceBundle)requested).getLocaleID()); + return obj; + } + //protected byte[] version; + protected byte[] rawData; + protected long rootResource; + protected boolean noFallback; + + protected String localeID; + protected String baseName; + protected ULocale ulocale; + protected ClassLoader loader; + + protected static final boolean ASSERT = false; + + /** + * + * @param baseName + * @param localeID + * @param root + * @return the new bundle + */ + public static ICUResourceBundle createBundle(String baseName, + String localeID, ClassLoader root) { + + ICUResourceBundleReader reader = ICUResourceBundleReader.getReader( baseName, localeID, root); + // could not open the .res file so return null + if (reader == null) { + return null; + } + return getBundle(reader, baseName, localeID, root); + } + + protected String getLocaleID() { + return localeID; + } + + protected String getBaseName() { + return baseName; + } + + public ULocale getULocale() { + return ulocale; + } + + public UResourceBundle getParent() { + return (UResourceBundle) parent; + } + + protected void setParent(ResourceBundle parent) { + this.parent = parent; + } + + /** + * Get the noFallback flag specified in the loaded bundle. + * @return The noFallback flag. + */ + protected boolean getNoFallback() { + return noFallback; + } + + private static ICUResourceBundle getBundle(ICUResourceBundleReader reader, String baseName, String localeID, ClassLoader loader) { + + long rootResource = (UNSIGNED_INT_MASK) & reader.getRootResource(); + + int type = RES_GET_TYPE(rootResource); + if (type == TABLE) { + ICUResourceBundleImpl.ResourceTable table = new ICUResourceBundleImpl.ResourceTable(reader, baseName, localeID, loader); + if(table.getSize()==1){ + UResourceBundle b = table.handleGet(0, table); + String itemKey = b.getKey(); + + // %%ALIAS is such a hack! + if (itemKey.equals("%%ALIAS")) { + String locale = b.getString(); + UResourceBundle actual = UResourceBundle.getBundleInstance(baseName, locale); + return (ICUResourceBundleImpl.ResourceTable) actual; + }else{ + return table; + } + }else { + return table; + } + } else if (type == TABLE32) { + + // genrb does not generate Table32 with %%ALIAS + return new ICUResourceBundleImpl.ResourceTable32(reader, baseName, localeID, loader); + } else { + throw new IllegalStateException("Invalid format error"); + } + } + // private constructor for inner classes + protected ICUResourceBundle(){}; + + public static final int RES_GET_TYPE(long res) { + return (int) ((res) >> 28L); + } + protected static final int RES_GET_OFFSET(long res) { + return (int) ((res & 0x0fffffff) * 4); + } + /* get signed and unsigned integer values directly from the Resource handle */ + protected static final int RES_GET_INT(long res) { + return (((int) ((res) << 4L)) >> 4L); + } + static final long RES_GET_UINT(long res) { + long t = ((res) & 0x0fffffffL); + return t; + } + static final StringBuffer RES_GET_KEY(byte[] rawData, + int keyOffset) { + char ch = 0xFFFF; //sentinel + StringBuffer key = new StringBuffer(); + while ((ch = (char) rawData[keyOffset]) != 0) { + key.append(ch); + keyOffset++; + } + return key; + } + protected static final int getIntOffset(int offset) { + return (offset * 4); + } + static final int getCharOffset(int offset) { + return (offset * 2); + } + protected final ICUResourceBundle createBundleObject(String key, + long resource, String resPath, HashMap table, + UResourceBundle requested, ICUResourceBundle bundle) { + //if (resource != RES_BOGUS) { + switch (RES_GET_TYPE(resource)) { + case STRING : { + return new ICUResourceBundleImpl.ResourceString(key, resPath, resource, this); + } + case BINARY : { + return new ICUResourceBundleImpl.ResourceBinary(key, resPath, resource, this); + } + case ALIAS : { + return findResource(key, resource, table, requested); + } + case INT : { + return new ICUResourceBundleImpl.ResourceInt(key, resPath, resource, this); + } + case INT_VECTOR : { + return new ICUResourceBundleImpl.ResourceIntVector(key, resPath, resource, this); + } + case ARRAY : { + return new ICUResourceBundleImpl.ResourceArray(key, resPath, resource, this); + } + case TABLE32 : { + return new ICUResourceBundleImpl.ResourceTable32(key, resPath, resource, this); + } + case TABLE : { + return new ICUResourceBundleImpl.ResourceTable(key, resPath, resource, this); + } + default : + throw new IllegalStateException("The resource type is unknown"); + } + //} + //return null; + } + + static final void assign(ICUResourceBundle b1, ICUResourceBundle b2){ + b1.rawData = b2.rawData; + b1.rootResource = b2.rootResource; + b1.noFallback = b2.noFallback; + b1.baseName = b2.baseName; + b1.localeID = b2.localeID; + b1.ulocale = b2.ulocale; + b1.loader = b2.loader; + b1.parent = b2.parent; + } + + int findKey(int size, int currentOffset, ICUResourceBundle res, String target) { + int mid = 0, start = 0, limit = size, rc; + int lastMid = -1; + //int myCharOffset = 0, keyOffset = 0; + for (;;) { + mid = ((start + limit) / 2); + if (lastMid == mid) { /* Have we moved? */ + break; /* We haven't moved, and it wasn't found. */ + } + lastMid = mid; + String comp = res.getKey(currentOffset, mid); + rc = target.compareTo(comp); + if (rc < 0) { + limit = mid; + } else if (rc > 0) { + start = mid; + } else { + return mid; + } + } + return -1; + } + + public String getKey(int currentOfset, int index){ + return null; + } + + private static char makeChar(byte b1, byte b0) { + return (char)((b1 << 8) | (b0 & 0xff)); + } + static char getChar(byte[]data, int offset){ + return makeChar(data[offset], data[offset+1]); + } + private static int makeInt(byte b3, byte b2, byte b1, byte b0) { + return (int)((((b3 & 0xff) << 24) | + ((b2 & 0xff) << 16) | + ((b1 & 0xff) << 8) | + ((b0 & 0xff) << 0))); + } + + protected static int getInt(byte[] data, int offset){ + if (ASSERT) Assert.assrt("offset < data.length", offset < data.length); + return makeInt(data[offset], data[offset+1], + data[offset+2], data[offset+3]); + } + + String getStringValue(long resource) { + if (resource == 0) { + /* + * The data structure is documented as supporting resource==0 for empty strings. + * Return a fixed pointer in such a case. + * This was dropped in uresdata.c 1.17 as part of Jitterbug 1005 work + * on code coverage for ICU 2.0. + * Re-added for consistency with the design and with other code. + */ + return ""; + } + int offset = RES_GET_OFFSET(resource); + int length = getInt(rawData,offset); + int stringOffset = offset + getIntOffset(1); + char[] dst = new char[length]; + if (ASSERT) Assert.assrt("(stringOffset+getCharOffset(length)) < rawData.length", (stringOffset+getCharOffset(length)) < rawData.length); + for(int i=0; i= 1) { + return Integer.valueOf(s).intValue(); + } + return -1; + } + private ICUResourceBundle findResource(String key, long resource, + HashMap table, + UResourceBundle requested) { + ClassLoader loaderToUse = loader; + String locale = null, keyPath = null; + String bundleName; + String resPath = getStringValue(resource); + if (table == null) { + table = new HashMap(); + } + if (table.get(resPath) != null) { + throw new IllegalArgumentException( + "Circular references in the resource bundles"); + } + table.put(resPath, ""); + if (resPath.indexOf(RES_PATH_SEP_CHAR) == 0) { + int i = resPath.indexOf(RES_PATH_SEP_CHAR, 1); + int j = resPath.indexOf(RES_PATH_SEP_CHAR, i + 1); + bundleName = resPath.substring(1, i); + locale = resPath.substring(i + 1); + if (j != -1) { + locale = resPath.substring(i + 1, j); + keyPath = resPath.substring(j + 1, resPath.length()); + } + //there is a path included + if (bundleName.equals(ICUDATA)) { + bundleName = ICU_BASE_NAME; + loaderToUse = ICU_DATA_CLASS_LOADER; + }else if(bundleName.indexOf(ICUDATA)>-1){ + int idx = bundleName.indexOf(HYPHEN); + if(idx>-1){ + bundleName = ICU_BASE_NAME+RES_PATH_SEP_STR+bundleName.substring(idx+1,bundleName.length()); + loaderToUse = ICU_DATA_CLASS_LOADER; + } + } + } else { + //no path start with locale + int i = resPath.indexOf(RES_PATH_SEP_CHAR); + keyPath = resPath.substring(i + 1); + if (i != -1) { + locale = resPath.substring(0, i); + } else { + locale = keyPath; + keyPath = null;//keyPath.substring(i, keyPath.length()); + } + bundleName = baseName; + } + ICUResourceBundle bundle = null; + ICUResourceBundle sub = null; + if(bundleName.equals(LOCALE)){ + bundleName = baseName; + bundle = (ICUResourceBundle)requested; + keyPath = resPath.substring(LOCALE.length() + 2/* prepending and appending / */, resPath.length()); + locale = ((ICUResourceBundle)requested).getLocaleID(); + sub = ICUResourceBundle.findResourceWithFallback(keyPath, requested, null); + sub.resPath = "/" + sub.getLocaleID() + "/" + keyPath; + }else{ + if (locale == null) { + // {dlf} must use requestor's class loader to get resources from same jar + bundle = (ICUResourceBundle) getBundleInstance(bundleName, "", + loaderToUse, false); + } else { + bundle = (ICUResourceBundle) getBundleInstance(bundleName, locale, + loaderToUse, false); + } + if (keyPath != null) { + StringTokenizer st = new StringTokenizer(keyPath, "/"); + ICUResourceBundle current = bundle; + while (st.hasMoreTokens()) { + String subKey = st.nextToken(); + sub = (ICUResourceBundle)((ICUResourceBundle) current).get(subKey, table, requested); + if (sub == null) { + break; + } + current = sub; + } + } else { + // if the sub resource is not found + // try fetching the sub resource with + // the key of this alias resource + sub = (ICUResourceBundle)bundle.get(key); + } + sub.resPath = resPath; + } + if (sub == null) { + throw new MissingResourceException(localeID, baseName, key); + } + return sub; + } } diff --git a/icu4j/src/com/ibm/icu/impl/ICUResourceBundleImpl.java b/icu4j/src/com/ibm/icu/impl/ICUResourceBundleImpl.java index 61cf43e03b5..9b03f1fa9eb 100644 --- a/icu4j/src/com/ibm/icu/impl/ICUResourceBundleImpl.java +++ b/icu4j/src/com/ibm/icu/impl/ICUResourceBundleImpl.java @@ -1,224 +1,165 @@ -//##header -/* - ****************************************************************************** - * Copyright (C) 2004-2006, International Business Machines Corporation and * - * others. All Rights Reserved. * - ****************************************************************************** +/** + ******************************************************************************* + * Copyright (C) 2001-2007, International Business Machines Corporation and * + * others. All Rights Reserved. * + ******************************************************************************* */ package com.ibm.icu.impl; +import java.nio.ByteBuffer; import java.util.HashMap; import java.util.MissingResourceException; -import java.util.ResourceBundle; -import com.ibm.icu.util.StringTokenizer; import com.ibm.icu.util.ULocale; import com.ibm.icu.util.UResourceBundle; +import com.ibm.icu.util.UResourceBundleIterator; import com.ibm.icu.util.UResourceTypeMismatchException; -//#ifndef FOUNDATION -import java.nio.ByteBuffer; -//#endif - -/** - * @author ram - */ -public class ICUResourceBundleImpl extends ICUResourceBundle { - //protected byte[] version; - private byte[] rawData; - private long rootResource; - private boolean noFallback; - - private String localeID; - private String baseName; - private ULocale ulocale; - private ClassLoader loader; - - private static final boolean ASSERT = false; +class ICUResourceBundleImpl { - /** - * - * @param baseName - * @param localeID - * @param root - * @return the new bundle - */ - public static ICUResourceBundle createBundle(String baseName, - String localeID, ClassLoader root) { - - ICUResourceBundleReader reader = ICUResourceBundleReader.getReader( - baseName, localeID, root); - - // could not open the .res file so return null - if (reader == null) { - return null; - } - - ICUResourceBundleImpl bundle = new ICUResourceBundleImpl(reader, - baseName, localeID, root); - return bundle.getBundle(); - } - - protected String getLocaleID() { - return localeID; - } - - protected String getBaseName() { - return baseName; - } - - public ULocale getULocale() { - return ulocale; - } - - public UResourceBundle getParent() { - return (UResourceBundle) parent; - } - - protected void setParent(ResourceBundle parent) { - this.parent = parent; - } - - /** - * Get the noFallback flag specified in the loaded bundle. - * @return The noFallback flag. - */ - protected boolean getNoFallback() { - return noFallback; - } - - private ICUResourceBundle getBundle() { - int type = RES_GET_TYPE(rootResource); - if (type == TABLE) { - ResourceTable table = new ResourceTable(null, rootResource, "", true); - if(table.size==1){ - ICUResourceBundle b = table.handleGet(0, table); - String itemKey = b.getKey(); - - // %%ALIAS is such a hack! - if (itemKey.equals("%%ALIAS")) { - String locale = b.getString(); - ICUResourceBundle actual = (ICUResourceBundle) UResourceBundle.getBundleInstance(baseName, locale); - return (ResourceTable) actual; - }else{ - return table; - } - }else { - return table; + static final class ResourceArray extends ICUResourceBundle { + protected String[] handleGetStringArray() { + String[] strings = new String[size]; + UResourceBundleIterator iter = getIterator(); + int i = 0; + while (iter.hasNext()) { + strings[i++] = iter.next().getString(); } - } else if (type == TABLE32) { - - // genrb does not generate Table32 with %%ALIAS - return new ResourceTable32(null, rootResource, "", true); - } else { - throw new IllegalStateException("Invalid format error"); + return strings; + } + /** + * @internal ICU 3.0 + */ + public String[] getStringArray() { + return handleGetStringArray(); + } + protected UResourceBundle handleGet(String index, UResourceBundle requested) { + return handleGet(index, null, requested); + } + protected UResourceBundle handleGet(String index, HashMap table, UResourceBundle requested) { + int val = getIndex(index); + if (val > -1) { + return handleGet(val, table, requested); + } + throw new UResourceTypeMismatchException("Could not get the correct value for index: "+ index); + } + protected UResourceBundle handleGet(int index, UResourceBundle requested) { + return handleGet(index, null, requested); + } + protected UResourceBundle handleGet(int index, HashMap table, UResourceBundle requested) { + if (index > size) { + throw new IndexOutOfBoundsException(); + } + int offset = RES_GET_OFFSET(resource); + int itemOffset = offset + getIntOffset(index + 1); + long itemResource = (UNSIGNED_INT_MASK) & ICUResourceBundle.getInt(rawData,itemOffset); + String path = (isTopLevel == true) ? Integer.toString(index) : resPath + "/" + index; + return createBundleObject(null, itemResource, path, table, requested, this); + } + private int countItems() { + int offset = RES_GET_OFFSET(resource); + int value = getInt(rawData,offset); + return value; + } + ResourceArray(String key, String resPath, long resource, ICUResourceBundle bundle) { + assign(this, bundle); + this.resource = resource; + this.key = key; + this.size = countItems(); + this.resPath = resPath; } } - private ICUResourceBundleImpl(ICUResourceBundleReader reader, String baseName, - String localeID, ClassLoader loader) { - this.rawData = reader.getData(); - this.rootResource = (UNSIGNED_INT_MASK) & reader.getRootResource(); - this.noFallback = reader.getNoFallback(); - this.baseName = baseName; - this.localeID = localeID; - this.ulocale = new ULocale(localeID); - this.loader = loader; - } - static final int RES_GET_TYPE(long res) { - return (int) ((res) >> 28L); - } - private static final int RES_GET_OFFSET(long res) { - return (int) ((res & 0x0fffffff) * 4); - } - /* get signed and unsigned integer values directly from the Resource handle */ - private static final int RES_GET_INT(long res) { - return (((int) ((res) << 4L)) >> 4L); - } - private static final long RES_GET_UINT(long res) { - long t = ((res) & 0x0fffffffL); - return t; - } - private static final StringBuffer RES_GET_KEY(byte[] rawData, - int keyOffset) { - char ch = 0xFFFF; //sentinel - StringBuffer key = new StringBuffer(); - while ((ch = (char) rawData[keyOffset]) != 0) { - key.append(ch); - keyOffset++; + static final class ResourceBinary extends ICUResourceBundle { + private byte[] value; + public ByteBuffer getBinary() { + return ByteBuffer.wrap(value); + } + public byte [] getBinary(byte []ba) { + return value; } - return key; - } - private static final int getIntOffset(int offset) { - return (offset * 4); - } - private static final int getCharOffset(int offset) { - return (offset * 2); - } - private final ICUResourceBundle createBundleObject(String key, - long resource, String resPath, HashMap table, ICUResourceBundle requested) { - //if (resource != RES_BOGUS) { - switch (RES_GET_TYPE(resource)) { - case STRING : { - return new ResourceString(key, resPath, resource); - } - case BINARY : { - return new ResourceBinary(key, resPath, resource); - } - case ALIAS : { - return findResource(key, resource, table, requested); - } - case INT : { - return new ResourceInt(key, resPath, resource); - } - case INT_VECTOR : { - return new ResourceIntVector(key, resPath, resource); - } - case ARRAY : { - return new ResourceArray(key, resPath, resource); - } - case TABLE32 : { - return new ResourceTable32(key, resPath, resource); - } - case TABLE : { - return new ResourceTable(key, resPath, resource); - } - default : - throw new IllegalStateException("The resource type is unknown"); + private byte[] getValue() { + int offset = RES_GET_OFFSET(resource); + int length = ICUResourceBundle.getInt(rawData,offset); + int byteOffset = offset + getIntOffset(1); + byte[] dst = new byte[length]; + if (ASSERT) Assert.assrt("byteOffset+length < rawData.length", byteOffset+length < rawData.length); + System.arraycopy(rawData, byteOffset, dst, 0, length); + return dst; } - //} - //return null; - } - private int findKey(int size, int currentOffset, Resource res, String target) { - int mid = 0, start = 0, limit = size, rc; - int lastMid = -1; - //int myCharOffset = 0, keyOffset = 0; - for (;;) { - mid = ((start + limit) / 2); - if (lastMid == mid) { /* Have we moved? */ - break; /* We haven't moved, and it wasn't found. */ - } - lastMid = mid; - String comp = res.getKey(currentOffset, mid); - rc = target.compareTo(comp); - if (rc < 0) { - limit = mid; - } else if (rc > 0) { - start = mid; - } else { - return mid; - } + ResourceBinary(String key, String resPath, long resource, ICUResourceBundle bundle) { + assign(this, bundle); + this.resource = resource; + this.key = key; + this.resPath = resPath; + value = getValue(); + } - return -1; } - private interface Resource { - public String getKey(int currentOfset, int index); + static final class ResourceInt extends ICUResourceBundle { + public int getInt() { + return RES_GET_INT(resource); + } + public int getUInt() { + long ret = RES_GET_UINT(resource); + return (int) ret; + } + ResourceInt(String key, String resPath, long resource, ICUResourceBundle bundle) { + assign(this, bundle); + this.key = key; + this.resource = resource; + this.resPath = resPath; + } } - private class ResourceTable extends ICUResourceBundle implements Resource { + + static final class ResourceString extends ICUResourceBundle { + private String value; + public String getString() { + return value; + } + ResourceString(String key, String resPath, long resource, ICUResourceBundle bundle) { + assign(this, bundle); + value = getStringValue(resource); + this.key = key; + this.resource = resource; + this.resPath = resPath; + } + } + + static final class ResourceIntVector extends ICUResourceBundle { + private int[] value; + public int[] getIntVector() { + return value; + } + private int[] getValue() { + int offset = RES_GET_OFFSET(resource); + int length = ICUResourceBundle.getInt(rawData,offset); + int intOffset = offset + getIntOffset(1); + int[] val = new int[length]; + int byteLength = getIntOffset(length); + + if (ASSERT) Assert.assrt("(intOffset+byteLength) size) { throw new IndexOutOfBoundsException(); } @@ -260,54 +201,56 @@ public class ICUResourceBundleImpl extends ICUResourceBundle { String itemKey = getKey(currentOffset, index); currentOffset += getCharOffset(size + (~size & 1)) + getIntOffset(index); - long resource = (UNSIGNED_INT_MASK) & ICUResourceBundleImpl.getInt(rawData,currentOffset); + long resource = (UNSIGNED_INT_MASK) & ICUResourceBundle.getInt(rawData,currentOffset); String path = (isTopLevel == true) ? Integer.toString(index) : resPath + "/" + index; - return createBundleObject(itemKey, resource, path, table, requested); + return createBundleObject(itemKey, resource, path, table, requested, this); } private int countItems() { int offset = RES_GET_OFFSET(resource); int value = getChar(rawData,offset); return value; } - private ResourceTable(String key, String resPath, long resource) { - this(key, resource, resPath, false); + ResourceTable(String key, String resPath, long resource, ICUResourceBundle bundle) { + this(key, resPath, resource, bundle, false); } - private ResourceTable(String key, long resource, String resPath, - boolean isTopLevel) { + ResourceTable(ICUResourceBundleReader reader, String baseName, String localeID, ClassLoader loader) { + + this.rawData = reader.getData(); + this.rootResource = (UNSIGNED_INT_MASK) & reader.getRootResource(); + this.noFallback = reader.getNoFallback(); + this.baseName = baseName; + this.localeID = localeID; + this.ulocale = new ULocale(localeID); + this.loader = loader; + initialize(null, "", rootResource, null, isTopLevel); + } + void initialize(String key, String resPath, long resource, + ICUResourceBundle bundle, boolean isTopLevel){ + if(bundle!=null){ + assign(this, bundle); + } this.key = key; this.resource = resource; this.isTopLevel = isTopLevel; this.size = countItems(); this.resPath = resPath; } - protected String getLocaleID() { - return localeID; - } - protected String getBaseName() { - return baseName; - } - - public ULocale getULocale() { - return ulocale; - } - public UResourceBundle getParent() { - return ICUResourceBundleImpl.this.getParent(); - } - protected void setParent(ResourceBundle parent) { - ICUResourceBundleImpl.this.setParent(parent); + ResourceTable(String key, String resPath, long resource, + ICUResourceBundle bundle, boolean isTopLevel) { + initialize(key, resPath, resource, bundle, isTopLevel); } } - private class ResourceTable32 extends ICUResourceBundle implements Resource { + static final class ResourceTable32 extends ICUResourceBundle{ - protected ICUResourceBundle handleGet(String key, ICUResourceBundle requested) { + protected UResourceBundle handleGet(String key, UResourceBundle requested) { if(size<=0){ return null; } return handleGet(key, null, requested); } - protected ICUResourceBundle handleGet(String key, HashMap table, ICUResourceBundle requested) { + protected UResourceBundle handleGet(String key, HashMap table, UResourceBundle requested) { int offset = RES_GET_OFFSET(resource); // offset+0 contains number of entries // offset+1 contains the keyOffset @@ -322,19 +265,19 @@ public class ICUResourceBundleImpl extends ICUResourceBundle { key); } currentOffset += getIntOffset(size) + getIntOffset(foundOffset); - long resource = (UNSIGNED_INT_MASK) & ICUResourceBundleImpl.getInt(rawData,currentOffset); + long resource = (UNSIGNED_INT_MASK) & ICUResourceBundle.getInt(rawData,currentOffset); String path = (isTopLevel == true) ? key : resPath + "/" + key; - return createBundleObject(key, resource, path, table, requested); + return createBundleObject(key, resource, path, table, requested, this); } - protected ICUResourceBundle handleGet(int index, ICUResourceBundle requested) { + protected UResourceBundle handleGet(int index, UResourceBundle requested) { return handleGet(index, null, requested); } public String getKey(int currentOffset, int index) { int charOffset = currentOffset + getIntOffset(index); - int keyOffset = ICUResourceBundleImpl.getInt(rawData,charOffset); + int keyOffset = ICUResourceBundle.getInt(rawData,charOffset); return RES_GET_KEY(rawData, keyOffset).toString(); } - protected ICUResourceBundle handleGet(int index, HashMap table, ICUResourceBundle requested) { + protected UResourceBundle handleGet(int index, HashMap table, UResourceBundle requested) { if (index > size) { throw new IndexOutOfBoundsException(); } @@ -345,380 +288,46 @@ public class ICUResourceBundleImpl extends ICUResourceBundle { + getIntOffset(index); String itemKey = getKey(currentOffset, 0); currentOffset += getIntOffset(size); - long resource = (UNSIGNED_INT_MASK) & ICUResourceBundleImpl.getInt(rawData,currentOffset); + long resource = (UNSIGNED_INT_MASK) & ICUResourceBundle.getInt(rawData,currentOffset); String path = (isTopLevel == true) ? Integer.toString(index) : resPath + "/" + index; - return createBundleObject(itemKey, resource, path, table, requested); + return createBundleObject(itemKey, resource, path, table, requested, this); } private int countItems() { int offset = RES_GET_OFFSET(resource); - int value = ICUResourceBundleImpl.getInt(rawData, offset); + int value = ICUResourceBundle.getInt(rawData, offset); return value; } - private ResourceTable32(String key, long resource, String resPath, - boolean isTopLevel) { - this.resource = resource; + ResourceTable32(String key, String resPath, long resource, ICUResourceBundle bundle) { + this(key, resPath, resource, bundle, false); + } + ResourceTable32(ICUResourceBundleReader reader, String baseName, String localeID, ClassLoader loader) { + + this.rawData = reader.getData(); + this.rootResource = (UNSIGNED_INT_MASK) & reader.getRootResource(); + this.noFallback = reader.getNoFallback(); + this.baseName = baseName; + this.localeID = localeID; + this.ulocale = new ULocale(localeID); + this.loader = loader; + initialize(null, "", rootResource, null, isTopLevel); + } + void initialize(String key, String resPath, long resource, + ICUResourceBundle bundle, boolean isTopLevel){ + if(bundle!=null){ + assign(this, bundle); + } this.key = key; + this.resource = resource; this.isTopLevel = isTopLevel; this.size = countItems(); this.resPath = resPath; - } - private ResourceTable32(String key, String resPath, long resource) { - this(key, resource, resPath, false); - } - - protected String getLocaleID() { - return localeID; - } - protected String getBaseName() { - return baseName; - } - - public ULocale getULocale() { - return ulocale; - } - public UResourceBundle getParent() { - return ICUResourceBundleImpl.this.getParent(); - } - protected void setParent(ResourceBundle parent) { - ICUResourceBundleImpl.this.setParent(parent); - } - - } - private class ResourceString extends ICUResourceBundle { - private String value; - public String getString() { - return value; - } - private ResourceString(String key, String resPath, long resource) { - value = getStringValue(resource); - this.key = key; - this.resource = resource; - this.resPath = resPath; - } - protected String getLocaleID() { - return localeID; - } - protected String getBaseName() { - return baseName; - } - - public ULocale getULocale() { - return ulocale; - } - public UResourceBundle getParent() { - return ICUResourceBundleImpl.this.getParent(); - } - } - private class ResourceInt extends ICUResourceBundle { - public int getInt() { - return RES_GET_INT(resource); - } - public int getUInt() { - long ret = RES_GET_UINT(resource); - return (int) ret; - } - private ResourceInt(String key, String resPath, long resource) { - this.key = key; - this.resource = resource; - this.resPath = resPath; - } - protected String getLocaleID() { - return localeID; - } - protected String getBaseName() { - return baseName; - } - - public ULocale getULocale() { - return ulocale; - } - public UResourceBundle getParent() { - return ICUResourceBundleImpl.this.getParent(); - } - } - private class ResourceArray extends ICUResourceBundle { - protected String[] handleGetStringArray() { - String[] strings = new String[size]; - ICUResourceBundleIterator iter = getIterator(); - int i = 0; - while (iter.hasNext()) { - strings[i++] = iter.next().getString(); - } - return strings; - } - /** - * @internal ICU 3.0 - */ - public String[] getStringArray() { - return handleGetStringArray(); - } - protected ICUResourceBundle handleGet(String index, ICUResourceBundle requested) { - return handleGet(index, null, requested); - } - protected ICUResourceBundle handleGet(String index, HashMap table, ICUResourceBundle requested) { - int val = getIndex(index); - if (val > -1) { - return handleGet(val, table, requested); - } - throw new UResourceTypeMismatchException("Could not get the correct value for index: "+ index); - } - protected ICUResourceBundle handleGet(int index, ICUResourceBundle requested) { - return handleGet(index, null, requested); - } - protected ICUResourceBundle handleGet(int index, HashMap table, ICUResourceBundle requested) { - if (index > size) { - throw new IndexOutOfBoundsException(); - } - int offset = RES_GET_OFFSET(resource); - int itemOffset = offset + getIntOffset(index + 1); - long itemResource = (UNSIGNED_INT_MASK) & ICUResourceBundleImpl.getInt(rawData,itemOffset); - String path = (isTopLevel == true) ? Integer.toString(index) : resPath + "/" + index; - return createBundleObject(null, itemResource, path, table, requested); - } - private int countItems() { - int offset = RES_GET_OFFSET(resource); - int value = ICUResourceBundleImpl.getInt(rawData,offset); - return value; - } - private ResourceArray(String key, String resPath, long resource) { - this.resource = resource; - this.key = key; - this.size = countItems(); - this.resPath = resPath; - } - protected String getLocaleID() { - return localeID; - } - protected String getBaseName() { - return baseName; - } - - public ULocale getULocale() { - return ulocale; - } - public UResourceBundle getParent() { - return ICUResourceBundleImpl.this.getParent(); - } - } - private static char makeChar(byte b1, byte b0) { - return (char)((b1 << 8) | (b0 & 0xff)); - } - private static char getChar(byte[]data, int offset){ - return makeChar(data[offset], data[offset+1]); - } - private static int makeInt(byte b3, byte b2, byte b1, byte b0) { - return (int)((((b3 & 0xff) << 24) | - ((b2 & 0xff) << 16) | - ((b1 & 0xff) << 8) | - ((b0 & 0xff) << 0))); - } - - private static int getInt(byte[] data, int offset){ - if (ASSERT) Assert.assrt("offset < data.length", offset < data.length); - return makeInt(data[offset], data[offset+1], - data[offset+2], data[offset+3]); - } - - private class ResourceBinary extends ICUResourceBundle { - private byte[] value; - public ByteBuffer getBinary() { - return ByteBuffer.wrap(value); - } - public byte [] getBinary(byte []ba) { - return value; - } - private byte[] getValue() { - int offset = RES_GET_OFFSET(resource); - int length = ICUResourceBundleImpl.getInt(rawData,offset); - int byteOffset = offset + getIntOffset(1); - byte[] dst = new byte[length]; - if (ASSERT) Assert.assrt("byteOffset+length < rawData.length", byteOffset+length < rawData.length); - System.arraycopy(rawData, byteOffset, dst, 0, length); - return dst; - } - public ResourceBinary(String key, String resPath, long resource) { - this.resource = resource; - this.key = key; - this.resPath = resPath; - value = getValue(); - } - protected String getLocaleID() { - return localeID; - } - protected String getBaseName() { - return baseName; - } - - public ULocale getULocale() { - return ulocale; - } - public UResourceBundle getParent() { - return ICUResourceBundleImpl.this.getParent(); - } - } - private class ResourceIntVector extends ICUResourceBundle { - private int[] value; - public int[] getIntVector() { - return value; - } - private int[] getValue() { - int offset = RES_GET_OFFSET(resource); - int length = ICUResourceBundleImpl.getInt(rawData,offset); - int intOffset = offset + getIntOffset(1); - int[] val = new int[length]; - int byteLength = getIntOffset(length); - if (ASSERT) Assert.assrt("(intOffset+byteLength)= 1) { - return Integer.valueOf(s).intValue(); - } - return -1; - } - private ICUResourceBundle findResource(String key, long resource, - HashMap table, - ICUResourceBundle requested) { - ClassLoader loaderToUse = loader; - String locale = null, keyPath = null; - String bundleName; - String resPath = getStringValue(resource); - if (table == null) { - table = new HashMap(); - } - if (table.get(resPath) != null) { - throw new IllegalArgumentException( - "Circular references in the resource bundles"); - } - table.put(resPath, ""); - if (resPath.indexOf(RES_PATH_SEP_CHAR) == 0) { - int i = resPath.indexOf(RES_PATH_SEP_CHAR, 1); - int j = resPath.indexOf(RES_PATH_SEP_CHAR, i + 1); - bundleName = resPath.substring(1, i); - locale = resPath.substring(i + 1); - if (j != -1) { - locale = resPath.substring(i + 1, j); - keyPath = resPath.substring(j + 1, resPath.length()); - } - //there is a path included - if (bundleName.equals(ICUDATA)) { - bundleName = ICU_BASE_NAME; - loaderToUse = ICU_DATA_CLASS_LOADER; - }else if(bundleName.indexOf(ICUDATA)>-1){ - int idx = bundleName.indexOf(HYPHEN); - if(idx>-1){ - bundleName = ICU_BASE_NAME+RES_PATH_SEP_STR+bundleName.substring(idx+1,bundleName.length()); - loaderToUse = ICU_DATA_CLASS_LOADER; - } - } - } else { - //no path start with locale - int i = resPath.indexOf(RES_PATH_SEP_CHAR); - keyPath = resPath.substring(i + 1); - if (i != -1) { - locale = resPath.substring(0, i); - } else { - locale = keyPath; - keyPath = null;//keyPath.substring(i, keyPath.length()); - } - bundleName = baseName; - } - ICUResourceBundle bundle = null; - ICUResourceBundle sub = null; - if(bundleName.equals(LOCALE)){ - bundleName = baseName; - bundle = requested; - keyPath = resPath.substring(LOCALE.length() + 2/* prepending and appending / */, resPath.length()); - locale = requested.getLocaleID(); - sub = ICUResourceBundle.findResourceWithFallback(keyPath, requested, null); - sub.resPath = "/" + sub.getLocaleID() + "/" + keyPath; - }else{ - if (locale == null) { - // {dlf} must use requestor's class loader to get resources from same jar - bundle = (ICUResourceBundle) getBundleInstance(bundleName, "", - loaderToUse, false); - } else { - bundle = (ICUResourceBundle) getBundleInstance(bundleName, locale, - loaderToUse, false); - } - if (keyPath != null) { - StringTokenizer st = new StringTokenizer(keyPath, "/"); - ICUResourceBundle current = bundle; - while (st.hasMoreTokens()) { - String subKey = st.nextToken(); - sub = current.getImpl(subKey, table, requested); - if (sub == null) { - break; - } - current = sub; - } - } else { - // if the sub resource is not found - // try fetching the sub resource with - // the key of this alias resource - sub = bundle.get(key); - } - sub.resPath = resPath; - } - if (sub == null) { - throw new MissingResourceException(localeID, baseName, key); - } - return sub; } } - diff --git a/icu4j/src/com/ibm/icu/impl/OlsonTimeZone.java b/icu4j/src/com/ibm/icu/impl/OlsonTimeZone.java index dc242bcf565..63e7d40b533 100644 --- a/icu4j/src/com/ibm/icu/impl/OlsonTimeZone.java +++ b/icu4j/src/com/ibm/icu/impl/OlsonTimeZone.java @@ -14,6 +14,7 @@ import com.ibm.icu.util.Calendar; import com.ibm.icu.util.GregorianCalendar; import com.ibm.icu.util.SimpleTimeZone; import com.ibm.icu.util.TimeZone; +import com.ibm.icu.util.UResourceBundle; /** * A time zone based on the Olson database. Olson time zones change @@ -340,10 +341,10 @@ public class OlsonTimeZone extends TimeZone { * to lookup the rule that `res' may refer to, if there is one. * @param res the resource bundle of the zone to be constructed */ - public OlsonTimeZone(ICUResourceBundle top, ICUResourceBundle res){ + public OlsonTimeZone(UResourceBundle top, UResourceBundle res){ construct(top, res); } - private void construct(ICUResourceBundle top, ICUResourceBundle res){ + private void construct(UResourceBundle top, UResourceBundle res){ if ((top == null || res == null)) { throw new IllegalArgumentException(); @@ -368,7 +369,7 @@ public class OlsonTimeZone extends TimeZone { } // Transitions list may be empty - ICUResourceBundle r = res.get(0); + UResourceBundle r = res.get(0); transitionTimes = r.getIntVector(); if ((transitionTimes.length<0 || transitionTimes.length>0x7FFF) ) { @@ -446,8 +447,8 @@ public class OlsonTimeZone extends TimeZone { public OlsonTimeZone(String id){ - ICUResourceBundle top = (ICUResourceBundle)ICUResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "zoneinfo", ICUResourceBundle.ICU_DATA_CLASS_LOADER); - ICUResourceBundle res = ZoneMeta.openOlsonResource(id); + UResourceBundle top = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "zoneinfo", ICUResourceBundle.ICU_DATA_CLASS_LOADER); + UResourceBundle res = ZoneMeta.openOlsonResource(id); construct(top, res); if(finalZone!=null){ finalZone.setID(id); @@ -641,8 +642,8 @@ public class OlsonTimeZone extends TimeZone { return julian - JULIAN_1970_CE; // JD => epoch day } - private static ICUResourceBundle loadRule(ICUResourceBundle top, String ruleid) { - ICUResourceBundle r = top.get("Rules"); + private static UResourceBundle loadRule(UResourceBundle top, String ruleid) { + UResourceBundle r = top.get("Rules"); r = r.get(ruleid); return r; } diff --git a/icu4j/src/com/ibm/icu/impl/ZoneMeta.java b/icu4j/src/com/ibm/icu/impl/ZoneMeta.java index 38162bc7df1..cca5258790a 100644 --- a/icu4j/src/com/ibm/icu/impl/ZoneMeta.java +++ b/icu4j/src/com/ibm/icu/impl/ZoneMeta.java @@ -1,6 +1,6 @@ /* ********************************************************************** -* Copyright (c) 2003-2006, International Business Machines +* Copyright (c) 2003-20067 International Business Machines * Corporation and others. All Rights Reserved. ********************************************************************** * Author: Alan Liu @@ -56,10 +56,10 @@ public final class ZoneMeta { return EMPTY; } try{ - ICUResourceBundle top = (ICUResourceBundle)ICUResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "zoneinfo", ICUResourceBundle.ICU_DATA_CLASS_LOADER); - ICUResourceBundle regions = top.get(kREGIONS); - ICUResourceBundle names = top.get(kNAMES); // dereference Zones section - ICUResourceBundle temp = regions.get(country); + UResourceBundle top = (ICUResourceBundle)ICUResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "zoneinfo", ICUResourceBundle.ICU_DATA_CLASS_LOADER); + UResourceBundle regions = top.get(kREGIONS); + UResourceBundle names = top.get(kNAMES); // dereference Zones section + UResourceBundle temp = regions.get(country); int[] vector = temp.getIntVector(); if (ASSERT) Assert.assrt("vector.length>0", vector.length>0); String[] ret = new String[vector.length]; @@ -79,8 +79,8 @@ public final class ZoneMeta { return EMPTY; } try{ - ICUResourceBundle top = (ICUResourceBundle)ICUResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "zoneinfo", ICUResourceBundle.ICU_DATA_CLASS_LOADER); - ICUResourceBundle names = top.get(kNAMES); // dereference Zones section + UResourceBundle top = (ICUResourceBundle)ICUResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "zoneinfo", ICUResourceBundle.ICU_DATA_CLASS_LOADER); + UResourceBundle names = top.get(kNAMES); // dereference Zones section return names.getStringArray(); }catch(MissingResourceException ex){ //throw away the exception @@ -110,8 +110,8 @@ public final class ZoneMeta { } private static String getID(int i) { try{ - ICUResourceBundle top = (ICUResourceBundle)ICUResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "zoneinfo", ICUResourceBundle.ICU_DATA_CLASS_LOADER); - ICUResourceBundle names = top.get(kNAMES); // dereference Zones section + UResourceBundle top = (ICUResourceBundle)ICUResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "zoneinfo", ICUResourceBundle.ICU_DATA_CLASS_LOADER); + UResourceBundle names = top.get(kNAMES); // dereference Zones section return names.getString(i); }catch(MissingResourceException ex){ //throw away the exception @@ -133,10 +133,10 @@ public final class ZoneMeta { */ public static synchronized int countEquivalentIDs(String id) { - ICUResourceBundle res = openOlsonResource(id); + UResourceBundle res = openOlsonResource(id); int size = res.getSize(); if (size == 4 || size == 6) { - ICUResourceBundle r=res.get(size-1); + UResourceBundle r=res.get(size-1); //result = ures_getSize(&r); // doesn't work int[] v = r.getIntVector(); return v.length; @@ -164,19 +164,19 @@ public final class ZoneMeta { */ public static synchronized String getEquivalentID(String id, int index) { String result=""; - ICUResourceBundle res = openOlsonResource(id); + UResourceBundle res = openOlsonResource(id); int zone = -1; int size = res.getSize(); if (size == 4 || size == 6) { - ICUResourceBundle r = res.get(size-1); + UResourceBundle r = res.get(size-1); int[] v = r.getIntVector(); if (index >= 0 && index < v.length && getOlsonMeta()) { zone = v[index]; } } if (zone >= 0) { - ICUResourceBundle top = (ICUResourceBundle)ICUResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "zoneinfo", ICUResourceBundle.ICU_DATA_CLASS_LOADER); - ICUResourceBundle ares = top.get(kNAMES); // dereference Zones section + UResourceBundle top = UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "zoneinfo", ICUResourceBundle.ICU_DATA_CLASS_LOADER); + UResourceBundle ares = top.get(kNAMES); // dereference Zones section result = ares.getString(zone); } @@ -379,19 +379,19 @@ public final class ZoneMeta { * @param id zone id * @return top-level resource bundle */ - public static ICUResourceBundle openOlsonResource(String id) + public static UResourceBundle openOlsonResource(String id) { if(!getOlsonMeta()){ return null; } - ICUResourceBundle top = (ICUResourceBundle)ICUResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "zoneinfo", ICUResourceBundle.ICU_DATA_CLASS_LOADER); - ICUResourceBundle res = getZoneByName(top, id); + ICUResourceBundle top = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "zoneinfo", ICUResourceBundle.ICU_DATA_CLASS_LOADER); + UResourceBundle res = getZoneByName(top, id); // Dereference if this is an alias. Docs say result should be 1 // but it is 0 in 2.8 (?). if (res.getSize() <= 1 && getOlsonMeta(top)) { int deref = res.getInt() + 0; - ICUResourceBundle ares = top.get(kZONES); // dereference Zones section - res = ares.get(deref); + UResourceBundle ares = top.get(kZONES); // dereference Zones section + res = (ICUResourceBundle) ares.get(deref); } return res; } @@ -401,16 +401,16 @@ public final class ZoneMeta { * @param id Time zone ID * @return the zone's bundle if found, or undefined if error. Reuses oldbundle. */ - private static ICUResourceBundle getZoneByName(ICUResourceBundle top, String id) { + private static UResourceBundle getZoneByName(UResourceBundle top, String id) { // load the Rules object - ICUResourceBundle tmp = top.get(kNAMES); + UResourceBundle tmp = top.get(kNAMES); // search for the string int idx = findInStringArray(tmp, id); if((idx == -1)) { // not found - throw new MissingResourceException(kNAMES, tmp.resPath, id); + throw new MissingResourceException(kNAMES, ((ICUResourceBundle)tmp).getResPath(), id); //ures_close(oldbundle); //oldbundle = NULL; } else { @@ -419,7 +419,7 @@ public final class ZoneMeta { } return tmp; } - private static int findInStringArray(ICUResourceBundle array, String id){ + private static int findInStringArray(UResourceBundle array, String id){ int start = 0; int limit = array.getSize(); int mid; @@ -476,7 +476,7 @@ public final class ZoneMeta { */ private static boolean getOlsonMeta(ICUResourceBundle top) { if (OLSON_ZONE_START < 0) { - ICUResourceBundle res = top.get(kZONES); + UResourceBundle res = top.get(kZONES); OLSON_ZONE_COUNT = res.getSize(); OLSON_ZONE_START = 0; } @@ -502,8 +502,8 @@ public final class ZoneMeta { TimeZone z = (TimeZone)zoneCache.get(id); if (z == null) { try{ - ICUResourceBundle top = (ICUResourceBundle)ICUResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "zoneinfo", ICUResourceBundle.ICU_DATA_CLASS_LOADER); - ICUResourceBundle res = openOlsonResource(id); + UResourceBundle top = UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, "zoneinfo", ICUResourceBundle.ICU_DATA_CLASS_LOADER); + UResourceBundle res = openOlsonResource(id); z = new OlsonTimeZone(top, res); z.setID(id); zoneCache.put(id, z); diff --git a/icu4j/src/com/ibm/icu/lang/UScript.java b/icu4j/src/com/ibm/icu/lang/UScript.java index 3a1cd75a5f6..208b90b2032 100755 --- a/icu4j/src/com/ibm/icu/lang/UScript.java +++ b/icu4j/src/com/ibm/icu/lang/UScript.java @@ -1,6 +1,6 @@ /** ******************************************************************************* -* Copyright (C) 2001-2006, International Business Machines Corporation and * +* Copyright (C) 2001-20067 International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* */ @@ -664,7 +664,7 @@ public final class UScript { if(rb.getLoadingStatus()==ICUResourceBundle.FROM_DEFAULT && ! locale.equals(ULocale.getDefault())){ return null; } - ICUResourceBundle sub = rb.get(kLocaleScript); + UResourceBundle sub = rb.get(kLocaleScript); int[] result = new int[sub.getSize()]; int w = 0; diff --git a/icu4j/src/com/ibm/icu/text/DateFormatSymbols.java b/icu4j/src/com/ibm/icu/text/DateFormatSymbols.java index ceb26d4c325..901f5076cc8 100755 --- a/icu4j/src/com/ibm/icu/text/DateFormatSymbols.java +++ b/icu4j/src/com/ibm/icu/text/DateFormatSymbols.java @@ -1,6 +1,6 @@ /* ******************************************************************************* - * Copyright (C) 1996-2006, International Business Machines Corporation and * + * Copyright (C) 1996-2007, International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* */ @@ -1299,7 +1299,7 @@ public class DateFormatSymbols implements Serializable, Cloneable { ICUResourceBundle bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, tempLocale); ICUResourceBundle zoneStringsBundle = bundle.getWithFallback("zoneStrings"); for(int i = 0; i < zoneStringsBundle.getSize(); i++){ - ICUResourceBundle zoneTable = zoneStringsBundle.get(i); + ICUResourceBundle zoneTable = (ICUResourceBundle)zoneStringsBundle.get(i); String key = Utility.replaceAll(zoneTable.getKey(), ":", "/"); // hack for the root zone strings if(key.length() == 0|| zoneTable.getType() != ICUResourceBundle.TABLE){ diff --git a/icu4j/src/com/ibm/icu/text/DecimalFormatSymbols.java b/icu4j/src/com/ibm/icu/text/DecimalFormatSymbols.java index e7b9f8a509b..53146a7d71f 100755 --- a/icu4j/src/com/ibm/icu/text/DecimalFormatSymbols.java +++ b/icu4j/src/com/ibm/icu/text/DecimalFormatSymbols.java @@ -1,6 +1,6 @@ /* ******************************************************************************* - * Copyright (C) 1996-2006, International Business Machines Corporation and * + * Copyright (C) 1996-2007, International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* */ @@ -658,7 +658,7 @@ final public class DecimalFormatSymbols implements Cloneable, Serializable { try{ currency = currency.getWithFallback(currencyCode); if(currency.getSize()>2) { - currency = currency.get(2); + currency = (ICUResourceBundle)currency.get(2); currencyPattern = currency.getString(0); monetarySeparator = currency.getString(1).charAt(0); monetaryGroupingSeparator = currency.getString(2).charAt(0); diff --git a/icu4j/src/com/ibm/icu/text/RuleBasedNumberFormat.java b/icu4j/src/com/ibm/icu/text/RuleBasedNumberFormat.java index 0d7d0af9776..4bfe7f78aca 100755 --- a/icu4j/src/com/ibm/icu/text/RuleBasedNumberFormat.java +++ b/icu4j/src/com/ibm/icu/text/RuleBasedNumberFormat.java @@ -1,7 +1,7 @@ //##header /* ******************************************************************************* - * Copyright (C) 1996-2006, International Business Machines Corporation and * + * Copyright (C) 1996-2007, International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* */ @@ -747,7 +747,7 @@ public class RuleBasedNumberFormat extends NumberFormat { try { description = bundle.getString(rulenames[format-1]); - ICUResourceBundle locb = bundle.get(locnames[format-1]); + UResourceBundle locb = bundle.get(locnames[format-1]); localizations = new String[locb.getSize()][]; for (int i = 0; i < localizations.length; ++i) { localizations[i] = locb.get(i).getStringArray(); diff --git a/icu4j/src/com/ibm/icu/text/Transliterator.java b/icu4j/src/com/ibm/icu/text/Transliterator.java index 9b03dbc00d6..bae57514273 100755 --- a/icu4j/src/com/ibm/icu/text/Transliterator.java +++ b/icu4j/src/com/ibm/icu/text/Transliterator.java @@ -1826,8 +1826,8 @@ public abstract class Transliterator { * * 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); + UResourceBundle bundle, transIDs, colBund; + bundle = UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_TRANSLIT_BASE_NAME, INDEX); transIDs = bundle.get(RB_RULE_BASED_IDS); int row, maxRows; @@ -1835,7 +1835,7 @@ public abstract class Transliterator { for (row = 0; row < maxRows; row++) { colBund = transIDs.get(row); String ID = colBund.getKey(); - ICUResourceBundle res = colBund.get(0); + UResourceBundle res = colBund.get(0); String type = res.getKey(); if (type.equals("file") || type.equals("internal")) { // Rest of line is :: diff --git a/icu4j/src/com/ibm/icu/util/Calendar.java b/icu4j/src/com/ibm/icu/util/Calendar.java index c44a2682f9b..f606e678dae 100755 --- a/icu4j/src/com/ibm/icu/util/Calendar.java +++ b/icu4j/src/com/ibm/icu/util/Calendar.java @@ -1,12 +1,11 @@ /* -* Copyright (C) 1996-2006, International Business Machines +* Copyright (C) 1996-2007, International Business Machines * Corporation and others. All Rights Reserved. */ package com.ibm.icu.util; import com.ibm.icu.impl.ICULocaleService; -import com.ibm.icu.impl.ICULocaleService.LocaleKeyFactory; import com.ibm.icu.impl.ICUResourceBundle; import com.ibm.icu.impl.ICUService.Factory; import com.ibm.icu.impl.CalendarData; diff --git a/icu4j/src/com/ibm/icu/util/Currency.java b/icu4j/src/com/ibm/icu/util/Currency.java index 6ffb6d4dee6..2ff8a539224 100644 --- a/icu4j/src/com/ibm/icu/util/Currency.java +++ b/icu4j/src/com/ibm/icu/util/Currency.java @@ -1,6 +1,6 @@ /** ******************************************************************************* - * Copyright (C) 2001-2006, International Business Machines Corporation and * + * Copyright (C) 2001-2007, International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* */ @@ -135,7 +135,7 @@ public class Currency extends MeasureUnit implements Serializable { if(bundle==null){ //throw new MissingResourceException() } - ICUResourceBundle cm = bundle.get("CurrencyMap"); + UResourceBundle cm = bundle.get("CurrencyMap"); // Do a linear search String curriso = null; @@ -383,8 +383,8 @@ public class Currency extends MeasureUnit implements Serializable { String s = null; try { - ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,locale); - ICUResourceBundle currencies = rb.get("Currencies"); + UResourceBundle rb = UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,locale); + ICUResourceBundle currencies = (ICUResourceBundle)rb.get("Currencies"); // Fetch resource with multi-level resource inheritance fallback s = currencies.getWithFallback(isoCode).getString(nameStyle); @@ -474,15 +474,15 @@ public class Currency extends MeasureUnit implements Serializable { // Multi-level resource inheritance fallback loop while (locale != null) { - ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,locale); + UResourceBundle rb = UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,locale); // We can't cast this to String[][]; the cast has to happen later try { - ICUResourceBundle currencies = rb.get("Currencies"); + UResourceBundle currencies = rb.get("Currencies"); // Do a linear search for (int i=0; iWarning: Integer array is treated as a scalar type. There are no + * APIs to access individual members of an integer array. It + * is always returned as a whole. + * @return number of resources in a given resource. + * @draft ICU 3.8 + */ + public int getSize() { + return size; + } + + /** + * Returns the type of a resource. + * Available types are {@link #INT INT}, {@link #ARRAY ARRAY}, + * {@link #BINARY BINARY}, {@link #INT_VECTOR INT_VECTOR}, + * {@link #STRING STRING}, {@link #TABLE TABLE}. + * + * @return type of the given resource. + * @draft ICU 3.8 + */ + public int getType() { + int type = ICUResourceBundle.RES_GET_TYPE(resource); + if(type==TABLE32){ + return TABLE; //Mask the table32's real type + } + return type; + } + + /** + * Return the version number associated with this UResourceBundle as an + * VersionInfo object. + * @return VersionInfo object containing the version of the bundle + * @draft ICU 3.8 + */ + public VersionInfo getVersion() { + return null; + } + + /** + * Returns the iterator which iterates over this + * resource bundle + * @draft ICU 3.8 + */ + public UResourceBundleIterator getIterator() { + return new UResourceBundleIterator(this); + } + /** + * Returns the key associated with a given resource. Not all the resources have a key - only + * those that are members of a table. + * @return a key associated to this resource, or NULL if it doesn't have a key + * @draft ICU 3.8 + */ + public String getKey() { + return key; + } + /** + * Resource type constant for "no resource". + * @draft ICU 3.8 + */ + public static final int NONE = -1; + + /** + * Resource type constant for strings. + * @draft ICU 3.8 + */ + public static final int STRING = 0; + + /** + * Resource type constant for binary data. + * @draft ICU 3.8 + */ + public static final int BINARY = 1; + + /** + * Resource type constant for tables of key-value pairs. + * @draft ICU 3.8 + */ + public static final int TABLE = 2; + + /** + * Resource type constant for aliases; + * internally stores a string which identifies the actual resource + * storing the data (can be in a different resource bundle). + * Resolved internally before delivering the actual resource through the API. + * @draft ICU 3.8 + * @internal + */ + protected static final int ALIAS = 3; + + /** + * Internal use only. + * Alternative resource type constant for tables of key-value pairs. + * Never returned by getType(). + * @internal + * @draft ICU 3.8 + */ + protected static final int TABLE32 = 4; + + /** + * Resource type constant for a single 28-bit integer, interpreted as + * signed or unsigned by the getInt() function. + * @see #getInt + * @draft ICU 3.8 + */ + public static final int INT = 7; + + /** + * Resource type constant for arrays of resources. + * @draft ICU 3.8 + */ + public static final int ARRAY = 8; + + /** + * Resource type constant for vectors of 32-bit integers. + * @see #getIntVector + * @draft ICU 3.8 + */ + public static final int INT_VECTOR = 14; + + //====== protected members ============== + protected String key; + protected int size = 1; + protected long resource = RES_BOGUS; + protected boolean isTopLevel = false; + + protected static final long RES_BOGUS = 0xffffffff; + + protected UResourceBundle handleGet(String key, HashMap table, UResourceBundle requested) { + return null; + } + + protected UResourceBundle handleGet(int index, HashMap table, UResourceBundle requested) { + return null; + } + + protected UResourceBundle handleGet(int index, UResourceBundle requested) { + return null; + } + + protected UResourceBundle handleGet(String key, UResourceBundle requested) { + return null; + } + + protected String[] handleGetStringArray() { + return null; + } + protected Enumeration handleGetKeys(){ + Vector keys = new Vector(); + UResourceBundle item = null; + for (int i = 0; i < size; i++) { + item = get(i); + keys.add(item.getKey()); + } + return keys.elements(); + } + // this method is declared in ResourceBundle class + // so cannot change the signature + protected Object handleGetObject(String key) { + return handleGetObjectImpl(key, this); + } + + // To facilitate XPath style aliases we need a way to pass the reference + // to requested locale. The only way I could figure out is to implement + // the look up logic here. This has a disadvantage that if the client + // loads an ICUResourceBundle, calls ResourceBundle.getObject method + // with a key that does not exist in the bundle then the lookup is + // done twice before throwing a MissingResourceExpection. + private Object handleGetObjectImpl(String key, UResourceBundle requested) { + Object obj = resolveObject(key, requested); + if (obj == null) { + UResourceBundle parent = getParent(); + if (parent != null) { + obj = parent.handleGetObjectImpl(key, requested); + } + if (obj == null) + throw new MissingResourceException( + "Can't find resource for bundle " + + this.getClass().getName() + ", key " + key, + this.getClass().getName(), key); + } + return obj; + } + // Routine for figuring out the type of object to be returned + // string or string array + private Object resolveObject(String key, UResourceBundle requested) { + if (getType() == STRING) { + return getString(); + } + UResourceBundle obj = handleGet(key, requested); + if (obj != null) { + if (obj.getType() == STRING) { + return obj.getString(); + } + try { + if (obj.getType() == ARRAY) { + return obj.handleGetStringArray(); + } + } catch (UResourceTypeMismatchException ex) { + return obj; + } + } + return obj; + } + /** * @internal * @deprecated This API is ICU internal only. diff --git a/icu4j/src/com/ibm/icu/impl/ICUResourceBundleIterator.java b/icu4j/src/com/ibm/icu/util/UResourceBundleIterator.java similarity index 87% rename from icu4j/src/com/ibm/icu/impl/ICUResourceBundleIterator.java rename to icu4j/src/com/ibm/icu/util/UResourceBundleIterator.java index 6698d322328..773a5e1e33a 100644 --- a/icu4j/src/com/ibm/icu/impl/ICUResourceBundleIterator.java +++ b/icu4j/src/com/ibm/icu/util/UResourceBundleIterator.java @@ -1,98 +1,97 @@ -/* -****************************************************************************** -* Copyright (C) 2004, International Business Machines Corporation and * -* others. All Rights Reserved. * -****************************************************************************** -*/ - -package com.ibm.icu.impl; - -import java.util.NoSuchElementException; - -import com.ibm.icu.util.UResourceTypeMismatchException; - - -/** - *

Class for enabling iteration over UResourceBundle objects. - * Example of use:
- *

- * ICUResourceBundleIterator iterator = resB.getIterator();
- * ICUResourceBundle temp;
- * while (iterator.hasNext()) {
- *    temp = iterartor.next();  
- *    int type = temp.getType();
- *    switch(type){
- *      case UResourceBundle.STRING:
- *          str = temp.getString();
- *          break;
- *      case UResourceBundle.INT:
- *          integer = temp.getInt();
- *          break;
- *     .....
- *    }
- *   // do something interesting with data collected
- * }
- * 
- * @author ram - * @draft ICU 3.0 - */ -public class ICUResourceBundleIterator{ - private ICUResourceBundle bundle; - private int index = 0; - private int size = 0; - /** - * Construct a resource bundle iterator for the - * given resource bundle - * - * @param bndl The resource bundle to iterate over - * @draft ICU 3.0 - */ - public ICUResourceBundleIterator(ICUResourceBundle bndl){ - bundle = bndl; - size = bundle.getSize(); - } - - /** - * Returns the next element of this iterator if this iterator object has at least one more element to provide - * @return the UResourceBundle object - * @throws NoSuchElementException - * @draft ICU 3.0 - */ - public ICUResourceBundle next()throws NoSuchElementException{ - if(indexClass for enabling iteration over UResourceBundle objects. + * Example of use:
+ *
+ * ICUResourceBundleIterator iterator = resB.getIterator();
+ * ICUResourceBundle temp;
+ * while (iterator.hasNext()) {
+ *    temp = iterartor.next();  
+ *    int type = temp.getType();
+ *    switch(type){
+ *      case UResourceBundle.STRING:
+ *          str = temp.getString();
+ *          break;
+ *      case UResourceBundle.INT:
+ *          integer = temp.getInt();
+ *          break;
+ *     .....
+ *    }
+ *   // do something interesting with data collected
+ * }
+ * 
+ * @author ram + * @draft ICU 3.0 + */ +public class UResourceBundleIterator{ + private UResourceBundle bundle; + private int index = 0; + private int size = 0; + /** + * Construct a resource bundle iterator for the + * given resource bundle + * + * @param bndl The resource bundle to iterate over + * @draft ICU 3.0 + */ + public UResourceBundleIterator(UResourceBundle bndl){ + bundle = bndl; + size = bundle.getSize(); + } + + /** + * Returns the next element of this iterator if this iterator object has at least one more element to provide + * @return the UResourceBundle object + * @throws NoSuchElementException + * @draft ICU 3.0 + */ + public UResourceBundle next()throws NoSuchElementException{ + if(index