Ejemplo n.º 1
0
        /// <summary>
        /// Registers standard transliterators with the system.  Called by
        /// <see cref="Transliterator"/> during initialization.  Scan all current targets
        /// and register those that are scripts T as Any-T/V.
        /// </summary>
        internal static void Register()
        {
            IDictionary <string, ISet <string> > seen = new Dictionary <string, ISet <string> >(); // old code used set, but was dependent on order

            foreach (string source in Transliterator.GetAvailableSources())
            {
                // Ignore the "Any" source
                if (source.Equals(ANY, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                foreach (string target in Transliterator.GetAvailableTargets(source))
                {
                    // Get the script code for the target.  If not a script, ignore.
                    int targetScript = ScriptNameToCode(target);
                    if (targetScript == UScript.InvalidCode)
                    {
                        continue;
                    }

                    ISet <string> seenVariants = seen.Get(target);
                    if (seenVariants == null)
                    {
                        seen[target] = seenVariants = new HashSet <string>();
                    }

                    foreach (string variant in Transliterator.GetAvailableVariants(source, target))
                    {
                        // Only process each target/variant pair once
                        if (seenVariants.Contains(variant))
                        {
                            continue;
                        }
                        seenVariants.Add(variant);

                        string id;
                        id = TransliteratorIDParser.STVtoID(ANY, target, variant);
                        AnyTransliterator trans = new AnyTransliterator(id, target, variant,
                                                                        targetScript);
                        Transliterator.RegisterInstance(trans);
                        Transliterator.RegisterSpecialInverse(target, NULL_ID, false);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Given an Entry object, instantiate it.  Caller owns result.  Return
        /// 0 on failure.
        /// <para/>
        /// Return a non-empty <paramref name="aliasReturn"/> value if the <paramref name="ID"/> points to an alias.
        /// We cannot instantiate it ourselves because the alias may contain
        /// filters or compounds, which we do not understand.  Caller should
        /// make <paramref name="aliasReturn"/> empty before calling.
        /// <para/>
        /// The entry object is assumed to reside in the dynamic store.  It may be
        /// modified.
        /// </summary>
        private Transliterator InstantiateEntry(string ID,
                                                object[] entryWrapper,
                                                StringBuffer aliasReturn)
        {
            // We actually modify the entry object in some cases.  If it
            // is a string, we may partially parse it and turn it into a
            // more processed precursor.  This makes the next
            // instantiation faster and allows sharing of immutable
            // components like the RuleBasedTransliterator.Data objects.
            // For this reason, the entry object is an Object[] of length
            // 1.

            for (; ;)
            {
                object entry = entryWrapper[0];
#pragma warning disable 612, 618
                if (entry is RuleBasedTransliterator.Data)
                {
                    RuleBasedTransliterator.Data data = (RuleBasedTransliterator.Data)entry;
                    return(new RuleBasedTransliterator(ID, data, null));

#pragma warning restore 612, 618
                }
                else if (entry is Type)
                {
                    try
                    {
                        //return (Transliterator)((Type)entry).newInstance();
                        return((Transliterator)Activator.CreateInstance((Type)entry));
                    }
                    catch (TargetInvocationException)
                    {
                    }
                    catch (MethodAccessException) { }
                    return(null);
                }
                else if (entry is AliasEntry)
                {
                    aliasReturn.Append(((AliasEntry)entry).Alias);
                    return(null);
                }
                else if (entry is ITransliteratorFactory)
                {
                    return(((ITransliteratorFactory)entry).GetInstance(ID));
                }
                else if (entry is CompoundRBTEntry)
                {
                    return(((CompoundRBTEntry)entry).GetInstance());
                }
                else if (entry is AnyTransliterator)
                {
                    AnyTransliterator temp = (AnyTransliterator)entry;
                    return(temp.SafeClone());
                }
#pragma warning disable 612, 618
                else if (entry is RuleBasedTransliterator)
                {
                    RuleBasedTransliterator temp = (RuleBasedTransliterator)entry;
                    return(temp.SafeClone());
                }
#pragma warning restore 612, 618
                else if (entry is CompoundTransliterator)
                {
                    CompoundTransliterator temp = (CompoundTransliterator)entry;
                    return(temp.SafeClone());
                }
                else if (entry is Transliterator)
                {
                    return((Transliterator)entry);
                }

                // At this point entry type must be either RULES_FORWARD or
                // RULES_REVERSE.  We process the rule data into a
                // TransliteratorRuleData object, and possibly also into an
                // .id header and/or footer.  Then we modify the registry with
                // the parsed data and retry.

                TransliteratorParser parser = new TransliteratorParser();

                try
                {
                    ResourceEntry re = (ResourceEntry)entry;
                    parser.Parse(re.Resource, re.Direction);
                }
                catch (InvalidCastException)
                {
                    // If we pull a rule from a locale resource bundle it will
                    // be a LocaleEntry.
                    LocaleEntry le = (LocaleEntry)entry;
                    parser.Parse(le.Rule, le.Direction);
                }

                // Reset entry to something that we process at the
                // top of the loop, then loop back to the top.  As long as we
                // do this, we only loop through twice at most.
                // NOTE: The logic here matches that in
                // Transliterator.createFromRules().
                if (parser.IdBlockVector.Count == 0 && parser.DataVector.Count == 0)
                {
                    // No idBlock, no data -- this is just an
                    // alias for Null
                    entryWrapper[0] = new AliasEntry(NullTransliterator._ID);
                }
                else if (parser.IdBlockVector.Count == 0 && parser.DataVector.Count == 1)
                {
                    // No idBlock, data != 0 -- this is an
                    // ordinary RBT_DATA
                    entryWrapper[0] = parser.DataVector[0];
                }
                else if (parser.IdBlockVector.Count == 1 && parser.DataVector.Count == 0)
                {
                    // idBlock, no data -- this is an alias.  The ID has
                    // been munged from reverse into forward mode, if
                    // necessary, so instantiate the ID in the forward
                    // direction.
                    if (parser.CompoundFilter != null)
                    {
                        entryWrapper[0] = new AliasEntry(parser.CompoundFilter.ToPattern(false) + ";"
                                                         + parser.IdBlockVector[0]);
                    }
                    else
                    {
                        entryWrapper[0] = new AliasEntry(parser.IdBlockVector[0]);
                    }
                }
                else
                {
                    entryWrapper[0] = new CompoundRBTEntry(ID, parser.IdBlockVector, parser.DataVector,
                                                           parser.CompoundFilter);
                }
            }
        }