2 // System.Web.StaticSiteMapProvider.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
6 // Ben Maurer (bmaurer@users.sourceforge.net)
7 // Juraj Skripsky (js@hotfeet.ch)
10 // (C) 2005 Novell, Inc (http://www.novell.com)
11 // (C) 2007 HotFeet GmbH (http://www.hotfeet.ch)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections.Generic;
34 using System.Web.Util;
38 public abstract class StaticSiteMapProvider : SiteMapProvider
40 Dictionary<string, SiteMapNode> keyToNode;
41 Dictionary<SiteMapNode, SiteMapNode> nodeToParent;
42 Dictionary<SiteMapNode, SiteMapNodeCollection> nodeToChildren;
43 Dictionary<string, SiteMapNode> urlToNode;
45 protected StaticSiteMapProvider ()
47 keyToNode = new Dictionary<string, SiteMapNode> ();
48 nodeToParent = new Dictionary<SiteMapNode, SiteMapNode> ();
49 nodeToChildren = new Dictionary<SiteMapNode, SiteMapNodeCollection> ();
50 urlToNode = new Dictionary<string, SiteMapNode> (StringComparer.InvariantCultureIgnoreCase);
53 internal protected override void AddNode (SiteMapNode node, SiteMapNode parentNode)
56 throw new ArgumentNullException ("node");
59 string nodeKey = node.Key;
60 if (FindSiteMapNodeFromKey (nodeKey) != null && node.Provider == this)
61 throw new InvalidOperationException (string.Format ("A node with key '{0}' already exists.",nodeKey));
63 string nodeUrl = node.Url;
64 if (!String.IsNullOrEmpty (nodeUrl)) {
65 string url = MapUrl (nodeUrl);
66 SiteMapNode foundNode = FindSiteMapNode (url);
67 if (foundNode != null && String.Compare (foundNode.Url, url, RuntimeHelpers.StringComparison) == 0)
68 throw new InvalidOperationException (String.Format (
69 "Multiple nodes with the same URL '{0}' were found. " +
70 "StaticSiteMapProvider requires that sitemap nodes have unique URLs.",
74 urlToNode.Add (url, node);
76 keyToNode.Add (nodeKey, node);
81 if (parentNode == null)
82 parentNode = RootNode;
84 nodeToParent.Add (node, parentNode);
86 SiteMapNodeCollection children;
87 if (!nodeToChildren.TryGetValue (parentNode, out children))
88 nodeToChildren.Add (parentNode, children = new SiteMapNodeCollection ());
94 protected virtual void Clear ()
98 nodeToChildren.Clear ();
99 nodeToParent.Clear ();
104 public override SiteMapNode FindSiteMapNode (string rawUrl)
107 throw new ArgumentNullException ("rawUrl");
109 if (rawUrl == String.Empty)
114 if (VirtualPathUtility.IsAppRelative (rawUrl))
115 rawUrl = VirtualPathUtility.ToAbsolute (rawUrl, HttpRuntime.AppDomainAppVirtualPath, false);
117 if (!urlToNode.TryGetValue (rawUrl, out node))
120 return CheckAccessibility (node);
123 public override SiteMapNodeCollection GetChildNodes (SiteMapNode node)
126 throw new ArgumentNullException ("node");
129 SiteMapNodeCollection col;
130 if (!nodeToChildren.TryGetValue (node, out col))
131 return SiteMapNodeCollection.EmptyCollection;
133 SiteMapNodeCollection ret = null;
134 for (int n=0; n<col.Count; n++) {
135 if (!IsAccessibleToUser (HttpContext.Current, col[n])) {
137 ret = new SiteMapNodeCollection ();
138 for (int m=0; m<n; m++)
141 } else if (ret != null)
146 return SiteMapNodeCollection.ReadOnly (col);
147 else if (ret.Count > 0)
148 return SiteMapNodeCollection.ReadOnly (ret);
150 return SiteMapNodeCollection.EmptyCollection;
153 public override SiteMapNode GetParentNode (SiteMapNode node)
156 throw new ArgumentNullException ("node");
160 nodeToParent.TryGetValue (node, out parent);
161 return CheckAccessibility (parent);
164 protected override void RemoveNode (SiteMapNode node)
167 throw new ArgumentNullException("node");
169 string key = node.Key;
173 if (keyToNode.ContainsKey (key))
174 keyToNode.Remove (key);
176 if (!String.IsNullOrEmpty (url)) {
178 if (urlToNode.ContainsKey (url))
179 urlToNode.Remove (url);
182 if (node == RootNode)
186 if (nodeToParent.TryGetValue (node, out parent)) {
187 nodeToParent.Remove (node);
189 if (nodeToChildren.ContainsKey (parent))
190 nodeToChildren [parent].Remove (node);
195 public override SiteMapNode FindSiteMapNodeFromKey (string key)
198 throw new ArgumentNullException ("key");
201 keyToNode.TryGetValue (key, out ret);
202 return CheckAccessibility (ret);
205 public abstract SiteMapNode BuildSiteMap ();
207 SiteMapNode CheckAccessibility (SiteMapNode node) {
208 return (node != null && IsAccessibleToUser (HttpContext.Current, node)) ? node : null;
211 internal string MapUrl (string url)
213 if (String.IsNullOrEmpty (url))
216 string appVPath = HttpRuntime.AppDomainAppVirtualPath;
217 if (String.IsNullOrEmpty (appVPath))
220 if (VirtualPathUtility.IsAppRelative (url))
221 return VirtualPathUtility.ToAbsolute (url, appVPath, true);
223 return VirtualPathUtility.ToAbsolute (UrlUtils.Combine (appVPath, url), appVPath, true);