コード例 #1
0
        public override SiteMapNode BuildSiteMap()
        {
            SiteMapNode tempNode = _siteMapNode;

            // If siteMap is already constructed, simply returns it.
            // Child providers will only be updated when the parent providers need to access them.
            if (tempNode != null)
            {
                return(tempNode);
            }

            XmlDocument document = GetConfigDocument();

            lock (_lock) {
                if (_siteMapNode != null)
                {
                    return(_siteMapNode);
                }

                Clear();

                // Need to check if the sitemap file exists before opening it.
                CheckSiteMapFileExists();

                try {
                    using (Stream stream = _normalizedVirtualPath.OpenFile()) {
                        XmlReader reader = new XmlTextReader(stream);
                        document.Load(reader);
                    }
                }
                catch (XmlException e) {
                    string sourceFile  = _virtualPath.VirtualPathString;
                    string physicalDir = _normalizedVirtualPath.MapPathInternal();
                    if (physicalDir != null && HttpRuntime.HasPathDiscoveryPermission(physicalDir))
                    {
                        sourceFile = physicalDir;
                    }

                    throw new ConfigurationErrorsException(
                              SR.GetString(SR.XmlSiteMapProvider_Error_loading_Config_file, _virtualPath, e.Message),
                              e, sourceFile, e.LineNumber);
                }
                catch (Exception e) {
                    throw new ConfigurationErrorsException(
                              SR.GetString(SR.XmlSiteMapProvider_Error_loading_Config_file, _virtualPath, e.Message), e);
                }

                XmlNode node = null;
                foreach (XmlNode siteMapMode in document.ChildNodes)
                {
                    if (String.Equals(siteMapMode.Name, "siteMap", StringComparison.Ordinal))
                    {
                        node = siteMapMode;
                        break;
                    }
                }

                if (node == null)
                {
                    throw new ConfigurationErrorsException(
                              SR.GetString(SR.XmlSiteMapProvider_Top_Element_Must_Be_SiteMap),
                              document);
                }

                bool enableLocalization = false;
                HandlerBase.GetAndRemoveBooleanAttribute(node, "enableLocalization", ref enableLocalization);
                EnableLocalization = enableLocalization;

                XmlNode topElement = null;
                foreach (XmlNode subNode in node.ChildNodes)
                {
                    if (subNode.NodeType == XmlNodeType.Element)
                    {
                        if (!_siteMapNodeName.Equals(subNode.Name))
                        {
                            throw new ConfigurationErrorsException(
                                      SR.GetString(SR.XmlSiteMapProvider_Only_SiteMapNode_Allowed),
                                      subNode);
                        }

                        if (topElement != null)
                        {
                            throw new ConfigurationErrorsException(
                                      SR.GetString(SR.XmlSiteMapProvider_Only_One_SiteMapNode_Required_At_Top),
                                      subNode);
                        }

                        topElement = subNode;
                    }
                }

                if (topElement == null)
                {
                    throw new ConfigurationErrorsException(
                              SR.GetString(SR.XmlSiteMapProvider_Only_One_SiteMapNode_Required_At_Top),
                              node);
                }

                Queue queue = new Queue(50);

                // The parentnode of the top node does not exist,
                // simply add a null to satisfy the ConvertFromXmlNode condition.
                queue.Enqueue(null);
                queue.Enqueue(topElement);
                _siteMapNode = ConvertFromXmlNode(queue);

                return(_siteMapNode);
            }
        }
コード例 #2
0
        public override SiteMapNode BuildSiteMap()
        {
            SiteMapNode node = this._siteMapNode;

            if (node != null)
            {
                return(node);
            }
            XmlDocument configDocument = this.GetConfigDocument();

            lock (base._lock)
            {
                if (this._siteMapNode == null)
                {
                    this.Clear();
                    this.CheckSiteMapFileExists();
                    try
                    {
                        using (Stream stream = this._normalizedVirtualPath.OpenFile())
                        {
                            XmlReader reader = new XmlTextReader(stream);
                            configDocument.Load(reader);
                        }
                    }
                    catch (XmlException exception)
                    {
                        string virtualPathString = this._virtualPath.VirtualPathString;
                        string path = this._normalizedVirtualPath.MapPathInternal();
                        if ((path != null) && HttpRuntime.HasPathDiscoveryPermission(path))
                        {
                            virtualPathString = path;
                        }
                        throw new ConfigurationErrorsException(System.Web.SR.GetString("XmlSiteMapProvider_Error_loading_Config_file", new object[] { this._virtualPath, exception.Message }), exception, virtualPathString, exception.LineNumber);
                    }
                    catch (Exception exception2)
                    {
                        throw new ConfigurationErrorsException(System.Web.SR.GetString("XmlSiteMapProvider_Error_loading_Config_file", new object[] { this._virtualPath, exception2.Message }), exception2);
                    }
                    XmlNode node2 = null;
                    foreach (XmlNode node3 in configDocument.ChildNodes)
                    {
                        if (string.Equals(node3.Name, "siteMap", StringComparison.Ordinal))
                        {
                            node2 = node3;
                            break;
                        }
                    }
                    if (node2 == null)
                    {
                        throw new ConfigurationErrorsException(System.Web.SR.GetString("XmlSiteMapProvider_Top_Element_Must_Be_SiteMap"), configDocument);
                    }
                    bool val = false;
                    System.Web.Configuration.HandlerBase.GetAndRemoveBooleanAttribute(node2, "enableLocalization", ref val);
                    base.EnableLocalization = val;
                    XmlNode node4 = null;
                    foreach (XmlNode node5 in node2.ChildNodes)
                    {
                        if (node5.NodeType == XmlNodeType.Element)
                        {
                            if (!"siteMapNode".Equals(node5.Name))
                            {
                                throw new ConfigurationErrorsException(System.Web.SR.GetString("XmlSiteMapProvider_Only_SiteMapNode_Allowed"), node5);
                            }
                            if (node4 != null)
                            {
                                throw new ConfigurationErrorsException(System.Web.SR.GetString("XmlSiteMapProvider_Only_One_SiteMapNode_Required_At_Top"), node5);
                            }
                            node4 = node5;
                        }
                    }
                    if (node4 == null)
                    {
                        throw new ConfigurationErrorsException(System.Web.SR.GetString("XmlSiteMapProvider_Only_One_SiteMapNode_Required_At_Top"), node2);
                    }
                    Queue queue = new Queue(50);
                    queue.Enqueue(null);
                    queue.Enqueue(node4);
                    this._siteMapNode = this.ConvertFromXmlNode(queue);
                }
                return(this._siteMapNode);
            }
        }