コード例 #1
0
            public int CompareNamespacesByPreference(string ns1, string ns2)
            {
                string str;
                string str2;
                string str3;

                if (KS.Eq(ns1, ns2))
                {
                    return(0);
                }
                for (str = this.GetNewNs(ns1); str != null; str = this.GetNewNs(str))
                {
                    if (str == ns2)
                    {
                        return(1);
                    }
                }
                for (str = this.GetNewNs(ns2); str != null; str = this.GetNewNs(str))
                {
                    if (str == ns1)
                    {
                        return(-1);
                    }
                }
                if (this.GetNewNs(ns1) == null)
                {
                    if (this.GetNewNs(ns2) != null)
                    {
                        return(-1);
                    }
                }
                else if (this.GetNewNs(ns2) == null)
                {
                    return(1);
                }
                int num  = 0;
                int num2 = 0;

                this._subsumeCount.TryGetValue(ns1, out num);
                this._subsumeCount.TryGetValue(ns2, out num2);
                if (num > num2)
                {
                    return(-1);
                }
                if (num2 > num)
                {
                    return(1);
                }
                this._nsInfo.Prefixes.TryGetValue(ns1, out str2);
                this._nsInfo.Prefixes.TryGetValue(ns2, out str3);
                if (string.IsNullOrEmpty(str2))
                {
                    if (!string.IsNullOrEmpty(str3))
                    {
                        return(1);
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(str3))
                    {
                        return(-1);
                    }
                    if (str2.Length < str3.Length)
                    {
                        return(-1);
                    }
                    if (str3.Length < str2.Length)
                    {
                        return(1);
                    }
                }
                return(StringComparer.Ordinal.Compare(ns1, ns2));
            }
コード例 #2
0
ファイル: XmlNsInfo.cs プロジェクト: Tana0910/wpf
            public int CompareNamespacesByPreference(string ns1, string ns2)
            {
                if (KS.Eq(ns1, ns2))
                {
                    return(0);
                }
                const int Prefer_NS1 = -1;
                const int Prefer_NS2 = 1;

                // If one namespace subsumes the other, favor the subsumer
                string newNs = GetNewNs(ns1);

                while (newNs != null)
                {
                    if (newNs == ns2)
                    {
                        return(Prefer_NS2);
                    }
                    newNs = GetNewNs(newNs);
                }
                newNs = GetNewNs(ns2);
                while (newNs != null)
                {
                    if (newNs == ns1)
                    {
                        return(Prefer_NS1);
                    }
                    newNs = GetNewNs(newNs);
                }

                // Favor namespaces that aren't subsumed over ones that are
                if (GetNewNs(ns1) == null)
                {
                    if (GetNewNs(ns2) != null)
                    {
                        return(Prefer_NS1);
                    }
                }
                else if (GetNewNs(ns2) == null)
                {
                    return(Prefer_NS2);
                }

                // Favor namespaces that subsume a greater number of other namespaces
                int ns1count = 0, ns2count = 0;

                _subsumeCount.TryGetValue(ns1, out ns1count);
                _subsumeCount.TryGetValue(ns2, out ns2count);
                if (ns1count > ns2count)
                {
                    return(Prefer_NS1);
                }
                else if (ns2count > ns1count)
                {
                    return(Prefer_NS2);
                }

                // Favor namespaces with prefixes over namespaces without prefixes
                // Favor namespaces with shorter prefixes over namespaces with longer ones
                string prefix1, prefix2;

                _nsInfo.Prefixes.TryGetValue(ns1, out prefix1);
                _nsInfo.Prefixes.TryGetValue(ns2, out prefix2);
                if (string.IsNullOrEmpty(prefix1))
                {
                    if (!string.IsNullOrEmpty(prefix2))
                    {
                        return(Prefer_NS2);
                    }
                }
                else if (string.IsNullOrEmpty(prefix2))
                {
                    return(Prefer_NS1);
                }
                else if (prefix1.Length < prefix2.Length)
                {
                    return(Prefer_NS1);
                }
                else if (prefix2.Length < prefix1.Length)
                {
                    return(Prefer_NS2);
                }

                // fall back to ordinal comparison
                return(StringComparer.Ordinal.Compare(ns1, ns2));
            }