Ejemplo n.º 1
0
        private static BreakIterator CreateBreakInstance(UCultureInfo locale, int kind)
        {
            RuleBasedBreakIterator iter = null;
            ICUResourceBundle      rb   = ICUResourceBundle.
                                          GetBundleInstance(ICUData.IcuBreakIteratorBaseName, locale,
                                                            OpenType.LocaleRoot);

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

            if (kind == BreakIterator.KIND_LINE)
            {
                locale.Keywords.TryGetValue("lb", out string lbKeyValue);
                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.IcuBreakIteratorName + '/' + 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.
            UCultureInfo uloc = rb.Culture.ToUCultureInfo();
            iter.SetCulture(uloc, uloc);
            iter.BreakType = kind;

            // filtered break
            if (kind == BreakIterator.KIND_SENTENCE)
            {
                if (locale.Keywords.TryGetValue("ss", out string ssKeyword) && ssKeyword != null && ssKeyword.Equals("standard"))
                {
                    UCultureInfo @base = new UCultureInfo(locale.Name);
                    return(FilteredBreakIteratorBuilder.GetInstance(@base).WrapIteratorWithFilter(iter));
                }
            }

            return(iter);
        }