2 // System.Web.StaticSiteMapProvider.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
6 // Ben Maurer (bmaurer@users.sourceforge.net)
9 // (C) 2005 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
34 using System.Collections.Specialized;
36 using System.Configuration.Provider;
37 using System.Web.Util;
38 using System.Globalization;
42 public abstract class StaticSiteMapProvider : SiteMapProvider
44 Hashtable nodeToParent;
45 Hashtable nodeToChildren;
49 internal protected override void AddNode (SiteMapNode node, SiteMapNode parentNode)
52 throw new ArgumentNullException ("node");
55 string url = node.Url;
56 if (url != null && url.Length > 0) {
57 if (UrlUtils.IsRelativeUrl (url))
58 url = UrlUtils.Combine (HttpRuntime.AppDomainAppVirtualPath, url);
60 url = UrlUtils.ResolveVirtualPathFromAppAbsolute (url);
62 if (FindSiteMapNode (url) != null)
63 throw new InvalidOperationException ();
65 UrlToNode [url] = node;
68 if (FindSiteMapNodeFromKey (node.Key) != null)
69 throw new InvalidOperationException (string.Format ("A node with key {0} already exists.",node.Key));
70 KeyToNode [node.Key] = node;
72 if (parentNode != null) {
73 NodeToParent [node] = parentNode;
74 if (NodeToChildren [parentNode] == null)
75 NodeToChildren [parentNode] = new SiteMapNodeCollection ();
77 ((SiteMapNodeCollection) NodeToChildren [parentNode]).Add (node);
82 Hashtable NodeToParent {
85 if (nodeToParent == null)
86 nodeToParent = new Hashtable ();
92 Hashtable NodeToChildren {
95 if (nodeToChildren == null)
96 nodeToChildren = new Hashtable ();
98 return nodeToChildren;
102 Hashtable UrlToNode {
105 if (urlToNode == null) {
106 urlToNode = new Hashtable (
107 CaseInsensitiveHashCodeProvider.DefaultInvariant,
108 CaseInsensitiveComparer.DefaultInvariant
116 Hashtable KeyToNode {
119 if (keyToNode == null)
120 keyToNode = new Hashtable ();
126 protected virtual void Clear ()
129 if (urlToNode != null)
131 if (nodeToChildren != null)
132 nodeToChildren.Clear ();
133 if (nodeToParent != null)
134 nodeToParent.Clear ();
135 if (keyToNode != null)
140 public override SiteMapNode FindSiteMapNode (string rawUrl)
143 throw new ArgumentNullException ("rawUrl");
145 if (rawUrl.Length > 0) {
147 rawUrl = UrlUtils.ResolveVirtualPathFromAppAbsolute (rawUrl);
148 SiteMapNode node = (SiteMapNode) UrlToNode [rawUrl];
149 if (node != null && IsAccessibleToUser (HttpContext.Current, node))
155 public override SiteMapNodeCollection GetChildNodes (SiteMapNode node)
158 throw new ArgumentNullException ("node");
161 SiteMapNodeCollection col = (SiteMapNodeCollection) NodeToChildren [node];
162 if (col == null) return SiteMapNodeCollection.EmptyCollection;
164 SiteMapNodeCollection ret = null;
165 for (int n=0; n<col.Count; n++) {
166 if (!IsAccessibleToUser (HttpContext.Current, col[n])) {
168 ret = new SiteMapNodeCollection ();
169 for (int m=0; m<n; m++)
172 } else if (ret != null)
178 return SiteMapNodeCollection.ReadOnly (ret);
180 return SiteMapNodeCollection.ReadOnly (col);
185 public override SiteMapNode GetParentNode (SiteMapNode node)
188 throw new ArgumentNullException ("node");
190 SiteMapNode parent = (SiteMapNode) NodeToParent [node];
191 return parent != null && IsAccessibleToUser (HttpContext.Current, parent) ? parent : null;
194 public override void RemoveNode (SiteMapNode node)
197 throw new ArgumentNullException("node");
200 SiteMapNode parent = (SiteMapNode) NodeToParent [node];
201 if (NodeToParent.Contains (node))
202 NodeToParent.Remove (node);
204 if (node.Url != null && node.Url.Length > 0 && UrlToNode.Contains (node.Url))
205 UrlToNode.Remove (node.Url);
207 if (parent != null) {
208 SiteMapNodeCollection siblings = (SiteMapNodeCollection) NodeToChildren [node];
209 if (siblings != null && siblings.Contains (node))
210 siblings.Remove (node);
215 public override SiteMapNode FindSiteMapNodeFromKey (string key)
218 throw new ArgumentNullException ("key");
220 SiteMapNode ret = (SiteMapNode) KeyToNode [key];
221 return ret != null && IsAccessibleToUser (HttpContext.Current, ret) ? ret : null;
224 public abstract SiteMapNode BuildSiteMap ();