Esempio n. 1
0
        private static BreakIterator CreateBreakInstance(ULocale locale, int kind)
        {
            BreakIterator     iter = null;
            ICUResourceBundle rb   = (ICUResourceBundle)IBM.ICU.Util.UResourceBundle
                                     .GetBundleInstance(IBM.ICU.Impl.ICUResourceBundle.ICU_BRKITR_BASE_NAME,
                                                        locale);

            //
            // Get the binary rules. These are needed for both normal
            // RulesBasedBreakIterators
            // and for Dictionary iterators.
            //
            Stream ruleStream = null;

            try {
                String typeKey       = KIND_NAMES[kind];
                String brkfname      = rb.GetStringWithFallback("boundaries/" + typeKey);
                String rulesFileName = IBM.ICU.Impl.ICUResourceBundle.ICU_BASE_NAME /*+
                                                                                     * IBM.ICU.Impl.ICUResourceBundle.ICU_BUNDLE*/
                                       + IBM.ICU.Impl.ICUResourceBundle.ICU_BRKITR_NAME + "/" + brkfname;
                ruleStream = IBM.ICU.Impl.ICUData.GetStream(rulesFileName);
            } catch (Exception e) {
                throw new MissingManifestResourceException(e.ToString());
            }

            //
            // Check whether a dictionary exists, and create a DBBI iterator is
            // one does.
            //
            if (DICTIONARY_POSSIBLE[kind])
            {
                // This type of break iterator could potentially use a dictionary.
                //
                try {
                    // ICUResourceBundle dictRes =
                    // (ICUResourceBundle)rb.getObject("BreakDictionaryData");
                    // byte[] dictBytes = null;
                    // dictBytes = dictRes.getBinary(dictBytes);
                    // TODO: Hard code this for now! fix it once
                    // CompactTrieDictionary is ported
                    if (locale.Equals("th"))
                    {
                        String fileName = "data/th.brk";
                        Stream mask0    = IBM.ICU.Impl.ICUData.GetStream(fileName);
                        iter = new DictionaryBasedBreakIterator(ruleStream, mask0);
                    }
                } catch (MissingManifestResourceException e_0) {
                    // Couldn't find a dictionary.
                    // This is normal, and will occur whenever creating a word or
                    // line
                    // break iterator for a locale that does not have a
                    // BreakDictionaryData
                    // resource - meaning for all but Thai.
                    // Fall through to creating a normal RulebasedBreakIterator.
                } catch (IOException e_1) {
                    IBM.ICU.Impl.Assert.Fail(e_1);
                }
            }

            if (iter == null)
            {
                //
                // Create a normal RuleBasedBreakIterator.
                // We have determined that this is not supposed to be a dictionary
                // iterator.
                //
                try {
                    iter = IBM.ICU.Text.RuleBasedBreakIterator
                           .GetInstanceFromCompiledRules(ruleStream);
                } catch (IOException e_2) {
                    // Shouldn't be possible to get here.
                    // If it happens, the compiled rules are probably corrupted in
                    // some way.
                    IBM.ICU.Impl.Assert.Fail(e_2);
                }
            }
            // TODO: Determine valid and actual locale correctly.
            ULocale uloc = IBM.ICU.Util.ULocale.ForLocale(rb.GetLocale());

            iter.SetLocale(uloc, uloc);

            return(iter);
        }
Esempio n. 2
0
        private static BreakIterator CreateBreakInstance(ULocale locale, int kind)
        {
            RuleBasedBreakIterator iter = null;
            ICUResourceBundle      rb   = ICUResourceBundle.
                                          GetBundleInstance(ICUData.ICU_BRKITR_BASE_NAME, locale,
                                                            ICUResourceBundle.OpenType.LOCALE_ROOT);

            //
            //  Get the binary rules.
            //
            ByteBuffer bytes      = null;
            string     typeKeyExt = null;

            if (kind == BreakIterator.KIND_LINE)
            {
                string lbKeyValue = locale.GetKeywordValue("lb");
                if (lbKeyValue != null && (lbKeyValue.Equals("strict") || lbKeyValue.Equals("normal") || lbKeyValue.Equals("loose")))
                {
                    typeKeyExt = "_" + lbKeyValue;
                }
            }

            try
            {
                string typeKey       = (typeKeyExt == null) ? KIND_NAMES[kind] : KIND_NAMES[kind] + typeKeyExt;
                string brkfname      = rb.GetStringWithFallback("boundaries/" + typeKey);
                string rulesFileName = ICUData.ICU_BRKITR_NAME + '/' + brkfname;
                bytes = ICUBinary.GetData(rulesFileName);
            }
            catch (Exception e)
            {
                throw new MissingManifestResourceException(e.ToString(), e /*, "", ""*/);
            }

            //
            // Create a normal RuleBasedBreakIterator.
            //
            try
            {
#pragma warning disable 612, 618
                iter = RuleBasedBreakIterator.GetInstanceFromCompiledRules(bytes);
#pragma warning restore 612, 618
            }
            catch (IOException e)
            {
                // Shouldn't be possible to get here.
                // If it happens, the compiled rules are probably corrupted in some way.
                Assert.Fail(e);
            }
            // TODO: Determine valid and actual locale correctly.
            ULocale uloc = ULocale.ForLocale(rb.GetLocale());
            iter.SetLocale(uloc, uloc);
            iter.BreakType = kind;

            // filtered break
            if (kind == BreakIterator.KIND_SENTENCE)
            {
                string ssKeyword = locale.GetKeywordValue("ss");
                if (ssKeyword != null && ssKeyword.Equals("standard"))
                {
                    ULocale @base = new ULocale(locale.GetBaseName());
                    return(FilteredBreakIteratorBuilder.GetInstance(@base).WrapIteratorWithFilter(iter));
                }
            }

            return(iter);
        }