예제 #1
0
        /// <summary>Given a prefix mapping for a specific URI, this returns
        /// the prefix mapping previously in effect for the same URI.</summary>
        /// <remarks>This can be used to iterate backwards through the stack of
        /// prefix mappings for a given URI.</remarks>
        /// <param name="prefix"><see cref="ActiveMapping"/> instance for which
        /// we want to find the instance previously in effect with the same URI.</param>
        /// <returns><see cref="ActiveMapping"/> instance to look for,
        /// or <c>null</c> if none exists.</returns>
        public ActiveMapping PreviousUriMapping(ActiveMapping prefix)
        {
            string uri = prefix.Uri;
            // check active mapping record
            NamespaceMapping nsMapping = prefix.mappingsTop;

            if (nsMapping == null || !nsMapping.declared)
            {
                string msg = Resources.GetString(RsId.UndeclaredMapping);
                throw new XmlNamespacesException(String.Format(msg, prefix.Prefix));
            }
            // find a parent scope where the same URI maps to an "active" prefix
            NamespaceScope scope = nsMapping.scope.parent;

            while (scope != null)
            {
                nsMapping = scope.FindByUri(uri);
                if (nsMapping != null)
                {
                    ActiveMapping result = nsMapping.prefix;
                    // result must be different from prefix, since the same prefix
                    // cannot be "declared" with two different URIs at the same time
                    if (result.IsDeclared)
                    {
                        Debug.Assert(result != prefix, Resources.GetString(RsId.InternalNsError));
                        return(result);
                    }
                }
                scope = scope.parent;
            }
            return(null);
        }
예제 #2
0
        /// <summary>Returns the most recently added namespace mapping "in effect"
        /// for a given URI.</summary>
        /// <remarks>"In effect" means that the prefix mapping must <b>not</b> have
        /// been "undeclared".</remarks>
        /// <param name="uri">Namespace URI to get mapping for.</param>
        /// <returns><see cref="ActiveMapping"/> instance for <c>uri</c>, or <c>null</c>
        /// if there is no such mapping.</returns>
        public ActiveMapping GetUriMapping(string uri)
        {
            NamespaceScope scope = topScope;

            while (scope != null)
            {
                NamespaceMapping nsMapping = scope.FindByUri(uri);
                if (nsMapping != null && nsMapping.declared)
                {
                    return(nsMapping.prefix);
                }
                scope = scope.parent;
            }
            return(null);
        }