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.
35 using System.Collections.Generic;
39 public abstract class StaticSiteMapProvider : SiteMapProvider
41 Dictionary<string, SiteMapNode> keyToNode;
42 Dictionary<SiteMapNode, SiteMapNode> nodeToParent;
43 Dictionary<SiteMapNode, SiteMapNodeCollection> nodeToChildren;
44 Dictionary<string, SiteMapNode> urlToNode;
46 public StaticSiteMapProvider ()
48 keyToNode = new Dictionary<string, SiteMapNode> ();
49 nodeToParent = new Dictionary<SiteMapNode, SiteMapNode> ();
50 nodeToChildren = new Dictionary<SiteMapNode, SiteMapNodeCollection> ();
51 urlToNode = new Dictionary<string, SiteMapNode> (StringComparer.InvariantCultureIgnoreCase);
54 internal protected override void AddNode (SiteMapNode node, SiteMapNode parentNode)
57 throw new ArgumentNullException ("node");
60 if (FindSiteMapNodeFromKey (node.Key) != null)
61 throw new InvalidOperationException (string.Format ("A node with key '{0}' already exists.",node.Key));
63 if (!String.IsNullOrEmpty (node.Url)) {
64 string url = MapUrl (node.Url);
66 if (FindSiteMapNode (url) != null)
67 throw new InvalidOperationException (String.Format (
68 "Multiple nodes with the same URL '{0}' were found. " +
69 "StaticSiteMapProvider requires that sitemap nodes have unique URLs.",
73 urlToNode.Add (url, node);
75 keyToNode.Add (node.Key, node);
80 if (parentNode == null)
81 parentNode = RootNode;
83 nodeToParent.Add (node, parentNode);
85 SiteMapNodeCollection children;
86 if (!nodeToChildren.TryGetValue (parentNode, out children))
87 nodeToChildren.Add (parentNode, children = new SiteMapNodeCollection ());
93 protected virtual void Clear ()
97 nodeToChildren.Clear ();
98 nodeToParent.Clear ();
103 public override SiteMapNode FindSiteMapNode (string rawUrl)
106 throw new ArgumentNullException ("rawUrl");
108 if (rawUrl == String.Empty)
113 urlToNode.TryGetValue (MapUrl (rawUrl), out node);
114 return CheckAccessibility (node);
117 public override SiteMapNodeCollection GetChildNodes (SiteMapNode node)
120 throw new ArgumentNullException ("node");
123 SiteMapNodeCollection col;
124 if (!nodeToChildren.TryGetValue (node, out col))
125 return SiteMapNodeCollection.EmptyCollection;
127 SiteMapNodeCollection ret = null;
128 for (int n=0; n<col.Count; n++) {
129 if (!IsAccessibleToUser (HttpContext.Current, col[n])) {
131 ret = new SiteMapNodeCollection ();
132 for (int m=0; m<n; m++)
135 } else if (ret != null)
140 return SiteMapNodeCollection.ReadOnly (col);
141 else if (ret.Count > 0)
142 return SiteMapNodeCollection.ReadOnly (ret);
144 return SiteMapNodeCollection.EmptyCollection;
147 public override SiteMapNode GetParentNode (SiteMapNode node)
150 throw new ArgumentNullException ("node");
154 nodeToParent.TryGetValue (node, out parent);
155 return CheckAccessibility (parent);
158 protected override void RemoveNode (SiteMapNode node)
161 throw new ArgumentNullException("node");
164 keyToNode.Remove (node.Key);
165 if (!String.IsNullOrEmpty (node.Url))
166 urlToNode.Remove (MapUrl (node.Url));
168 if (node == RootNode)
171 SiteMapNode parent = nodeToParent [node];
172 nodeToParent.Remove (node);
173 nodeToChildren [parent].Remove (node);
177 public override SiteMapNode FindSiteMapNodeFromKey (string key)
180 throw new ArgumentNullException ("key");
183 keyToNode.TryGetValue (key, out ret);
184 return CheckAccessibility (ret);
187 public abstract SiteMapNode BuildSiteMap ();
189 SiteMapNode CheckAccessibility (SiteMapNode node) {
190 return (node != null && IsAccessibleToUser (HttpContext.Current, node)) ? node : null;
193 string MapUrl (string url)
195 if (VirtualPathUtility.IsAppRelative (url))
196 return VirtualPathUtility.ToAbsolute (url);