示例#1
0
        // Routine for figuring out the type of object to be returned
        // string or string array
        private Object ResolveObject(String key_0, UResourceBundle requested)
        {
            if (GetType() == STRING)
            {
                return(GetString());
            }
            UResourceBundle obj = HandleGet(key_0, null, 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);
        }
示例#2
0
 /// <summary>
 /// Construct a resource bundle iterator for the given resource bundle
 /// </summary>
 ///
 /// <param name="bndl">The resource bundle to iterate over</param>
 /// @draft ICU 3.8
 /// @provisional This API might change or be removed in a future release.
 public UResourceBundleIterator(UResourceBundle bndl)
 {
     this.index = 0;
     this.size  = 0;
     bundle     = bndl;
     size       = bundle.GetSize();
 }
示例#3
0
 /// <exclude/>
 /// <summary>
 /// Method used by subclasses to add the a particular resource bundle object
 /// to the managed cache
 /// </summary>
 ///
 protected static internal void AddToCache(Assembly cl, String fullName,
                                           ULocale defaultLocale, UResourceBundle b)
 {
     lock (cacheKey) {
         cacheKey.SetKeyValues(cl, fullName, defaultLocale);
         AddToCache((UResourceBundle.ResourceCacheKey)cacheKey.Clone(), b);
     }
 }
示例#4
0
        /// <summary>
        /// Returns the display name for the given currency in the given locale. For
        /// example, the display name for the USD currency object in the en_US locale
        /// is "$".
        /// </summary>
        ///
        /// <param name="locale">locale in which to display currency</param>
        /// <param name="nameStyle">selector for which kind of name to return</param>
        /// <param name="isChoiceFormat">fill-in; isChoiceFormat[0] is set to true if the returnedvalue is a ChoiceFormat pattern; otherwise it is set to false</param>
        /// <returns>display string for this currency. If the resource data contains
        /// no entry for this currency, then the ISO 4217 code is returned.
        /// If isChoiceFormat[0] is true, then the result is a ChoiceFormat
        /// pattern. Otherwise it is a static string.</returns>
        /// @stable ICU 3.2
        public String GetName(ULocale locale, int nameStyle,
                              bool[] isChoiceFormat)
        {
            // Look up the Currencies resource for the given locale. The
            // Currencies locale data looks like this:
            // |en {
            // | Currencies {
            // | USD { "US$", "US Dollar" }
            // | CHF { "Sw F", "Swiss Franc" }
            // | INR { "=0#Rs|1#Re|1<Rs", "=0#Rupees|1#Rupee|1<Rupees" }
            // | //...
            // | }
            // |}

            if (nameStyle < 0 || nameStyle > 1)
            {
                throw new ArgumentException();
            }

            String s = null;

            try {
                UResourceBundle rb = IBM.ICU.Util.UResourceBundle.GetBundleInstance(
                    IBM.ICU.Impl.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);
            } catch (MissingManifestResourceException e) {
                // TODO what should be done here?
            }

            // Determine if this is a ChoiceFormat pattern. One leading mark
            // indicates a ChoiceFormat. Two indicates a static string that
            // starts with a mark. In either case, the first mark is ignored,
            // if present. Marks in the rest of the string have no special
            // meaning.
            isChoiceFormat[0] = false;
            if (s != null)
            {
                int i = 0;
                while (i < s.Length && s[i] == '=' && i < 2)
                {
                    ++i;
                }
                isChoiceFormat[0] = (i == 1);
                if (i != 0)
                {
                    // Skip over first mark
                    s = s.Substring(1);
                }
                return(s);
            }

            // If we fail to find a match, use the ISO 4217 code
            return(isoCode);
        }
示例#5
0
        /// <summary>
        /// Returns the size of paper used in the locale. The paper sizes returned
        /// are always in <em> milli-meters<em>.
        /// </summary>
        ///
        /// <param name="locale">The locale for which the measurement system to be retrieved.</param>
        /// <returns>The paper size used in the locale</returns>
        /// @stable ICU 3.0
        public static LocaleData.PaperSize  GetPaperSize(ULocale locale)
        {
            UResourceBundle bundle_0 = (ICUResourceBundle)IBM.ICU.Util.UResourceBundle
                                       .GetBundleInstance(IBM.ICU.Impl.ICUResourceBundle.ICU_BASE_NAME, locale);
            UResourceBundle obj = bundle_0.Get(PAPER_SIZE);

            int[] size = obj.GetIntVector();
            return(new LocaleData.PaperSize(size[0], size[1]));
        }
示例#6
0
 public static String GetTZDataVersion()
 {
     if (TZDATA_VERSION == null)
     {
         UResourceBundle tzbundle = IBM.ICU.Util.UResourceBundle.GetBundleInstance(
             "com/ibm/icu/impl/data/icudt"
             + IBM.ICU.Util.VersionInfo.ICU_DATA_VERSION, "zoneinfo");
         TZDATA_VERSION = tzbundle.GetString("TZVersion");
     }
     return(TZDATA_VERSION);
 }
示例#7
0
        /// <summary>
        /// Loads a new resource bundle for the give base name, locale and class
        /// loader. Optionally will disable loading of fallback bundles.
        /// </summary>
        ///
        /// <param name="baseName">the base name of the resource bundle, a fully qualified classname</param>
        /// <param name="localeName">the locale for which a resource bundle is desired</param>
        /// <param name="root">the class object from which to load the resource bundle</param>
        /// <param name="disableFallback">disables loading of fallback lookup chain</param>
        /// <exception cref="MissingResourceException">if no resource bundle for the specified base name can befound</exception>
        /// <returns>a resource bundle for the given base name and locale</returns>
        /// @stable ICU 3.0
        protected static internal UResourceBundle InstantiateBundle(String baseName,
                                                                    String localeName, Assembly root, bool disableFallback)
        {
            UResourceBundle b        = null;
            int             rootType = GetRootType(baseName, root);

            ULocale defaultLocale_0 = IBM.ICU.Util.ULocale.GetDefault();

            switch (rootType)
            {
            case ROOT_ICU:
                if (disableFallback)
                {
                    String fullName = IBM.ICU.Impl.ICUResourceBundleReader.GetFullName(baseName,
                                                                                       localeName);
                    lock (cacheKey) {
                        cacheKey.SetKeyValues(root, fullName, defaultLocale_0);
                        b = LoadFromCache(cacheKey);
                    }

                    if (b == null)
                    {
                        b = IBM.ICU.Impl.ICUResourceBundle.GetBundleInstance(baseName,
                                                                             localeName, root, disableFallback);
                        // cacheKey.setKeyValues(root, fullName, defaultLocale);
                        AddToCache(cacheKey, b);
                    }
                }
                else
                {
                    b = IBM.ICU.Impl.ICUResourceBundle.GetBundleInstance(baseName, localeName,
                                                                         root, disableFallback);
                }

                return(b);

            case ROOT_JAVA:
                return(IBM.ICU.Impl.ResourceBundleWrapper.GetBundleInstance(baseName,
                                                                            localeName, root, disableFallback));

            default:
                try {
                    b = IBM.ICU.Impl.ICUResourceBundle.GetBundleInstance(baseName, localeName,
                                                                         root, disableFallback);
                    SetRootType(baseName, ROOT_ICU);
                } catch (MissingManifestResourceException ex) {
                    b = IBM.ICU.Impl.ResourceBundleWrapper.GetBundleInstance(baseName,
                                                                             localeName, root, disableFallback);
                    SetRootType(baseName, ROOT_JAVA);
                }
                return(b);
            }
        }
示例#8
0
        /// <summary>
        /// Actual worker method for fetching the keys of resources contained in the
        /// resource. Sub classes must override this method if they support keys and
        /// associated resources.
        /// </summary>
        ///
        /// <returns>Enumeration An enumeration of all the keys in this resource.</returns>
        /// @draft ICU 3.8
        /// @provisional This API might change or be removed in a future release.
        protected internal IIterator HandleGetKeys()
        {
            ArrayList       keys_0 = new ArrayList();
            UResourceBundle item   = null;

            for (int i = 0; i < size; i++)
            {
                item = Get(i);
                keys_0.Add(item.GetKey());
            }
            return(new ILOG.J2CsMapping.Collections.IteratorAdapter(keys_0.GetEnumerator()));
        }
示例#9
0
        /// <summary>
        /// Instantiate a currency from a resource bundle found in Locale loc.
        /// </summary>
        ///
        /* package */ static internal Currency CreateCurrency(ULocale loc)
        {
            String country   = loc.GetCountry();
            String variant   = loc.GetVariant();
            bool   isPreEuro = variant.Equals("PREEURO");
            bool   isEuro    = variant.Equals("EURO");
            // TODO: ICU4C has service registration, and the currency is requested
            // from the service here.
            ICUResourceBundle bundle = (ICUResourceBundle)IBM.ICU.Util.UResourceBundle
                                       .GetBundleInstance(IBM.ICU.Impl.ICUResourceBundle.ICU_BASE_NAME,
                                                          "supplementalData",
                                                          IBM.ICU.Impl.ICUResourceBundle.ICU_DATA_CLASS_LOADER);

            if (bundle == null)
            {
                // throw new MissingResourceException()
                return(null);
            }
            try {
                UResourceBundle cm           = bundle.Get("CurrencyMap");
                String          curriso      = null;
                UResourceBundle countryArray = cm.Get(country);
                // Some regions can have more than one current currency in use.
                // The latest default currency is always the first one.
                UResourceBundle currencyReq = countryArray.Get(0);
                curriso = currencyReq.GetString("id");
                if (isPreEuro && curriso.Equals(EUR_STR))
                {
                    currencyReq = countryArray.Get(1);
                    curriso     = currencyReq.GetString("id");
                }
                else if (isEuro)
                {
                    curriso = EUR_STR;
                }
                if (curriso != null)
                {
                    return(new Currency(curriso));
                }
            } catch (MissingManifestResourceException ex) {
                // We don't know about this region.
                // As of CLDR 1.5.1, the data includes deprecated region history
                // too.
                // So if we get here, either the region doesn't exist, or the data
                // is really bad.
                // Deprecated regions should return the last valid currency for that
                // region in the data.
                // We don't try to resolve it to a new region.
            }
            return(null);
        }
示例#10
0
        private static void AddToCache(UResourceBundle.ResourceCacheKey key, UResourceBundle b)
        {
            IDictionary m = null;

            if (BUNDLE_CACHE != null)
            {
                m = (IDictionary)BUNDLE_CACHE.Target;
            }
            if (m == null)
            {
                m            = new Hashtable();
                BUNDLE_CACHE = new WeakReference(m);
            }
            ILOG.J2CsMapping.Collections.Collections.Put(m, key, b);
        }
示例#11
0
        /// <summary>
        /// Override the superclass method
        /// </summary>
        ///
        // 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_0, UResourceBundle requested)
        {
            Object obj = ResolveObject(key_0, requested);

            if (obj == null)
            {
                UResourceBundle parent = GetParent();
                if (parent != null)
                {
                    obj = parent.HandleGetObjectImpl(key_0, requested);
                }
                if (obj == null)
                {
                    throw new MissingManifestResourceException("Can't find resource for bundle "
                                                               + ((object)this).GetType().FullName + ", key " + key_0);
                }
            }
            return(obj);
        }
示例#12
0
        /// <summary>
        /// Returns the measurement system used in the locale specified by the
        /// locale.
        /// </summary>
        ///
        /// <param name="locale">The locale for which the measurement system to be retrieved.</param>
        /// <returns>MeasurementSystem the measurement system used in the locale.</returns>
        /// @stable ICU 3.0
        public static LocaleData.MeasurementSystem  GetMeasurementSystem(ULocale locale)
        {
            UResourceBundle bundle_0 = (ICUResourceBundle)IBM.ICU.Util.UResourceBundle
                                       .GetBundleInstance(IBM.ICU.Impl.ICUResourceBundle.ICU_BASE_NAME, locale);
            UResourceBundle sysBundle = bundle_0.Get(MEASUREMENT_SYSTEM);

            int system = sysBundle.GetInt();

            if (IBM.ICU.Util.LocaleData.MeasurementSystem.US.Equals(system))
            {
                return(IBM.ICU.Util.LocaleData.MeasurementSystem.US);
            }
            if (IBM.ICU.Util.LocaleData.MeasurementSystem.SI.Equals(system))
            {
                return(IBM.ICU.Util.LocaleData.MeasurementSystem.SI);
            }
            // return null if the object is null or is not an instance
            // of integer indicating an error
            return(null);
        }
示例#13
0
        /// <summary>
        /// Internal function to look up currency data. Result is an array of two
        /// Integers. The first is the fraction digits. The second is the rounding
        /// increment, or 0 if none. The rounding increment is in units of
        /// 10^(-fraction_digits).
        /// </summary>
        ///
        private int[] FindData()
        {
            try {
                // Get CurrencyMeta resource out of root locale file. [This may
                // move out of the root locale file later; if it does, update this
                // code.]
                UResourceBundle root = IBM.ICU.Util.UResourceBundle.GetBundleInstance(
                    IBM.ICU.Impl.ICUResourceBundle.ICU_BASE_NAME, "supplementalData",
                    IBM.ICU.Impl.ICUResourceBundle.ICU_DATA_CLASS_LOADER);
                UResourceBundle currencyMeta = root.Get("CurrencyMeta");

                // Integer[] i = null;
                // int defaultPos = -1;
                int[] i = currencyMeta.Get(isoCode).GetIntVector();

                // Do a linear search for isoCode. At the same time,
                // record the position of the DEFAULT meta data. If the
                // meta data becomes large, make this faster.

                /*
                 * for (int j=0; j<currencyMeta.length; ++j) { Object[] row =
                 * currencyMeta[j]; String s = (String) row[0]; int c =
                 * isoCode.compareToIgnoreCase(s); if (c == 0) { i = (Integer[])
                 * row[1]; break; } if ("DEFAULT".equalsIgnoreCase(s)) { defaultPos
                 * = j; } if (c < 0 && defaultPos >= 0) { break; } }
                 */
                if (i == null)
                {
                    i = currencyMeta.Get("DEFAULT").GetIntVector();
                }

                if (i != null && i.Length >= 2)
                {
                    return(i);
                }
            } catch (MissingManifestResourceException e) {
            }

            // Config/build error; return hard-coded defaults
            return(LAST_RESORT_DATA);
        }
示例#14
0
        /// <summary>
        /// Returns the resource in a given resource at the specified index.
        /// </summary>
        ///
        /// <param name="index">an index to the wanted resource.</param>
        /// <returns>the sub resource UResourceBundle object</returns>
        /// <exception cref="IndexOutOfBoundsException"></exception>
        /// <exception cref="MissingResourceException"></exception>
        /// @draft ICU 3.8
        /// @provisional This API might change or be removed in a future release.
        public UResourceBundle Get(int index)
        {
            UResourceBundle obj = HandleGet(index, null, this);

            if (obj == null)
            {
                obj = (ICUResourceBundle)GetParent();
                if (obj != null)
                {
                    obj = obj.Get(index);
                }
                if (obj == null)
                {
                    throw new MissingManifestResourceException("Can't find resource for bundle "
                                                               + ((object)this).GetType().FullName + ", key "
                                                               + GetKey());
                }
            }
            IBM.ICU.Impl.ICUResourceBundle.SetLoadingStatus(obj, GetLocaleID());
            return(obj);
        }
示例#15
0
        /// <summary>
        /// Returns a resource in a given resource that has a given key.
        /// </summary>
        ///
        /// <param name="key">a key associated with the wanted resource</param>
        /// <returns>a resource bundle object representing rhe resource</returns>
        /// <exception cref="MissingResourceException"></exception>
        /// @draft ICU 3.8
        /// @provisional This API might change or be removed in a future release.
        public UResourceBundle Get(String key)
        {
            UResourceBundle obj = HandleGet(key, null, this);
            UResourceBundle res = this;

            if (obj == null)
            {
                while ((res = res.GetParent()) != null && obj == null)
                {
                    // call the get method to recursively fetch the resource
                    obj = res.HandleGet(key, null, this);
                }
                if (obj == null)
                {
                    String fullName = IBM.ICU.Impl.ICUResourceBundleReader.GetFullName(
                        GetBaseName(), GetLocaleID());
                    throw new MissingManifestResourceException("Can't find resource for bundle " + fullName + ", key "
                                                               + key);
                }
            }
            IBM.ICU.Impl.ICUResourceBundle.SetLoadingStatus(obj, GetLocaleID());
            return(obj);
        }
示例#16
0
 /// <summary>
 /// Actual worker method for fetching a resource based on the given key. Sub
 /// classes must override this method if they support resources with keys.
 /// </summary>
 ///
 /// <param name="key_0">the key string of the resource to be fetched</param>
 /// <param name="table">hastable object to hold references of resources already seen</param>
 /// <param name="requested">the original resource bundle object on which the get methodwas invoked. The requested bundle and the bundle on which thismethod is invoked are the same, except in the cases wherealiases are involved.</param>
 /// <returns>UResourceBundle a resource assoicated with the key</returns>
 /// @draft ICU 3.8
 /// @provisional This API might change or be removed in a future release.
 protected internal virtual UResourceBundle HandleGet(String key_0, Hashtable table,
                                                      UResourceBundle requested)
 {
     return(null);
 }
示例#17
0
        /// <exclude/>
        /// <summary>
        /// Attempt to parse the given string as a currency, either as a display name
        /// in the given locale, or as a 3-letter ISO 4217 code. If multiple display
        /// names match, then the longest one is selected. If both a display name and
        /// a 3-letter ISO code match, then the display name is preferred, unless
        /// it's length is less than 3.
        /// </summary>
        ///
        /// <param name="locale">the locale of the display names to match</param>
        /// <param name="text">the text to parse</param>
        /// <param name="pos">input-output position; on input, the position within text tomatch; must have 0 <= pos.getIndex() < text.length(); onoutput, the position after the last matched character. If theparse fails, the position in unchanged upon output.</param>
        /// <returns>the ISO 4217 code, as a string, of the best match, or null if
        /// there is no match</returns>
        public static String Parse(ULocale locale, String text, ILOG.J2CsMapping.Text.ParsePosition pos)
        {
            // TODO: There is a slight problem with the pseudo-multi-level
            // fallback implemented here. More-specific locales don't
            // properly shield duplicate entries in less-specific locales.
            // This problem will go away when real multi-level fallback is
            // implemented. We could also fix this by recording (in a
            // hash) which codes are used at each level of fallback, but
            // this doesn't seem warranted.

            int    start    = pos.GetIndex();
            String fragment = text.Substring(start);

            String iso = null;
            int    max = 0;

            // Look up the Currencies resource for the given locale. The
            // Currencies locale data looks like this:
            // |en {
            // | Currencies {
            // | USD { "US$", "US Dollar" }
            // | CHF { "Sw F", "Swiss Franc" }
            // | INR { "=0#Rs|1#Re|1<Rs", "=0#Rupees|1#Rupee|1<Rupees" }
            // | //...
            // | }
            // |}

            // In the future, resource bundles may implement multi-level
            // fallback. That is, if a currency is not found in the en_US
            // Currencies data, then the en Currencies data will be searched.
            // Currently, if a Currencies datum exists in en_US and en, the
            // en_US entry hides that in en.

            // We want multi-level fallback for this resource, so we implement
            // it manually.

            // Multi-level resource inheritance fallback loop

            while (locale != null)
            {
                UResourceBundle rb = IBM.ICU.Util.UResourceBundle.GetBundleInstance(
                    IBM.ICU.Impl.ICUResourceBundle.ICU_BASE_NAME, locale);
                // We can't cast this to String[][]; the cast has to happen later

                try {
                    UResourceBundle currencies = rb.Get("Currencies");
                    // Do a linear search
                    for (int i = 0; i < currencies.GetSize(); ++i)
                    {
                        // String name = ((String[]) currencies[i][1])[0];
                        UResourceBundle item = currencies.Get(i);
                        String          name = item.GetString(0);
                        if (name.Length < 1)
                        {
                            // Ignore zero-length names -- later, change this
                            // when zero-length is used to mean something.
                            continue;
                        }
                        else if (name[0] == '=')
                        {
                            name = name.Substring(1);
                            if (name.Length > 0 && name[0] != '=')
                            {
                                ChoiceFormat choice = new ChoiceFormat(name);
                                // Number n =
                                choice.Parse(text, pos);
                                int len = pos.GetIndex() - start;
                                if (len > max)
                                {
                                    iso = item.GetKey();
                                    max = len;
                                }
                                pos.SetIndex(start);
                                continue;
                            }
                        }
                        if (name.Length > max && fragment.StartsWith(name))
                        {
                            iso = item.GetKey();
                            max = name.Length;
                        }
                    }
                } catch (MissingManifestResourceException e) {
                }

                locale = locale.GetFallback();
            }

            /*
             * 1. Look at the Currencies array from the locale 1a. Iterate through
             * it, and check each row to see if row[1] matches 1a1. If row[1] is a
             * pattern, use ChoiceFormat to attempt a parse 1b. Upon a match, return
             * the ISO code stored at row[0] 2. If there is no match, fall back to
             * "en" and try again 3. If there is no match, fall back to root and try
             * again 4. If still no match, parse 3-letter ISO {this code is probably
             * unchanged}.
             *
             * ICUResourceBundle rb =
             * (ICUResourceBundle)UResourceBundle.getBundleInstance
             * (UResourceBundle.ICU_BASE_NAME, locale); ICUResourceBundle currencies
             * = rb.get("Currencies");
             */
            // If display name parse fails or if it matches fewer than 3
            // characters, try to parse 3-letter ISO. Do this after the
            // display name processing so 3-letter display names are
            // preferred. Consider /[A-Z]{3}/ to be valid ISO, and parse
            // it manually--UnicodeSet/regex are too slow and heavy.
            if (max < 3 && (text.Length - start) >= 3)
            {
                bool valid = true;
                for (int k = 0; k < 3; ++k)
                {
                    char ch = text[start + k];     // 16-bit ok
                    if (ch < 'A' || ch > 'Z')
                    {
                        valid = false;
                        break;
                    }
                }
                if (valid)
                {
                    iso = text.Substring(start, (start + 3) - (start));
                    max = 3;
                }
            }

            pos.SetIndex(start + max);
            return(iso);
        }
示例#18
0
 /// <summary>
 /// Actual worker method for fetching a resource based on the given index.
 /// Sub classes must override this method if they support arrays of
 /// resources.
 /// </summary>
 ///
 /// <param name="index">the index of the resource to be fetched</param>
 /// <param name="table">hastable object to hold references of resources already seen</param>
 /// <param name="requested">the original resource bundle object on which the get methodwas invoked. The requested bundle and the bundle on which thismethod is invoked are the same, except in the cases wherealiases are involved.</param>
 /// <returns>UResourceBundle a resource assoicated with the index</returns>
 /// @draft ICU 3.8
 /// @provisional This API might change or be removed in a future release.
 protected internal virtual UResourceBundle HandleGet(int index, Hashtable table,
                                                      UResourceBundle requested)
 {
     return(null);
 }