GetNamespacesInScope() public method

public GetNamespacesInScope ( XmlNamespaceScope scope ) : string>.IDictionary
scope XmlNamespaceScope
return string>.IDictionary
        public BindingExpressionContext(BaseParser parser, XPathNavigator boundNode, IDictionary<string, string> inScopeNamespaces)
        {
            this.Parser = parser;
             this.BoundNode = boundNode;

             if (inScopeNamespaces == null) {
            inScopeNamespaces = boundNode.GetNamespacesInScope(XmlNamespaceScope.All);
             }

             this._InScopeNamespaces = inScopeNamespaces;
        }
Example #2
0
        /// <summary>
        /// Determines if the <see cref="SyndicationExtension"/> exists in the XML data in the supplied <see cref="XPathNavigator"/>.
        /// </summary>
        /// <param name="source">The <see cref="XPathNavigator"/> to parse.</param>
        /// <returns><b>true</b> if the <see cref="SyndicationExtension"/> elements or attributes are present in the <paramref name="source"/>; otherwise <b>false</b>.</returns>
        /// <remarks>
        ///     <para>
        ///         This method should be as lightweight as possible when determining if the <see cref="SyndicationExtension"/> or its related entities are present in the <paramref name="source"/>. 
        ///         The default implementation utilizes the <see cref="XPathNavigator.GetNamespacesInScope(XmlNamespaceScope)"/> method to determine if the <paramref name="source"/> contains 
        ///         the expected namespace(s) for this <see cref="SyndicationExtension"/>.
        ///     </para>
        ///     <para>It is recommended that you call this method prior to executing a possibly costly load operation using the <see cref="SyndicationExtension.Load(IXPathNavigable)"/> method.</para>
        /// </remarks>
        public virtual bool ExistsInSource(XPathNavigator source)
        {
            //------------------------------------------------------------
            //	Local members
            //------------------------------------------------------------
            bool extensionExists = false;

            //------------------------------------------------------------
            //	Validate parameter
            //------------------------------------------------------------
            Guard.ArgumentNotNull(source, "source");

            //------------------------------------------------------------
            //	Determine if extension exists
            //------------------------------------------------------------
            Dictionary<string, string> namespaces   = (Dictionary<string, string>)source.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml);

            if (namespaces.ContainsValue(this.XmlNamespace))
            {
                extensionExists = true;
            }
            else if (namespaces.ContainsKey(this.XmlPrefix))
            {
                extensionExists = true;
            }

            return extensionExists;
        }
Example #3
0
        /// <summary>
        /// Initializes a <see cref="XmlNamespaceManager"/> object for resolving prefixed XML namespaces utilized by this <see cref="SyndicationExtension"/>.
        /// </summary>
        /// <param name="navigator">Provides a cursor model for navigating syndication extension data.</param>
        /// <returns>A <see cref="XmlNamespaceManager"/> that resolves prefixed XML namespaces and provides scope management for these namespaces.</returns>
        /// <remarks>
        ///     This method will return a <see cref="XmlNamespaceManager"/> that has a namespace added to it using the <see cref="XmlPrefix"/> and <see cref="XmlNamespace"/> 
        ///     of the extension unless the supplied <see cref="XPathNavigator"/> already has an XML namespace associated to the <see cref="XmlPrefix"/>, in which case 
        ///     the associated XML namespace is used instead. This is to prevent collisions and is an attempt to gracefully handle the case where a XML namespace that 
        ///     is not per the extension's specification has been declared on the syndication resource.
        /// </remarks>
        /// <exception cref="ArgumentNullException">The <paramref name="navigator"/> is a null reference (Nothing in Visual Basic).</exception>
        public XmlNamespaceManager CreateNamespaceManager(XPathNavigator navigator)
        {
            //------------------------------------------------------------
            //	Local members
            //------------------------------------------------------------
            XmlNamespaceManager manager = null;

            //------------------------------------------------------------
            //	Validate parameter
            //------------------------------------------------------------
            Guard.ArgumentNotNull(navigator, "navigator");

            //------------------------------------------------------------
            //	Initialize XML namespace resolver
            //------------------------------------------------------------
            manager = new XmlNamespaceManager(navigator.NameTable);

            Dictionary<string, string> namespaces   = (Dictionary<string, string>)navigator.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml);
            string existingXmlNamespace             = String.Empty;
            if (namespaces.ContainsKey(this.XmlPrefix))
            {
                existingXmlNamespace    = namespaces[this.XmlPrefix];
            }

            manager.AddNamespace(this.XmlPrefix, !String.IsNullOrEmpty(existingXmlNamespace) ? existingXmlNamespace : this.XmlNamespace);

            return manager;
        }
        private static void NormalizeNamespaces(XPathNavigator src, XPathNavigator dest) {
            IDictionary<string, string> dictLocal = src.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml);
            IDictionary<string, string> dictExclude = dest.GetNamespacesInScope(XmlNamespaceScope.Local);

            foreach(KeyValuePair<string, string> pair in dictLocal)
                if(!dictExclude.ContainsKey(pair.Key))
                    dest.CreateAttribute("xmlns", pair.Key, "http://www.w3.org/2000/xmlns/", pair.Value);
        }
        private void RrecursiveParse(XPathNavigator navigator)
        {
            var namespaces = navigator.GetNamespacesInScope(XmlNamespaceScope.Local);
            foreach (var map in namespaces)
            {
                if (String.IsNullOrEmpty(map.Key))
                    DefaultNamespaces.Add(map.Value);
                else if (!Namespaces.ContainsKey(map.Key))
                    Namespaces.Add(map.Key, map.Value);
            }

            // process child element nodes
            if (navigator.HasChildren
                && (ParseChildren || navigator.NodeType == XPathNodeType.Root)
                && navigator.MoveToFirstChild())
            {
                do
                {
                    RrecursiveParse(navigator);
                }
                while (navigator.MoveToNext(XPathNodeType.Element));

                // move back to the original parent node
                navigator.MoveToParent();
            }
        }
        protected static bool TryParseRssResource(XPathNavigator resource, out XPathNavigator navigator, out Version version)
        {
            //	Local members
            //------------------------------------------------------------
            bool resourceConformsToFormat   = false;
            XmlNamespaceManager manager     = null;

            //------------------------------------------------------------
            //	Validate parameter
            //------------------------------------------------------------
            Guard.ArgumentNotNull(resource, "resource");

            //------------------------------------------------------------
            //	Initialize namespace resolver
            //------------------------------------------------------------
            manager = new XmlNamespaceManager(resource.NameTable);
            manager.AddNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
            manager.AddNamespace("rss09", "http://my.netscape.com/rdf/simple/0.9/");
            manager.AddNamespace("rss10", "http://purl.org/rss/1.0/");

            //------------------------------------------------------------
            //	Determine if resource conforms to syndication format
            //------------------------------------------------------------
            version = null;
            if ((navigator = resource.SelectSingleNode("rss", manager)) != null)
            {
                //------------------------------------------------------------
                //	Extract version and namespaces of resource
                //------------------------------------------------------------
                version                                 = SyndicationResourceMetadata.GetVersionFromAttribute(navigator, "version");
                Dictionary<string, string> namespaces   = (Dictionary<string, string>)navigator.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml);

                resourceConformsToFormat    = true;
                if (version == null)
                {
                    version = new Version(2, 0);
                }
            }
            else if ((navigator = resource.SelectSingleNode("rdf:RDF", manager)) != null)
            {
                //------------------------------------------------------------
                //	Extract version and namespaces of resource
                //------------------------------------------------------------
                version                                 = SyndicationResourceMetadata.GetVersionFromAttribute(navigator, "version");
                Dictionary<string, string> namespaces   = (Dictionary<string, string>)navigator.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml);

                if (namespaces.ContainsValue("http://purl.org/rss/1.0/"))
                {
                    resourceConformsToFormat    = true;
                    version                     = new Version(1, 0);
                }
                else if (namespaces.ContainsValue("http://my.netscape.com/rdf/simple/0.9/"))
                {
                    resourceConformsToFormat    = true;
                    version                     = new Version(0, 9);
                }
            }

            return resourceConformsToFormat;
        }
        protected static bool TryParseRsdResource(XPathNavigator resource, out XPathNavigator navigator, out Version version)
        {
            //------------------------------------------------------------
            //	Local members
            //------------------------------------------------------------
            bool resourceConformsToFormat   = false;
            XmlNamespaceManager manager     = null;

            //------------------------------------------------------------
            //	Validate parameter
            //------------------------------------------------------------
            Guard.ArgumentNotNull(resource, "resource");

            //------------------------------------------------------------
            //	Initialize namespace resolver
            //------------------------------------------------------------
            manager = new XmlNamespaceManager(resource.NameTable);
            manager.AddNamespace("rsd", "http://archipelago.phrasewise.com/rsd");

            //------------------------------------------------------------
            //	Determine if resource conforms to syndication format
            //------------------------------------------------------------
            version = null;
            if ((navigator = resource.SelectSingleNode("rsd", manager)) != null || (navigator = resource.SelectSingleNode("rsd:rsd", manager)) != null)
            {
                //------------------------------------------------------------
                //	Extract version and namespaces of resource
                //------------------------------------------------------------
                version                                 = SyndicationResourceMetadata.GetVersionFromAttribute(navigator, "version");
                Dictionary<string, string> namespaces   = (Dictionary<string, string>)navigator.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml);

                if (namespaces.ContainsValue("http://archipelago.phrasewise.com/rsd"))
                {
                    resourceConformsToFormat = true;
                    if (version == null)
                    {
                        version = new Version(1, 0);
                    }
                }
                else if (String.Compare(navigator.Name, "rsd", StringComparison.OrdinalIgnoreCase) == 0 && version != null)
                {
                    //  Most web log software actually fails to provide the default XML namespace per RSD spec, so this is a hack/compromise
                    resourceConformsToFormat    = true;
                }
            }

            return resourceConformsToFormat;
        }
        protected static bool TryParseOpenSearchDescriptionResource(XPathNavigator resource, out XPathNavigator navigator, out Version version)
        {
            //------------------------------------------------------------
            //	Local members
            //------------------------------------------------------------
            bool resourceConformsToFormat   = false;
            XmlNamespaceManager manager     = null;

            //------------------------------------------------------------
            //	Validate parameter
            //------------------------------------------------------------
            Guard.ArgumentNotNull(resource, "resource");

            //------------------------------------------------------------
            //	Initialize namespace resolver
            //------------------------------------------------------------
            manager = new XmlNamespaceManager(resource.NameTable);
            manager.AddNamespace("search", "http://a9.com/-/spec/opensearch/1.1/");

            //------------------------------------------------------------
            //	Determine if resource conforms to syndication format
            //------------------------------------------------------------
            version = null;
            if ((navigator = resource.SelectSingleNode("OpenSearchDescription", manager)) != null || (navigator = resource.SelectSingleNode("search:OpenSearchDescription", manager)) != null)
            {
                //------------------------------------------------------------
                //	Extract version and namespaces of resource
                //------------------------------------------------------------
                version                                 = SyndicationResourceMetadata.GetVersionFromAttribute(navigator, "version");
                Dictionary<string, string> namespaces   = (Dictionary<string, string>)navigator.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml);

                if (namespaces.ContainsValue("http://a9.com/-/spec/opensearch/1.1/"))
                {
                    resourceConformsToFormat = true;
                    if (version == null)
                    {
                        version = new Version(1, 1);
                    }
                }
            }

            return resourceConformsToFormat;
        }
        protected static bool TryParseMicroSummaryGeneratorResource(XPathNavigator resource, out XPathNavigator navigator, out Version version)
        {
            //------------------------------------------------------------
            //	Local members
            //------------------------------------------------------------
            bool resourceConformsToFormat   = false;
            XmlNamespaceManager manager     = null;

            //------------------------------------------------------------
            //	Validate parameter
            //------------------------------------------------------------
            Guard.ArgumentNotNull(resource, "resource");

            //------------------------------------------------------------
            //	Initialize namespace resolver
            //------------------------------------------------------------
            manager = new XmlNamespaceManager(resource.NameTable);
            manager.AddNamespace("micro", "http://www.mozilla.org/microsummaries/0.1");

            //------------------------------------------------------------
            //	Determine if resource conforms to syndication format
            //------------------------------------------------------------
            version = null;
            if ((navigator = resource.SelectSingleNode("generator", manager)) != null || (navigator = resource.SelectSingleNode("micro:generator", manager)) != null)
            {
                //------------------------------------------------------------
                //	Extract version and namespaces of resource
                //------------------------------------------------------------
                version                                 = SyndicationResourceMetadata.GetVersionFromAttribute(navigator, "version");
                Dictionary<string, string> namespaces   = (Dictionary<string, string>)navigator.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml);

                if (namespaces.ContainsValue("http://www.mozilla.org/microsummaries/0.1"))
                {
                    resourceConformsToFormat = true;
                    if (version == null)
                    {
                        version = new Version(0, 1);
                    }
                }
            }

            return resourceConformsToFormat;
        }
        protected static bool TryParseAtomResource(XPathNavigator resource, out XPathNavigator navigator, out Version version)
        {
            //------------------------------------------------------------
            //	Local members
            //------------------------------------------------------------
            bool resourceConformsToFormat   = false;
            XmlNamespaceManager manager     = null;

            //------------------------------------------------------------
            //	Validate parameter
            //------------------------------------------------------------
            Guard.ArgumentNotNull(resource, "resource");

            //------------------------------------------------------------
            //	Initialize namespace resolver
            //------------------------------------------------------------
            manager = new XmlNamespaceManager(resource.NameTable);
            manager.AddNamespace("atom", "http://www.w3.org/2005/Atom");
            manager.AddNamespace("atom03", "http://purl.org/atom/ns#");

            //------------------------------------------------------------
            //	Determine if resource conforms to syndication format
            //------------------------------------------------------------
            version = null;
            if ((navigator = resource.SelectSingleNode("feed", manager)) != null || (navigator = resource.SelectSingleNode("atom:feed", manager)) != null || (navigator = resource.SelectSingleNode("atom03:feed", manager)) != null)
            {
                //------------------------------------------------------------
                //	Extract version and namespaces of feed
                //------------------------------------------------------------
                version                                 = SyndicationResourceMetadata.GetVersionFromAttribute(navigator, "version");
                Dictionary<string, string> namespaces   = (Dictionary<string, string>)navigator.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml);

                if (namespaces.ContainsValue("http://www.w3.org/2005/Atom"))
                {
                    resourceConformsToFormat    = true;
                    if (version == null)
                    {
                        version = new Version(1, 0);
                    }
                }
                else if (namespaces.ContainsValue("http://purl.org/atom/ns#"))
                {
                    resourceConformsToFormat    = true;
                    if (version == null)
                    {
                        version = new Version(0, 3);
                    }
                }
            }
            else if ((navigator = resource.SelectSingleNode("entry", manager)) != null || (navigator = resource.SelectSingleNode("atom:entry", manager)) != null || (navigator = resource.SelectSingleNode("atom03:entry", manager)) != null)
            {
                //------------------------------------------------------------
                //	Extract version and namespaces of entry
                //------------------------------------------------------------
                version                                 = SyndicationResourceMetadata.GetVersionFromAttribute(navigator, "version");
                Dictionary<string, string> namespaces   = (Dictionary<string, string>)navigator.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml);

                if (namespaces.ContainsValue("http://www.w3.org/2005/Atom"))
                {
                    resourceConformsToFormat    = true;
                    if (version == null)
                    {
                        version = new Version(1, 0);
                    }
                }
                else if (namespaces.ContainsValue("http://purl.org/atom/ns#"))
                {
                    resourceConformsToFormat    = true;
                    if (version == null)
                    {
                        version = new Version(0, 3);
                    }
                }
            }

            return resourceConformsToFormat;
        }
        /// <summary>
        /// Extracts the content format, version, and XML namespaces for a syndication resource from the supplied <see cref="XPathNavigator"/>.
        /// </summary>
        /// <param name="resource">The <see cref="XPathNavigator"/> to extract the syndication resource meta-data from.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="resource"/> is a null reference (Nothing in Visual Basic).</exception>
        private void Load(XPathNavigator resource)
        {
            //------------------------------------------------------------
            //	Local members
            //------------------------------------------------------------
            XPathNavigator navigator    = null;
            Version version             = null;

            //------------------------------------------------------------
            //	Validate parameter
            //------------------------------------------------------------
            Guard.ArgumentNotNull(resource, "resource");

            //------------------------------------------------------------
            //	Get XML namespaces declared on syndication resource
            //------------------------------------------------------------
            Dictionary<string, string> namespaces   = (Dictionary<string, string>)resource.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml);
            foreach (string prefix in namespaces.Keys)
            {
                resourceNamespaces.Add(prefix, namespaces[prefix]);
            }

            //------------------------------------------------------------
            //	Get syndication resource format version using
            //  version XML attribute
            //------------------------------------------------------------
            resourceVersion     = SyndicationResourceMetadata.GetVersionFromAttribute(resource, "version");

            //------------------------------------------------------------
            //	Determine content format based on root element qualified name
            //  and expected XML namespace(s)
            //------------------------------------------------------------
            if (SyndicationResourceMetadata.TryParseApmlResource(resource, out navigator, out version))
            {
                resourceFormat      = SyndicationContentFormat.Apml;
                resourceRootNode    = navigator;
                resourceVersion     = version;
            }
            else if (SyndicationResourceMetadata.TryParseAtomResource(resource, out navigator, out version))
            {
                resourceFormat      = SyndicationContentFormat.Atom;
                resourceRootNode    = navigator;
                resourceVersion     = version;
            }
            else if (SyndicationResourceMetadata.TryParseAtomPublishingCategoriesResource(resource, out navigator, out version))
            {
                resourceFormat      = SyndicationContentFormat.AtomCategoryDocument;
                resourceRootNode    = navigator;
                resourceVersion     = version;
            }
            else if (SyndicationResourceMetadata.TryParseAtomPublishingServiceResource(resource, out navigator, out version))
            {
                resourceFormat      = SyndicationContentFormat.AtomServiceDocument;
                resourceRootNode    = navigator;
                resourceVersion     = version;
            }
            else if (SyndicationResourceMetadata.TryParseBlogMLResource(resource, out navigator, out version))
            {
                resourceFormat      = SyndicationContentFormat.BlogML;
                resourceRootNode    = navigator;
                resourceVersion     = version;
            }
            else if (SyndicationResourceMetadata.TryParseMicroSummaryGeneratorResource(resource, out navigator, out version))
            {
                resourceFormat      = SyndicationContentFormat.MicroSummaryGenerator;
                resourceRootNode    = navigator;
                resourceVersion     = version;
            }
            else if (SyndicationResourceMetadata.TryParseNewsMLResource(resource, out navigator, out version))
            {
                resourceFormat      = SyndicationContentFormat.NewsML;
                resourceRootNode    = navigator;
                resourceVersion     = version;
            }
            else if (SyndicationResourceMetadata.TryParseOpenSearchDescriptionResource(resource, out navigator, out version))
            {
                resourceFormat      = SyndicationContentFormat.OpenSearchDescription;
                resourceRootNode    = navigator;
                resourceVersion     = version;
            }
            else if (SyndicationResourceMetadata.TryParseOpmlResource(resource, out navigator, out version))
            {
                resourceFormat      = SyndicationContentFormat.Opml;
                resourceRootNode    = navigator;
                resourceVersion     = version;
            }
            else if (SyndicationResourceMetadata.TryParseRsdResource(resource, out navigator, out version))
            {
                resourceFormat      = SyndicationContentFormat.Rsd;
                resourceRootNode    = navigator;
                resourceVersion     = version;
            }
            else if (SyndicationResourceMetadata.TryParseRssResource(resource, out navigator, out version))
            {
                resourceFormat      = SyndicationContentFormat.Rss;
                resourceRootNode    = navigator;
                resourceVersion     = version;
            }
            else
            {
                resourceFormat      = SyndicationContentFormat.None;
                resourceRootNode    = null;
                resourceVersion     = null;
            }
        }
Example #12
0
        /// <summary>
        /// Reads an XPathNavigator object and returns an XmlNamespaceManager containing all namespaces found within it. The default namespace is x.
        /// </summary>
        /// <param name="xpn">The XPathNavigator to be assessed for namespaces</param>
        /// <returns></returns>
        public static XmlNamespaceManager GetXmlNamespaceManager(XPathNavigator xpn)
        {
            xpn.MoveToFollowing(XPathNodeType.Element);

            XmlNamespaceManager xmlnsm = new XmlNamespaceManager(xpn.NameTable);
            xmlnsm.AddNamespace("x", xpn.NamespaceURI);

            foreach (KeyValuePair<string, string> xns in xpn.GetNamespacesInScope(XmlNamespaceScope.All))
                xmlnsm.AddNamespace(xns.Key, xns.Value);

            return xmlnsm;
        }
Example #13
0
        protected object namespace_uri_for_prefix(object prefix, XPathNavigator element)
        {
            // fn:namespace-uri-for-prefix($prefix as xs:string?, $element as element()) as xs:anyURI?

             IDictionary<string, string> namespaces = element.GetNamespacesInScope(XmlNamespaceScope.All);

             string p = ExtensionObjectConvert.ToString(prefix) ?? "";

             string ns;

             if (!namespaces.TryGetValue(p, out ns)) {
            return ExtensionObjectConvert.EmptyIterator;
             }

             return ns;
        }
Example #14
0
 protected object in_scope_prefixes(XPathNavigator element)
 {
     // fn:in-scope-prefixes($element as element()) as xs:string*
      return ExtensionObjectConvert.ToInputOrEmpty(element.GetNamespacesInScope(XmlNamespaceScope.All).Keys);
 }
 IDictionary <string, string> IXmlNamespaceResolver.GetNamespacesInScope(XmlNamespaceScope scope)
 {
     return(_nav.GetNamespacesInScope(scope));
 }